| Flutter Windows Embedder |

flutter_windows_view_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 "flutter/shell/platform/windows/flutter_windows_view.h"
6
7 #include <UIAutomation.h>
8 #include <comdef.h>
9 #include <comutil.h>
10 #include <oleacc.h>
11
12 #include <future>
13 #include <vector>
14
15 #include "flutter/fml/synchronization/waitable_event.h"
16 #include "flutter/shell/platform/common/json_message_codec.h"
17 #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
18 #include "flutter/shell/platform/windows/flutter_window.h"
19 #include "flutter/shell/platform/windows/flutter_windows_engine.h"
20 #include "flutter/shell/platform/windows/flutter_windows_texture_registrar.h"
21 #include "flutter/shell/platform/windows/flutter_windows_view_controller.h"
22 #include "flutter/shell/platform/windows/testing/egl/mock_context.h"
23 #include "flutter/shell/platform/windows/testing/egl/mock_manager.h"
24 #include "flutter/shell/platform/windows/testing/egl/mock_window_surface.h"
25 #include "flutter/shell/platform/windows/testing/engine_modifier.h"
26 #include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
27 #include "flutter/shell/platform/windows/testing/mock_windows_proc_table.h"
28 #include "flutter/shell/platform/windows/testing/test_keyboard.h"
29 #include "flutter/shell/platform/windows/testing/view_modifier.h"
30
31 #include "gmock/gmock.h"
32 #include "gtest/gtest.h"
33
34 namespace flutter {
35 namespace testing {
36
37 using ::testing::_;
38 using ::testing::InSequence;
39 using ::testing::NiceMock;
40 using ::testing::Return;
41
42 constexpr uint64_t kScanCodeKeyA = 0x1e;
43 constexpr uint64_t kVirtualKeyA = 0x41;
44
45 namespace {
46
47 // A struct to use as a FlutterPlatformMessageResponseHandle so it can keep the
48 // callbacks and user data passed to the engine's
49 // PlatformMessageCreateResponseHandle for use in the SendPlatformMessage
50 // overridden function.
51 struct TestResponseHandle {
52 FlutterDesktopBinaryReply callback;
53 void* user_data;
54 };
55
56 static bool test_response = false;
57
58 constexpr uint64_t kKeyEventFromChannel = 0x11;
59 constexpr uint64_t kKeyEventFromEmbedder = 0x22;
60 static std::vector<int> key_event_logs;
61
62 std::unique_ptr<std::vector<uint8_t>> keyHandlingResponse(bool handled) {
63 rapidjson::Document document;
64 auto& allocator = document.GetAllocator();
65 document.SetObject();
66 document.AddMember("handled", test_response, allocator);
67 return flutter::JsonMessageCodec::GetInstance().EncodeMessage(document);
68 }
69
70 // Returns a Flutter project with the required path values to create
71 // a test engine.
72 FlutterProjectBundle GetTestProject() {
73 FlutterDesktopEngineProperties properties = {};
74 properties.assets_path = L"C:\\foo\\flutter_assets";
75 properties.icu_data_path = L"C:\\foo\\icudtl.dat";
76 properties.aot_library_path = L"C:\\foo\\aot.so";
77
78 return FlutterProjectBundle{properties};
79 }
80
81 // Returns an engine instance configured with test project path values, and
82 // overridden methods for sending platform messages, so that the engine can
83 // respond as if the framework were connected.
84 std::unique_ptr<FlutterWindowsEngine> GetTestEngine(
85 std::shared_ptr<WindowsProcTable> windows_proc_table = nullptr) {
86 auto engine = std::make_unique<FlutterWindowsEngine>(
87 GetTestProject(), std::move(windows_proc_table));
88
89 EngineModifier modifier(engine.get());
90 modifier.SetEGLManager(nullptr);
91
92 auto key_response_controller = std::make_shared<MockKeyResponseController>();
93 key_response_controller->SetChannelResponse(
94 [](MockKeyResponseController::ResponseCallback callback) {
95 key_event_logs.push_back(kKeyEventFromChannel);
96 callback(test_response);
97 });
98 key_response_controller->SetEmbedderResponse(
99 [](const FlutterKeyEvent* event,
100 MockKeyResponseController::ResponseCallback callback) {
101 key_event_logs.push_back(kKeyEventFromEmbedder);
102 callback(test_response);
103 });
104 modifier.embedder_api().NotifyDisplayUpdate =
105 MOCK_ENGINE_PROC(NotifyDisplayUpdate,
106 ([engine_instance = engine.get()](
107 FLUTTER_API_SYMBOL(FlutterEngine) raw_engine,
108 const FlutterEngineDisplaysUpdateType update_type,
109 const FlutterEngineDisplay* embedder_displays,
110 size_t display_count) { return kSuccess; }));
111
112 MockEmbedderApiForKeyboard(modifier, key_response_controller);
113
114 engine->Run();
115 return engine;
116 }
117
118 class MockFlutterWindowsEngine : public FlutterWindowsEngine {
119 public:
120 explicit MockFlutterWindowsEngine(
121 std::shared_ptr<WindowsProcTable> windows_proc_table = nullptr)
122 : FlutterWindowsEngine(GetTestProject(), std::move(windows_proc_table)) {}
123
124 MOCK_METHOD(bool, running, (), (const));
125 MOCK_METHOD(bool, Stop, (), ());
126 MOCK_METHOD(void, RemoveView, (FlutterViewId view_id), ());
127 MOCK_METHOD(bool, PostRasterThreadTask, (fml::closure), (const));
128
129 private:
130 FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsEngine);
131 };
132
133 } // namespace
134
135 // Ensure that submenu buttons have their expanded/collapsed status set
136 // apropriately.
137 TEST(FlutterWindowsViewTest, SubMenuExpandedState) {
138 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
139 EngineModifier modifier(engine.get());
140 modifier.embedder_api().UpdateSemanticsEnabled =
141 [](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) {
142 return kSuccess;
143 };
144
145 auto window_binding_handler =
146 std::make_unique<NiceMock<MockWindowBindingHandler>>();
147 std::unique_ptr<FlutterWindowsView> view =
148 engine->CreateView(std::move(window_binding_handler));
149
150 // Enable semantics to instantiate accessibility bridge.
151 view->OnUpdateSemanticsEnabled(true);
152
153 auto bridge = view->accessibility_bridge().lock();
154 ASSERT_TRUE(bridge);
155
156 FlutterSemanticsNode2 root{sizeof(FlutterSemanticsNode2), 0};
157 root.id = 0;
158 root.label = "root";
159 root.hint = "";
160 root.value = "";
161 root.increased_value = "";
162 root.decreased_value = "";
163 root.child_count = 0;
164 root.custom_accessibility_actions_count = 0;
165 root.flags = static_cast<FlutterSemanticsFlag>(
166 FlutterSemanticsFlag::kFlutterSemanticsFlagHasExpandedState |
167 FlutterSemanticsFlag::kFlutterSemanticsFlagIsExpanded);
168 bridge->AddFlutterSemanticsNodeUpdate(root);
169
170 bridge->CommitUpdates();
171
172 {
173 auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
174 EXPECT_TRUE(root_node->GetData().HasState(ax::mojom::State::kExpanded));
175
176 // Get the IAccessible for the root node.
177 IAccessible* native_view = root_node->GetNativeViewAccessible();
178 ASSERT_TRUE(native_view != nullptr);
179
180 // Look up against the node itself (not one of its children).
181 VARIANT varchild = {};
182 varchild.vt = VT_I4;
183
184 // Verify the submenu is expanded.
185 varchild.lVal = CHILDID_SELF;
186 VARIANT native_state = {};
187 ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
188 EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_EXPANDED);
189
190 // Perform similar tests for UIA value;
191 IRawElementProviderSimple* uia_node;
192 native_view->QueryInterface(IID_PPV_ARGS(&uia_node));
193 ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue(
194 UIA_ExpandCollapseExpandCollapseStatePropertyId, &native_state)));
195 EXPECT_EQ(native_state.lVal, ExpandCollapseState_Expanded);
196
197 ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue(
198 UIA_AriaPropertiesPropertyId, &native_state)));
199 EXPECT_NE(std::wcsstr(native_state.bstrVal, L"expanded=true"), nullptr);
200 }
201
202 // Test collapsed too.
203 root.flags = static_cast<FlutterSemanticsFlag>(
204 FlutterSemanticsFlag::kFlutterSemanticsFlagHasExpandedState);
205 bridge->AddFlutterSemanticsNodeUpdate(root);
206 bridge->CommitUpdates();
207
208 {
209 auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
210 EXPECT_TRUE(root_node->GetData().HasState(ax::mojom::State::kCollapsed));
211
212 // Get the IAccessible for the root node.
213 IAccessible* native_view = root_node->GetNativeViewAccessible();
214 ASSERT_TRUE(native_view != nullptr);
215
216 // Look up against the node itself (not one of its children).
217 VARIANT varchild = {};
218 varchild.vt = VT_I4;
219
220 // Verify the submenu is collapsed.
221 varchild.lVal = CHILDID_SELF;
222 VARIANT native_state = {};
223 ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
224 EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_COLLAPSED);
225
226 // Perform similar tests for UIA value;
227 IRawElementProviderSimple* uia_node;
228 native_view->QueryInterface(IID_PPV_ARGS(&uia_node));
229 ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue(
230 UIA_ExpandCollapseExpandCollapseStatePropertyId, &native_state)));
231 EXPECT_EQ(native_state.lVal, ExpandCollapseState_Collapsed);
232
233 ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue(
234 UIA_AriaPropertiesPropertyId, &native_state)));
235 EXPECT_NE(std::wcsstr(native_state.bstrVal, L"expanded=false"), nullptr);
236 }
237 }
238
239 // The view's surface must be destroyed after the engine is shutdown.
240 // See: https://github.com/flutter/flutter/issues/124463
241 TEST(FlutterWindowsViewTest, Shutdown) {
242 auto engine = std::make_unique<MockFlutterWindowsEngine>();
243 auto window_binding_handler =
244 std::make_unique<NiceMock<MockWindowBindingHandler>>();
245 auto egl_manager = std::make_unique<egl::MockManager>();
246 auto surface = std::make_unique<egl::MockWindowSurface>();
247 egl::MockContext render_context;
248
249 auto engine_ptr = engine.get();
250 auto surface_ptr = surface.get();
251 auto egl_manager_ptr = egl_manager.get();
252
253 EngineModifier modifier{engine.get()};
254 modifier.SetEGLManager(std::move(egl_manager));
255
256 InSequence s;
257 std::unique_ptr<FlutterWindowsView> view;
258
259 // Mock render surface initialization.
260 {
261 EXPECT_CALL(*egl_manager_ptr, CreateWindowSurface)
262 .WillOnce(Return(std::move(surface)));
263 EXPECT_CALL(*engine_ptr, running).WillOnce(Return(false));
264 EXPECT_CALL(*surface_ptr, IsValid).WillOnce(Return(true));
265 EXPECT_CALL(*surface_ptr, MakeCurrent).WillOnce(Return(true));
266 EXPECT_CALL(*surface_ptr, SetVSyncEnabled).WillOnce(Return(true));
267 EXPECT_CALL(*egl_manager_ptr, render_context)
268 .WillOnce(Return(&render_context));
269 EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
270
271 view = engine->CreateView(std::move(window_binding_handler));
272 }
273
274 // The view must be removed before the surface can be destroyed.
275 {
276 auto view_id = view->view_id();
277 FlutterWindowsViewController controller{std::move(engine), std::move(view)};
278
279 EXPECT_CALL(*engine_ptr, running).WillOnce(Return(true));
280 EXPECT_CALL(*engine_ptr, RemoveView(view_id)).Times(1);
281 EXPECT_CALL(*engine_ptr, running).WillOnce(Return(true));
282 EXPECT_CALL(*engine_ptr, PostRasterThreadTask)
283 .WillOnce([](fml::closure callback) {
284 callback();
285 return true;
286 });
287 EXPECT_CALL(*surface_ptr, Destroy).Times(1);
288 }
289 }
290
291 TEST(FlutterWindowsViewTest, KeySequence) {
292 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
293
294 test_response = false;
295
296 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
297 std::make_unique<NiceMock<MockWindowBindingHandler>>());
298
299 view->OnKey(kVirtualKeyA, kScanCodeKeyA, WM_KEYDOWN, 'a', false, false,
300 [](bool handled) {});
301
302 EXPECT_EQ(key_event_logs.size(), 2);
303 EXPECT_EQ(key_event_logs[0], kKeyEventFromEmbedder);
304 EXPECT_EQ(key_event_logs[1], kKeyEventFromChannel);
305
306 key_event_logs.clear();
307 }
308
309 TEST(FlutterWindowsViewTest, KeyEventCallback) {
310 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
311
312 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
313 std::make_unique<NiceMock<MockWindowBindingHandler>>());
314
315 class MockCallback {
316 public:
317 MOCK_METHOD(void, Call, ());
318 };
319
320 NiceMock<MockCallback> callback_with_valid_view;
321 NiceMock<MockCallback> callback_with_invalid_view;
322
323 auto trigger_key_event = [&](NiceMock<MockCallback>& callback) {
324 view->OnKey(kVirtualKeyA, kScanCodeKeyA, WM_KEYDOWN, 'a', false, false,
325 [&](bool) { callback.Call(); });
326 };
327
328 EXPECT_CALL(callback_with_valid_view, Call()).Times(1);
329 EXPECT_CALL(callback_with_invalid_view, Call()).Times(0);
330
331 trigger_key_event(callback_with_valid_view);
332 engine->RemoveView(view->view_id());
333 trigger_key_event(callback_with_invalid_view);
334
335 key_event_logs.clear();
336 }
337
338 TEST(FlutterWindowsViewTest, EnableSemantics) {
339 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
340 EngineModifier modifier(engine.get());
341
342 bool semantics_enabled = false;
343 modifier.embedder_api().UpdateSemanticsEnabled = MOCK_ENGINE_PROC(
344 UpdateSemanticsEnabled,
345 [&semantics_enabled](FLUTTER_API_SYMBOL(FlutterEngine) engine,
346 bool enabled) {
347 semantics_enabled = enabled;
348 return kSuccess;
349 });
350
351 auto window_binding_handler =
352 std::make_unique<NiceMock<MockWindowBindingHandler>>();
353 std::unique_ptr<FlutterWindowsView> view =
354 engine->CreateView(std::move(window_binding_handler));
355
356 view->OnUpdateSemanticsEnabled(true);
357 EXPECT_TRUE(semantics_enabled);
358 }
359
360 TEST(FlutterWindowsViewTest, AddSemanticsNodeUpdate) {
361 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
362 EngineModifier modifier(engine.get());
363 modifier.embedder_api().UpdateSemanticsEnabled =
364 [](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) {
365 return kSuccess;
366 };
367
368 auto window_binding_handler =
369 std::make_unique<NiceMock<MockWindowBindingHandler>>();
370 std::unique_ptr<FlutterWindowsView> view =
371 engine->CreateView(std::move(window_binding_handler));
372
373 // Enable semantics to instantiate accessibility bridge.
374 view->OnUpdateSemanticsEnabled(true);
375
376 auto bridge = view->accessibility_bridge().lock();
377 ASSERT_TRUE(bridge);
378
379 // Add root node.
380 FlutterSemanticsNode2 node{sizeof(FlutterSemanticsNode2), 0};
381 node.label = "name";
382 node.value = "value";
383 node.platform_view_id = -1;
384 bridge->AddFlutterSemanticsNodeUpdate(node);
385 bridge->CommitUpdates();
386
387 // Look up the root windows node delegate.
388 auto node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
389 ASSERT_TRUE(node_delegate);
390 EXPECT_EQ(node_delegate->GetChildCount(), 0);
391
392 // Get the native IAccessible object.
393 IAccessible* native_view = node_delegate->GetNativeViewAccessible();
394 ASSERT_TRUE(native_view != nullptr);
395
396 // Property lookups will be made against this node itself.
397 VARIANT varchild{};
398 varchild.vt = VT_I4;
399 varchild.lVal = CHILDID_SELF;
400
401 // Verify node name matches our label.
402 BSTR bname = nullptr;
403 ASSERT_EQ(native_view->get_accName(varchild, &bname), S_OK);
404 std::string name(_com_util::ConvertBSTRToString(bname));
405 EXPECT_EQ(name, "name");
406
407 // Verify node value matches.
408 BSTR bvalue = nullptr;
409 ASSERT_EQ(native_view->get_accValue(varchild, &bvalue), S_OK);
410 std::string value(_com_util::ConvertBSTRToString(bvalue));
411 EXPECT_EQ(value, "value");
412
413 // Verify node type is static text.
414 VARIANT varrole{};
415 varrole.vt = VT_I4;
416 ASSERT_EQ(native_view->get_accRole(varchild, &varrole), S_OK);
417 EXPECT_EQ(varrole.lVal, ROLE_SYSTEM_STATICTEXT);
418
419 // Get the IRawElementProviderFragment object.
420 IRawElementProviderSimple* uia_view;
421 native_view->QueryInterface(IID_PPV_ARGS(&uia_view));
422 ASSERT_TRUE(uia_view != nullptr);
423
424 // Verify name property matches our label.
425 VARIANT varname{};
426 ASSERT_EQ(uia_view->GetPropertyValue(UIA_NamePropertyId, &varname), S_OK);
427 EXPECT_EQ(varname.vt, VT_BSTR);
428 name = _com_util::ConvertBSTRToString(varname.bstrVal);
429 EXPECT_EQ(name, "name");
430
431 // Verify value property matches our label.
432 VARIANT varvalue{};
433 ASSERT_EQ(uia_view->GetPropertyValue(UIA_ValueValuePropertyId, &varvalue),
434 S_OK);
435 EXPECT_EQ(varvalue.vt, VT_BSTR);
436 value = _com_util::ConvertBSTRToString(varvalue.bstrVal);
437 EXPECT_EQ(value, "value");
438
439 // Verify node control type is text.
440 varrole = {};
441 ASSERT_EQ(uia_view->GetPropertyValue(UIA_ControlTypePropertyId, &varrole),
442 S_OK);
443 EXPECT_EQ(varrole.vt, VT_I4);
444 EXPECT_EQ(varrole.lVal, UIA_TextControlTypeId);
445 }
446
447 // Verify the native IAccessible COM object tree is an accurate reflection of
448 // the platform-agnostic tree. Verify both a root node with children as well as
449 // a non-root node with children, since the AX tree includes special handling
450 // for the root.
451 //
452 // node0
453 // / \
454 // node1 node2
455 // |
456 // node3
457 //
458 // node0 and node2 are grouping nodes. node1 and node2 are static text nodes.
459 TEST(FlutterWindowsViewTest, AddSemanticsNodeUpdateWithChildren) {
460 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
461 EngineModifier modifier(engine.get());
462 modifier.embedder_api().UpdateSemanticsEnabled =
463 [](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) {
464 return kSuccess;
465 };
466
467 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
468 std::make_unique<NiceMock<MockWindowBindingHandler>>());
469
470 // Enable semantics to instantiate accessibility bridge.
471 view->OnUpdateSemanticsEnabled(true);
472
473 auto bridge = view->accessibility_bridge().lock();
474 ASSERT_TRUE(bridge);
475
476 // Add root node.
477 FlutterSemanticsNode2 node0{sizeof(FlutterSemanticsNode2), 0};
478 std::vector<int32_t> node0_children{1, 2};
479 node0.child_count = node0_children.size();
480 node0.children_in_traversal_order = node0_children.data();
481 node0.children_in_hit_test_order = node0_children.data();
482
483 FlutterSemanticsNode2 node1{sizeof(FlutterSemanticsNode2), 1};
484 node1.label = "prefecture";
485 node1.value = "Kyoto";
486 FlutterSemanticsNode2 node2{sizeof(FlutterSemanticsNode2), 2};
487 std::vector<int32_t> node2_children{3};
488 node2.child_count = node2_children.size();
489 node2.children_in_traversal_order = node2_children.data();
490 node2.children_in_hit_test_order = node2_children.data();
491 FlutterSemanticsNode2 node3{sizeof(FlutterSemanticsNode2), 3};
492 node3.label = "city";
493 node3.value = "Uji";
494
495 bridge->AddFlutterSemanticsNodeUpdate(node0);
496 bridge->AddFlutterSemanticsNodeUpdate(node1);
497 bridge->AddFlutterSemanticsNodeUpdate(node2);
498 bridge->AddFlutterSemanticsNodeUpdate(node3);
499 bridge->CommitUpdates();
500
501 // Look up the root windows node delegate.
502 auto node_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
503 ASSERT_TRUE(node_delegate);
504 EXPECT_EQ(node_delegate->GetChildCount(), 2);
505
506 // Get the native IAccessible object.
507 IAccessible* node0_accessible = node_delegate->GetNativeViewAccessible();
508 ASSERT_TRUE(node0_accessible != nullptr);
509
510 // Property lookups will be made against this node itself.
511 VARIANT varchild{};
512 varchild.vt = VT_I4;
513 varchild.lVal = CHILDID_SELF;
514
515 // Verify node type is a group.
516 VARIANT varrole{};
517 varrole.vt = VT_I4;
518 ASSERT_EQ(node0_accessible->get_accRole(varchild, &varrole), S_OK);
519 EXPECT_EQ(varrole.lVal, ROLE_SYSTEM_GROUPING);
520
521 // Verify child count.
522 long node0_child_count = 0;
523 ASSERT_EQ(node0_accessible->get_accChildCount(&node0_child_count), S_OK);
524 EXPECT_EQ(node0_child_count, 2);
525
526 {
527 // Look up first child of node0 (node1), a static text node.
528 varchild.lVal = 1;
529 IDispatch* node1_dispatch = nullptr;
530 ASSERT_EQ(node0_accessible->get_accChild(varchild, &node1_dispatch), S_OK);
531 ASSERT_TRUE(node1_dispatch != nullptr);
532 IAccessible* node1_accessible = nullptr;
533 ASSERT_EQ(node1_dispatch->QueryInterface(
534 IID_IAccessible, reinterpret_cast<void**>(&node1_accessible)),
535 S_OK);
536 ASSERT_TRUE(node1_accessible != nullptr);
537
538 // Verify node name matches our label.
539 varchild.lVal = CHILDID_SELF;
540 BSTR bname = nullptr;
541 ASSERT_EQ(node1_accessible->get_accName(varchild, &bname), S_OK);
542 std::string name(_com_util::ConvertBSTRToString(bname));
543 EXPECT_EQ(name, "prefecture");
544
545 // Verify node value matches.
546 BSTR bvalue = nullptr;
547 ASSERT_EQ(node1_accessible->get_accValue(varchild, &bvalue), S_OK);
548 std::string value(_com_util::ConvertBSTRToString(bvalue));
549 EXPECT_EQ(value, "Kyoto");
550
551 // Verify node type is static text.
552 VARIANT varrole{};
553 varrole.vt = VT_I4;
554 ASSERT_EQ(node1_accessible->get_accRole(varchild, &varrole), S_OK);
555 EXPECT_EQ(varrole.lVal, ROLE_SYSTEM_STATICTEXT);
556
557 // Verify the parent node is the root.
558 IDispatch* parent_dispatch;
559 node1_accessible->get_accParent(&parent_dispatch);
560 IAccessible* parent_accessible;
561 ASSERT_EQ(
562 parent_dispatch->QueryInterface(
563 IID_IAccessible, reinterpret_cast<void**>(&parent_accessible)),
564 S_OK);
565 EXPECT_EQ(parent_accessible, node0_accessible);
566 }
567
568 // Look up second child of node0 (node2), a parent group for node3.
569 varchild.lVal = 2;
570 IDispatch* node2_dispatch = nullptr;
571 ASSERT_EQ(node0_accessible->get_accChild(varchild, &node2_dispatch), S_OK);
572 ASSERT_TRUE(node2_dispatch != nullptr);
573 IAccessible* node2_accessible = nullptr;
574 ASSERT_EQ(node2_dispatch->QueryInterface(
575 IID_IAccessible, reinterpret_cast<void**>(&node2_accessible)),
576 S_OK);
577 ASSERT_TRUE(node2_accessible != nullptr);
578
579 {
580 // Verify child count.
581 long node2_child_count = 0;
582 ASSERT_EQ(node2_accessible->get_accChildCount(&node2_child_count), S_OK);
583 EXPECT_EQ(node2_child_count, 1);
584
585 // Verify node type is static text.
586 varchild.lVal = CHILDID_SELF;
587 VARIANT varrole{};
588 varrole.vt = VT_I4;
589 ASSERT_EQ(node2_accessible->get_accRole(varchild, &varrole), S_OK);
590 EXPECT_EQ(varrole.lVal, ROLE_SYSTEM_GROUPING);
591
592 // Verify the parent node is the root.
593 IDispatch* parent_dispatch;
594 node2_accessible->get_accParent(&parent_dispatch);
595 IAccessible* parent_accessible;
596 ASSERT_EQ(
597 parent_dispatch->QueryInterface(
598 IID_IAccessible, reinterpret_cast<void**>(&parent_accessible)),
599 S_OK);
600 EXPECT_EQ(parent_accessible, node0_accessible);
601 }
602
603 {
604 // Look up only child of node2 (node3), a static text node.
605 varchild.lVal = 1;
606 IDispatch* node3_dispatch = nullptr;
607 ASSERT_EQ(node2_accessible->get_accChild(varchild, &node3_dispatch), S_OK);
608 ASSERT_TRUE(node3_dispatch != nullptr);
609 IAccessible* node3_accessible = nullptr;
610 ASSERT_EQ(node3_dispatch->QueryInterface(
611 IID_IAccessible, reinterpret_cast<void**>(&node3_accessible)),
612 S_OK);
613 ASSERT_TRUE(node3_accessible != nullptr);
614
615 // Verify node name matches our label.
616 varchild.lVal = CHILDID_SELF;
617 BSTR bname = nullptr;
618 ASSERT_EQ(node3_accessible->get_accName(varchild, &bname), S_OK);
619 std::string name(_com_util::ConvertBSTRToString(bname));
620 EXPECT_EQ(name, "city");
621
622 // Verify node value matches.
623 BSTR bvalue = nullptr;
624 ASSERT_EQ(node3_accessible->get_accValue(varchild, &bvalue), S_OK);
625 std::string value(_com_util::ConvertBSTRToString(bvalue));
626 EXPECT_EQ(value, "Uji");
627
628 // Verify node type is static text.
629 VARIANT varrole{};
630 varrole.vt = VT_I4;
631 ASSERT_EQ(node3_accessible->get_accRole(varchild, &varrole), S_OK);
632 EXPECT_EQ(varrole.lVal, ROLE_SYSTEM_STATICTEXT);
633
634 // Verify the parent node is node2.
635 IDispatch* parent_dispatch;
636 node3_accessible->get_accParent(&parent_dispatch);
637 IAccessible* parent_accessible;
638 ASSERT_EQ(
639 parent_dispatch->QueryInterface(
640 IID_IAccessible, reinterpret_cast<void**>(&parent_accessible)),
641 S_OK);
642 EXPECT_EQ(parent_accessible, node2_accessible);
643 }
644 }
645
646 // Flutter used to assume that the accessibility root had ID 0.
647 // In a multi-view world, each view has its own accessibility root
648 // with a globally unique node ID.
649 //
650 // node1
651 // |
652 // node2
653 //
654 // node1 is a grouping node, node0 is a static text node.
655 TEST(FlutterWindowsViewTest, NonZeroSemanticsRoot) {
656 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
657 EngineModifier modifier(engine.get());
658 modifier.embedder_api().UpdateSemanticsEnabled =
659 [](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) {
660 return kSuccess;
661 };
662
663 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
664 std::make_unique<NiceMock<MockWindowBindingHandler>>());
665
666 // Enable semantics to instantiate accessibility bridge.
667 view->OnUpdateSemanticsEnabled(true);
668
669 auto bridge = view->accessibility_bridge().lock();
670 ASSERT_TRUE(bridge);
671
672 // Add root node.
673 FlutterSemanticsNode2 node1{sizeof(FlutterSemanticsNode2), 1};
674 std::vector<int32_t> node1_children{2};
675 node1.child_count = node1_children.size();
676 node1.children_in_traversal_order = node1_children.data();
677 node1.children_in_hit_test_order = node1_children.data();
678
679 FlutterSemanticsNode2 node2{sizeof(FlutterSemanticsNode2), 2};
680 node2.label = "prefecture";
681 node2.value = "Kyoto";
682
683 bridge->AddFlutterSemanticsNodeUpdate(node1);
684 bridge->AddFlutterSemanticsNodeUpdate(node2);
685 bridge->CommitUpdates();
686
687 // Look up the root windows node delegate.
688 auto root_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
689 ASSERT_TRUE(root_delegate);
690 EXPECT_EQ(root_delegate->GetChildCount(), 1);
691
692 // Look up the child node delegate
693 auto child_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(2).lock();
694 ASSERT_TRUE(child_delegate);
695 EXPECT_EQ(child_delegate->GetChildCount(), 0);
696
697 // Ensure a node with ID 0 does not exist.
698 auto fake_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
699 ASSERT_FALSE(fake_delegate);
700
701 // Get the root's native IAccessible object.
702 IAccessible* node1_accessible = root_delegate->GetNativeViewAccessible();
703 ASSERT_TRUE(node1_accessible != nullptr);
704
705 // Property lookups will be made against this node itself.
706 VARIANT varchild{};
707 varchild.vt = VT_I4;
708 varchild.lVal = CHILDID_SELF;
709
710 // Verify node type is a group.
711 VARIANT varrole{};
712 varrole.vt = VT_I4;
713 ASSERT_EQ(node1_accessible->get_accRole(varchild, &varrole), S_OK);
714 EXPECT_EQ(varrole.lVal, ROLE_SYSTEM_GROUPING);
715
716 // Verify child count.
717 long node1_child_count = 0;
718 ASSERT_EQ(node1_accessible->get_accChildCount(&node1_child_count), S_OK);
719 EXPECT_EQ(node1_child_count, 1);
720
721 {
722 // Look up first child of node1 (node0), a static text node.
723 varchild.lVal = 1;
724 IDispatch* node2_dispatch = nullptr;
725 ASSERT_EQ(node1_accessible->get_accChild(varchild, &node2_dispatch), S_OK);
726 ASSERT_TRUE(node2_dispatch != nullptr);
727 IAccessible* node2_accessible = nullptr;
728 ASSERT_EQ(node2_dispatch->QueryInterface(
729 IID_IAccessible, reinterpret_cast<void**>(&node2_accessible)),
730 S_OK);
731 ASSERT_TRUE(node2_accessible != nullptr);
732
733 // Verify node name matches our label.
734 varchild.lVal = CHILDID_SELF;
735 BSTR bname = nullptr;
736 ASSERT_EQ(node2_accessible->get_accName(varchild, &bname), S_OK);
737 std::string name(_com_util::ConvertBSTRToString(bname));
738 EXPECT_EQ(name, "prefecture");
739
740 // Verify node value matches.
741 BSTR bvalue = nullptr;
742 ASSERT_EQ(node2_accessible->get_accValue(varchild, &bvalue), S_OK);
743 std::string value(_com_util::ConvertBSTRToString(bvalue));
744 EXPECT_EQ(value, "Kyoto");
745
746 // Verify node type is static text.
747 VARIANT varrole{};
748 varrole.vt = VT_I4;
749 ASSERT_EQ(node2_accessible->get_accRole(varchild, &varrole), S_OK);
750 EXPECT_EQ(varrole.lVal, ROLE_SYSTEM_STATICTEXT);
751
752 // Verify the parent node is the root.
753 IDispatch* parent_dispatch;
754 node2_accessible->get_accParent(&parent_dispatch);
755 IAccessible* parent_accessible;
756 ASSERT_EQ(
757 parent_dispatch->QueryInterface(
758 IID_IAccessible, reinterpret_cast<void**>(&parent_accessible)),
759 S_OK);
760 EXPECT_EQ(parent_accessible, node1_accessible);
761 }
762 }
763
764 // Verify the native IAccessible accHitTest method returns the correct
765 // IAccessible COM object for the given coordinates.
766 //
767 // +-----------+
768 // | | |
769 // node0 | | B |
770 // / \ | A |-----|
771 // node1 node2 | | C |
772 // | | | |
773 // node3 +-----------+
774 //
775 // node0 and node2 are grouping nodes. node1 and node2 are static text nodes.
776 //
777 // node0 is located at 0,0 with size 500x500. It spans areas A, B, and C.
778 // node1 is located at 0,0 with size 250x500. It spans area A.
779 // node2 is located at 250,0 with size 250x500. It spans areas B and C.
780 // node3 is located at 250,250 with size 250x250. It spans area C.
781 TEST(FlutterWindowsViewTest, AccessibilityHitTesting) {
782 constexpr FlutterTransformation kIdentityTransform = {1, 0, 0, //
783 0, 1, 0, //
784 0, 0, 1};
785
786 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
787 EngineModifier modifier(engine.get());
788 modifier.embedder_api().UpdateSemanticsEnabled =
789 [](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) {
790 return kSuccess;
791 };
792
793 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
794 std::make_unique<NiceMock<MockWindowBindingHandler>>());
795
796 // Enable semantics to instantiate accessibility bridge.
797 view->OnUpdateSemanticsEnabled(true);
798
799 auto bridge = view->accessibility_bridge().lock();
800 ASSERT_TRUE(bridge);
801
802 // Add root node at origin. Size 500x500.
803 FlutterSemanticsNode2 node0{sizeof(FlutterSemanticsNode2), 0};
804 std::vector<int32_t> node0_children{1, 2};
805 node0.rect = {0, 0, 500, 500};
806 node0.transform = kIdentityTransform;
807 node0.child_count = node0_children.size();
808 node0.children_in_traversal_order = node0_children.data();
809 node0.children_in_hit_test_order = node0_children.data();
810
811 // Add node 1 located at 0,0 relative to node 0. Size 250x500.
812 FlutterSemanticsNode2 node1{sizeof(FlutterSemanticsNode2), 1};
813 node1.rect = {0, 0, 250, 500};
814 node1.transform = kIdentityTransform;
815 node1.label = "prefecture";
816 node1.value = "Kyoto";
817
818 // Add node 2 located at 250,0 relative to node 0. Size 250x500.
819 FlutterSemanticsNode2 node2{sizeof(FlutterSemanticsNode2), 2};
820 std::vector<int32_t> node2_children{3};
821 node2.rect = {0, 0, 250, 500};
822 node2.transform = {1, 0, 250, 0, 1, 0, 0, 0, 1};
823 node2.child_count = node2_children.size();
824 node2.children_in_traversal_order = node2_children.data();
825 node2.children_in_hit_test_order = node2_children.data();
826
827 // Add node 3 located at 0,250 relative to node 2. Size 250, 250.
828 FlutterSemanticsNode2 node3{sizeof(FlutterSemanticsNode2), 3};
829 node3.rect = {0, 0, 250, 250};
830 node3.transform = {1, 0, 0, 0, 1, 250, 0, 0, 1};
831 node3.label = "city";
832 node3.value = "Uji";
833
834 bridge->AddFlutterSemanticsNodeUpdate(node0);
835 bridge->AddFlutterSemanticsNodeUpdate(node1);
836 bridge->AddFlutterSemanticsNodeUpdate(node2);
837 bridge->AddFlutterSemanticsNodeUpdate(node3);
838 bridge->CommitUpdates();
839
840 // Look up the root windows node delegate.
841 auto node0_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
842 ASSERT_TRUE(node0_delegate);
843 auto node1_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock();
844 ASSERT_TRUE(node1_delegate);
845 auto node2_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(2).lock();
846 ASSERT_TRUE(node2_delegate);
847 auto node3_delegate = bridge->GetFlutterPlatformNodeDelegateFromID(3).lock();
848 ASSERT_TRUE(node3_delegate);
849
850 // Get the native IAccessible root object.
851 IAccessible* node0_accessible = node0_delegate->GetNativeViewAccessible();
852 ASSERT_TRUE(node0_accessible != nullptr);
853
854 // Perform a hit test that should hit node 1.
855 VARIANT varchild{};
856 ASSERT_TRUE(SUCCEEDED(node0_accessible->accHitTest(150, 150, &varchild)));
857 EXPECT_EQ(varchild.vt, VT_DISPATCH);
858 EXPECT_EQ(varchild.pdispVal, node1_delegate->GetNativeViewAccessible());
859
860 // Perform a hit test that should hit node 2.
861 varchild = {};
862 ASSERT_TRUE(SUCCEEDED(node0_accessible->accHitTest(450, 150, &varchild)));
863 EXPECT_EQ(varchild.vt, VT_DISPATCH);
864 EXPECT_EQ(varchild.pdispVal, node2_delegate->GetNativeViewAccessible());
865
866 // Perform a hit test that should hit node 3.
867 varchild = {};
868 ASSERT_TRUE(SUCCEEDED(node0_accessible->accHitTest(450, 450, &varchild)));
869 EXPECT_EQ(varchild.vt, VT_DISPATCH);
870 EXPECT_EQ(varchild.pdispVal, node3_delegate->GetNativeViewAccessible());
871 }
872
873 TEST(FlutterWindowsViewTest, WindowResizeTests) {
874 auto windows_proc_table = std::make_shared<NiceMock<MockWindowsProcTable>>();
875 std::unique_ptr<FlutterWindowsEngine> engine =
876 GetTestEngine(windows_proc_table);
877
878 EngineModifier engine_modifier{engine.get()};
879 engine_modifier.embedder_api().PostRenderThreadTask = MOCK_ENGINE_PROC(
880 PostRenderThreadTask,
881 ([](auto engine, VoidCallback callback, void* user_data) {
882 callback(user_data);
883 return kSuccess;
884 }));
885
886 auto egl_manager = std::make_unique<egl::MockManager>();
887 auto surface = std::make_unique<egl::MockWindowSurface>();
888 auto resized_surface = std::make_unique<egl::MockWindowSurface>();
889 egl::MockContext render_context;
890
891 auto surface_ptr = surface.get();
892 auto resized_surface_ptr = resized_surface.get();
893
894 // Mock render surface creation
895 EXPECT_CALL(*egl_manager, CreateWindowSurface)
896 .WillOnce(Return(std::move(surface)));
897 EXPECT_CALL(*surface_ptr, IsValid).WillRepeatedly(Return(true));
898 EXPECT_CALL(*surface_ptr, MakeCurrent).WillOnce(Return(true));
899 EXPECT_CALL(*surface_ptr, SetVSyncEnabled).WillOnce(Return(true));
900 EXPECT_CALL(*egl_manager, render_context).WillOnce(Return(&render_context));
901 EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
902
903 // Mock render surface resize
904 EXPECT_CALL(*surface_ptr, Destroy).WillOnce(Return(true));
905 EXPECT_CALL(*egl_manager.get(),
906 CreateWindowSurface(_, /*width=*/500, /*height=*/500))
907 .WillOnce(Return(std::move((resized_surface))));
908 EXPECT_CALL(*resized_surface_ptr, MakeCurrent).WillOnce(Return(true));
909 EXPECT_CALL(*resized_surface_ptr, SetVSyncEnabled).WillOnce(Return(true));
910 EXPECT_CALL(*windows_proc_table.get(), DwmFlush).WillOnce(Return(S_OK));
911
912 EXPECT_CALL(*resized_surface_ptr, Destroy).WillOnce(Return(true));
913
914 engine_modifier.SetEGLManager(std::move(egl_manager));
915
916 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
917 std::make_unique<NiceMock<MockWindowBindingHandler>>());
918
919 fml::AutoResetWaitableEvent metrics_sent_latch;
920 engine_modifier.embedder_api().SendWindowMetricsEvent = MOCK_ENGINE_PROC(
921 SendWindowMetricsEvent,
922 ([&metrics_sent_latch](auto engine,
923 const FlutterWindowMetricsEvent* event) {
924 metrics_sent_latch.Signal();
925 return kSuccess;
926 }));
927
928 fml::AutoResetWaitableEvent resized_latch;
929 std::thread([&resized_latch, &view]() {
930 // Start the window resize. This sends the new window metrics
931 // and then blocks until another thread completes the window resize.
932 EXPECT_TRUE(view->OnWindowSizeChanged(500, 500));
933 resized_latch.Signal();
934 }).detach();
935
936 // Wait until the platform thread has started the window resize.
937 metrics_sent_latch.Wait();
938
939 // Complete the window resize by reporting a frame with the new window size.
940 ASSERT_TRUE(view->OnFrameGenerated(500, 500));
941 view->OnFramePresented();
942 resized_latch.Wait();
943 }
944
945 // Verify that an empty frame completes a view resize.
946 TEST(FlutterWindowsViewTest, TestEmptyFrameResizes) {
947 auto windows_proc_table = std::make_shared<NiceMock<MockWindowsProcTable>>();
948 std::unique_ptr<FlutterWindowsEngine> engine =
949 GetTestEngine(windows_proc_table);
950
951 EngineModifier engine_modifier{engine.get()};
952 engine_modifier.embedder_api().PostRenderThreadTask = MOCK_ENGINE_PROC(
953 PostRenderThreadTask,
954 ([](auto engine, VoidCallback callback, void* user_data) {
955 callback(user_data);
956 return kSuccess;
957 }));
958
959 auto egl_manager = std::make_unique<egl::MockManager>();
960 auto surface = std::make_unique<egl::MockWindowSurface>();
961 auto resized_surface = std::make_unique<egl::MockWindowSurface>();
962 auto resized_surface_ptr = resized_surface.get();
963
964 EXPECT_CALL(*surface.get(), IsValid).WillRepeatedly(Return(true));
965 EXPECT_CALL(*surface.get(), Destroy).WillOnce(Return(true));
966
967 EXPECT_CALL(*egl_manager.get(),
968 CreateWindowSurface(_, /*width=*/500, /*height=*/500))
969 .WillOnce(Return(std::move((resized_surface))));
970 EXPECT_CALL(*resized_surface_ptr, MakeCurrent).WillOnce(Return(true));
971 EXPECT_CALL(*resized_surface_ptr, SetVSyncEnabled).WillOnce(Return(true));
972 EXPECT_CALL(*windows_proc_table.get(), DwmFlush).WillOnce(Return(S_OK));
973
974 EXPECT_CALL(*resized_surface_ptr, Destroy).WillOnce(Return(true));
975
976 fml::AutoResetWaitableEvent metrics_sent_latch;
977 engine_modifier.embedder_api().SendWindowMetricsEvent = MOCK_ENGINE_PROC(
978 SendWindowMetricsEvent,
979 ([&metrics_sent_latch](auto engine,
980 const FlutterWindowMetricsEvent* event) {
981 metrics_sent_latch.Signal();
982 return kSuccess;
983 }));
984
985 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
986 std::make_unique<NiceMock<MockWindowBindingHandler>>());
987
988 ViewModifier view_modifier{view.get()};
989 engine_modifier.SetEGLManager(std::move(egl_manager));
990 view_modifier.SetSurface(std::move(surface));
991
992 fml::AutoResetWaitableEvent resized_latch;
993 std::thread([&resized_latch, &view]() {
994 // Start the window resize. This sends the new window metrics
995 // and then blocks until another thread completes the window resize.
996 EXPECT_TRUE(view->OnWindowSizeChanged(500, 500));
997 resized_latch.Signal();
998 }).detach();
999
1000 // Wait until the platform thread has started the window resize.
1001 metrics_sent_latch.Wait();
1002
1003 // Complete the window resize by reporting an empty frame.
1004 view->OnEmptyFrameGenerated();
1005 view->OnFramePresented();
1006 resized_latch.Wait();
1007 }
1008
1009 // A window resize can be interleaved between a frame generation and
1010 // presentation. This should not crash the app. Regression test for:
1011 // https://github.com/flutter/flutter/issues/141855
1012 TEST(FlutterWindowsViewTest, WindowResizeRace) {
1013 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
1014
1015 EngineModifier engine_modifier(engine.get());
1016 engine_modifier.embedder_api().PostRenderThreadTask = MOCK_ENGINE_PROC(
1017 PostRenderThreadTask,
1018 ([](auto engine, VoidCallback callback, void* user_data) {
1019 callback(user_data);
1020 return kSuccess;
1021 }));
1022
1023 auto egl_manager = std::make_unique<egl::MockManager>();
1024 auto surface = std::make_unique<egl::MockWindowSurface>();
1025
1026 EXPECT_CALL(*surface.get(), IsValid).WillRepeatedly(Return(true));
1027 EXPECT_CALL(*surface.get(), Destroy).WillOnce(Return(true));
1028
1029 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
1030 std::make_unique<NiceMock<MockWindowBindingHandler>>());
1031
1032 ViewModifier view_modifier{view.get()};
1033 engine_modifier.SetEGLManager(std::move(egl_manager));
1034 view_modifier.SetSurface(std::move(surface));
1035
1036 // Begin a frame.
1037 ASSERT_TRUE(view->OnFrameGenerated(100, 100));
1038
1039 // Inject a window resize between the frame generation and
1040 // frame presentation. The new size invalidates the current frame.
1041 fml::AutoResetWaitableEvent resized_latch;
1042 std::thread([&resized_latch, &view]() {
1043 // The resize is never completed. The view times out and returns false.
1044 EXPECT_FALSE(view->OnWindowSizeChanged(500, 500));
1045 resized_latch.Signal();
1046 }).detach();
1047
1048 // Wait until the platform thread has started the window resize.
1049 resized_latch.Wait();
1050
1051 // Complete the invalidated frame while a resize is pending. Although this
1052 // might mean that we presented a frame with the wrong size, this should not
1053 // crash the app.
1054 view->OnFramePresented();
1055 }
1056
1057 // Window resize should succeed even if the render surface could not be created
1058 // even though EGL initialized successfully.
1059 TEST(FlutterWindowsViewTest, WindowResizeInvalidSurface) {
1060 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
1061
1062 EngineModifier engine_modifier(engine.get());
1063 engine_modifier.embedder_api().PostRenderThreadTask = MOCK_ENGINE_PROC(
1064 PostRenderThreadTask,
1065 ([](auto engine, VoidCallback callback, void* user_data) {
1066 callback(user_data);
1067 return kSuccess;
1068 }));
1069
1070 auto egl_manager = std::make_unique<egl::MockManager>();
1071 auto surface = std::make_unique<egl::MockWindowSurface>();
1072
1073 EXPECT_CALL(*egl_manager.get(), CreateWindowSurface).Times(0);
1074 EXPECT_CALL(*surface.get(), IsValid).WillRepeatedly(Return(false));
1075 EXPECT_CALL(*surface.get(), Destroy).WillOnce(Return(false));
1076
1077 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
1078 std::make_unique<NiceMock<MockWindowBindingHandler>>());
1079
1080 ViewModifier view_modifier{view.get()};
1081 engine_modifier.SetEGLManager(std::move(egl_manager));
1082 view_modifier.SetSurface(std::move(surface));
1083
1084 auto metrics_sent = false;
1085 engine_modifier.embedder_api().SendWindowMetricsEvent = MOCK_ENGINE_PROC(
1086 SendWindowMetricsEvent,
1087 ([&metrics_sent](auto engine, const FlutterWindowMetricsEvent* event) {
1088 metrics_sent = true;
1089 return kSuccess;
1090 }));
1091
1092 view->OnWindowSizeChanged(500, 500);
1093 }
1094
1095 // Window resize should succeed even if EGL initialized successfully
1096 // but the EGL surface could not be created.
1097 TEST(FlutterWindowsViewTest, WindowResizeWithoutSurface) {
1098 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
1099 EngineModifier modifier(engine.get());
1100
1101 auto egl_manager = std::make_unique<egl::MockManager>();
1102
1103 EXPECT_CALL(*egl_manager.get(), CreateWindowSurface).Times(0);
1104
1105 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
1106 std::make_unique<NiceMock<MockWindowBindingHandler>>());
1107
1108 modifier.SetEGLManager(std::move(egl_manager));
1109
1110 auto metrics_sent = false;
1111 modifier.embedder_api().SendWindowMetricsEvent = MOCK_ENGINE_PROC(
1112 SendWindowMetricsEvent,
1113 ([&metrics_sent](auto engine, const FlutterWindowMetricsEvent* event) {
1114 metrics_sent = true;
1115 return kSuccess;
1116 }));
1117
1118 view->OnWindowSizeChanged(500, 500);
1119 }
1120
1121 TEST(FlutterWindowsViewTest, WindowRepaintTests) {
1122 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
1123 EngineModifier modifier(engine.get());
1124
1125 FlutterWindowsView view{kImplicitViewId, engine.get(),
1126 std::make_unique<flutter::FlutterWindow>(100, 100)};
1127
1128 bool schedule_frame_called = false;
1129 modifier.embedder_api().ScheduleFrame =
1130 MOCK_ENGINE_PROC(ScheduleFrame, ([&schedule_frame_called](auto engine) {
1131 schedule_frame_called = true;
1132 return kSuccess;
1133 }));
1134
1135 view.OnWindowRepaint();
1136 EXPECT_TRUE(schedule_frame_called);
1137 }
1138
1139 // Ensure that checkboxes have their checked status set apropriately
1140 // Previously, only Radios could have this flag updated
1141 // Resulted in the issue seen at
1142 // https://github.com/flutter/flutter/issues/96218
1143 // This test ensures that the native state of Checkboxes on Windows,
1144 // specifically, is updated as desired.
1145 TEST(FlutterWindowsViewTest, CheckboxNativeState) {
1146 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
1147 EngineModifier modifier(engine.get());
1148 modifier.embedder_api().UpdateSemanticsEnabled =
1149 [](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) {
1150 return kSuccess;
1151 };
1152
1153 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
1154 std::make_unique<NiceMock<MockWindowBindingHandler>>());
1155
1156 // Enable semantics to instantiate accessibility bridge.
1157 view->OnUpdateSemanticsEnabled(true);
1158
1159 auto bridge = view->accessibility_bridge().lock();
1160 ASSERT_TRUE(bridge);
1161
1162 FlutterSemanticsNode2 root{sizeof(FlutterSemanticsNode2), 0};
1163 root.id = 0;
1164 root.label = "root";
1165 root.hint = "";
1166 root.value = "";
1167 root.increased_value = "";
1168 root.decreased_value = "";
1169 root.child_count = 0;
1170 root.custom_accessibility_actions_count = 0;
1171 root.flags = static_cast<FlutterSemanticsFlag>(
1172 FlutterSemanticsFlag::kFlutterSemanticsFlagHasCheckedState |
1173 FlutterSemanticsFlag::kFlutterSemanticsFlagIsChecked);
1174 bridge->AddFlutterSemanticsNodeUpdate(root);
1175
1176 bridge->CommitUpdates();
1177
1178 {
1179 auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
1180 EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kCheckBox);
1181 EXPECT_EQ(root_node->GetData().GetCheckedState(),
1182 ax::mojom::CheckedState::kTrue);
1183
1184 // Get the IAccessible for the root node.
1185 IAccessible* native_view = root_node->GetNativeViewAccessible();
1186 ASSERT_TRUE(native_view != nullptr);
1187
1188 // Look up against the node itself (not one of its children).
1189 VARIANT varchild = {};
1190 varchild.vt = VT_I4;
1191
1192 // Verify the checkbox is checked.
1193 varchild.lVal = CHILDID_SELF;
1194 VARIANT native_state = {};
1195 ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
1196 EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_CHECKED);
1197
1198 // Perform similar tests for UIA value;
1199 IRawElementProviderSimple* uia_node;
1200 native_view->QueryInterface(IID_PPV_ARGS(&uia_node));
1201 ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue(
1202 UIA_ToggleToggleStatePropertyId, &native_state)));
1203 EXPECT_EQ(native_state.lVal, ToggleState_On);
1204
1205 ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue(
1206 UIA_AriaPropertiesPropertyId, &native_state)));
1207 EXPECT_NE(std::wcsstr(native_state.bstrVal, L"checked=true"), nullptr);
1208 }
1209
1210 // Test unchecked too.
1211 root.flags = static_cast<FlutterSemanticsFlag>(
1212 FlutterSemanticsFlag::kFlutterSemanticsFlagHasCheckedState);
1213 bridge->AddFlutterSemanticsNodeUpdate(root);
1214 bridge->CommitUpdates();
1215
1216 {
1217 auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
1218 EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kCheckBox);
1219 EXPECT_EQ(root_node->GetData().GetCheckedState(),
1220 ax::mojom::CheckedState::kFalse);
1221
1222 // Get the IAccessible for the root node.
1223 IAccessible* native_view = root_node->GetNativeViewAccessible();
1224 ASSERT_TRUE(native_view != nullptr);
1225
1226 // Look up against the node itself (not one of its children).
1227 VARIANT varchild = {};
1228 varchild.vt = VT_I4;
1229
1230 // Verify the checkbox is unchecked.
1231 varchild.lVal = CHILDID_SELF;
1232 VARIANT native_state = {};
1233 ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
1234 EXPECT_FALSE(native_state.lVal & STATE_SYSTEM_CHECKED);
1235
1236 // Perform similar tests for UIA value;
1237 IRawElementProviderSimple* uia_node;
1238 native_view->QueryInterface(IID_PPV_ARGS(&uia_node));
1239 ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue(
1240 UIA_ToggleToggleStatePropertyId, &native_state)));
1241 EXPECT_EQ(native_state.lVal, ToggleState_Off);
1242
1243 ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue(
1244 UIA_AriaPropertiesPropertyId, &native_state)));
1245 EXPECT_NE(std::wcsstr(native_state.bstrVal, L"checked=false"), nullptr);
1246 }
1247
1248 // Now check mixed state.
1249 root.flags = static_cast<FlutterSemanticsFlag>(
1250 FlutterSemanticsFlag::kFlutterSemanticsFlagHasCheckedState |
1251 FlutterSemanticsFlag::kFlutterSemanticsFlagIsCheckStateMixed);
1252 bridge->AddFlutterSemanticsNodeUpdate(root);
1253 bridge->CommitUpdates();
1254
1255 {
1256 auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
1257 EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kCheckBox);
1258 EXPECT_EQ(root_node->GetData().GetCheckedState(),
1259 ax::mojom::CheckedState::kMixed);
1260
1261 // Get the IAccessible for the root node.
1262 IAccessible* native_view = root_node->GetNativeViewAccessible();
1263 ASSERT_TRUE(native_view != nullptr);
1264
1265 // Look up against the node itself (not one of its children).
1266 VARIANT varchild = {};
1267 varchild.vt = VT_I4;
1268
1269 // Verify the checkbox is mixed.
1270 varchild.lVal = CHILDID_SELF;
1271 VARIANT native_state = {};
1272 ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
1273 EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_MIXED);
1274
1275 // Perform similar tests for UIA value;
1276 IRawElementProviderSimple* uia_node;
1277 native_view->QueryInterface(IID_PPV_ARGS(&uia_node));
1278 ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue(
1279 UIA_ToggleToggleStatePropertyId, &native_state)));
1280 EXPECT_EQ(native_state.lVal, ToggleState_Indeterminate);
1281
1282 ASSERT_TRUE(SUCCEEDED(uia_node->GetPropertyValue(
1283 UIA_AriaPropertiesPropertyId, &native_state)));
1284 EXPECT_NE(std::wcsstr(native_state.bstrVal, L"checked=mixed"), nullptr);
1285 }
1286 }
1287
1288 // Ensure that switches have their toggle status set apropriately
1289 TEST(FlutterWindowsViewTest, SwitchNativeState) {
1290 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
1291 EngineModifier modifier(engine.get());
1292 modifier.embedder_api().UpdateSemanticsEnabled =
1293 [](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) {
1294 return kSuccess;
1295 };
1296
1297 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
1298 std::make_unique<NiceMock<MockWindowBindingHandler>>());
1299
1300 // Enable semantics to instantiate accessibility bridge.
1301 view->OnUpdateSemanticsEnabled(true);
1302
1303 auto bridge = view->accessibility_bridge().lock();
1304 ASSERT_TRUE(bridge);
1305
1306 FlutterSemanticsNode2 root{sizeof(FlutterSemanticsNode2), 0};
1307 root.id = 0;
1308 root.label = "root";
1309 root.hint = "";
1310 root.value = "";
1311 root.increased_value = "";
1312 root.decreased_value = "";
1313 root.child_count = 0;
1314 root.custom_accessibility_actions_count = 0;
1315 root.flags = static_cast<FlutterSemanticsFlag>(
1316 FlutterSemanticsFlag::kFlutterSemanticsFlagHasToggledState |
1317 FlutterSemanticsFlag::kFlutterSemanticsFlagIsToggled);
1318 bridge->AddFlutterSemanticsNodeUpdate(root);
1319
1320 bridge->CommitUpdates();
1321
1322 {
1323 auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
1324 EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kSwitch);
1325 EXPECT_EQ(root_node->GetData().GetCheckedState(),
1326 ax::mojom::CheckedState::kTrue);
1327
1328 // Get the IAccessible for the root node.
1329 IAccessible* native_view = root_node->GetNativeViewAccessible();
1330 ASSERT_TRUE(native_view != nullptr);
1331
1332 // Look up against the node itself (not one of its children).
1333 VARIANT varchild = {};
1334 varchild.vt = VT_I4;
1335
1336 varchild.lVal = CHILDID_SELF;
1337 VARIANT varrole = {};
1338
1339 // Verify the role of the switch is CHECKBUTTON
1340 ASSERT_EQ(native_view->get_accRole(varchild, &varrole), S_OK);
1341 ASSERT_EQ(varrole.lVal, ROLE_SYSTEM_CHECKBUTTON);
1342
1343 // Verify the switch is pressed.
1344 VARIANT native_state = {};
1345 ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
1346 EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_PRESSED);
1347 EXPECT_TRUE(native_state.lVal & STATE_SYSTEM_CHECKED);
1348
1349 // Test similarly on UIA node.
1350 IRawElementProviderSimple* uia_node;
1351 native_view->QueryInterface(IID_PPV_ARGS(&uia_node));
1352 ASSERT_EQ(uia_node->GetPropertyValue(UIA_ControlTypePropertyId, &varrole),
1353 S_OK);
1354 EXPECT_EQ(varrole.lVal, UIA_ButtonControlTypeId);
1355 ASSERT_EQ(uia_node->GetPropertyValue(UIA_ToggleToggleStatePropertyId,
1356 &native_state),
1357 S_OK);
1358 EXPECT_EQ(native_state.lVal, ToggleState_On);
1359 ASSERT_EQ(
1360 uia_node->GetPropertyValue(UIA_AriaPropertiesPropertyId, &native_state),
1361 S_OK);
1362 EXPECT_NE(std::wcsstr(native_state.bstrVal, L"pressed=true"), nullptr);
1363 }
1364
1365 // Test unpressed too.
1366 root.flags = static_cast<FlutterSemanticsFlag>(
1367 FlutterSemanticsFlag::kFlutterSemanticsFlagHasToggledState);
1368 bridge->AddFlutterSemanticsNodeUpdate(root);
1369 bridge->CommitUpdates();
1370
1371 {
1372 auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
1373 EXPECT_EQ(root_node->GetData().role, ax::mojom::Role::kSwitch);
1374 EXPECT_EQ(root_node->GetData().GetCheckedState(),
1375 ax::mojom::CheckedState::kFalse);
1376
1377 // Get the IAccessible for the root node.
1378 IAccessible* native_view = root_node->GetNativeViewAccessible();
1379 ASSERT_TRUE(native_view != nullptr);
1380
1381 // Look up against the node itself (not one of its children).
1382 VARIANT varchild = {};
1383 varchild.vt = VT_I4;
1384
1385 // Verify the switch is not pressed.
1386 varchild.lVal = CHILDID_SELF;
1387 VARIANT native_state = {};
1388 ASSERT_TRUE(SUCCEEDED(native_view->get_accState(varchild, &native_state)));
1389 EXPECT_FALSE(native_state.lVal & STATE_SYSTEM_PRESSED);
1390 EXPECT_FALSE(native_state.lVal & STATE_SYSTEM_CHECKED);
1391
1392 // Test similarly on UIA node.
1393 IRawElementProviderSimple* uia_node;
1394 native_view->QueryInterface(IID_PPV_ARGS(&uia_node));
1395 ASSERT_EQ(uia_node->GetPropertyValue(UIA_ToggleToggleStatePropertyId,
1396 &native_state),
1397 S_OK);
1398 EXPECT_EQ(native_state.lVal, ToggleState_Off);
1399 ASSERT_EQ(
1400 uia_node->GetPropertyValue(UIA_AriaPropertiesPropertyId, &native_state),
1401 S_OK);
1402 EXPECT_NE(std::wcsstr(native_state.bstrVal, L"pressed=false"), nullptr);
1403 }
1404 }
1405
1406 TEST(FlutterWindowsViewTest, TooltipNodeData) {
1407 std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
1408 EngineModifier modifier(engine.get());
1409 modifier.embedder_api().UpdateSemanticsEnabled =
1410 [](FLUTTER_API_SYMBOL(FlutterEngine) engine, bool enabled) {
1411 return kSuccess;
1412 };
1413
1414 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
1415 std::make_unique<NiceMock<MockWindowBindingHandler>>());
1416
1417 // Enable semantics to instantiate accessibility bridge.
1418 view->OnUpdateSemanticsEnabled(true);
1419
1420 auto bridge = view->accessibility_bridge().lock();
1421 ASSERT_TRUE(bridge);
1422
1423 FlutterSemanticsNode2 root{sizeof(FlutterSemanticsNode2), 0};
1424 root.id = 0;
1425 root.label = "root";
1426 root.hint = "";
1427 root.value = "";
1428 root.increased_value = "";
1429 root.decreased_value = "";
1430 root.tooltip = "tooltip";
1431 root.child_count = 0;
1432 root.custom_accessibility_actions_count = 0;
1433 root.flags = static_cast<FlutterSemanticsFlag>(
1434 FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField);
1435 bridge->AddFlutterSemanticsNodeUpdate(root);
1436
1437 bridge->CommitUpdates();
1438 auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock();
1439 std::string tooltip = root_node->GetData().GetStringAttribute(
1440 ax::mojom::StringAttribute::kTooltip);
1441 EXPECT_EQ(tooltip, "tooltip");
1442
1443 // Check that MSAA name contains the tooltip.
1444 IAccessible* native_view = bridge->GetFlutterPlatformNodeDelegateFromID(0)
1445 .lock()
1446 ->GetNativeViewAccessible();
1447 VARIANT varchild = {.vt = VT_I4, .lVal = CHILDID_SELF};
1448 BSTR bname;
1449 ASSERT_EQ(native_view->get_accName(varchild, &bname), S_OK);
1450 EXPECT_NE(std::wcsstr(bname, L"tooltip"), nullptr);
1451
1452 // Check that UIA help text is equal to the tooltip.
1453 IRawElementProviderSimple* uia_node;
1454 native_view->QueryInterface(IID_PPV_ARGS(&uia_node));
1455 VARIANT varname{};
1456 ASSERT_EQ(uia_node->GetPropertyValue(UIA_HelpTextPropertyId, &varname), S_OK);
1457 std::string uia_tooltip = _com_util::ConvertBSTRToString(varname.bstrVal);
1458 EXPECT_EQ(uia_tooltip, "tooltip");
1459 }
1460
1461 // Don't block until the v-blank if it is disabled by the window.
1462 // The surface is updated on the platform thread at startup.
1463 TEST(FlutterWindowsViewTest, DisablesVSyncAtStartup) {
1464 auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
1465 auto engine = std::make_unique<MockFlutterWindowsEngine>(windows_proc_table);
1466 auto egl_manager = std::make_unique<egl::MockManager>();
1467 egl::MockContext render_context;
1468 auto surface = std::make_unique<egl::MockWindowSurface>();
1469 auto surface_ptr = surface.get();
1470
1471 EXPECT_CALL(*engine.get(), running).WillRepeatedly(Return(false));
1472 EXPECT_CALL(*engine.get(), PostRasterThreadTask).Times(0);
1473
1474 EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
1475 .WillOnce(Return(true));
1476
1477 EXPECT_CALL(*egl_manager.get(), render_context)
1478 .WillOnce(Return(&render_context));
1479 EXPECT_CALL(*surface_ptr, IsValid).WillOnce(Return(true));
1480
1481 InSequence s;
1482 EXPECT_CALL(*egl_manager.get(), CreateWindowSurface)
1483 .WillOnce(Return(std::move(surface)));
1484 EXPECT_CALL(*surface_ptr, MakeCurrent).WillOnce(Return(true));
1485 EXPECT_CALL(*surface_ptr, SetVSyncEnabled(false)).WillOnce(Return(true));
1486 EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
1487
1488 EXPECT_CALL(*surface_ptr, Destroy).Times(1);
1489
1490 EngineModifier modifier{engine.get()};
1491 modifier.SetEGLManager(std::move(egl_manager));
1492
1493 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
1494 std::make_unique<NiceMock<MockWindowBindingHandler>>());
1495 }
1496
1497 // Blocks until the v-blank if it is enabled by the window.
1498 // The surface is updated on the platform thread at startup.
1499 TEST(FlutterWindowsViewTest, EnablesVSyncAtStartup) {
1500 auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
1501 auto engine = std::make_unique<MockFlutterWindowsEngine>(windows_proc_table);
1502 auto egl_manager = std::make_unique<egl::MockManager>();
1503 egl::MockContext render_context;
1504 auto surface = std::make_unique<egl::MockWindowSurface>();
1505 auto surface_ptr = surface.get();
1506
1507 EXPECT_CALL(*engine.get(), running).WillRepeatedly(Return(false));
1508 EXPECT_CALL(*engine.get(), PostRasterThreadTask).Times(0);
1509 EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
1510 .WillOnce(Return(false));
1511
1512 EXPECT_CALL(*egl_manager.get(), render_context)
1513 .WillOnce(Return(&render_context));
1514 EXPECT_CALL(*surface_ptr, IsValid).WillOnce(Return(true));
1515
1516 InSequence s;
1517 EXPECT_CALL(*egl_manager.get(), CreateWindowSurface)
1518 .WillOnce(Return(std::move(surface)));
1519 EXPECT_CALL(*surface_ptr, MakeCurrent).WillOnce(Return(true));
1520 EXPECT_CALL(*surface_ptr, SetVSyncEnabled(true)).WillOnce(Return(true));
1521 EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
1522
1523 EXPECT_CALL(*surface_ptr, Destroy).Times(1);
1524
1525 EngineModifier modifier{engine.get()};
1526 modifier.SetEGLManager(std::move(egl_manager));
1527
1528 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
1529 std::make_unique<NiceMock<MockWindowBindingHandler>>());
1530 }
1531
1532 // Don't block until the v-blank if it is disabled by the window.
1533 // The surface is updated on the raster thread if the engine is running.
1534 TEST(FlutterWindowsViewTest, DisablesVSyncAfterStartup) {
1535 auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
1536 auto engine = std::make_unique<MockFlutterWindowsEngine>(windows_proc_table);
1537 auto egl_manager = std::make_unique<egl::MockManager>();
1538 egl::MockContext render_context;
1539 auto surface = std::make_unique<egl::MockWindowSurface>();
1540 auto surface_ptr = surface.get();
1541
1542 EXPECT_CALL(*engine.get(), running).WillRepeatedly(Return(true));
1543 EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
1544 .WillOnce(Return(true));
1545
1546 EXPECT_CALL(*egl_manager.get(), render_context)
1547 .WillOnce(Return(&render_context));
1548 EXPECT_CALL(*surface_ptr, IsValid).WillOnce(Return(true));
1549
1550 InSequence s;
1551 EXPECT_CALL(*egl_manager.get(), CreateWindowSurface)
1552 .WillOnce(Return(std::move(surface)));
1553 EXPECT_CALL(*engine.get(), PostRasterThreadTask)
1554 .WillOnce([](fml::closure callback) {
1555 callback();
1556 return true;
1557 });
1558 EXPECT_CALL(*surface_ptr, MakeCurrent).WillOnce(Return(true));
1559 EXPECT_CALL(*surface_ptr, SetVSyncEnabled(false)).WillOnce(Return(true));
1560 EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
1561 EXPECT_CALL(*engine.get(), PostRasterThreadTask)
1562 .WillOnce([](fml::closure callback) {
1563 callback();
1564 return true;
1565 });
1566 EXPECT_CALL(*surface_ptr, Destroy).Times(1);
1567
1568 EngineModifier modifier{engine.get()};
1569 modifier.SetEGLManager(std::move(egl_manager));
1570
1571 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
1572 std::make_unique<NiceMock<MockWindowBindingHandler>>());
1573 }
1574
1575 // Blocks until the v-blank if it is enabled by the window.
1576 // The surface is updated on the raster thread if the engine is running.
1577 TEST(FlutterWindowsViewTest, EnablesVSyncAfterStartup) {
1578 auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
1579 auto engine = std::make_unique<MockFlutterWindowsEngine>(windows_proc_table);
1580 auto egl_manager = std::make_unique<egl::MockManager>();
1581 egl::MockContext render_context;
1582 auto surface = std::make_unique<egl::MockWindowSurface>();
1583 auto surface_ptr = surface.get();
1584
1585 EXPECT_CALL(*engine.get(), running).WillRepeatedly(Return(true));
1586
1587 EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
1588 .WillOnce(Return(false));
1589
1590 EXPECT_CALL(*egl_manager.get(), render_context)
1591 .WillOnce(Return(&render_context));
1592 EXPECT_CALL(*surface_ptr, IsValid).WillOnce(Return(true));
1593
1594 InSequence s;
1595 EXPECT_CALL(*egl_manager.get(), CreateWindowSurface)
1596 .WillOnce(Return(std::move(surface)));
1597 EXPECT_CALL(*engine.get(), PostRasterThreadTask)
1598 .WillOnce([](fml::closure callback) {
1599 callback();
1600 return true;
1601 });
1602
1603 EXPECT_CALL(*surface_ptr, MakeCurrent).WillOnce(Return(true));
1604 EXPECT_CALL(*surface_ptr, SetVSyncEnabled(true)).WillOnce(Return(true));
1605 EXPECT_CALL(render_context, ClearCurrent).WillOnce(Return(true));
1606
1607 EXPECT_CALL(*engine.get(), PostRasterThreadTask)
1608 .WillOnce([](fml::closure callback) {
1609 callback();
1610 return true;
1611 });
1612 EXPECT_CALL(*surface_ptr, Destroy).Times(1);
1613
1614 EngineModifier modifier{engine.get()};
1615 modifier.SetEGLManager(std::move(egl_manager));
1616
1617 std::unique_ptr<FlutterWindowsView> view = engine->CreateView(
1618 std::make_unique<NiceMock<MockWindowBindingHandler>>());
1619 }
1620
1621 // Desktop Window Manager composition can be disabled on Windows 7.
1622 // If this happens, the app must synchronize with the vsync to prevent
1623 // screen tearing.
1624 TEST(FlutterWindowsViewTest, UpdatesVSyncOnDwmUpdates) {
1625 auto windows_proc_table = std::make_shared<MockWindowsProcTable>();
1626 auto engine = std::make_unique<MockFlutterWindowsEngine>(windows_proc_table);
1627 auto egl_manager = std::make_unique<egl::MockManager>();
1628 egl::MockContext render_context;
1629 auto surface = std::make_unique<egl::MockWindowSurface>();
1630 auto surface_ptr = surface.get();
1631
1632 EXPECT_CALL(*engine.get(), running).WillRepeatedly(Return(true));
1633
1634 EXPECT_CALL(*engine.get(), PostRasterThreadTask)
1635 .WillRepeatedly([](fml::closure callback) {
1636 callback();
1637 return true;
1638 });
1639
1640 EXPECT_CALL(*egl_manager.get(), render_context)
1641 .WillRepeatedly(Return(&render_context));
1642
1643 EXPECT_CALL(*surface_ptr, IsValid).WillRepeatedly(Return(true));
1644 EXPECT_CALL(*surface_ptr, MakeCurrent).WillRepeatedly(Return(true));
1645 EXPECT_CALL(*surface_ptr, Destroy).Times(1);
1646 EXPECT_CALL(render_context, ClearCurrent).WillRepeatedly(Return(true));
1647
1648 InSequence s;
1649
1650 // Mock render surface initialization.
1651 std::unique_ptr<FlutterWindowsView> view;
1652 {
1653 EXPECT_CALL(*egl_manager, CreateWindowSurface)
1654 .WillOnce(Return(std::move(surface)));
1655 EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
1656 .WillOnce(Return(true));
1657 EXPECT_CALL(*surface_ptr, SetVSyncEnabled).WillOnce(Return(true));
1658
1659 EngineModifier engine_modifier{engine.get()};
1660 engine_modifier.SetEGLManager(std::move(egl_manager));
1661
1662 view = engine->CreateView(
1663 std::make_unique<NiceMock<MockWindowBindingHandler>>());
1664 }
1665
1666 // Disabling DWM composition should enable vsync blocking on the surface.
1667 {
1668 EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
1669 .WillOnce(Return(false));
1670 EXPECT_CALL(*surface_ptr, SetVSyncEnabled(true)).WillOnce(Return(true));
1671
1672 engine->OnDwmCompositionChanged();
1673 }
1674
1675 // Enabling DWM composition should disable vsync blocking on the surface.
1676 {
1677 EXPECT_CALL(*windows_proc_table.get(), DwmIsCompositionEnabled)
1678 .WillOnce(Return(true));
1679 EXPECT_CALL(*surface_ptr, SetVSyncEnabled(false)).WillOnce(Return(true));
1680
1681 engine->OnDwmCompositionChanged();
1682 }
1683 }
1684
1685 } // namespace testing
1686 } // namespace flutter
flutter::kImplicitViewId
constexpr FlutterViewId kImplicitViewId
Definition: flutter_windows_engine.h:55
FlutterDesktopEngineProperties::aot_library_path
const wchar_t * aot_library_path
Definition: flutter_windows.h:54
flutter_windows_texture_registrar.h
flutter::FlutterWindowsView
Definition: flutter_windows_view.h:34
flutter::JsonMessageCodec::GetInstance
static const JsonMessageCodec & GetInstance()
Definition: json_message_codec.cc:17
FlutterDesktopEngineProperties
Definition: flutter_windows.h:39
flutter::FlutterEngine
Definition: flutter_engine.h:28
FlutterDesktopBinaryReply
void(* FlutterDesktopBinaryReply)(const uint8_t *data, size_t data_size, void *user_data)
Definition: flutter_messenger.h:26
user_data
void * user_data
Definition: flutter_windows_view_unittests.cc:53
FlutterDesktopEngineProperties::icu_data_path
const wchar_t * icu_data_path
Definition: flutter_windows.h:48
json_message_codec.h
flutter_windows_view.h
flutter_window.h
flutter::FlutterWindowsViewController
Controls a view that displays Flutter content.
Definition: flutter_windows_view_controller.h:17
flutter::testing::kScanCodeKeyA
constexpr uint64_t kScanCodeKeyA
Definition: flutter_windows_view_unittests.cc:42
flutter::FlutterViewId
int64_t FlutterViewId
Definition: flutter_view.h:13
flutter
Definition: accessibility_bridge_windows.cc:11
flutter_windows_view_controller.h
flutter_windows_engine.h
VoidCallback
void(* VoidCallback)(void *)
Definition: flutter_windows.h:21
flutter::MessageCodec::EncodeMessage
std::unique_ptr< std::vector< uint8_t > > EncodeMessage(const T &message) const
Definition: message_codec.h:45
flutter::testing::TEST
TEST(AccessibilityBridgeWindows, GetParent)
Definition: accessibility_bridge_windows_unittests.cc:237
FlutterDesktopEngineProperties::assets_path
const wchar_t * assets_path
Definition: flutter_windows.h:43
flutter::testing::kVirtualKeyA
constexpr uint64_t kVirtualKeyA
Definition: flutter_windows_view_unittests.cc:43
callback
FlutterDesktopBinaryReply callback
Definition: flutter_windows_view_unittests.cc:52
node_delegate
std::shared_ptr< FlutterPlatformNodeDelegateWindows > node_delegate
Definition: accessibility_bridge_windows_unittests.cc:33