1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//! A view that draws nowhere, whose metrics a test sets.
use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};
use std::sync::Arc;
use waft_embedder::{
GestureSettings, RequestId, Scene, ScreenId, Size, TitleBarArea, View, ViewConstraints,
ViewHost, ViewId, ViewMetrics, ViewPadding, WindowRef,
};
use crate::TestWindow;
use crate::view_host::TestViewHost;
/// The id of a [`TestView`] given none: the view of a test with one, as the engine numbers
/// Flutter's implicit view.
pub const IMPLICIT_VIEW_ID: ViewId = ViewId(0);
/// What hears that a [`TestView`]'s metrics changed: the host's client, which
/// `EmbedderClient::view_metrics_changed` reaches in an app a host runs.
pub type MetricsListener = Rc<dyn Fn(ViewId)>;
/// A view that counts the scenes presented to it, keeps the last and the size it came at, shows
/// none, and records what it asks of the system in its host: `flutter_test`'s `TestFlutterView`.
/// A frame of a size other than the view had is heard by its host
/// ([`ViewHost::view_size_changed`]), as a host that shows views sized to their content tells it.
///
/// Its metrics are what it was made with until a test sets one; each setter, and each `reset_*`
/// that puts a metric back to what the view was made with, tells the [`MetricsListener`], as
/// `TestFlutterView`'s setters call `onMetricsChanged`.
pub struct TestView {
id: ViewId,
/// What the view was made with, which the `reset_*` methods return to.
initial_metrics: ViewMetrics,
metrics: Cell<ViewMetrics>,
gesture_settings: Cell<GestureSettings>,
metrics_listener: RefCell<Option<MetricsListener>>,
presented: Cell<u32>,
last_scene: RefCell<Option<Arc<Scene>>>,
/// The size of the frame presented last, in physical pixels.
last_frame_size: Cell<Option<Size>>,
/// The view's size in physical pixels: what its metrics last gave it, or the size of a frame
/// presented since.
size: Cell<Size>,
request_id: Option<RequestId>,
initial_data: Option<String>,
/// The window the view is in, until it closes; held weakly, since the window holds the view.
window: RefCell<Option<Weak<TestWindow>>>,
/// What the view asks of the system, recorded.
host: TestViewHost,
/// The host the app gave a view it added, which takes what the view asks of the system in
/// place of `host`.
app_host: Option<Rc<dyn ViewHost>>,
}
impl TestView {
/// View [`IMPLICIT_VIEW_ID`] at pixel ratio 1, `width` by `height` physical pixels.
pub fn new(width: f64, height: f64) -> TestView {
TestView::with_pixel_ratio(width, height, 1.0)
}
/// View [`IMPLICIT_VIEW_ID`] at `device_pixel_ratio`, `width` by `height` physical pixels.
pub fn with_pixel_ratio(width: f64, height: f64, device_pixel_ratio: f64) -> TestView {
TestView::with_metrics(ViewMetrics {
physical_size: [width, height],
physical_constraints: ViewConstraints::tight(width, height),
device_pixel_ratio,
..ViewMetrics::default()
})
}
/// View [`IMPLICIT_VIEW_ID`] at `metrics`, as a host gives a view the metrics it was asked
/// for.
pub fn with_metrics(metrics: ViewMetrics) -> TestView {
let [width, height] = metrics.physical_size;
TestView {
id: IMPLICIT_VIEW_ID,
initial_metrics: metrics,
metrics: Cell::new(metrics),
gesture_settings: Cell::new(GestureSettings::default()),
metrics_listener: RefCell::new(None),
presented: Cell::new(0),
last_scene: RefCell::new(None),
last_frame_size: Cell::new(None),
size: Cell::new(Size::new(width, height)),
request_id: None,
initial_data: None,
window: RefCell::new(None),
host: TestViewHost::default(),
app_host: None,
}
}
/// The same view under another id, for a platform with several.
pub fn with_id(mut self, id: ViewId) -> TestView {
self.id = id;
self
}
/// The same view as one that arrived for request `id`, carrying `initial_data`.
pub fn with_request(mut self, id: RequestId, initial_data: Option<String>) -> TestView {
self.request_id = Some(id);
self.initial_data = initial_data;
self
}
/// The same view asking what it asks of the system of `host`, as a view the app adds on a
/// layer does; its own [`test_host`](Self::test_host) hears nothing then.
pub fn with_host(mut self, host: Rc<dyn ViewHost>) -> TestView {
self.app_host = Some(host);
self
}
/// The same view on `screen`, as its metrics say.
pub fn on_screen(mut self, screen: ScreenId) -> TestView {
self.initial_metrics.screen = Some(screen);
self.metrics.set(self.initial_metrics);
self
}
/// The same view as one an embedder gave with `initial_data`.
pub fn with_initial_data(mut self, initial_data: impl Into<String>) -> TestView {
self.initial_data = Some(initial_data.into());
self
}
/// Makes `listener` the one that hears of each change to the view's metrics, in place of the
/// one before: what runs the app installs it, as a host hands a view's changes to its client.
pub fn set_metrics_listener(&self, listener: MetricsListener) {
*self.metrics_listener.borrow_mut() = Some(listener);
}
/// Dart's `tester.view.physicalSize = size`, which also makes the physical constraints tight
/// to it.
pub fn set_physical_size(&self, size: Size) {
self.change_metrics(|metrics| {
metrics.physical_size = [size.width(), size.height()];
metrics.physical_constraints = ViewConstraints::tight(size.width(), size.height());
});
}
/// Dart's `tester.view.resetPhysicalSize()`, which resets the physical constraints too.
pub fn reset_physical_size(&self) {
let initial = self.initial_metrics;
self.change_metrics(|metrics| {
metrics.physical_size = initial.physical_size;
metrics.physical_constraints = initial.physical_constraints;
});
}
/// Dart's `tester.view.physicalConstraints = constraints`.
pub fn set_physical_constraints(&self, constraints: ViewConstraints) {
self.change_metrics(|metrics| metrics.physical_constraints = constraints);
}
/// Dart's `tester.view.resetPhysicalConstraints()`.
pub fn reset_physical_constraints(&self) {
let initial = self.initial_metrics.physical_constraints;
self.change_metrics(|metrics| metrics.physical_constraints = initial);
}
/// Dart's `tester.view.devicePixelRatio = ratio`.
pub fn set_device_pixel_ratio(&self, ratio: f64) {
self.change_metrics(|metrics| metrics.device_pixel_ratio = ratio);
}
/// Dart's `tester.view.resetDevicePixelRatio()`.
pub fn reset_device_pixel_ratio(&self) {
let initial = self.initial_metrics.device_pixel_ratio;
self.change_metrics(|metrics| metrics.device_pixel_ratio = initial);
}
/// Dart's `tester.view.padding = padding`, in physical pixels.
pub fn set_padding(&self, padding: ViewPadding) {
self.change_metrics(|metrics| metrics.padding = padding);
}
/// Dart's `tester.view.resetPadding()`.
pub fn reset_padding(&self) {
let initial = self.initial_metrics.padding;
self.change_metrics(|metrics| metrics.padding = initial);
}
/// Dart's `tester.view.viewPadding = padding`, in physical pixels.
pub fn set_view_padding(&self, padding: ViewPadding) {
self.change_metrics(|metrics| metrics.view_padding = padding);
}
/// Dart's `tester.view.resetViewPadding()`.
pub fn reset_view_padding(&self) {
let initial = self.initial_metrics.view_padding;
self.change_metrics(|metrics| metrics.view_padding = initial);
}
/// Dart's `tester.view.viewInsets = insets`, in physical pixels.
pub fn set_view_insets(&self, insets: ViewPadding) {
self.change_metrics(|metrics| metrics.view_insets = insets);
}
/// Dart's `tester.view.resetViewInsets()`.
pub fn reset_view_insets(&self) {
let initial = self.initial_metrics.view_insets;
self.change_metrics(|metrics| metrics.view_insets = initial);
}
/// Dart's `tester.view.systemGestureInsets = insets`, in physical pixels.
pub fn set_system_gesture_insets(&self, insets: ViewPadding) {
self.change_metrics(|metrics| metrics.system_gesture_insets = insets);
}
/// Dart's `tester.view.resetSystemGestureInsets()`.
pub fn reset_system_gesture_insets(&self) {
let initial = self.initial_metrics.system_gesture_insets;
self.change_metrics(|metrics| metrics.system_gesture_insets = initial);
}
/// The strip the window buttons sit in, in physical pixels, as a windowed iPad reports it;
/// Dart's `TestFlutterView` has no counterpart.
pub fn set_title_bar_area(&self, area: Option<TitleBarArea>) {
self.change_metrics(|metrics| metrics.title_bar_area = area);
}
/// Dart's `tester.view.gestureSettings = settings`.
pub fn set_gesture_settings(&self, settings: GestureSettings) {
self.gesture_settings.set(settings);
self.notify_metrics_changed();
}
/// Dart's `tester.view.resetGestureSettings()`.
pub fn reset_gesture_settings(&self) {
self.set_gesture_settings(GestureSettings::default());
}
/// Dart's `tester.view.reset()`: every metric back to what the view was made with, told as
/// one change.
pub fn reset(&self) {
self.gesture_settings.set(GestureSettings::default());
let initial = self.initial_metrics;
self.change_metrics(|metrics| *metrics = initial);
}
/// How many pictures have been presented.
pub fn presented(&self) -> u32 {
self.presented.get()
}
/// The scene presented last, the frame's layer tree, which `waft_embedder::slices` turns
/// into what it draws and the composition layers it places.
pub fn last_scene(&self) -> Option<Arc<Scene>> {
self.last_scene.borrow().clone()
}
/// The size the frame presented last came at, in physical pixels: the size the view laid
/// itself out at within its physical constraints.
pub fn last_frame_size(&self) -> Option<Size> {
self.last_frame_size.get()
}
/// The view's host, which records the cursors shown over the view, the focus requested for it,
/// what its text input was asked, what the framework said of handling Back in it and the bars'
/// styles set over it.
pub fn test_host(&self) -> &TestViewHost {
&self.host
}
/// The window the view is in, until it closes, with what it records: what `View::window`
/// answers, as the test's own window.
pub fn test_window(&self) -> Option<Rc<TestWindow>> {
self.window.borrow().as_ref()?.upgrade()
}
/// The view is in `window` from now on, or in none.
pub(crate) fn set_window(&self, window: Option<Weak<TestWindow>>) {
*self.window.borrow_mut() = window;
}
/// Applies `change` to the metrics, then tells the listener. A new physical size is the
/// view's size from now on.
fn change_metrics(&self, change: impl FnOnce(&mut ViewMetrics)) {
let previous = self.metrics.get();
let mut metrics = previous;
change(&mut metrics);
self.metrics.set(metrics);
if metrics.physical_size != previous.physical_size {
let [width, height] = metrics.physical_size;
self.size.set(Size::new(width, height));
}
self.notify_metrics_changed();
}
/// Tells the listener, if one is installed, that the metrics changed; outside its borrow, so
/// the listener may install another.
fn notify_metrics_changed(&self) {
let listener = self.metrics_listener.borrow().clone();
if let Some(listener) = listener {
listener(self.id);
}
}
}
impl View for TestView {
fn id(&self) -> ViewId {
self.id
}
fn metrics(&self) -> ViewMetrics {
self.metrics.get()
}
fn gesture_settings(&self) -> GestureSettings {
self.gesture_settings.get()
}
fn present(&self, scene: Arc<Scene>, size: Size) {
self.presented.set(self.presented.get() + 1);
*self.last_scene.borrow_mut() = Some(scene);
self.last_frame_size.set(Some(size));
if self.size.replace(size) != size {
self.host().view_size_changed(size);
}
}
fn host(&self) -> &dyn ViewHost {
self.app_host.as_deref().unwrap_or(&self.host)
}
fn initial_data(&self) -> Option<String> {
self.initial_data.clone()
}
fn request_id(&self) -> Option<RequestId> {
self.request_id
}
fn window(&self) -> Option<WindowRef> {
Some(self.test_window()? as WindowRef)
}
}