| Flutter macOS Embedder |

FlutterCompositorTest.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 #import <Foundation/Foundation.h>
6 #import <Metal/Metal.h>
7 #import <OCMock/OCMock.h>
8
9 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h"
10 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
11 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewProvider.h"
12 #import "flutter/testing/testing.h"
13
14 @interface FlutterViewMockProvider : NSObject <FlutterViewProvider> {
15 FlutterView* _implicitView;
16 }
17 /**
18 * Create a FlutterViewMockProvider with the provided view as the implicit view.
19 */
20 - (nonnull instancetype)initWithImplicitView:(nonnull FlutterView*)view;
21 @end
22
23 @implementation FlutterViewMockProvider
24
25 - (nonnull instancetype)initWithImplicitView:(nonnull FlutterView*)view {
26 self = [super init];
27 if (self != nil) {
28 _implicitView = view;
29 }
30 return self;
31 }
32
33 - (nullable FlutterView*)viewForId:(FlutterViewId)viewId {
34 if (viewId == kFlutterImplicitViewId) {
35 return _implicitView;
36 }
37 return nil;
38 }
39
40 @end
41
42 namespace flutter::testing {
43 namespace {
44
45 typedef void (^PresentBlock)(NSArray<FlutterSurfacePresentInfo*>*);
46
47 id<FlutterViewProvider> MockViewProvider(PresentBlock onPresent = nil) {
48 FlutterView* viewMock = OCMClassMock([FlutterView class]);
49 FlutterSurfaceManager* surfaceManagerMock = OCMClassMock([FlutterSurfaceManager class]);
50 FlutterSurface* surfaceMock = OCMClassMock([FlutterSurface class]);
51 __block id<MTLTexture> textureMock = OCMProtocolMock(@protocol(MTLTexture));
52
53 OCMStub([viewMock surfaceManager]).andReturn(surfaceManagerMock);
54
55 OCMStub([surfaceManagerMock surfaceForSize:CGSize{}])
56 .ignoringNonObjectArgs()
57 .andDo(^(NSInvocation* invocation) {
58 CGSize size;
59 [invocation getArgument:&size atIndex:2];
60 OCMStub([textureMock width]).andReturn(size.width);
61 OCMStub([textureMock height]).andReturn(size.height);
62 })
63 .andReturn(surfaceMock);
64
65 FlutterMetalTexture texture = {
66 .struct_size = sizeof(FlutterMetalTexture),
67 .texture_id = 1,
68 .texture = (__bridge void*)textureMock,
69 .user_data = (__bridge void*)surfaceMock,
70 .destruction_callback = nullptr,
71 };
72
73 OCMStub([surfaceManagerMock present:[OCMArg any] notify:[OCMArg any]])
74 .andDo(^(NSInvocation* invocation) {
75 NSArray<FlutterSurfacePresentInfo*>* info;
76 [invocation getArgument:&info atIndex:2];
77 if (onPresent != nil) {
78 onPresent(info);
79 }
80 });
81
82 OCMStub([surfaceMock asFlutterMetalTexture]).andReturn(texture);
83
84 return [[FlutterViewMockProvider alloc] initWithImplicitView:viewMock];
85 }
86 } // namespace
87
88 TEST(FlutterCompositorTest, TestCreate) {
89 std::unique_ptr<flutter::FlutterCompositor> macos_compositor =
90 std::make_unique<FlutterCompositor>(MockViewProvider(),
91 /*platform_view_controller*/ nullptr);
92
93 FlutterBackingStore backing_store;
94 FlutterBackingStoreConfig config;
95 config.struct_size = sizeof(FlutterBackingStoreConfig);
96 config.size.width = 800;
97 config.size.height = 600;
98 macos_compositor->CreateBackingStore(&config, &backing_store);
99
100 ASSERT_EQ(backing_store.type, kFlutterBackingStoreTypeMetal);
101 ASSERT_NE(backing_store.metal.texture.texture, nil);
102 id<MTLTexture> texture = (__bridge id<MTLTexture>)backing_store.metal.texture.texture;
103 ASSERT_EQ(texture.width, 800ul);
104 ASSERT_EQ(texture.height, 600ul);
105 }
106
107 TEST(FlutterCompositorTest, TestPresent) {
108 __block NSArray<FlutterSurfacePresentInfo*>* presentedSurfaces = nil;
109
110 auto onPresent = ^(NSArray<FlutterSurfacePresentInfo*>* info) {
111 presentedSurfaces = info;
112 };
113
114 std::unique_ptr<flutter::FlutterCompositor> macos_compositor =
115 std::make_unique<FlutterCompositor>(MockViewProvider(onPresent),
116 /*platform_view_controller*/ nullptr);
117
118 FlutterBackingStore backing_store;
119 FlutterBackingStoreConfig config;
120 config.struct_size = sizeof(FlutterBackingStoreConfig);
121 config.size.width = 800;
122 config.size.height = 600;
123 macos_compositor->CreateBackingStore(&config, &backing_store);
124
125 FlutterLayer layers[] = {{
126 .struct_size = sizeof(FlutterLayer),
127 .type = kFlutterLayerContentTypeBackingStore,
128 .backing_store = &backing_store,
129 .offset = {0, 0},
130 .size = {800, 600},
131 }};
132 const FlutterLayer* layers_ptr = layers;
133
134 macos_compositor->Present(kFlutterImplicitViewId, &layers_ptr, 1);
135
136 ASSERT_EQ(presentedSurfaces.count, 1ul);
137 }
138
139 } // namespace flutter::testing
FlutterViewMockProvider::_implicitView
FlutterView * _implicitView
Definition: FlutterCompositorTest.mm:15
FlutterViewMockProvider
Definition: FlutterCompositorTest.mm:14
FlutterViewProvider.h
FlutterSurfaceManager
Definition: FlutterSurfaceManager.h:41
flutter::testing
Definition: AccessibilityBridgeMacTest.mm:11
kFlutterImplicitViewId
constexpr FlutterViewId kFlutterImplicitViewId
Definition: FlutterView.h:23
FlutterSurface
Definition: FlutterSurface.h:13
FlutterViewProvider-p
Definition: FlutterViewProvider.h:14
flutter::testing::TEST
TEST(AccessibilityBridgeMacTest, SendsAccessibilityCreateNotificationToWindowOfFlutterView)
Definition: AccessibilityBridgeMacTest.mm:61
FlutterView
Definition: FlutterView.h:39
texture_id
int64_t texture_id
Definition: texture_registrar_unittests.cc:24
FlutterView.h
FlutterCompositor.h
FlutterViewId
int64_t FlutterViewId
Definition: FlutterView.h:12