Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Register native callback before running engine. #37600

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 35 additions & 28 deletions shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ @interface FlutterEngine (Test)
}

TEST_F(FlutterEngineTest, HasNonNullExecutableName) {
// Launch the test entrypoint.
FlutterEngine* engine = GetFlutterEngine();
std::string executable_name = [[engine executableName] UTF8String];
ASSERT_FALSE(executable_name.empty());
EXPECT_TRUE([engine runWithEntrypoint:@"executableNameNotNull"]);

// Block until notified by the Dart test of the value of Platform.executable.
fml::AutoResetWaitableEvent latch;
Expand All @@ -53,6 +51,10 @@ @interface FlutterEngine (Test)
EXPECT_EQ(executable_name, dart_string);
latch.Signal();
}));

// Launch the test entrypoint.
EXPECT_TRUE([engine runWithEntrypoint:@"executableNameNotNull"]);

latch.Wait();
}

Expand All @@ -76,6 +78,11 @@ @interface FlutterEngine (Test)
}

TEST_F(FlutterEngineTest, CanLogToStdout) {
// Block until completion of print statement.
fml::AutoResetWaitableEvent latch;
AddNativeCallback("SignalNativeTest",
CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { latch.Signal(); }));

// Replace stdout stream buffer with our own.
std::stringstream buffer;
std::streambuf* old_buffer = std::cout.rdbuf();
Expand All @@ -86,10 +93,6 @@ @interface FlutterEngine (Test)
EXPECT_TRUE([engine runWithEntrypoint:@"canLogToStdout"]);
EXPECT_TRUE(engine.running);

// Block until completion of print statement.
fml::AutoResetWaitableEvent latch;
AddNativeCallback("SignalNativeTest",
CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { latch.Signal(); }));
latch.Wait();

// Restore old stdout stream buffer.
Expand All @@ -101,19 +104,7 @@ @interface FlutterEngine (Test)
}

TEST_F(FlutterEngineTest, BackgroundIsBlack) {
// Launch the test entrypoint.
FlutterEngine* engine = GetFlutterEngine();
EXPECT_TRUE([engine runWithEntrypoint:@"backgroundTest"]);
EXPECT_TRUE(engine.running);

NSString* fixtures = @(flutter::testing::GetFixturesPath());
FlutterDartProject* project = [[FlutterDartProject alloc]
initWithAssetsPath:fixtures
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithProject:project];
[viewController loadView];
viewController.flutterView.frame = CGRectMake(0, 0, 800, 600);
[engine setViewController:viewController];

// Latch to ensure the entire layer tree has been generated and presented.
fml::AutoResetWaitableEvent latch;
Expand All @@ -127,12 +118,8 @@ @interface FlutterEngine (Test)
}
latch.Signal();
}));
latch.Wait();
}

TEST_F(FlutterEngineTest, CanOverrideBackgroundColor) {
// Launch the test entrypoint.
FlutterEngine* engine = GetFlutterEngine();
EXPECT_TRUE([engine runWithEntrypoint:@"backgroundTest"]);
EXPECT_TRUE(engine.running);

Expand All @@ -144,7 +131,12 @@ @interface FlutterEngine (Test)
[viewController loadView];
viewController.flutterView.frame = CGRectMake(0, 0, 800, 600);
[engine setViewController:viewController];
viewController.flutterView.backgroundColor = [NSColor whiteColor];

latch.Wait();
}

TEST_F(FlutterEngineTest, CanOverrideBackgroundColor) {
FlutterEngine* engine = GetFlutterEngine();

// Latch to ensure the entire layer tree has been generated and presented.
fml::AutoResetWaitableEvent latch;
Expand All @@ -158,6 +150,21 @@ @interface FlutterEngine (Test)
}
latch.Signal();
}));

// Launch the test entrypoint.
EXPECT_TRUE([engine runWithEntrypoint:@"backgroundTest"]);
EXPECT_TRUE(engine.running);

NSString* fixtures = @(flutter::testing::GetFixturesPath());
FlutterDartProject* project = [[FlutterDartProject alloc]
initWithAssetsPath:fixtures
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];
FlutterViewController* viewController = [[FlutterViewController alloc] initWithProject:project];
[viewController loadView];
viewController.flutterView.frame = CGRectMake(0, 0, 800, 600);
[engine setViewController:viewController];
viewController.flutterView.backgroundColor = [NSColor whiteColor];

latch.Wait();
}

Expand Down Expand Up @@ -425,17 +432,17 @@ @interface FlutterEngine (Test)
}

TEST_F(FlutterEngineTest, NativeCallbacks) {
FlutterEngine* engine = GetFlutterEngine();
EXPECT_TRUE([engine runWithEntrypoint:@"nativeCallback"]);
EXPECT_TRUE(engine.running);

fml::AutoResetWaitableEvent latch;
bool latch_called = false;

AddNativeCallback("SignalNativeTest", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
latch_called = true;
latch.Signal();
}));

FlutterEngine* engine = GetFlutterEngine();
EXPECT_TRUE([engine runWithEntrypoint:@"nativeCallback"]);
EXPECT_TRUE(engine.running);

latch.Wait();
ASSERT_TRUE(latch_called);
}
Expand Down