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/glfw/client_wrapper/include/flutter/flutter_engine.h"
9 #include "flutter/shell/platform/glfw/client_wrapper/testing/stub_flutter_glfw_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 TestGlfwApi : public testing::StubFlutterGlfwApi {
18  public:
19  // |flutter::testing::StubFlutterGlfwApi|
20  FlutterDesktopEngineRef RunEngine(
21  const FlutterDesktopEngineProperties& properties) override {
22  run_called_ = true;
23  return reinterpret_cast<FlutterDesktopEngineRef>(1);
24  }
25 
26  // |flutter::testing::StubFlutterGlfwApi|
27  void RunEngineEventLoopWithTimeout(uint32_t millisecond_timeout) override {
28  last_run_loop_timeout_ = millisecond_timeout;
29  }
30 
31  // |flutter::testing::StubFlutterGlfwApi|
32  bool ShutDownEngine() override {
33  shut_down_called_ = true;
34  return true;
35  }
36 
37  bool run_called() { return run_called_; }
38 
39  bool shut_down_called() { return shut_down_called_; }
40 
41  uint32_t last_run_loop_timeout() { return last_run_loop_timeout_; }
42 
43  private:
44  bool run_called_ = false;
45  bool shut_down_called_ = false;
46  uint32_t last_run_loop_timeout_ = 0;
47 };
48 
49 } // namespace
50 
51 TEST(FlutterEngineTest, CreateDestroy) {
52  const std::string icu_data_path = "fake/path/to/icu";
53  const std::string assets_path = "fake/path/to/assets";
54  testing::ScopedStubFlutterGlfwApi scoped_api_stub(
55  std::make_unique<TestGlfwApi>());
56  auto test_api = static_cast<TestGlfwApi*>(scoped_api_stub.stub());
57  {
59  engine.Start(icu_data_path, assets_path, {});
60  EXPECT_EQ(test_api->run_called(), true);
61  EXPECT_EQ(test_api->shut_down_called(), false);
62  }
63  // Destroying should implicitly shut down if it hasn't been done manually.
64  EXPECT_EQ(test_api->shut_down_called(), true);
65 }
66 
67 TEST(FlutterEngineTest, ExplicitShutDown) {
68  const std::string icu_data_path = "fake/path/to/icu";
69  const std::string assets_path = "fake/path/to/assets";
70  testing::ScopedStubFlutterGlfwApi scoped_api_stub(
71  std::make_unique<TestGlfwApi>());
72  auto test_api = static_cast<TestGlfwApi*>(scoped_api_stub.stub());
73 
75  engine.Start(icu_data_path, assets_path, {});
76  EXPECT_EQ(test_api->run_called(), true);
77  EXPECT_EQ(test_api->shut_down_called(), false);
78  engine.ShutDown();
79  EXPECT_EQ(test_api->shut_down_called(), true);
80 }
81 
82 TEST(FlutterEngineTest, RunloopTimeoutTranslation) {
83  const std::string icu_data_path = "fake/path/to/icu";
84  const std::string assets_path = "fake/path/to/assets";
85  testing::ScopedStubFlutterGlfwApi scoped_api_stub(
86  std::make_unique<TestGlfwApi>());
87  auto test_api = static_cast<TestGlfwApi*>(scoped_api_stub.stub());
88 
90  engine.Start(icu_data_path, assets_path, {});
91 
92  engine.RunEventLoopWithTimeout(std::chrono::milliseconds(100));
93  EXPECT_EQ(test_api->last_run_loop_timeout(), 100U);
94 
95  engine.RunEventLoopWithTimeout(std::chrono::milliseconds::max() -
96  std::chrono::milliseconds(1));
97  EXPECT_EQ(test_api->last_run_loop_timeout(), UINT32_MAX);
98 
99  engine.RunEventLoopWithTimeout(std::chrono::milliseconds::max());
100  EXPECT_EQ(test_api->last_run_loop_timeout(), 0U);
101 }
102 
103 } // namespace flutter
bool Start(const std::string &icu_data_path, const std::string &assets_path, const std::vector< std::string > &arguments, const std::string &aot_library_path="")
void RunEventLoopWithTimeout(std::chrono::milliseconds timeout=std::chrono::milliseconds::max())
TEST(DartServiceIsolateTest, CanAddAndRemoveHandles)