Flutter Windows Embedder
flutter_view_controller_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 
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 TestWindowsApi : public testing::StubFlutterWindowsApi {
18  public:
19  // |flutter::testing::StubFlutterWindowsApi|
20  FlutterDesktopViewControllerRef ViewControllerCreate(
21  int width,
22  int height,
23  FlutterDesktopEngineRef engine) override {
24  return reinterpret_cast<FlutterDesktopViewControllerRef>(2);
25  }
26 
27  // |flutter::testing::StubFlutterWindowsApi|
28  void ViewControllerDestroy() override { view_controller_destroyed_ = true; }
29 
30  // |flutter::testing::StubFlutterWindowsApi|
31  void ViewControllerForceRedraw() override {
32  view_controller_force_redrawed_ = true;
33  }
34 
35  // |flutter::testing::StubFlutterWindowsApi|
36  FlutterDesktopEngineRef EngineCreate(
37  const FlutterDesktopEngineProperties& engine_properties) override {
38  return reinterpret_cast<FlutterDesktopEngineRef>(1);
39  }
40 
41  // |flutter::testing::StubFlutterWindowsApi|
42  bool EngineDestroy() override {
43  engine_destroyed_ = true;
44  return true;
45  }
46 
47  bool engine_destroyed() { return engine_destroyed_; }
48  bool view_controller_destroyed() { return view_controller_destroyed_; }
49  bool view_controller_force_redrawed() {
50  return view_controller_force_redrawed_;
51  }
52 
53  private:
54  bool engine_destroyed_ = false;
55  bool view_controller_destroyed_ = false;
56  bool view_controller_force_redrawed_ = false;
57 };
58 
59 } // namespace
60 
61 TEST(FlutterViewControllerTest, CreateDestroy) {
62  DartProject project(L"data");
63  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
64  std::make_unique<TestWindowsApi>());
65  auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
66  { FlutterViewController controller(100, 100, project); }
67  EXPECT_TRUE(test_api->view_controller_destroyed());
68  // Per the C API, once a view controller has taken ownership of an engine
69  // the engine destruction method should not be called.
70  EXPECT_FALSE(test_api->engine_destroyed());
71 }
72 
73 TEST(FlutterViewControllerTest, GetViewId) {
74  DartProject project(L"data");
75  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
76  std::make_unique<TestWindowsApi>());
77  auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
78  FlutterViewController controller(100, 100, project);
79  EXPECT_EQ(controller.view_id(), 1);
80 }
81 
82 TEST(FlutterViewControllerTest, GetEngine) {
83  DartProject project(L"data");
84  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
85  std::make_unique<TestWindowsApi>());
86  auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
87  FlutterViewController controller(100, 100, project);
88  EXPECT_NE(controller.engine(), nullptr);
89 }
90 
91 TEST(FlutterViewControllerTest, GetView) {
92  DartProject project(L"data");
93  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
94  std::make_unique<TestWindowsApi>());
95  auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
96  FlutterViewController controller(100, 100, project);
97  EXPECT_NE(controller.view(), nullptr);
98 }
99 
100 TEST(FlutterViewControllerTest, ForceRedraw) {
101  DartProject project(L"data");
102  testing::ScopedStubFlutterWindowsApi scoped_api_stub(
103  std::make_unique<TestWindowsApi>());
104  auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
105  FlutterViewController controller(100, 100, project);
106 
107  controller.ForceRedraw();
108  EXPECT_TRUE(test_api->view_controller_force_redrawed());
109 }
110 
111 } // namespace flutter
flutter::FlutterViewController::view_id
FlutterViewId view_id() const
Definition: flutter_view_controller.cc:32
FlutterDesktopEngineProperties
Definition: flutter_windows.h:39
flutter::DartProject
Definition: dart_project.h:14
flutter::TEST
TEST(FlutterEngineTest, CreateDestroy)
Definition: flutter_engine_unittests.cc:103
FlutterDesktopEngineRef
struct FlutterDesktopEngine * FlutterDesktopEngineRef
Definition: flutter_windows.h:33
flutter::FlutterViewController
Definition: flutter_view_controller.h:25
flutter
Definition: accessibility_bridge_windows.cc:11
FlutterDesktopViewControllerRef
struct FlutterDesktopViewController * FlutterDesktopViewControllerRef
Definition: flutter_windows.h:25
flutter::FlutterViewController::engine
FlutterEngine * engine() const
Definition: flutter_view_controller.h:45
flutter::FlutterViewController::view
FlutterView * view() const
Definition: flutter_view_controller.h:48
flutter_view_controller.h
flutter::FlutterViewController::ForceRedraw
void ForceRedraw()
Definition: flutter_view_controller.cc:38