1use std::cell::RefCell;
11use std::future::Future;
12use std::pin::Pin;
13use std::rc::Rc;
14use std::task::{Context, Poll};
15
16use day_core::conformance::{
17 Case, Drive, DriveBackend, DriveOp, Fail, NativeExpect, OpFuture, TestKind,
18};
19use serde::{Deserialize, Serialize};
20
21use crate::{Reply, Step, b64encode, next_capture_revision};
22
23pub use day_script_protocol::ShotPolicy;
24
25#[derive(Clone, Debug, Serialize, Deserialize)]
27pub struct Outcome {
28 pub name: String,
29 pub kind: String,
30 pub verdict: String,
32 #[serde(default, skip_serializing_if = "Option::is_none")]
33 pub message: Option<String>,
34 #[serde(default, skip_serializing_if = "Option::is_none")]
35 pub reason: Option<String>,
36 pub ms: u64,
37 pub proves: Vec<String>,
38 #[serde(default)]
40 pub shots: Vec<String>,
41 #[serde(default, skip_serializing_if = "Vec::is_empty")]
44 pub native_unread: Vec<String>,
45}
46
47#[derive(Clone, Debug, Serialize, Deserialize)]
49pub struct Shot {
50 pub test: String,
51 pub name: String,
52 pub png_base64: String,
53}
54
55#[derive(Clone, Debug, Default, Serialize, Deserialize)]
57pub struct RunReport {
58 pub tests: Vec<Outcome>,
59 #[serde(default)]
60 pub shots: Vec<Shot>,
61 #[serde(default)]
64 pub caps: std::collections::BTreeMap<String, String>,
65}
66
67struct Run {
68 report: RunReport,
69 done: bool,
70}
71
72thread_local! {
73 static RUN: RefCell<Option<Rc<RefCell<Run>>>> = const { RefCell::new(None) };
74}
75
76pub fn listing() -> Result<serde_json::Value, String> {
82 day_core::conformance::validate_registry()?;
83 let cases = day_core::conformance::cases();
84 Ok(serde_json::Value::Array(
85 cases
86 .iter()
87 .map(|c| {
88 serde_json::json!({
89 "name": c.name(),
90 "kind": kind_name(c.kind()),
91 "proves": c.proves_keys(),
92 "requires": c.requirements().iter().map(|r| format!("{r:?}")).collect::<Vec<_>>(),
93 })
94 })
95 .collect(),
96 ))
97}
98
99pub fn run_tests(
108 filter: &[String],
109 shots: ShotPolicy,
110 case_timeout_secs: Option<f64>,
111) -> Result<Reply, Reply> {
112 day_core::conformance::validate_registry().map_err(|e| Reply::fail(e, false))?;
113 let run = RUN.with(|r| r.borrow().clone());
114 match run {
115 None => {
116 start(filter.to_vec(), shots, case_timeout_secs);
117 Err(Reply::fail("tests running", true))
118 }
119 Some(run) => {
120 if !run.borrow().done {
121 return Err(Reply::fail("tests running", true));
122 }
123 RUN.with(|r| *r.borrow_mut() = None);
124 let report = std::mem::take(&mut run.borrow_mut().report);
125 let data = serde_json::to_value(&report)
126 .map_err(|e| Reply::fail(format!("test report: {e}"), false))?;
127 Ok(Reply {
128 data: Some(data),
129 ..Reply::ok()
130 })
131 }
132 }
133}
134
135fn kind_name(kind: TestKind) -> &'static str {
136 match kind {
137 TestKind::Gui => "gui",
138 TestKind::Headless => "headless",
139 }
140}
141
142fn matches(filter: &[String], name: &str) -> bool {
143 filter.is_empty() || filter.iter().any(|f| glob(f, name))
144}
145
146fn glob(pattern: &str, name: &str) -> bool {
148 let mut parts = pattern.split('*');
149 let Some(first) = parts.next() else {
150 return pattern == name;
151 };
152 if !pattern.contains('*') {
153 return pattern == name;
154 }
155 if !name.starts_with(first) {
156 return false;
157 }
158 let mut rest = &name[first.len()..];
159 let parts: Vec<&str> = parts.collect();
160 for (i, part) in parts.iter().enumerate() {
161 if part.is_empty() {
162 continue;
163 }
164 let last = i + 1 == parts.len() && !pattern.ends_with('*');
165 if last {
166 return rest.ends_with(part);
167 }
168 match rest.find(part) {
169 Some(at) => rest = &rest[at + part.len()..],
170 None => return false,
171 }
172 }
173 true
174}
175
176const DEFAULT_CASE_TIMEOUT_SECS: f64 = 30.0;
178
179fn start(filter: Vec<String>, policy: ShotPolicy, case_timeout_secs: Option<f64>) {
180 let run = Rc::new(RefCell::new(Run {
181 report: RunReport::default(),
182 done: false,
183 }));
184 run.borrow_mut().report.caps = day_spec::Cap::ALL
185 .iter()
186 .map(|cap| {
187 let answer = match day_core::capability(*cap) {
188 day_spec::Support::Native => "N",
189 day_spec::Support::Emulated => "E",
190 day_spec::Support::Unsupported => "-",
191 };
192 (format!("{cap:?}"), answer.to_owned())
193 })
194 .collect();
195 RUN.with(|r| *r.borrow_mut() = Some(run.clone()));
196 day_core::conformance::set_intercepting_urls(true);
198 day_core::task(async move {
199 let cases: Vec<Case> = day_core::conformance::cases()
200 .into_iter()
201 .filter(|c| matches(&filter, c.name()))
202 .collect();
203 let mut gui = false;
204 for (i, case) in cases.iter().enumerate() {
205 let mut shots = Vec::new();
206 let twin = cases
209 .iter()
210 .enumerate()
211 .any(|(j, c)| j != i && c.name() == case.name());
212 let outcome = if twin {
213 let mut o = outcome_for(case);
214 o.verdict = "fail".into();
215 o.message = Some(format!(
216 "two #[day::test] functions are named {}; rename one",
217 case.name()
218 ));
219 o
220 } else {
221 gui |= case.kind() == TestKind::Gui;
222 log::info!("conformance: {} ({}/{})", case.name(), i + 1, cases.len());
226 run_case(case, policy, case_timeout_secs, &mut shots).await
227 };
228 let mut run = run.borrow_mut();
229 run.report.shots.extend(shots);
230 run.report.tests.push(outcome);
231 }
232 if gui {
234 day_core::conformance::clear_active();
235 }
236 day_core::conformance::set_intercepting_urls(false);
237 run.borrow_mut().done = true;
238 });
239}
240
241fn outcome_for(case: &Case) -> Outcome {
242 Outcome {
243 name: case.name().to_owned(),
244 kind: kind_name(case.kind()).to_owned(),
245 verdict: "pass".into(),
246 message: None,
247 reason: None,
248 ms: 0,
249 proves: case.proves_keys().to_vec(),
250 shots: Vec::new(),
251 native_unread: Vec::new(),
252 }
253}
254
255async fn run_case(
256 case: &Case,
257 policy: ShotPolicy,
258 run_timeout_secs: Option<f64>,
259 shots: &mut Vec<Shot>,
260) -> Outcome {
261 let started = Stopwatch::start();
262 let mut outcome = outcome_for(case);
263 if let Some(cap) = case
266 .requirements()
267 .iter()
268 .find(|c| day_core::capability(**c) == day_spec::Support::Unsupported)
269 {
270 outcome.verdict = "skip".into();
271 outcome.reason = Some(format!("Cap::{cap:?} is Unsupported on this toolkit"));
272 return outcome;
273 }
274 if let Some(what) = case.missing_support() {
275 outcome.verdict = "skip".into();
276 outcome.reason = Some(format!("{what} is Unsupported on this toolkit"));
277 return outcome;
278 }
279 let backend = Rc::new(Engine {
280 shots: Rc::new(RefCell::new(Vec::new())),
281 unread: Rc::new(RefCell::new(Vec::new())),
282 });
283 let drive = Drive::new(backend.clone());
284 let limit = match run_timeout_secs {
287 Some(t) if t <= 0.0 => None,
288 run => Some(
289 case.timeout_secs()
290 .or(run)
291 .unwrap_or(DEFAULT_CASE_TIMEOUT_SECS),
292 ),
293 };
294 let body = {
295 let (case, drive) = (case.clone(), drive.clone());
296 async move {
297 if case.kind() == TestKind::Gui {
298 if !day_core::conformance::set_active(case.clone()) {
301 return Err(Fail(
302 "the app shows no test host: root its test build in \
303 `day::test_host(..)` (docs/testing.md)"
304 .into(),
305 ));
306 }
307 day_reactive::flush_sync();
308 drive.wait_idle().await?;
309 if let Some(message) = day_core::conformance::take_page_panic() {
310 return Err(Fail(format!("the page panicked: {message}")));
311 }
312 for name in case.shots() {
313 drive.shot(name).await?;
314 }
315 }
316 case.run_body(drive).await
317 }
318 };
319 let result = Guarded::new(Box::pin(body), limit).await;
320 let placeholders = day_spec::placeholder::seen();
323 if let Some(kind) = case.proves_keys().iter().find_map(|k| {
324 k.strip_prefix("kind:")
325 .filter(|kind| placeholders.iter().any(|seen| seen == kind))
326 }) {
327 outcome.verdict = "skip".into();
328 outcome.reason = Some(format!("no {kind} renderer on this toolkit"));
329 outcome.ms = started.elapsed_ms();
330 return outcome;
331 }
332 if let Err(Fail(message)) = &result {
333 outcome.verdict = "fail".into();
334 outcome.message = Some(message.clone());
335 if policy != ShotPolicy::Never {
336 let _ = drive.shot("failed").await;
338 }
339 }
340 outcome.ms = started.elapsed_ms();
341 outcome.native_unread = std::mem::take(&mut *backend.unread.borrow_mut());
342 outcome.native_unread.dedup();
343 let keep = match policy {
344 ShotPolicy::Never => false,
345 ShotPolicy::OnFailure => result.is_err(),
346 ShotPolicy::Always => true,
347 };
348 if keep {
349 for (name, png) in backend.shots.borrow_mut().drain(..) {
350 outcome.shots.push(name.clone());
351 shots.push(Shot {
352 test: case.name().to_owned(),
353 name,
354 png_base64: b64encode(&png),
355 });
356 }
357 }
358 outcome
359}
360
361struct Guarded {
367 body: Pin<Box<dyn Future<Output = Result<(), Fail>>>>,
368 limit: Option<(f64, Pin<Box<day_core::Sleep>>)>,
369}
370
371impl Guarded {
372 fn new(body: Pin<Box<dyn Future<Output = Result<(), Fail>>>>, secs: Option<f64>) -> Self {
373 Guarded {
374 body,
375 limit: secs.map(|s| (s, Box::pin(day_core::sleep((s * 1000.0) as u32)))),
376 }
377 }
378}
379
380impl Future for Guarded {
381 type Output = Result<(), Fail>;
382
383 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
384 let this = &mut *self;
385 let polled =
386 std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| this.body.as_mut().poll(cx)));
387 match polled {
388 Ok(Poll::Ready(result)) => return Poll::Ready(result),
389 Err(payload) => {
390 let message = day_core::conformance::panic_message(payload.as_ref());
391 return Poll::Ready(Err(Fail(format!("panicked: {message}"))));
392 }
393 Ok(Poll::Pending) => {}
394 }
395 if let Some((secs, timer)) = &mut this.limit
396 && timer.as_mut().poll(cx).is_ready()
397 {
398 return Poll::Ready(Err(Fail(format!("timed out after {secs} s"))));
399 }
400 Poll::Pending
401 }
402}
403
404struct Engine {
412 shots: Rc<RefCell<Vec<Capture>>>,
413 unread: Rc<RefCell<Vec<String>>>,
415}
416
417type Capture = (String, Vec<u8>);
419
420impl DriveBackend for Engine {
421 fn run(&self, op: DriveOp) -> OpFuture {
422 let (shots, unread) = (self.shots.clone(), self.unread.clone());
423 Box::pin(async move {
424 if let DriveOp::Pause(secs) = op {
425 day_core::sleep((secs * 1000.0) as u32).await;
426 return Ok(());
427 }
428 run_step(&op, &shots, &unread).await?;
429 if let Some(native) = native_follow_up(&op) {
430 run_step(&native, &shots, &unread).await?;
431 }
432 Ok(())
433 })
434 }
435}
436
437fn native_follow_up(op: &DriveOp) -> Option<DriveOp> {
439 let (id, expect) = match op {
440 DriveOp::AssertText(id, text) => (
441 id,
442 NativeExpect {
443 text: Some(text.clone()),
444 ..Default::default()
445 },
446 ),
447 DriveOp::AssertValue(id, value) => (
448 id,
449 NativeExpect {
450 number: Some(*value),
451 ..Default::default()
452 },
453 ),
454 DriveOp::AssertOn(id, on) => (
455 id,
456 NativeExpect {
457 checked: Some(*on),
458 ..Default::default()
459 },
460 ),
461 _ => return None,
462 };
463 Some(DriveOp::AssertNative(id.clone(), expect))
464}
465
466async fn run_step(
468 op: &DriveOp,
469 shots: &Rc<RefCell<Vec<Capture>>>,
470 unread: &Rc<RefCell<Vec<String>>>,
471) -> Result<(), Fail> {
472 let step = step_for(op)?;
473 let clock = Stopwatch::start();
478 let mut waited_ms = 0u32;
479 let budget_ms = (crate::DEFAULT_TIMEOUT_SECS * 1000.0) as u32;
480 let revision = next_capture_revision();
483 loop {
484 let reply = crate::exec(step.clone(), revision);
485 if reply.ok {
486 if let (DriveOp::Shot(name), Some(png)) = (op, &reply.png_base64) {
487 shots
488 .borrow_mut()
489 .push((name.clone(), crate::b64decode(png)));
490 }
491 let checked = match op {
492 DriveOp::AssertNative(id, _)
493 | DriveOp::AssertFrame(id, _)
494 | DriveOp::AssertDisplayed(id, _)
495 | DriveOp::SamplePixel(id, ..) => Some(id),
496 _ => None,
497 };
498 if let (Some(id), Some(data)) = (checked, &reply.data) {
499 let fields = data.get("unread").and_then(|u| u.as_array());
500 for f in fields.into_iter().flatten().filter_map(|f| f.as_str()) {
501 unread.borrow_mut().push(format!("{id} {f}"));
502 }
503 }
504 return Ok(());
505 }
506 let message = reply.error.unwrap_or_else(|| "failed".into());
507 let elapsed_ms = u32::try_from(clock.elapsed_ms()).unwrap_or(u32::MAX);
508 if !reply.retryable || waited_ms.max(elapsed_ms) >= budget_ms {
509 return Err(Fail(format!("{}: {message}", op_name(op))));
510 }
511 let wait = if reply.capture_pending {
512 16
513 } else {
514 crate::RETRY_MS
515 };
516 waited_ms += wait;
517 day_core::sleep(wait).await;
518 }
519}
520
521fn op_name(op: &DriveOp) -> String {
522 match op {
523 DriveOp::Tap(id) | DriveOp::TapAt(id, ..) => format!("tap {id}"),
524 DriveOp::Input(id, _) => format!("input {id}"),
525 DriveOp::Toggle(id, _) => format!("toggle {id}"),
526 DriveOp::SetValue(id, _) => format!("set_value {id}"),
527 DriveOp::Select(id, _) => format!("select {id}"),
528 DriveOp::Focus(id) => format!("focus {id}"),
529 DriveOp::Submit(id) => format!("submit {id}"),
530 DriveOp::Navigate(route) => format!("navigate {route}"),
531 DriveOp::WaitIdle => "wait_idle".into(),
532 DriveOp::Pause(_) => "pause".into(),
533 DriveOp::Shot(name) => format!("shot {name}"),
534 DriveOp::AssertText(id, _) => format!("assert_text {id}"),
535 DriveOp::AssertVisible(id) => format!("assert_visible {id}"),
536 DriveOp::AssertMissing(id) => format!("assert_missing {id}"),
537 DriveOp::AssertHidden(id) => format!("assert_hidden {id}"),
538 DriveOp::Hover(id, _) | DriveOp::HoverLeave(id) => format!("hover {id}"),
539 DriveOp::Pan(id, ..) => format!("pan {id}"),
540 DriveOp::Pinch(id, _) => format!("pinch {id}"),
541 DriveOp::Rotate(id, _) => format!("rotate {id}"),
542 DriveOp::Drag(id, ..) | DriveOp::Fling(id, ..) => format!("drag {id}"),
543 DriveOp::Key(_, key) | DriveOp::KeyPhase(_, key, _) => format!("key {key}"),
544 DriveOp::Press(id, _) => format!("press {id}"),
545 DriveOp::LongPress(id) => format!("long_press {id}"),
546 DriveOp::DoubleTap(id) => format!("double_tap {id}"),
547 DriveOp::Touches(id, ..) => format!("touches {id}"),
548 DriveOp::Pointer(kind) => format!("pointer {kind}"),
549 DriveOp::DropText(id, ..) | DriveOp::DropFiles(id, _) => format!("drop {id}"),
550 DriveOp::AssertAnnounced(text) => format!("assert_announced {text}"),
551 DriveOp::AssertValue(id, _) | DriveOp::AssertOn(id, _) => format!("assert_value {id}"),
552 DriveOp::AssertFocused(id, _) => format!("assert_focused {id}"),
553 DriveOp::AssertRoute(route) => format!("assert_route {route}"),
554 DriveOp::A11yAudit(_) => "a11y_audit".into(),
555 DriveOp::AssertNative(id, _) => format!("assert_native {id}"),
556 DriveOp::AssertFrame(id, _) => format!("assert_frame {id}"),
557 DriveOp::AssertDisplayed(id, _) => format!("assert_displayed {id}"),
558 DriveOp::SamplePixel(id, ..) => format!("sample_pixel {id}"),
559 DriveOp::AssertOpenedUrl(url) => format!("assert_opened_url {url}"),
560 DriveOp::Activate(id, _) => format!("activate {id}"),
561 DriveOp::Reorder(id, ..) => format!("reorder {id}"),
562 DriveOp::DeleteRow(id, _) => format!("delete_row {id}"),
563 DriveOp::SwipeRow(id, ..) => format!("swipe_row {id}"),
564 DriveOp::ScrollTo(id, _) => format!("scroll_to {id}"),
565 DriveOp::Expand(id, row, _) => format!("expand {id} {row}"),
566 DriveOp::NavBack => "nav_back".into(),
567 DriveOp::TreeMove(id, row, ..) => format!("tree_move {id} {row}"),
568 DriveOp::ContextMenu(id, item) => format!("context_menu {id} {item}"),
569 DriveOp::Menu(item) => format!("menu {item}"),
570 DriveOp::ToolbarPress(item) | DriveOp::ToolbarToggle(item, _) => format!("toolbar {item}"),
571 DriveOp::DialogScripted(_) => "dialog_mode".into(),
572 DriveOp::AssertPresented(_) => "assert_presented".into(),
573 DriveOp::Respond(_) | DriveOp::RespondText(_) => "respond".into(),
574 }
575}
576
577fn step_for(op: &DriveOp) -> Result<Step, Fail> {
580 use serde_json::json;
581 let v = match op {
582 DriveOp::Tap(id) => json!({"op": "tap", "id": id}),
583 DriveOp::Input(id, text) => json!({"op": "input", "id": id, "text": text}),
584 DriveOp::Toggle(id, on) => json!({"op": "toggle", "id": id, "value": on}),
585 DriveOp::SetValue(id, value) => json!({"op": "set_value", "id": id, "value": value}),
586 DriveOp::Select(id, index) => json!({"op": "select", "id": id, "index": index}),
587 DriveOp::Focus(id) => json!({"op": "focus", "id": id}),
588 DriveOp::Submit(id) => json!({"op": "submit", "id": id}),
589 DriveOp::Navigate(route) => json!({"op": "navigate", "route": route}),
590 DriveOp::WaitIdle => json!({"op": "wait_idle"}),
591 DriveOp::Pause(secs) => json!({"op": "pause", "secs": secs}),
592 DriveOp::Shot(name) => json!({"op": "screenshot", "name": name, "in_process": true}),
593 DriveOp::AssertText(id, text) => json!({"op": "assert_text", "id": id, "text": text}),
594 DriveOp::AssertVisible(id) => json!({"op": "assert_visible", "id": id}),
595 DriveOp::AssertMissing(id) => json!({"op": "assert_missing", "id": id}),
596 DriveOp::AssertHidden(id) => json!({"op": "assert_hidden", "id": id}),
597 DriveOp::Hover(id, at) => json!({"op": "hover", "id": id, "at": at.map(|(x, y)| [x, y])}),
598 DriveOp::HoverLeave(id) => json!({"op": "hover", "id": id, "leave": true}),
599 DriveOp::Pan(id, dx, dy) => json!({"op": "pan", "id": id, "by": [dx, dy]}),
600 DriveOp::Pinch(id, scale) => json!({"op": "pinch", "id": id, "scale": scale}),
601 DriveOp::Rotate(id, rotation) => {
602 json!({"op": "rotate", "id": id, "rotation": rotation})
603 }
604 DriveOp::Drag(id, (fx, fy), (tx, ty)) => {
605 json!({"op": "drag", "id": id, "from": [fx, fy], "to": [tx, ty]})
606 }
607 DriveOp::TapAt(id, x, y) => json!({"op": "tap", "id": id, "at": [x, y]}),
608 DriveOp::Key(id, key) => json!({"op": "key", "id": id, "key": key}),
609 DriveOp::KeyPhase(id, key, phase) => {
610 json!({"op": "key", "id": id, "key": key, "phase": phase})
611 }
612 DriveOp::Press(id, pressed) => json!({"op": "press", "id": id, "release": !pressed}),
613 DriveOp::LongPress(id) => json!({"op": "long_press", "id": id}),
614 DriveOp::DoubleTap(id) => json!({"op": "double_tap", "id": id}),
615 DriveOp::Touches(id, points, phase) => {
616 let points: Vec<[f64; 2]> = points.iter().map(|(x, y)| [*x, *y]).collect();
617 json!({"op": "touches", "id": id, "points": points, "phase": phase})
618 }
619 DriveOp::Fling(id, (fx, fy), (tx, ty), (vx, vy)) => {
620 json!({"op": "drag", "id": id, "from": [fx, fy], "to": [tx, ty], "velocity": [vx, vy]})
621 }
622 DriveOp::Pointer(kind) => json!({"op": "pointer", "kind": kind}),
623 DriveOp::DropText(id, text, phase) => {
624 json!({"op": "drop", "id": id, "text": text, "phase": phase})
625 }
626 DriveOp::DropFiles(id, files) => json!({"op": "drop", "id": id, "files": files}),
627 DriveOp::AssertAnnounced(text) => json!({"op": "assert_announced", "text": text}),
628 DriveOp::AssertValue(id, value) => json!({"op": "assert_value", "id": id, "value": value}),
629 DriveOp::AssertOn(id, on) => json!({"op": "assert_value", "id": id, "value": on}),
630 DriveOp::AssertFocused(id, focused) => {
631 json!({"op": "assert_focused", "id": id, "focused": focused})
632 }
633 DriveOp::AssertRoute(route) => json!({"op": "assert_route", "route": route}),
634 DriveOp::A11yAudit(id) => json!({"op": "a11y_audit", "id": id}),
635 DriveOp::AssertNative(id, e) => json!({
636 "op": "assert_native", "id": id, "text": e.text, "number": e.number,
637 "checked": e.checked, "enabled": e.enabled, "visible": e.visible,
638 }),
639 DriveOp::AssertDisplayed(id, e) => json!({
640 "op": "assert_displayed", "id": id, "opacity": e.opacity,
641 "tx": e.translation.map(|t| t.x), "ty": e.translation.map(|t| t.y),
642 "width": e.width, "height": e.height,
643 "background": e.background.map(|c| {
644 let byte = |v: f64| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
645 format!("#{:02x}{:02x}{:02x}", byte(c.r), byte(c.g), byte(c.b))
646 }),
647 "tolerance": e.tolerance,
648 }),
649 DriveOp::AssertFrame(id, f) => json!({
650 "op": "assert_frame", "id": id, "width": f.width, "height": f.height,
651 "x": f.x, "y": f.y, "relative_to": f.relative_to, "tolerance": f.tolerance,
652 }),
653 DriveOp::SamplePixel(id, x, y, color) => {
654 json!({"op": "sample_pixel", "id": id, "x": x, "y": y, "color": color})
655 }
656 DriveOp::AssertOpenedUrl(url) => json!({"op": "assert_opened_url", "url": url}),
657 DriveOp::Activate(id, index) => json!({"op": "activate", "id": id, "index": index}),
658 DriveOp::Reorder(id, from, to) => {
659 json!({"op": "reorder", "id": id, "from": from, "to": to})
660 }
661 DriveOp::DeleteRow(id, row) => json!({"op": "delete_row", "id": id, "row": row}),
662 DriveOp::SwipeRow(id, row, leading, action) => json!({
663 "op": "swipe_row", "id": id, "row": row, "action": action,
664 "edge": if *leading { "leading" } else { "trailing" },
665 }),
666 DriveOp::ScrollTo(id, edge) if edge.is_empty() => json!({"op": "scroll_to", "id": id}),
668 DriveOp::ScrollTo(id, edge) => json!({"op": "scroll_to", "id": id, "edge": edge}),
669 DriveOp::Expand(id, row, expanded) => {
670 json!({"op": "expand", "id": id, "row": row, "expanded": expanded})
671 }
672 DriveOp::NavBack => json!({"op": "nav_back"}),
673 DriveOp::TreeMove(id, row, parent, index) => json!({
674 "op": "tree_move", "id": id, "row": row, "parent": parent, "index": index,
675 }),
676 DriveOp::ContextMenu(id, item) => json!({"op": "context_menu", "id": id, "item": item}),
677 DriveOp::Menu(item) => json!({"op": "menu", "id": item}),
678 DriveOp::ToolbarPress(item) => json!({"op": "toolbar", "item": item}),
679 DriveOp::ToolbarToggle(item, on) => json!({"op": "toolbar", "item": item, "on": on}),
680 DriveOp::DialogScripted(scripted) => json!({
681 "op": "dialog_mode", "mode": if *scripted { "scripted" } else { "native" },
682 }),
683 DriveOp::AssertPresented(title) => json!({"op": "assert_presented", "title": title}),
684 DriveOp::Respond(Some(button)) => json!({"op": "respond", "button": button}),
685 DriveOp::Respond(None) => json!({"op": "respond", "dismiss": true}),
686 DriveOp::RespondText(text) => json!({"op": "respond", "text": text}),
687 };
688 serde_json::from_value(v).map_err(|e| Fail(format!("{}: {e}", op_name(op))))
689}
690
691struct Stopwatch {
694 #[cfg(not(target_arch = "wasm32"))]
695 started: std::time::Instant,
696}
697
698impl Stopwatch {
699 fn start() -> Self {
700 Stopwatch {
701 #[cfg(not(target_arch = "wasm32"))]
702 started: std::time::Instant::now(),
703 }
704 }
705 #[cfg_attr(
706 target_arch = "wasm32",
707 expect(
708 clippy::unused_self,
709 reason = "wasm has no clock; the native arm reads the start time"
710 )
711 )]
712 fn elapsed_ms(&self) -> u64 {
713 #[cfg(not(target_arch = "wasm32"))]
714 {
715 self.started.elapsed().as_millis() as u64
716 }
717 #[cfg(target_arch = "wasm32")]
718 {
719 0
720 }
721 }
722}
723
724#[cfg(test)]
725mod tests {
726 use super::glob;
727
728 #[test]
729 fn globs_match_prefixes_suffixes_and_middles() {
730 assert!(glob("button-*", "button-status"));
731 assert!(glob("*-status", "button-status"));
732 assert!(glob("button*status", "button-status"));
733 assert!(glob("*", "anything"));
734 assert!(glob("button-status", "button-status"));
735 assert!(!glob("button-*", "slider-range"));
736 assert!(!glob("button", "button-status"));
737 }
738}