Flutter Engine
The 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
12namespace flutter {
13
14namespace {
15
16// Stub implementation to validate calls to the API.
17class TestGlfwApi : public testing::StubFlutterGlfwApi {
18 public:
19 // |flutter::testing::StubFlutterGlfwApi|
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
51TEST(FlutterEngineTest, CreateDestroy) {
52 const std::string icu_data_path = "fake/path/to/icu";
53 const std::string assets_path = "fake/path/to/assets";
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
67TEST(FlutterEngineTest, ExplicitShutDown) {
68 const std::string icu_data_path = "fake/path/to/icu";
69 const std::string assets_path = "fake/path/to/assets";
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);
79 EXPECT_EQ(test_api->shut_down_called(), true);
80}
81
82TEST(FlutterEngineTest, RunloopTimeoutTranslation) {
83 const std::string icu_data_path = "fake/path/to/icu";
84 const std::string assets_path = "fake/path/to/assets";
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
96 std::chrono::milliseconds(1));
97 EXPECT_EQ(test_api->last_run_loop_timeout(), UINT32_MAX);
98
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())
FlutterEngine engine
Definition: main.cc:68
static float max(float r, float g, float b)
Definition: hsl.cpp:49
TEST(FrameTimingsRecorderTest, RecordVsync)