1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | #define FML_USED_ON_EMBEDDER |
6 | |
7 | #include <memory> |
8 | |
9 | #include "flutter/lib/ui/window/platform_configuration.h" |
10 | |
11 | #include "flutter/common/task_runners.h" |
12 | #include "flutter/fml/synchronization/waitable_event.h" |
13 | #include "flutter/lib/ui/painting/vertices.h" |
14 | #include "flutter/runtime/dart_vm.h" |
15 | #include "flutter/shell/common/shell_test.h" |
16 | #include "flutter/shell/common/thread_host.h" |
17 | #include "flutter/testing/testing.h" |
18 | |
19 | namespace flutter { |
20 | namespace testing { |
21 | |
22 | class DummyPlatformConfigurationClient : public PlatformConfigurationClient { |
23 | public: |
24 | DummyPlatformConfigurationClient() { |
25 | std::vector<uint8_t> data; |
26 | isolate_data_.reset(new ::fml::DataMapping(data)); |
27 | } |
28 | std::string DefaultRouteName() override { return "TestRoute" ; } |
29 | void ScheduleFrame() override {} |
30 | void Render(Scene* scene) override {} |
31 | void UpdateSemantics(SemanticsUpdate* update) override {} |
32 | void HandlePlatformMessage(fml::RefPtr<PlatformMessage> message) override {} |
33 | FontCollection& GetFontCollection() override { return font_collection_; } |
34 | void UpdateIsolateDescription(const std::string isolate_name, |
35 | int64_t isolate_port) override {} |
36 | void SetNeedsReportTimings(bool value) override {} |
37 | std::shared_ptr<const fml::Mapping> GetPersistentIsolateData() override { |
38 | return isolate_data_; |
39 | } |
40 | std::unique_ptr<std::vector<std::string>> ComputePlatformResolvedLocale( |
41 | const std::vector<std::string>& supported_locale_data) override { |
42 | return nullptr; |
43 | }; |
44 | |
45 | private: |
46 | FontCollection font_collection_; |
47 | std::shared_ptr<const fml::Mapping> isolate_data_; |
48 | }; |
49 | |
50 | TEST_F(ShellTest, PlatformConfigurationInitialization) { |
51 | auto message_latch = std::make_shared<fml::AutoResetWaitableEvent>(); |
52 | |
53 | auto nativeValidateConfiguration = [message_latch]( |
54 | Dart_NativeArguments args) { |
55 | PlatformConfiguration* configuration = |
56 | UIDartState::Current()->platform_configuration(); |
57 | ASSERT_NE(configuration->window(), nullptr); |
58 | ASSERT_EQ(configuration->window()->viewport_metrics().device_pixel_ratio, |
59 | 1.0); |
60 | ASSERT_EQ(configuration->window()->viewport_metrics().physical_width, 0.0); |
61 | ASSERT_EQ(configuration->window()->viewport_metrics().physical_height, 0.0); |
62 | |
63 | message_latch->Signal(); |
64 | }; |
65 | |
66 | Settings settings = CreateSettingsForFixture(); |
67 | TaskRunners task_runners("test" , // label |
68 | GetCurrentTaskRunner(), // platform |
69 | CreateNewThread(), // raster |
70 | CreateNewThread(), // ui |
71 | CreateNewThread() // io |
72 | ); |
73 | |
74 | AddNativeCallback("ValidateConfiguration" , |
75 | CREATE_NATIVE_ENTRY(nativeValidateConfiguration)); |
76 | |
77 | std::unique_ptr<Shell> shell = |
78 | CreateShell(std::move(settings), std::move(task_runners)); |
79 | |
80 | ASSERT_TRUE(shell->IsSetup()); |
81 | auto run_configuration = RunConfiguration::InferFromSettings(settings); |
82 | run_configuration.SetEntrypoint("validateConfiguration" ); |
83 | |
84 | shell->RunEngine(std::move(run_configuration), [&](auto result) { |
85 | ASSERT_EQ(result, Engine::RunStatus::Success); |
86 | }); |
87 | |
88 | message_latch->Wait(); |
89 | DestroyShell(std::move(shell), std::move(task_runners)); |
90 | } |
91 | |
92 | TEST_F(ShellTest, PlatformConfigurationWindowMetricsUpdate) { |
93 | auto message_latch = std::make_shared<fml::AutoResetWaitableEvent>(); |
94 | |
95 | auto nativeValidateConfiguration = [message_latch]( |
96 | Dart_NativeArguments args) { |
97 | PlatformConfiguration* configuration = |
98 | UIDartState::Current()->platform_configuration(); |
99 | |
100 | ASSERT_NE(configuration->window(), nullptr); |
101 | configuration->window()->UpdateWindowMetrics( |
102 | ViewportMetrics{2.0, 10.0, 20.0}); |
103 | ASSERT_EQ(configuration->window()->viewport_metrics().device_pixel_ratio, |
104 | 2.0); |
105 | ASSERT_EQ(configuration->window()->viewport_metrics().physical_width, 10.0); |
106 | ASSERT_EQ(configuration->window()->viewport_metrics().physical_height, |
107 | 20.0); |
108 | |
109 | message_latch->Signal(); |
110 | }; |
111 | |
112 | Settings settings = CreateSettingsForFixture(); |
113 | TaskRunners task_runners("test" , // label |
114 | GetCurrentTaskRunner(), // platform |
115 | CreateNewThread(), // raster |
116 | CreateNewThread(), // ui |
117 | CreateNewThread() // io |
118 | ); |
119 | |
120 | AddNativeCallback("ValidateConfiguration" , |
121 | CREATE_NATIVE_ENTRY(nativeValidateConfiguration)); |
122 | |
123 | std::unique_ptr<Shell> shell = |
124 | CreateShell(std::move(settings), std::move(task_runners)); |
125 | |
126 | ASSERT_TRUE(shell->IsSetup()); |
127 | auto run_configuration = RunConfiguration::InferFromSettings(settings); |
128 | run_configuration.SetEntrypoint("validateConfiguration" ); |
129 | |
130 | shell->RunEngine(std::move(run_configuration), [&](auto result) { |
131 | ASSERT_EQ(result, Engine::RunStatus::Success); |
132 | }); |
133 | |
134 | message_latch->Wait(); |
135 | DestroyShell(std::move(shell), std::move(task_runners)); |
136 | } |
137 | |
138 | } // namespace testing |
139 | } // namespace flutter |
140 | |