Skip to content

Instantly share code, notes, and snippets.

@HelloCore
Created January 11, 2022 08:10
Show Gist options
  • Save HelloCore/f41e784e31fd57ba960d7d77e74ac8a1 to your computer and use it in GitHub Desktop.
Save HelloCore/f41e784e31fd57ba960d7d77e74ac8a1 to your computer and use it in GitHub Desktop.
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@import Foundation;
@class UIImage;
NS_ASSUME_NONNULL_BEGIN
typedef void (^IntegrationTestResults)(NSString *testName, BOOL success, NSString *_Nullable failureMessage);
@interface IntegrationTestRunner : NSObject
/**
* Any screenshots captured by the plugin.
*/
//@property (copy, readonly) NSDictionary<NSString *, UIImage *> *capturedScreenshotsByName;
/*!
Start dart tests and wait for results.
@param testResult Will be called once per every completed dart test.
*/
- (void)testIntegrationTestWithResults:(NS_NOESCAPE IntegrationTestResults)testResult;
@end
NS_ASSUME_NONNULL_END
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "IntegrationTestRunner.h"
@import integration_test;
@import UIKit;
@interface IntegrationTestRunner ()
@property IntegrationTestPlugin *integrationTestPlugin;
@end
@implementation IntegrationTestRunner
- (instancetype)init {
self = [super init];
_integrationTestPlugin = [IntegrationTestPlugin instance];
return self;
}
- (void)testIntegrationTestWithResults:(NS_NOESCAPE IntegrationTestResults)testResult {
IntegrationTestPlugin *integrationTestPlugin = self.integrationTestPlugin;
UIViewController *rootViewController = UIApplication.sharedApplication.delegate.window.rootViewController;
if (![rootViewController isKindOfClass:[FlutterViewController class]]) {
testResult(@"setup", NO, @"rootViewController was not expected FlutterViewController");
}
FlutterViewController *flutterViewController = (FlutterViewController *)rootViewController;
[integrationTestPlugin setupChannels:flutterViewController.engine.binaryMessenger];
// Spin the runloop.
while (!integrationTestPlugin.testResults) {
[NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
}
[integrationTestPlugin.testResults enumerateKeysAndObjectsUsingBlock:^(NSString *test, NSString *result, BOOL *stop) {
if ([result isEqualToString:@"success"]) {
testResult(test, YES, nil);
} else {
testResult(test, NO, result);
}
}];
}
//- (NSDictionary<NSString *,UIImage *> *)capturedScreenshotsByName {
// return self.integrationTestPlugin.capturedScreenshotsByName;
//}
@end
@import XCTest;
@import ObjectiveC.runtime;
#import "IntegrationTestRunner.h"
@interface RunnerTests : XCTestCase
@end
@implementation RunnerTests
+ (NSArray<NSInvocation *> *)testInvocations {
IntegrationTestRunner *integrationTestRunner =
[IntegrationTestRunner new];
NSMutableArray<NSInvocation *> *testInvocations = [NSMutableArray new];
[integrationTestRunner
testIntegrationTestWithResults:^(NSString *testName, BOOL success,
NSString *failureMessage) {
IMP assertImplementation = imp_implementationWithBlock(^(id _self) {
XCTAssertTrue(success, @"%@", failureMessage);
});
NSString *upperCamelTestName = [testName.localizedCapitalizedString
stringByReplacingOccurrencesOfString:@" "
withString:@""];
NSString *testSelectorName =
[NSString stringWithFormat:@"test%@", upperCamelTestName];
SEL testSelector = NSSelectorFromString(testSelectorName);
class_addMethod(self, testSelector, assertImplementation, "v@:");
NSMethodSignature *signature =
[self instanceMethodSignatureForSelector:testSelector];
NSInvocation *invocation =
[NSInvocation invocationWithMethodSignature:signature];
invocation.selector = testSelector;
[testInvocations addObject:invocation];
}];
// NSDictionary<NSString *, UIImage *> *capturedScreenshotsByName =
// integrationTestRunner.capturedScreenshotsByName;
// if (capturedScreenshotsByName.count > 0) {
// IMP screenshotImplementation = imp_implementationWithBlock(^(id _self) {
// [capturedScreenshotsByName
// enumerateKeysAndObjectsUsingBlock:^(NSString *name,
// UIImage *screenshot, BOOL *stop) {
// XCTAttachment *attachment =
// [XCTAttachment attachmentWithImage:screenshot];
// attachment.lifetime = XCTAttachmentLifetimeKeepAlways;
// if (name != nil) {
// attachment.name = name;
// }
// [_self addAttachment:attachment];
// }];
// });
//
// SEL attachmentSelector = NSSelectorFromString(@"screenshotPlaceholder");
// class_addMethod(self, attachmentSelector, screenshotImplementation, "v@:");
//
// NSMethodSignature *attachmentSignature =
// [self instanceMethodSignatureForSelector:attachmentSelector];
// NSInvocation *attachmentInvocation =
// [NSInvocation invocationWithMethodSignature:attachmentSignature];
// attachmentInvocation.selector = attachmentSelector;
//
// [testInvocations addObject:attachmentInvocation];
// }
return testInvocations;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment