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  return reinterpret_cast<FlutterDesktopEngineRef>(1);
24  }
25 
26  // |flutter::testing::StubFlutterWindowsApi|
27  bool EngineRun(const char* entry_point) override {
28  run_called_ = true;
29  return true;
30  }
31 
32  // |flutter::testing::StubFlutterWindowsApi|
33  bool EngineDestroy() override {
34  destroy_called_ = true;
35  return true;
36  }
37 
38  // |flutter::testing::StubFlutterWindowsApi|
39  uint64_t EngineProcessMessages() override { return 99; }
40 
41  // |flutter::testing::StubFlutterWindowsApi|
42  void EngineReloadSystemFonts() override { reload_fonts_called_ = true; }
43 
44  bool create_called() { return create_called_; }
45 
46  bool run_called() { return run_called_; }
47 
48  bool destroy_called() { return destroy_called_; }
49 
50  bool reload_fonts_called() { return reload_fonts_called_; }
51 
52  private:
53  bool create_called_ = false;
54  bool run_called_ = false;
55  bool destroy_called_ = false;
56  bool reload_fonts_called_ = false;
57 };
58 
59 } // namespace
60 
61 TEST(FlutterEngineTest, CreateDestroy) {
62  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
63  std::make_unique<TestFlutterWindowsApi>());
64  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
65  {
66  FlutterEngine engine(DartProject(L"fake/project/path"));
67  engine.Run();
68  EXPECT_EQ(test_api->create_called(), true);
69  EXPECT_EQ(test_api->run_called(), true);
70  EXPECT_EQ(test_api->destroy_called(), false);
71  }
72  // Destroying should implicitly shut down if it hasn't been done manually.
73  EXPECT_EQ(test_api->destroy_called(), true);
74 }
75 
76 TEST(FlutterEngineTest, ExplicitShutDown) {
77  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
78  std::make_unique<TestFlutterWindowsApi>());
79  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
80 
81  FlutterEngine engine(DartProject(L"fake/project/path"));
82  engine.Run();
83  EXPECT_EQ(test_api->create_called(), true);
84  EXPECT_EQ(test_api->run_called(), true);
85  EXPECT_EQ(test_api->destroy_called(), false);
86  engine.ShutDown();
87  EXPECT_EQ(test_api->destroy_called(), true);
88 }
89 
90 TEST(FlutterEngineTest, ProcessMessages) {
92  std::make_unique<TestFlutterWindowsApi>());
93  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
94 
95  FlutterEngine engine(DartProject(L"fake/project/path"));
96  engine.Run();
97 
98  std::chrono::nanoseconds next_event_time = engine.ProcessMessages();
99  EXPECT_EQ(next_event_time.count(), 99);
100 }
101 
102 TEST(FlutterEngineTest, ReloadFonts) {
103  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
104  std::make_unique<TestFlutterWindowsApi>());
105  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
106 
107  FlutterEngine engine(DartProject(L"fake/project/path"));
108  engine.Run();
109 
110  engine.ReloadSystemFonts();
111  EXPECT_TRUE(test_api->reload_fonts_called());
112 }
113 
114 TEST(FlutterEngineTest, GetMessenger) {
115  DartProject project(L"data");
116  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
117  std::make_unique<TestFlutterWindowsApi>());
118  auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
119 
120  FlutterEngine engine(DartProject(L"fake/project/path"));
121  EXPECT_NE(engine.messenger(), nullptr);
122 }
123 
124 } // namespace flutter
BinaryMessenger * messenger()
bool Run(const char *entry_point=nullptr)
std::chrono::nanoseconds ProcessMessages()
TEST(DartServiceIsolateTest, CanAddAndRemoveHandles)