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
// SPDX-License-Identifier: MIT OR Apache-2.0 OR Zlib

//! Run tests using a `winit` context.

#![forbid(unsafe_code)]

/// Re-exporting `winit` for the sake of convenience.
pub use winit;

/// The whole point.
#[macro_export]
macro_rules! main {
    ($ty:ty => $($test:expr),*) => {
        #[cfg(target_arch = "wasm32")]
        $crate::__private::wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

        #[cfg(not(target_os = "android"))]
        #[cfg_attr(target_arch = "wasm32", $crate::__private::wasm_bindgen_test::wasm_bindgen_test)]
        fn main() -> Result<(), Box<dyn std::error::Error>> {
            const TESTS: &[$crate::__private::WinitBasedTest<$ty>] = &[$(
                $crate::__winit_test_internal_collect_test!($test)
            ),*];

            $crate::__private::run(TESTS, ());
            Ok(())
        }

        #[cfg(target_os = "android")]
        #[no_mangle]
        fn android_main(app: $crate::__private::Context) {
            const TESTS: &[$crate::__private::WinitBasedTest<$ty>] = &[$(
                $crate::__winit_test_internal_collect_test!($test)
            ),*];

            $crate::__private::run(TESTS, app);
        }
    };
    ($($tt:tt)*) => {
        $crate::main!(() => $($tt)*);
    }
}

#[doc(hidden)]
#[macro_export]
macro_rules! __winit_test_internal_collect_test {
    ($name:expr) => {
        $crate::__private::WinitBasedTest {
            name: stringify!($name),
            function: $crate::__private::TestFunction::Oneoff($name),
        }
    };
}

#[doc(hidden)]
// This part is semver-exempt.
pub mod __private {
    #[cfg(target_arch = "wasm32")]
    pub use wasm_bindgen_test;

    use winit::event_loop::EventLoopBuilder;
    pub use winit::event_loop::EventLoopWindowTarget;

    use owo_colors::OwoColorize;
    use std::any::Any;
    use std::panic::{catch_unwind, AssertUnwindSafe};
    #[cfg(not(target_arch = "wasm32"))]
    use std::time::Instant;
    #[cfg(target_arch = "wasm32")]
    use web_time::Instant;

    #[cfg(target_os = "android")]
    pub use winit::platform::android::{
        activity::AndroidApp as Context, EventLoopBuilderExtAndroid,
    };
    #[cfg(target_arch = "wasm32")]
    use winit::platform::web::EventLoopExtWebSys;
    #[cfg(not(target_os = "android"))]
    pub type Context = ();

    struct State {
        passed: i32,
        panics: Vec<(&'static str, Box<dyn Any + Send>)>,
        start: Instant,
        run: bool,
        code: i32,
    }

    /// Run a set of tests using a `winit` context.
    pub fn run<T: 'static>(tests: &'static [WinitBasedTest<T>], _ctx: Context) {
        // If we're on Miri, we can't run the tests.
        if cfg!(miri) {
            eprintln!();
            eprintln!(
                "{}: tests cannot be run under miri",
                "warning(winit-test)".yellow().bold()
            );
            eprintln!("    = See this issue for more information: https://github.com/notgull/winit-test/issues/2");
            eprintln!();
            return;
        }

        // Create a new event loop and obtain a window target.
        let mut builder = EventLoopBuilder::<T>::with_user_event();

        // Install the Android event loop extension if necessary.
        #[cfg(target_os = "android")]
        {
            builder.with_android_app(_ctx);
        }

        let event_loop = builder.build().expect("Failed to build event loop");

        println!("\nRunning {} tests...", tests.len());
        let mut state = State {
            passed: 0,
            panics: vec![],
            start: Instant::now(),
            run: false,
            code: 0,
        };

        // Run the tests.
        #[cfg(not(target_arch = "wasm32"))]
        event_loop
            .run(move |_, elwt| {
                run_internal(tests, &mut state, elwt);
            })
            .expect("Event loop failed to run");
        #[cfg(target_arch = "wasm32")]
        event_loop.spawn(move |_, elwt| {
            run_internal(tests, &mut state, elwt);
        });
    }

    /// Run a set of tests using a `winit` context.
    fn run_internal<T: 'static>(
        tests: &'static [WinitBasedTest<T>],
        state: &mut State,
        elwt: &EventLoopWindowTarget<T>,
    ) {
        if state.run {
            elwt.exit();
            return;
        }
        state.run = true;

        for test in tests {
            print!("test {} ... ", test.name);

            match test.function {
                TestFunction::Oneoff(f) => match catch_unwind(AssertUnwindSafe(move || f(elwt))) {
                    Ok(()) => {
                        println!("{}", "ok".green());
                        state.passed += 1;
                    }

                    Err(e) => {
                        println!("{}", "FAILED".red());
                        state.panics.push((test.name, e));
                    }
                },
            }
        }

        let failures = state.panics.len();
        println!();
        if !state.panics.is_empty() {
            println!("failures:\n");
            for (name, e) in state.panics.drain(..) {
                println!("---- {} panic ----", name);

                if let Some(s) = e.downcast_ref::<&'static str>() {
                    println!("{}", s.red());
                } else if let Some(s) = e.downcast_ref::<String>() {
                    println!("{}", s.red());
                } else {
                    println!("{}", "unknown panic type".red());
                }

                println!();
            }

            print!("test result: {}", "FAILED".red());
        } else {
            print!("test result: {}", "ok".green());
        }

        let elapsed = state.start.elapsed();
        println!(
            ". {} passed; {} failed; finished in {:?}",
            state.passed, failures, elapsed
        );

        state.code = if failures == 0 { 0 } else { 1 };
        elwt.exit();
    }

    pub struct WinitBasedTest<T: 'static> {
        pub name: &'static str,
        pub function: TestFunction<T>,
    }

    pub enum TestFunction<T: 'static> {
        Oneoff(fn(&EventLoopWindowTarget<T>)),
    }
}