Flutter Engine
flutter_engine_unittests.cc
Go to the documentation of this file.
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 #include <memory>
6 #include <string>
7 
8 #include "flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_engine.h"
9 #include "flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h"
10 #include "gtest/gtest.h"
11 
12 namespace flutter {
13 
14 namespace {
15 
16 // Stub implementation to validate calls to the API.
17 class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi {
18  public:
19  // |flutter::testing::StubFlutterWindowsApi|
20  FlutterDesktopEngineRef EngineCreate(
21  const FlutterDesktopEngineProperties& engine_properties) {
22  create_called_ = true;
23 
24  // dart_entrypoint_argv is only guaranteed to exist until this method
25  // returns, so copy it here for unit test validation
26  dart_entrypoint_arguments_.clear();
27  for (int i = 0; i < engine_properties.dart_entrypoint_argc; i++) {
28  dart_entrypoint_arguments_.push_back(
29  std::string(engine_properties.dart_entrypoint_argv[i]));
30  }
31  return reinterpret_cast<FlutterDesktopEngineRef>(1);
32  }
33 
34  // |flutter::testing::StubFlutterWindowsApi|
35  bool EngineRun(const char* entry_point) override {
36  run_called_ = true;
37  return true;
38  }
39 
40  // |flutter::testing::StubFlutterWindowsApi|
41  bool EngineDestroy() override {
42  destroy_called_ = true;
43  return true;
44  }
45 
46  // |flutter::testing::StubFlutterWindowsApi|
47  uint64_t EngineProcessMessages() override { return 99; }
48 
49  // |flutter::testing::StubFlutterWindowsApi|
50  void EngineReloadSystemFonts() override { reload_fonts_called_ = true; }
51 
52  // |flutter::testing::StubFlutterWindowsApi|
53  void EngineReloadPlatformBrightness() override {
54  reload_brightness_called_ = true;
55  }
56 
57  bool create_called() { return create_called_; }
58 
59  bool run_called() { return run_called_; }
60 
61  bool destroy_called() { return destroy_called_; }
62 
63  bool reload_fonts_called() { return reload_fonts_called_; }
64 
65  bool reload_brightness_called() { return reload_brightness_called_; }
66 
67  const std::vector<std::string>& dart_entrypoint_arguments() {
68  return dart_entrypoint_arguments_;
69  }
70 
71  private:
72  bool create_called_ = false;
73  bool run_called_ = false;
74  bool destroy_called_ = false;
75  bool reload_fonts_called_ = false;
76  bool reload_brightness_called_ = false;
77  std::vector<std::string> dart_entrypoint_arguments_;
78 };
79 
80 } // namespace
81 
82 TEST(FlutterEngineTest, CreateDestroy) {
83  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
84  std::make_unique<TestFlutterWindowsApi>());
85  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
86  {
87  FlutterEngine engine(DartProject(L"fake/project/path"));
88  engine.Run();
89  EXPECT_EQ(test_api->create_called(), true);
90  EXPECT_EQ(test_api->run_called(), true);
91  EXPECT_EQ(test_api->destroy_called(), false);
92  }
93  // Destroying should implicitly shut down if it hasn't been done manually.
94  EXPECT_EQ(test_api->destroy_called(), true);
95 }
96 
97 TEST(FlutterEngineTest, ExplicitShutDown) {
98  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
99  std::make_unique<TestFlutterWindowsApi>());
100  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
101 
102  FlutterEngine engine(DartProject(L"fake/project/path"));
103  engine.Run();
104  EXPECT_EQ(test_api->create_called(), true);
105  EXPECT_EQ(test_api->run_called(), true);
106  EXPECT_EQ(test_api->destroy_called(), false);
107  engine.ShutDown();
108  EXPECT_EQ(test_api->destroy_called(), true);
109 }
110 
111 TEST(FlutterEngineTest, ProcessMessages) {
112  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
113  std::make_unique<TestFlutterWindowsApi>());
114  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
115 
116  FlutterEngine engine(DartProject(L"fake/project/path"));
117  engine.Run();
118 
119  std::chrono::nanoseconds next_event_time = engine.ProcessMessages();
120  EXPECT_EQ(next_event_time.count(), 99);
121 }
122 
123 TEST(FlutterEngineTest, ReloadFonts) {
124  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
125  std::make_unique<TestFlutterWindowsApi>());
126  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
127 
128  FlutterEngine engine(DartProject(L"fake/project/path"));
129  engine.Run();
130 
131  engine.ReloadSystemFonts();
132  EXPECT_TRUE(test_api->reload_fonts_called());
133 }
134 
135 TEST(FlutterEngineTest, ReloadBrightness) {
136  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
137  std::make_unique<TestFlutterWindowsApi>());
138  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
139 
140  FlutterEngine engine(DartProject(L"fake/project/path"));
141  engine.Run();
142 
143  engine.ReloadPlatformBrightness();
144  EXPECT_TRUE(test_api->reload_brightness_called());
145 }
146 
147 TEST(FlutterEngineTest, GetMessenger) {
148  DartProject project(L"data");
149  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
150  std::make_unique<TestFlutterWindowsApi>());
151  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
152 
153  FlutterEngine engine(DartProject(L"fake/project/path"));
154  EXPECT_NE(engine.messenger(), nullptr);
155 }
156 
157 TEST(FlutterEngineTest, DartEntrypointArgs) {
158  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
159  std::make_unique<TestFlutterWindowsApi>());
160  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
161 
162  DartProject project(L"data");
163  std::vector<std::string> arguments = {"one", "two"};
164  project.set_dart_entrypoint_arguments(arguments);
165 
166  FlutterEngine engine(project);
167  const std::vector<std::string>& arguments_ref =
168  test_api->dart_entrypoint_arguments();
169  ASSERT_EQ(2, arguments_ref.size());
170  EXPECT_TRUE(arguments[0] == arguments_ref[0]);
171  EXPECT_TRUE(arguments[1] == arguments_ref[1]);
172 }
173 
174 } // namespace flutter
BinaryMessenger * messenger()
bool Run(const char *entry_point=nullptr)
std::chrono::nanoseconds ProcessMessages()
TEST(DartServiceIsolateTest, CanAddAndRemoveHandles)
void set_dart_entrypoint_arguments(std::vector< std::string > arguments)
Definition: dart_project.h:52