| Flutter macOS Embedder |

FlutterResizeSynchronizerTest.mm
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/fml/synchronization/waitable_event.h"
6 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h"
7 #import "flutter/testing/testing.h"
8
9 TEST(FlutterThreadSynchronizerTest, NotBlocked) {
10 [FlutterRunLoop ensureMainLoopInitialized];
11
12 FlutterResizeSynchronizer* synchronizer = [[FlutterResizeSynchronizer alloc] init];
13 __block BOOL performed = NO;
14
15 [NSThread detachNewThreadWithBlock:^{
16 [synchronizer performCommitForSize:CGSizeMake(100, 100)
17 notify:^{
18 performed = YES;
19 }
20 delay:0];
21 }];
22
23 CFTimeInterval start = CFAbsoluteTimeGetCurrent();
24
25 while (!performed && CFAbsoluteTimeGetCurrent() - start < 1.0) {
26 [FlutterRunLoop.mainRunLoop pollFlutterMessagesOnce];
27 }
28 EXPECT_EQ(performed, YES);
29 }
30
31 TEST(FlutterThreadSynchronizerTest, WaitForResize) {
32 [FlutterRunLoop ensureMainLoopInitialized];
33
34 FlutterResizeSynchronizer* synchronizer = [[FlutterResizeSynchronizer alloc] init];
35
36 __block BOOL commit1 = NO;
37 __block BOOL commit2 = NO;
38
39 // Capturing c++ objects in blocks requires copy constructor, that also applies
40 // to __block variables where the copy is made on heap.
41 fml::AutoResetWaitableEvent latch_;
42 fml::AutoResetWaitableEvent& latch = latch_;
43
44 // Resize synchronizer must have at received one frame in order to block.
45 __block BOOL didReceiveFrame = NO;
46 [synchronizer performCommitForSize:CGSizeMake(10, 10)
47 notify:^{
48 didReceiveFrame = YES;
49 }
50 delay:0];
51
52 CFTimeInterval start = CFAbsoluteTimeGetCurrent();
53 while (!didReceiveFrame && CFAbsoluteTimeGetCurrent() - start < 1.0) {
54 [FlutterRunLoop.mainRunLoop pollFlutterMessagesOnce];
55 }
56
57 // Now resize should block until it has received expected size.
58
59 [NSThread detachNewThreadWithBlock:^{
60 latch.Wait();
61
62 [synchronizer performCommitForSize:CGSizeMake(50, 100)
63 notify:^{
64 commit1 = YES;
65 }
66 delay:0];
67
68 [synchronizer performCommitForSize:CGSizeMake(100, 100)
69 notify:^{
70 commit2 = YES;
71 }
72 delay:0];
73 }];
74
75 [synchronizer beginResizeForSize:CGSizeMake(100, 100)
76 notify:^{
77 latch.Signal();
78 }];
79
80 EXPECT_EQ(commit1, YES);
81 EXPECT_EQ(commit2, YES);
82 }
83
84 TEST(FlutterThreadSynchronizerTest, UnblocksOnShutDown) {
85 [FlutterRunLoop ensureMainLoopInitialized];
86
87 FlutterResizeSynchronizer* synchronizer = [[FlutterResizeSynchronizer alloc] init];
88
89 // Resize synchronizer must have at received one frame in order to block.
90 __block BOOL didReceiveFrame = NO;
91 [synchronizer performCommitForSize:CGSizeMake(10, 10)
92 notify:^{
93 didReceiveFrame = YES;
94 }
95 delay:0];
96
97 CFTimeInterval start = CFAbsoluteTimeGetCurrent();
98 while (!didReceiveFrame && CFAbsoluteTimeGetCurrent() - start < 1.0) {
99 [FlutterRunLoop.mainRunLoop pollFlutterMessagesOnce];
100 }
101
102 fml::AutoResetWaitableEvent latch_;
103 fml::AutoResetWaitableEvent& latch = latch_;
104
105 [NSThread detachNewThreadWithBlock:^{
106 latch.Wait();
107
108 [synchronizer shutDown];
109 }];
110
111 [synchronizer beginResizeForSize:CGSizeMake(100, 100)
112 notify:^{
113 // Unblock resize
114 latch.Signal();
115 }];
116
117 // Subsequent calls should not block.
118 [synchronizer beginResizeForSize:CGSizeMake(100, 100)
119 notify:^{
120 }];
121 }
FlutterResizeSynchronizer.h
TEST
TEST(FlutterThreadSynchronizerTest, NotBlocked)
Definition: FlutterResizeSynchronizerTest.mm:9
FlutterResizeSynchronizer
Definition: FlutterResizeSynchronizer.h:17
FlutterRunLoop
Definition: FlutterRunLoop.h:16
+[FlutterRunLoop mainRunLoop]
FlutterRunLoop * mainRunLoop()
Definition: FlutterRunLoop.mm:118