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 #ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_ASSERTIONS_H_
6 #define FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_ASSERTIONS_H_
7
8 #include <sstream>
9
10 #include "flutter/fml/logging.h"
11 #include "flutter/shell/platform/embedder/embedder.h"
12 #include "flutter/testing/assertions.h"
13 #include "gtest/gtest.h"
14 #include "third_party/skia/include/core/SkPoint.h"
15 #include "third_party/skia/include/core/SkSize.h"
16
17 //------------------------------------------------------------------------------
18 // Equality
19 //------------------------------------------------------------------------------
20 inline bool operator==(const FlutterPoint& a, const FlutterPoint& b) {
21 return flutter::testing::NumberNear(a.x, b.x) &&
22 flutter::testing::NumberNear(a.y, b.y);
23 }
24
25 inline bool operator==(const FlutterSize& a, const FlutterSize& b) {
26 return flutter::testing::NumberNear(a.width, b.width) &&
27 flutter::testing::NumberNear(a.height, b.height);
28 }
29
30 inline bool operator==(const FlutterOpenGLTexture& a,
31 const FlutterOpenGLTexture& b) {
32 return a.target == b.target && a.name == b.name && a.format == b.format &&
33 a.user_data == b.user_data &&
34 a.destruction_callback == b.destruction_callback;
35 }
36
37 inline bool operator==(const FlutterOpenGLFramebuffer& a,
38 const FlutterOpenGLFramebuffer& b) {
39 return a.target == b.target && a.name == b.name &&
40 a.user_data == b.user_data &&
41 a.destruction_callback == b.destruction_callback;
42 }
43
44 inline bool operator==(const FlutterOpenGLBackingStore& a,
45 const FlutterOpenGLBackingStore& b) {
46 if (!(a.type == b.type)) {
47 return false;
48 }
49
50 switch (a.type) {
51 case kFlutterOpenGLTargetTypeTexture:
52 return a.texture == b.texture;
53 case kFlutterOpenGLTargetTypeFramebuffer:
54 return a.framebuffer == b.framebuffer;
55 }
56
57 return false;
58 }
59
60 inline bool operator==(const FlutterSoftwareBackingStore& a,
61 const FlutterSoftwareBackingStore& b) {
62 return a.allocation == b.allocation && a.row_bytes == b.row_bytes &&
63 a.height == b.height && a.user_data == b.user_data &&
64 a.destruction_callback == b.destruction_callback;
65 }
66
67 inline bool operator==(const FlutterBackingStore& a,
68 const FlutterBackingStore& b) {
69 if (!(a.struct_size == b.struct_size && a.user_data == b.user_data &&
70 a.type == b.type && a.did_update == b.did_update)) {
71 return false;
72 }
73
74 switch (a.type) {
75 case kFlutterBackingStoreTypeOpenGL:
76 return a.open_gl == b.open_gl;
77 case kFlutterBackingStoreTypeSoftware:
78 return a.software == b.software;
79 }
80
81 return false;
82 }
83
84 inline bool operator==(const FlutterPlatformView& a,
85 const FlutterPlatformView& b) {
86 return a.struct_size == b.struct_size && a.identifier == b.identifier;
87 }
88
89 inline bool operator==(const FlutterLayer& a, const FlutterLayer& b) {
90 if (!(a.struct_size == b.struct_size && a.type == b.type &&
91 a.offset == b.offset && a.size == b.size)) {
92 return false;
93 }
94
95 switch (a.type) {
96 case kFlutterLayerContentTypeBackingStore:
97 return *a.backing_store == *b.backing_store;
98 case kFlutterLayerContentTypePlatformView:
99 return *a.platform_view == *b.platform_view;
100 }
101
102 return false;
103 }
104
105 //------------------------------------------------------------------------------
106 // Printing
107 //------------------------------------------------------------------------------
108
109 inline std::ostream& operator<<(std::ostream& out, const FlutterPoint& point) {
110 return out << "(" << point.x << ", " << point.y << ")";
111 }
112
113 inline std::ostream& operator<<(std::ostream& out, const FlutterSize& size) {
114 return out << "(" << size.width << ", " << size.height << ")";
115 }
116
FlutterLayerContentTypeToString(FlutterLayerContentType type)117 inline std::string FlutterLayerContentTypeToString(
118 FlutterLayerContentType type) {
119 switch (type) {
120 case kFlutterLayerContentTypeBackingStore:
121 return "kFlutterLayerContentTypeBackingStore";
122 case kFlutterLayerContentTypePlatformView:
123 return "kFlutterLayerContentTypePlatformView";
124 }
125 return "Unknown";
126 }
127
FlutterBackingStoreTypeToString(FlutterBackingStoreType type)128 inline std::string FlutterBackingStoreTypeToString(
129 FlutterBackingStoreType type) {
130 switch (type) {
131 case kFlutterBackingStoreTypeOpenGL:
132 return "kFlutterBackingStoreTypeOpenGL";
133 case kFlutterBackingStoreTypeSoftware:
134 return "kFlutterBackingStoreTypeSoftware";
135 }
136 return "Unknown";
137 }
138
139 inline std::ostream& operator<<(std::ostream& out,
140 const FlutterOpenGLTexture& item) {
141 return out << "(FlutterOpenGLTexture) Target: 0x" << std::hex << item.target
142 << std::dec << " Name: " << item.name << " Format: " << item.format
143 << " User Data: " << item.user_data
144 << " Destruction Callback: " << item.destruction_callback;
145 }
146
147 inline std::ostream& operator<<(std::ostream& out,
148 const FlutterOpenGLFramebuffer& item) {
149 return out << "(FlutterOpenGLFramebuffer) Target: 0x" << std::hex
150 << item.target << std::dec << " Name: " << item.name
151 << " User Data: " << item.user_data
152 << " Destruction Callback: " << item.destruction_callback;
153 }
154
155 inline std::ostream& operator<<(std::ostream& out,
156 const FlutterPlatformView& platform_view) {
157 return out << "(FlutterPlatformView) Struct Size: "
158 << platform_view.struct_size
159 << " Identifier: " << platform_view.identifier;
160 }
161
FlutterOpenGLTargetTypeToString(FlutterOpenGLTargetType type)162 inline std::string FlutterOpenGLTargetTypeToString(
163 FlutterOpenGLTargetType type) {
164 switch (type) {
165 case kFlutterOpenGLTargetTypeTexture:
166 return "kFlutterOpenGLTargetTypeTexture";
167 case kFlutterOpenGLTargetTypeFramebuffer:
168 return "kFlutterOpenGLTargetTypeFramebuffer";
169 }
170 return "Unknown";
171 }
172
173 inline std::ostream& operator<<(std::ostream& out,
174 const FlutterOpenGLBackingStore& item) {
175 out << "(FlutterOpenGLBackingStore) Type: "
176 << FlutterOpenGLTargetTypeToString(item.type) << " ";
177 switch (item.type) {
178 case kFlutterOpenGLTargetTypeTexture:
179 out << item.texture;
180 break;
181 case kFlutterOpenGLTargetTypeFramebuffer:
182 out << item.framebuffer;
183 break;
184 }
185 return out;
186 }
187
188 inline std::ostream& operator<<(std::ostream& out,
189 const FlutterSoftwareBackingStore& item) {
190 return out << "(FlutterSoftwareBackingStore) Allocation: " << item.allocation
191 << " Row Bytes: " << item.row_bytes << " Height: " << item.height
192 << " User Data: " << item.user_data
193 << " Destruction Callback: " << item.destruction_callback;
194 }
195
196 inline std::ostream& operator<<(std::ostream& out,
197 const FlutterBackingStore& backing_store) {
198 out << "(FlutterBackingStore) Struct size: " << backing_store.struct_size
199 << " User Data: " << backing_store.user_data
200 << " Type: " << FlutterBackingStoreTypeToString(backing_store.type)
201 << " ";
202
203 switch (backing_store.type) {
204 case kFlutterBackingStoreTypeOpenGL:
205 out << backing_store.open_gl;
206 break;
207
208 case kFlutterBackingStoreTypeSoftware:
209 out << backing_store.software;
210 break;
211 }
212
213 return out;
214 }
215
216 inline std::ostream& operator<<(std::ostream& out, const FlutterLayer& layer) {
217 out << "(Flutter Layer) Struct size: " << layer.struct_size
218 << " Type: " << FlutterLayerContentTypeToString(layer.type);
219
220 switch (layer.type) {
221 case kFlutterLayerContentTypeBackingStore:
222 out << *layer.backing_store;
223 break;
224 case kFlutterLayerContentTypePlatformView:
225 out << *layer.platform_view;
226 break;
227 }
228
229 return out << " Offset: " << layer.offset << " Size: " << layer.size;
230 }
231
232 //------------------------------------------------------------------------------
233 // Factories and Casts
234 //------------------------------------------------------------------------------
235
FlutterPointMake(double x,double y)236 inline FlutterPoint FlutterPointMake(double x, double y) {
237 FlutterPoint point = {};
238 point.x = x;
239 point.y = y;
240 return point;
241 }
242
FlutterSizeMake(double width,double height)243 inline FlutterSize FlutterSizeMake(double width, double height) {
244 FlutterSize size = {};
245 size.width = width;
246 size.height = height;
247 return size;
248 }
249
250 #endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_ASSERTIONS_H_
251