Flutter Windows Embedder
compositor_software_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 <vector>
7 
10 #include "flutter/shell/platform/windows/testing/engine_modifier.h"
11 #include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
12 #include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
13 #include "flutter/shell/platform/windows/testing/windows_test.h"
14 #include "gmock/gmock.h"
15 #include "gtest/gtest.h"
16 
17 namespace flutter {
18 namespace testing {
19 
20 namespace {
21 using ::testing::Return;
22 
23 class MockFlutterWindowsView : public FlutterWindowsView {
24  public:
25  MockFlutterWindowsView(FlutterWindowsEngine* engine,
26  std::unique_ptr<WindowBindingHandler> window)
27  : FlutterWindowsView(kImplicitViewId, engine, std::move(window)) {}
28  virtual ~MockFlutterWindowsView() = default;
29 
30  MOCK_METHOD(bool,
31  PresentSoftwareBitmap,
32  (const void* allocation, size_t row_bytes, size_t height),
33  (override));
34  MOCK_METHOD(bool, ClearSoftwareBitmap, (), (override));
35 
36  private:
37  FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsView);
38 };
39 
40 class CompositorSoftwareTest : public WindowsTest {
41  public:
42  CompositorSoftwareTest() = default;
43  virtual ~CompositorSoftwareTest() = default;
44 
45  protected:
46  FlutterWindowsEngine* engine() { return engine_.get(); }
47  MockFlutterWindowsView* view() { return view_.get(); }
48 
49  void UseHeadlessEngine() {
50  FlutterWindowsEngineBuilder builder{GetContext()};
51 
52  engine_ = builder.Build();
53  }
54 
55  void UseEngineWithView() {
56  FlutterWindowsEngineBuilder builder{GetContext()};
57 
58  auto window = std::make_unique<MockWindowBindingHandler>();
59  EXPECT_CALL(*window.get(), SetView).Times(1);
60  EXPECT_CALL(*window.get(), GetWindowHandle).WillRepeatedly(Return(nullptr));
61 
62  engine_ = builder.Build();
63  view_ = std::make_unique<MockFlutterWindowsView>(engine_.get(),
64  std::move(window));
65 
66  EngineModifier modifier{engine_.get()};
67  modifier.SetImplicitView(view_.get());
68  }
69 
70  private:
71  std::unique_ptr<FlutterWindowsEngine> engine_;
72  std::unique_ptr<MockFlutterWindowsView> view_;
73 
74  FML_DISALLOW_COPY_AND_ASSIGN(CompositorSoftwareTest);
75 };
76 
77 } // namespace
78 
79 TEST_F(CompositorSoftwareTest, CreateBackingStore) {
80  UseHeadlessEngine();
81 
82  auto compositor = CompositorSoftware{engine()};
83 
84  FlutterBackingStoreConfig config = {};
85  FlutterBackingStore backing_store = {};
86 
87  ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
88  ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
89 }
90 
91 TEST_F(CompositorSoftwareTest, Present) {
92  UseEngineWithView();
93 
94  auto compositor = CompositorSoftware{engine()};
95 
96  FlutterBackingStoreConfig config = {};
97  FlutterBackingStore backing_store = {};
98 
99  ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
100 
101  FlutterLayer layer = {};
102  layer.type = kFlutterLayerContentTypeBackingStore;
103  layer.backing_store = &backing_store;
104  const FlutterLayer* layer_ptr = &layer;
105 
106  EXPECT_CALL(*view(), PresentSoftwareBitmap).WillOnce(Return(true));
107  EXPECT_TRUE(compositor.Present(view()->view_id(), &layer_ptr, 1));
108 
109  ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
110 }
111 
112 TEST_F(CompositorSoftwareTest, PresentEmpty) {
113  UseEngineWithView();
114 
115  auto compositor = CompositorSoftware{engine()};
116 
117  EXPECT_CALL(*view(), ClearSoftwareBitmap).WillOnce(Return(true));
118  EXPECT_TRUE(compositor.Present(view()->view_id(), nullptr, 0));
119 }
120 
121 TEST_F(CompositorSoftwareTest, UnknownViewIgnored) {
122  UseEngineWithView();
123 
124  auto compositor = CompositorSoftware{engine()};
125 
126  FlutterBackingStoreConfig config = {};
127  FlutterBackingStore backing_store = {};
128 
129  ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
130 
131  FlutterLayer layer = {};
132  layer.type = kFlutterLayerContentTypeBackingStore;
133  layer.backing_store = &backing_store;
134  const FlutterLayer* layer_ptr = &layer;
135 
136  FlutterViewId unknown_view_id = 123;
137  ASSERT_NE(view()->view_id(), unknown_view_id);
138  ASSERT_EQ(engine()->view(unknown_view_id), nullptr);
139 
140  EXPECT_FALSE(compositor.Present(unknown_view_id, &layer_ptr, 1));
141 
142  ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
143 }
144 
145 TEST_F(CompositorSoftwareTest, HeadlessPresentIgnored) {
146  UseHeadlessEngine();
147 
148  auto compositor = CompositorSoftware{engine()};
149 
150  FlutterBackingStoreConfig config = {};
151  FlutterBackingStore backing_store = {};
152 
153  ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
154 
155  FlutterLayer layer = {};
156  layer.type = kFlutterLayerContentTypeBackingStore;
157  layer.backing_store = &backing_store;
158  const FlutterLayer* layer_ptr = &layer;
159 
160  EXPECT_FALSE(compositor.Present(kImplicitViewId, &layer_ptr, 1));
161 
162  ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
163 }
164 
165 } // namespace testing
166 } // namespace flutter
flutter::kImplicitViewId
constexpr FlutterViewId kImplicitViewId
Definition: flutter_windows_engine.h:54
flutter::FlutterWindowsView::FlutterWindowsView
FlutterWindowsView(FlutterViewId view_id, FlutterWindowsEngine *engine, std::unique_ptr< WindowBindingHandler > window_binding, std::shared_ptr< WindowsProcTable > windows_proc_table=nullptr)
Definition: flutter_windows_view.cc:86
flutter::CompositorSoftware
Definition: compositor_software.h:16
flutter::testing::MockFlutterWindowsView::MockFlutterWindowsView
MockFlutterWindowsView(FlutterWindowsEngine *engine, std::unique_ptr< WindowBindingHandler > wbh)
Definition: flutter_windows_engine_unittests.cc:641
flutter_windows_view.h
flutter::FlutterViewId
int64_t FlutterViewId
Definition: flutter_view.h:13
flutter
Definition: accessibility_bridge_windows.cc:11
compositor_software.h
flutter::testing::MockFlutterWindowsView::MOCK_METHOD
MOCK_METHOD(void, NotifyWinEventWrapper,(ui::AXPlatformNodeWin *, ax::mojom::Event),(override))
flutter::testing::TEST_F
TEST_F(CompositorOpenGLTest, CreateBackingStore)
Definition: compositor_opengl_unittests.cc:128