This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Enforce the rule of calling FlutterView.Render
#45300
Merged
Merged
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f2c3cd7
Changes from Michael
dkwingsmt ca0fc96
Merge remote-tracking branch 'origin/main' into enforce-rendering-rule
dkwingsmt f783ef3
Remove RunOnPlatformTaskRunner
dkwingsmt 3c3ddbe
Impl
dkwingsmt 4326d57
Merge branch 'remove-RunOnPlatformTaskRunner' into enforce-rendering-…
dkwingsmt 9ac34e2
First running runtime controller
dkwingsmt 5e15f5d
CallingRenderOutOfScopeIsIgnored
dkwingsmt 8c7b5d6
Extract methods
dkwingsmt 0d563e8
DuplicateRenderCallsAreIgnored
dkwingsmt 49a3560
Docs and callback
dkwingsmt 713648e
Move destruction to destructor
dkwingsmt 48135a7
Format
dkwingsmt d0b1cf7
Extract Shell InferVmInitDataFromSettings
dkwingsmt 21b2787
lint
dkwingsmt c38c202
Revert contents of #45190
dkwingsmt c4d273c
Use new mocks
dkwingsmt 1f96e53
Merge remote-tracking branch 'origin/main' into enforce-rendering-rule
dkwingsmt 45f0149
Lint
dkwingsmt cb9b8e4
Merge remote-tracking branch 'origin/main' into enforce-rendering-rule
dkwingsmt bc11215
correctly post sync for begin frame
dkwingsmt 151ed77
Extract into methods
dkwingsmt 3370e8c
Address comments
dkwingsmt d768cc4
Remove exact
dkwingsmt 828b791
Merge remote-tracking branch 'dkwingsmt/enforce-rendering-rule' into …
dkwingsmt e266da1
Merge branch 'main' into enforce-rendering-rule
dkwingsmt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,10 +15,172 @@ | |||||
| #include "flutter/shell/common/shell_test.h" | ||||||
| #include "flutter/shell/common/thread_host.h" | ||||||
| #include "flutter/testing/testing.h" | ||||||
| #include "gmock/gmock.h" | ||||||
|
|
||||||
| namespace flutter { | ||||||
|
|
||||||
| namespace { | ||||||
|
|
||||||
| static constexpr int64_t kImplicitViewId = 0; | ||||||
|
|
||||||
| static void PostSync(const fml::RefPtr<fml::TaskRunner>& task_runner, | ||||||
| const fml::closure& task) { | ||||||
| fml::AutoResetWaitableEvent latch; | ||||||
| fml::TaskRunner::RunNowOrPostTask(task_runner, [&latch, &task] { | ||||||
| task(); | ||||||
| latch.Signal(); | ||||||
| }); | ||||||
| latch.Wait(); | ||||||
| } | ||||||
|
|
||||||
| class MockRuntimeDelegate : public RuntimeDelegate { | ||||||
| public: | ||||||
| MOCK_METHOD(std::string, DefaultRouteName, (), (override)); | ||||||
| MOCK_METHOD(void, ScheduleFrame, (bool), (override)); | ||||||
| MOCK_METHOD(void, | ||||||
| Render, | ||||||
| (std::unique_ptr<flutter::LayerTree>, float), | ||||||
| (override)); | ||||||
| MOCK_METHOD(void, | ||||||
| UpdateSemantics, | ||||||
| (SemanticsNodeUpdates, CustomAccessibilityActionUpdates), | ||||||
| (override)); | ||||||
| MOCK_METHOD(void, | ||||||
| HandlePlatformMessage, | ||||||
| (std::unique_ptr<PlatformMessage>), | ||||||
| (override)); | ||||||
| MOCK_METHOD(FontCollection&, GetFontCollection, (), (override)); | ||||||
| MOCK_METHOD(std::shared_ptr<AssetManager>, GetAssetManager, (), (override)); | ||||||
| MOCK_METHOD(void, OnRootIsolateCreated, (), (override)); | ||||||
| MOCK_METHOD(void, | ||||||
| UpdateIsolateDescription, | ||||||
| (const std::string, int64_t), | ||||||
| (override)); | ||||||
| MOCK_METHOD(void, SetNeedsReportTimings, (bool), (override)); | ||||||
| MOCK_METHOD(std::unique_ptr<std::vector<std::string>>, | ||||||
| ComputePlatformResolvedLocale, | ||||||
| (const std::vector<std::string>&), | ||||||
| (override)); | ||||||
| MOCK_METHOD(void, RequestDartDeferredLibrary, (intptr_t), (override)); | ||||||
| MOCK_METHOD(std::weak_ptr<PlatformMessageHandler>, | ||||||
| GetPlatformMessageHandler, | ||||||
| (), | ||||||
| (const, override)); | ||||||
| MOCK_METHOD(void, SendChannelUpdate, (std::string, bool), (override)); | ||||||
| MOCK_METHOD(double, | ||||||
| GetScaledFontSize, | ||||||
| (double font_size, int configuration_id), | ||||||
| (const, override)); | ||||||
| }; | ||||||
|
|
||||||
| class MockPlatformMessageHandler : public PlatformMessageHandler { | ||||||
| public: | ||||||
| MOCK_METHOD(void, | ||||||
| HandlePlatformMessage, | ||||||
| (std::unique_ptr<PlatformMessage> message), | ||||||
| (override)); | ||||||
| MOCK_METHOD(bool, | ||||||
| DoesHandlePlatformMessageOnPlatformThread, | ||||||
| (), | ||||||
| (const, override)); | ||||||
| MOCK_METHOD(void, | ||||||
| InvokePlatformMessageResponseCallback, | ||||||
| (int response_id, std::unique_ptr<fml::Mapping> mapping), | ||||||
| (override)); | ||||||
| MOCK_METHOD(void, | ||||||
| InvokePlatformMessageEmptyResponseCallback, | ||||||
| (int response_id), | ||||||
| (override)); | ||||||
| }; | ||||||
|
|
||||||
| // A class that can launch a RuntimeController with the specified | ||||||
| // RuntimeDelegate. | ||||||
| // | ||||||
| // To use this class, contruct this class with Create, call LaunchRootIsolate, | ||||||
| // and use the controller with ControllerTaskSync(). | ||||||
| class RuntimeControllerContext { | ||||||
| public: | ||||||
| using ControllerCallback = std::function<void(RuntimeController&)>; | ||||||
|
|
||||||
| [[nodiscard]] static std::unique_ptr<RuntimeControllerContext> Create( | ||||||
| Settings settings, // | ||||||
| const TaskRunners& task_runners, // | ||||||
| RuntimeDelegate& client) { | ||||||
| auto [vm, isolate_snapshot] = Shell::InferVmInitDataFromSettings(settings); | ||||||
| FML_CHECK(vm) << "Must be able to initialize the VM."; | ||||||
| // Construct the class with `new` because `make_unique` has no access to the | ||||||
| // private constructor. | ||||||
| RuntimeControllerContext* raw_pointer = new RuntimeControllerContext( | ||||||
| settings, task_runners, client, std::move(vm), isolate_snapshot); | ||||||
| return std::unique_ptr<RuntimeControllerContext>(raw_pointer); | ||||||
| } | ||||||
|
|
||||||
| ~RuntimeControllerContext() { | ||||||
| PostSync(task_runners_.GetUITaskRunner(), | ||||||
| [&]() { runtime_controller_.reset(); }); | ||||||
| } | ||||||
|
|
||||||
| // Launch the root isolate. The post_launch callback will be executed in the | ||||||
| // same UI task, which can be used to create initial views. | ||||||
| void LaunchRootIsolate(RunConfiguration& configuration, | ||||||
| ControllerCallback post_launch) { | ||||||
| PostSync(task_runners_.GetUITaskRunner(), [&]() { | ||||||
| bool launch_success = runtime_controller_->LaunchRootIsolate( | ||||||
| settings_, // | ||||||
| []() {}, // | ||||||
| configuration.GetEntrypoint(), // | ||||||
| configuration.GetEntrypointLibrary(), // | ||||||
| configuration.GetEntrypointArgs(), // | ||||||
| configuration.TakeIsolateConfiguration()); // | ||||||
| ASSERT_TRUE(launch_success); | ||||||
| post_launch(*runtime_controller_); | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| // Run a task that operates the RuntimeController on the UI thread, and wait | ||||||
| // for the task to end. | ||||||
| void ControllerTaskSync(ControllerCallback task) { | ||||||
| ASSERT_TRUE(runtime_controller_); | ||||||
| ASSERT_TRUE(task); | ||||||
| PostSync(task_runners_.GetUITaskRunner(), | ||||||
| [&]() { task(*runtime_controller_); }); | ||||||
| } | ||||||
|
|
||||||
| private: | ||||||
| RuntimeControllerContext(const Settings& settings, | ||||||
| const TaskRunners& task_runners, | ||||||
| RuntimeDelegate& client, | ||||||
| DartVMRef vm, | ||||||
| fml::RefPtr<const DartSnapshot> isolate_snapshot) | ||||||
| : settings_(settings), | ||||||
| task_runners_(task_runners), | ||||||
| isolate_snapshot_(std::move(isolate_snapshot)), | ||||||
| vm_(std::move(vm)), | ||||||
| runtime_controller_(std::make_unique<RuntimeController>( | ||||||
| client, | ||||||
| &vm_, | ||||||
| std::move(isolate_snapshot_), | ||||||
| settings.idle_notification_callback, // idle notification callback | ||||||
| flutter::PlatformData(), // platform data | ||||||
| settings.isolate_create_callback, // isolate create callback | ||||||
| settings.isolate_shutdown_callback, // isolate shutdown callback | ||||||
| settings.persistent_isolate_data, // persistent isolate data | ||||||
| UIDartState::Context{task_runners})) {} | ||||||
|
|
||||||
| Settings settings_; | ||||||
| TaskRunners task_runners_; | ||||||
| fml::RefPtr<const DartSnapshot> isolate_snapshot_; | ||||||
| DartVMRef vm_; | ||||||
| std::unique_ptr<RuntimeController> runtime_controller_; | ||||||
| }; | ||||||
| } // namespace | ||||||
|
|
||||||
| namespace testing { | ||||||
|
|
||||||
| using ::testing::_; | ||||||
| using ::testing::Exactly; | ||||||
| using ::testing::Return; | ||||||
|
|
||||||
| class PlatformConfigurationTest : public ShellTest {}; | ||||||
|
|
||||||
| TEST_F(PlatformConfigurationTest, Initialization) { | ||||||
|
|
@@ -332,5 +494,84 @@ TEST_F(PlatformConfigurationTest, SetDartPerformanceMode) { | |||||
| DestroyShell(std::move(shell), task_runners); | ||||||
| } | ||||||
|
|
||||||
| TEST_F(PlatformConfigurationTest, OutOfScopeRenderCallsAreIgnored) { | ||||||
| Settings settings = CreateSettingsForFixture(); | ||||||
| TaskRunners task_runners = GetTaskRunnersForFixture(); | ||||||
|
|
||||||
| MockRuntimeDelegate client; | ||||||
| auto platform_message_handler = | ||||||
| std::make_shared<MockPlatformMessageHandler>(); | ||||||
| EXPECT_CALL(client, GetPlatformMessageHandler) | ||||||
| .WillOnce(Return(platform_message_handler)); | ||||||
| // Render should not be called. | ||||||
| EXPECT_CALL(client, Render).Times(Exactly(0)); | ||||||
|
||||||
| EXPECT_CALL(client, Render).Times(Exactly(0)); | |
| EXPECT_CALL(client, Render).Times(0); |
Please update other locations too
Contributor
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I missed this comment. I've removed it. Didn't know that Exactly is the default (I thought the default was AtLeast. :)
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.