|
| 1 | +use webf_sys::{ExecutingContext, NativeValue, PerformanceMarkOptions, TimeoutCallback}; |
| 2 | +use webf_test_macros::{webf_test_callback, webf_test}; |
| 3 | +use webf_test_utils::{callback_runner::TestDone, common::TestCaseMetadata}; |
| 4 | + |
| 5 | +#[webf_test] |
| 6 | +pub fn test_time_origin(_metadata: TestCaseMetadata, context: ExecutingContext) { |
| 7 | + let performance = context.performance(); |
| 8 | + let time_origin = performance.time_origin(); |
| 9 | + assert!(time_origin > 0); |
| 10 | +} |
| 11 | + |
| 12 | +#[webf_test_callback] |
| 13 | +pub async fn test_now(_metadata: TestCaseMetadata, context: ExecutingContext, done: TestDone) { |
| 14 | + let (done_future, set_done) = done; |
| 15 | + let context_clone = context.clone(); |
| 16 | + let performance = context.performance(); |
| 17 | + let exception_state = context.create_exception_state(); |
| 18 | + |
| 19 | + let now = performance.now(&exception_state).unwrap(); |
| 20 | + |
| 21 | + let callback: TimeoutCallback = Box::new(move || { |
| 22 | + let exception_state = context_clone.create_exception_state(); |
| 23 | + let current = performance.now(&exception_state).unwrap(); |
| 24 | + assert!(current - now >= 300); |
| 25 | + set_done(); |
| 26 | + }); |
| 27 | + |
| 28 | + context.set_timeout_with_callback_and_timeout(callback, 300, &exception_state).unwrap(); |
| 29 | + |
| 30 | + done_future.await.unwrap(); |
| 31 | +} |
| 32 | + |
| 33 | +#[webf_test] |
| 34 | +pub fn test_clear_marks(_metadata: TestCaseMetadata, context: ExecutingContext) { |
| 35 | + let performance = context.performance(); |
| 36 | + let exception_state = context.create_exception_state(); |
| 37 | + let now = performance.now(&exception_state).unwrap(); |
| 38 | + let options = PerformanceMarkOptions { |
| 39 | + detail: NativeValue::new_null(), |
| 40 | + start_time: now as f64, |
| 41 | + }; |
| 42 | + |
| 43 | + performance.mark("abc", &options, &exception_state).unwrap(); |
| 44 | + performance.mark("efg", &options, &exception_state).unwrap(); |
| 45 | + |
| 46 | + let entries = performance.get_entries(&exception_state).unwrap(); |
| 47 | + let has_abc = entries.iter().any(|entry| entry.name() == "abc"); |
| 48 | + let has_efg = entries.iter().any(|entry| entry.name() == "efg"); |
| 49 | + assert!(has_abc); |
| 50 | + assert!(has_efg); |
| 51 | + |
| 52 | + performance.clear_marks("abc", &exception_state).unwrap(); |
| 53 | + |
| 54 | + let entries = performance.get_entries(&exception_state).unwrap(); |
| 55 | + let has_abc = entries.iter().any(|entry| entry.name() == "abc"); |
| 56 | + let has_efg = entries.iter().any(|entry| entry.name() == "efg"); |
| 57 | + assert!(!has_abc); |
| 58 | + assert!(has_efg); |
| 59 | + |
| 60 | + performance.clear_marks("efg", &exception_state).unwrap(); |
| 61 | +} |
0 commit comments