diff --git a/iOS/FlipperKitTests/FlipperClientTests.mm b/iOS/FlipperKitTests/FlipperClientTests.mm index 65c3467e5..c87129891 100644 --- a/iOS/FlipperKitTests/FlipperClientTests.mm +++ b/iOS/FlipperKitTests/FlipperClientTests.mm @@ -8,14 +8,14 @@ #import #if FB_SONARKIT_ENABLED -#import -#import #import +#import #import +#import #import -#import #import #import +#import #import #import @@ -24,218 +24,293 @@ @end @implementation FlipperClientTests -facebook::flipper::FlipperClient *client; -facebook::flipper::test::FlipperConnectionManagerMock *socket; -FlipperClient *objcClient; +facebook::flipper::FlipperClient* client; +facebook::flipper::test::FlipperConnectionManagerMock* socket; +FlipperClient* objcClient; - (void)setUp { - // Put setup code here. This method is called before the invocation of each test method in the class. - socket = new facebook::flipper::test::FlipperConnectionManagerMock; - auto state = std::make_shared(); - - client = new facebook::flipper::FlipperClient(std::unique_ptr{socket}, state); - objcClient = [[FlipperClient alloc] initWithCppClient:client]; + // Put setup code here. This method is called before the invocation of each + // test method in the class. + socket = new facebook::flipper::test::FlipperConnectionManagerMock; + auto state = std::make_shared(); + client = new facebook::flipper::FlipperClient( + std::unique_ptr{ + socket}, + state); + objcClient = [[FlipperClient alloc] initWithCppClient:client]; } - (void)tearDown { - // Put teardown code here. This method is called after the invocation of each test method in the class. - delete client; + // Put teardown code here. This method is called after the invocation of each + // test method in the class. + delete client; } - (void)testGetPlugin { + BlockBasedSonarPlugin* cat = + [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" + connect:nil + disconnect:nil]; + BlockBasedSonarPlugin* dog = + [[BlockBasedSonarPlugin alloc] initIdentifier:@"dog" + connect:nil + disconnect:nil]; - BlockBasedSonarPlugin *cat = [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" connect:nil disconnect:nil]; - BlockBasedSonarPlugin *dog = [[BlockBasedSonarPlugin alloc] initIdentifier:@"dog" connect:nil disconnect:nil]; + [objcClient addPlugin:cat]; + [objcClient addPlugin:dog]; - [objcClient addPlugin:cat]; - [objcClient addPlugin:dog]; - - NSObject *retrievedPlugin = [objcClient pluginWithIdentifier:@"cat"]; - XCTAssertEqual(retrievedPlugin, cat); - retrievedPlugin = [objcClient pluginWithIdentifier:@"dog"]; - XCTAssertEqual(retrievedPlugin, dog); + NSObject* retrievedPlugin = + [objcClient pluginWithIdentifier:@"cat"]; + XCTAssertEqual(retrievedPlugin, cat); + retrievedPlugin = [objcClient pluginWithIdentifier:@"dog"]; + XCTAssertEqual(retrievedPlugin, dog); } - - (void)testRemovePlugin { - BlockBasedSonarPlugin *cat = [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" connect:nil disconnect:nil]; + BlockBasedSonarPlugin* cat = + [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" + connect:nil + disconnect:nil]; - [objcClient addPlugin:cat]; - [objcClient removePlugin:cat]; + [objcClient addPlugin:cat]; + [objcClient removePlugin:cat]; - folly::dynamic message = folly::dynamic::object("id", 1)("method", "getPlugins"); + folly::dynamic message = + folly::dynamic::object("id", 1)("method", "getPlugins"); - std::vector successes = std::vector(); - std::vector errors = std::vector(); - std::unique_ptr responder = std::make_unique(&successes, &errors); - socket->callbacks->onMessageReceived(message, std::move(responder)); - folly::dynamic expected = folly::dynamic::object("plugins", folly::dynamic::array()); - XCTAssertEqual(successes.size(), 1); - XCTAssertEqual(errors.size(), 0); - XCTAssertEqual(successes[0], expected); + std::vector successes = std::vector(); + std::vector errors = std::vector(); + std::unique_ptr responder = + std::make_unique( + &successes, &errors); + socket->callbacks->onMessageReceived(message, std::move(responder)); + folly::dynamic expected = + folly::dynamic::object("plugins", folly::dynamic::array()); + XCTAssertEqual(successes.size(), 1); + XCTAssertEqual(errors.size(), 0); + XCTAssertEqual(successes[0], expected); } -- (void) testPluginActivatedInBackgroundMode { - __block BOOL pluginConnected = NO; - BlockBasedSonarPlugin *cat = [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" connect:^(id) { - pluginConnected = YES; - } disconnect:^{ - pluginConnected = NO; +- (void)testPluginActivatedInBackgroundMode { + __block BOOL pluginConnected = NO; + BlockBasedSonarPlugin* cat = + [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" + connect:^(id) { + pluginConnected = YES; + } + disconnect:^{ + pluginConnected = NO; + } + runInBackground:YES]; - } runInBackground: YES]; - - [objcClient addPlugin:cat]; - [objcClient start]; - XCTAssertTrue(pluginConnected); + [objcClient addPlugin:cat]; + [objcClient start]; + XCTAssertTrue(pluginConnected); } -- (void) testPluginNotActivatedInNonBackgroundMode { - __block BOOL pluginConnected = NO; - BlockBasedSonarPlugin *cat = [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" connect:^(id) { - pluginConnected = YES; - } disconnect:^{ - pluginConnected = NO; +- (void)testPluginNotActivatedInNonBackgroundMode { + __block BOOL pluginConnected = NO; + BlockBasedSonarPlugin* cat = + [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" + connect:^(id) { + pluginConnected = YES; + } + disconnect:^{ + pluginConnected = NO; + } + runInBackground:NO]; - } runInBackground: NO]; - - [objcClient addPlugin:cat]; - [objcClient start]; - XCTAssertFalse(pluginConnected); + [objcClient addPlugin:cat]; + [objcClient start]; + XCTAssertFalse(pluginConnected); } - (void)testConnectAndDisconnectCallbackForNonBackgroundCase { - __block BOOL pluginConnected = NO; - BlockBasedSonarPlugin *cat = [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" connect:^(id) { - pluginConnected = YES; - } disconnect:^{ - pluginConnected = NO; - } runInBackground: NO]; + __block BOOL pluginConnected = NO; + BlockBasedSonarPlugin* cat = + [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" + connect:^(id) { + pluginConnected = YES; + } + disconnect:^{ + pluginConnected = NO; + } + runInBackground:NO]; - [objcClient addPlugin:cat]; - [objcClient start]; + [objcClient addPlugin:cat]; + [objcClient start]; - folly::dynamic messageInit = folly::dynamic::object("method", "init")("params", folly::dynamic::object("plugin", "cat")); - std::unique_ptr responder = std::make_unique(); + folly::dynamic messageInit = folly::dynamic::object("method", "init")( + "params", folly::dynamic::object("plugin", "cat")); + std::unique_ptr responder = + std::make_unique(); - socket->callbacks->onMessageReceived(messageInit, std::move(responder)); - XCTAssertTrue(pluginConnected); - [objcClient stop]; - XCTAssertFalse(pluginConnected); + socket->callbacks->onMessageReceived(messageInit, std::move(responder)); + XCTAssertTrue(pluginConnected); + [objcClient stop]; + XCTAssertFalse(pluginConnected); } - (void)testConnectAndDisconnectCallbackForBackgroundCase { - __block BOOL pluginConnected = YES; - BlockBasedSonarPlugin *cat = [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" connect:^(id) { - pluginConnected = YES; - } disconnect:^{ - pluginConnected = NO; - } runInBackground: YES]; + __block BOOL pluginConnected = YES; + BlockBasedSonarPlugin* cat = + [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" + connect:^(id) { + pluginConnected = YES; + } + disconnect:^{ + pluginConnected = NO; + } + runInBackground:YES]; - [objcClient addPlugin:cat]; - [objcClient start]; - XCTAssertTrue(pluginConnected); - [objcClient stop]; - XCTAssertFalse(pluginConnected); + [objcClient addPlugin:cat]; + [objcClient start]; + XCTAssertTrue(pluginConnected); + [objcClient stop]; + XCTAssertFalse(pluginConnected); } - (void)testCrashSuppressionInDidConnectCallback { - __block BOOL pluginConnected = NO; - BlockBasedSonarPlugin *cat = [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" connect:^(id) { - pluginConnected = YES; - NSArray *array = @[]; - [array objectAtIndex:10]; //This will throw an exception - } disconnect:nil runInBackground: YES]; + __block BOOL pluginConnected = NO; + BlockBasedSonarPlugin* cat = [[BlockBasedSonarPlugin alloc] + initIdentifier:@"cat" + connect:^(id) { + pluginConnected = YES; + NSArray* array = @[]; + [array objectAtIndex:10]; // This will throw an exception + } + disconnect:nil + runInBackground:YES]; - [objcClient addPlugin:cat]; - // Since background plugin's didconnect is called as soon as flipper client starts - XCTAssertNoThrow([objcClient start]); - XCTAssertTrue(pluginConnected); // To be sure that connect block is called + [objcClient addPlugin:cat]; + // Since background plugin's didconnect is called as soon as flipper client + // starts + XCTAssertNoThrow([objcClient start]); + XCTAssertTrue(pluginConnected); // To be sure that connect block is called } - (void)testCrashSuppressionInDisconnectCallback { - __block BOOL isCalled = NO; - BlockBasedSonarPlugin *cat = [[BlockBasedSonarPlugin alloc] initIdentifier:@"cat" connect:nil disconnect:^{ - isCalled = YES; - NSArray *array = @[]; - [array objectAtIndex:10]; //This will throw an exception - } runInBackground: YES]; + __block BOOL isCalled = NO; + BlockBasedSonarPlugin* cat = [[BlockBasedSonarPlugin alloc] + initIdentifier:@"cat" + connect:nil + disconnect:^{ + isCalled = YES; + NSArray* array = @[]; + [array objectAtIndex:10]; // This will throw an exception + } + runInBackground:YES]; - [objcClient addPlugin:cat]; - [objcClient start]; + [objcClient addPlugin:cat]; + [objcClient start]; - XCTAssertNoThrow([objcClient stop]); // Stopping client will call disconnect of the plugin - XCTAssertTrue(isCalled); // To be sure that connect block is called + XCTAssertNoThrow( + [objcClient stop]); // Stopping client will call disconnect of the plugin + XCTAssertTrue(isCalled); // To be sure that connect block is called } - (void)testMethodBlockIsCalledNonBackgroundCase { - __block BOOL isCalled = NO; + __block BOOL isCalled = NO; - BlockBasedSonarPlugin *cat = [[BlockBasedSonarPlugin alloc] initIdentifier:@"PluginIdentifier" connect:^(id connection) { + BlockBasedSonarPlugin* cat = [[BlockBasedSonarPlugin alloc] + initIdentifier:@"PluginIdentifier" + connect:^(id connection) { + [connection + receive:@"MethodName" + withBlock:^( + NSDictionary* dict, id responder) { + isCalled = YES; + }]; + } + disconnect:nil]; - [connection receive:@"MethodName" withBlock:^(NSDictionary * dict, id responder) { - isCalled = YES; - }]; - } disconnect:nil]; + [objcClient addPlugin:cat]; + [objcClient start]; - [objcClient addPlugin:cat]; - [objcClient start]; + folly::dynamic messageInit = folly::dynamic::object("method", "init")( + "params", folly::dynamic::object("plugin", "PluginIdentifier")); + std::unique_ptr responder1 = + std::make_unique(); + socket->callbacks->onMessageReceived(messageInit, std::move(responder1)); + folly::dynamic message = folly::dynamic::object("id", 1)("method", "execute")( + "params", + folly::dynamic::object("api", "PluginIdentifier")( + "method", "MethodName")); + std::unique_ptr responder2 = + std::make_unique(); - folly::dynamic messageInit = folly::dynamic::object("method", "init")("params", folly::dynamic::object("plugin", "PluginIdentifier")); - std::unique_ptr responder1 = std::make_unique(); - socket->callbacks->onMessageReceived(messageInit, std::move(responder1)); - folly::dynamic message = folly::dynamic::object("id", 1)("method", "execute")("params", folly::dynamic::object("api", "PluginIdentifier")("method", "MethodName")); - std::unique_ptr responder2 = std::make_unique(); + socket->callbacks->onMessageReceived(message, std::move(responder2)); - socket->callbacks->onMessageReceived(message, std::move(responder2)); - - XCTAssertTrue(isCalled); + XCTAssertTrue(isCalled); } - (void)testMethodBlockIsCalledBackgroundCase { - __block BOOL isCalled = NO; + __block BOOL isCalled = NO; - BlockBasedSonarPlugin *cat = [[BlockBasedSonarPlugin alloc] initIdentifier:@"PluginIdentifier" connect:^(id connection) { + BlockBasedSonarPlugin* cat = [[BlockBasedSonarPlugin alloc] + initIdentifier:@"PluginIdentifier" + connect:^(id connection) { + [connection + receive:@"MethodName" + withBlock:^( + NSDictionary* dict, id responder) { + isCalled = YES; + }]; + } + disconnect:nil + runInBackground:YES]; - [connection receive:@"MethodName" withBlock:^(NSDictionary * dict, id responder) { - isCalled = YES; - }]; - } disconnect:nil runInBackground:YES]; + [objcClient addPlugin:cat]; + [objcClient start]; - [objcClient addPlugin:cat]; - [objcClient start]; + folly::dynamic message = folly::dynamic::object("id", 1)("method", "execute")( + "params", + folly::dynamic::object("api", "PluginIdentifier")( + "method", "MethodName")); + std::unique_ptr responder = + std::make_unique(); + socket->callbacks->onMessageReceived(message, std::move(responder)); - folly::dynamic message = folly::dynamic::object("id", 1)("method", "execute")("params", folly::dynamic::object("api", "PluginIdentifier")("method", "MethodName")); - std::unique_ptr responder = std::make_unique(); - socket->callbacks->onMessageReceived(message, std::move(responder)); - - XCTAssertTrue(isCalled); + XCTAssertTrue(isCalled); } - (void)testExceptionSuppressionInMethodBlock { - __block BOOL isCalled = NO; + __block BOOL isCalled = NO; - BlockBasedSonarPlugin *cat = [[BlockBasedSonarPlugin alloc] initIdentifier:@"PluginIdentifier" connect:^(id connection) { + BlockBasedSonarPlugin* cat = [[BlockBasedSonarPlugin alloc] + initIdentifier:@"PluginIdentifier" + connect:^(id connection) { + [connection + receive:@"MethodName" + withBlock:^( + NSDictionary* dict, id responder) { + isCalled = YES; + NSArray* array = @[]; + [array objectAtIndex:10]; // This will throw an exception + }]; + } + disconnect:nil + runInBackground:YES]; - [connection receive:@"MethodName" withBlock:^(NSDictionary * dict, id responder) { - isCalled = YES; - NSArray *array = @[]; - [array objectAtIndex:10]; //This will throw an exception - }]; - } disconnect:nil runInBackground:YES]; + [objcClient addPlugin:cat]; + [objcClient start]; - [objcClient addPlugin:cat]; - [objcClient start]; + folly::dynamic message = folly::dynamic::object("id", 1)("method", "execute")( + "params", + folly::dynamic::object("api", "PluginIdentifier")( + "method", "MethodName")); + std::vector successes = std::vector(); + std::vector errors = std::vector(); + std::unique_ptr responder = + std::make_unique( + &successes, &errors); - folly::dynamic message = folly::dynamic::object("id", 1)("method", "execute")("params", folly::dynamic::object("api", "PluginIdentifier")("method", "MethodName")); - std::vector successes = std::vector(); - std::vector errors = std::vector(); - std::unique_ptr responder = std::make_unique(&successes, &errors); - - XCTAssertNoThrow(socket->callbacks->onMessageReceived(message, std::move(responder))); - XCTAssertTrue(isCalled); - XCTAssertEqual(successes.size(), 0); - XCTAssertEqual(errors.size(), 1); + XCTAssertNoThrow( + socket->callbacks->onMessageReceived(message, std::move(responder))); + XCTAssertTrue(isCalled); + XCTAssertEqual(successes.size(), 0); + XCTAssertEqual(errors.size(), 1); } @end diff --git a/iOS/FlipperKitTests/FlipperCppBridgingTests.mm b/iOS/FlipperKitTests/FlipperCppBridgingTests.mm index e44a7f374..241457fa4 100644 --- a/iOS/FlipperKitTests/FlipperCppBridgingTests.mm +++ b/iOS/FlipperKitTests/FlipperCppBridgingTests.mm @@ -14,13 +14,17 @@ using facebook::flipper::FlipperCppWrapperPlugin; -@interface DummyPlugin : NSObject +@interface DummyPlugin : NSObject @end @implementation DummyPlugin -- (NSString *)identifier { return @"Dummy"; } -- (void)didConnect:(id)connection {} -- (void)didDisconnect {} +- (NSString*)identifier { + return @"Dummy"; +} +- (void)didConnect:(id)connection { +} +- (void)didDisconnect { +} @end @interface FlipperCppBridgingTests : XCTestCase @@ -29,10 +33,10 @@ using facebook::flipper::FlipperCppWrapperPlugin; @implementation FlipperCppBridgingTests - (void)testCppWrapperRetainsObjCPlugin { - NSObject *dummyPlugin = [DummyPlugin new]; - auto retainCountBefore = CFGetRetainCount((void *)dummyPlugin); + NSObject* dummyPlugin = [DummyPlugin new]; + auto retainCountBefore = CFGetRetainCount((void*)dummyPlugin); FlipperCppWrapperPlugin wrapperPlugin(dummyPlugin); - auto retainCountAfter = CFGetRetainCount((void *)dummyPlugin); + auto retainCountAfter = CFGetRetainCount((void*)dummyPlugin); XCTAssertTrue(retainCountAfter > retainCountBefore); } diff --git a/iOS/FlipperKitTests/FlipperUtilTests.mm b/iOS/FlipperKitTests/FlipperUtilTests.mm index 5c247f309..24c9b11fd 100644 --- a/iOS/FlipperKitTests/FlipperUtilTests.mm +++ b/iOS/FlipperKitTests/FlipperUtilTests.mm @@ -8,10 +8,10 @@ #import #if FB_SONARKIT_ENABLED -#import -#import #import +#import #import +#import #import #import #import @@ -26,35 +26,41 @@ @end @implementation FlipperUtilTests { - FlipperResponderMock *responder; + FlipperResponderMock* responder; } - - (void)setUp { - responder = [FlipperResponderMock new]; + responder = [FlipperResponderMock new]; } - (void)testPerformOnMainThreadSuccess { - FlipperPerformBlockOnMainThread(^{}, responder); - NSAssert([responder.successes count] == 0, @"No successes are output"); - NSAssert([responder.errors count] == 0, @"No errors are output"); + FlipperPerformBlockOnMainThread( + ^{ + }, + responder); + NSAssert([responder.successes count] == 0, @"No successes are output"); + NSAssert([responder.errors count] == 0, @"No errors are output"); } - (void)testPerformOnMainThreadStdException { - FlipperPerformBlockOnMainThread(^{ + FlipperPerformBlockOnMainThread( + ^{ throw new std::exception(); - }, responder); - NSAssert([responder.successes count] == 0, @"No successes are output"); - NSAssert([responder.errors count] == 1, @"1 error is output"); + }, + responder); + NSAssert([responder.successes count] == 0, @"No successes are output"); + NSAssert([responder.errors count] == 1, @"1 error is output"); } - (void)testPerformOnMainThreadNSException { - FlipperPerformBlockOnMainThread(^{ - NSArray *a = [NSArray init]; + FlipperPerformBlockOnMainThread( + ^{ + NSArray* a = [NSArray init]; [a objectAtIndex:1]; - }, responder); - NSAssert([responder.successes count] == 0, @"No successes are output"); - NSAssert([responder.errors count] == 1, @"1 error is output"); + }, + responder); + NSAssert([responder.successes count] == 0, @"No successes are output"); + NSAssert([responder.errors count] == 1, @"1 error is output"); } @end