Repository: bevyengine/bevy
File: crates/bevy_winit/src/state.rs (Rust)

Code before:

    if scale_factor_override.is_none() && !relative_eq!(scale_factor as f32, prior_factor) {
        window_scale_factor_changed.write(WindowScaleFactorChanged {
            window: window_entity,
            scale_factor,
        });
    }
}

#[cfg(test)]
mod tests {
    use bevy_app::Update;

    use super::*;

    #[test]
    fn test_react_to_scale_factor_change_with_changed_scale_factor() {
        let (mut app, window_entity) = setup_react_to_scale_factor_change_test_app(1.0, 2.0);
        app.update();

        let window = app.world().get::<Window>(window_entity);
        assert_eq!(window.unwrap().resolution.scale_factor(), 2.0);

        let window_backend_scale_factor_changed = app
            .world()
            .resource::<Events<WindowBackendScaleFactorChanged>>();
        assert_eq!(window_backend_scale_factor_changed.len(), 1);

        let mut window_backend_scale_factor_changed_reader =
            window_backend_scale_factor_changed.get_cursor();
        let mut window_backend_scale_factor_changed_iter =
            window_backend_scale_factor_changed_reader.read(window_backend_scale_factor_changed);
        assert_eq!(
            window_backend_scale_factor_changed_iter.next(),
            Some(&WindowBackendScaleFactorChanged {
                window: window_entity,
                scale_factor: 2.0,
            })
        );

        let window_scale_factor_changed =
            app.world().resource::<Events<WindowScaleFactorChanged>>();
        assert_eq!(window_scale_factor_changed.len(), 1);

        let mut window_scale_factor_changed_reader = window_scale_factor_changed.get_cursor();
        let mut window_scale_factor_changed_iter =
            window_scale_factor_changed_reader.read(window_scale_factor_changed);
        assert_eq!(
            window_scale_factor_changed_iter.next(),
            Some(&WindowScaleFactorChanged {
                window: window_entity,

Review comment (refactor):
You could simplify things here by just directly using `iter_current_update_events` or `drain` maybe? Same thing with the other test.

Code after:

    if scale_factor_override.is_none() && !relative_eq!(scale_factor as f32, prior_factor) {
        window_scale_factor_changed.write(WindowScaleFactorChanged {
            window: window_entity,
            scale_factor,
        });
    }
}

#[cfg(test)]
mod tests {
    use bevy_app::Update;

    use super::*;

    #[test]
    fn test_react_to_scale_factor_change_with_changed_scale_factor() {
        let (mut app, window_entity) = setup_react_to_scale_factor_change_test_app(1.0, 2.0);
        app.update();

        let window = app.world().get::<Window>(window_entity);
        assert_eq!(window.unwrap().resolution.scale_factor(), 2.0);

        let window_backend_scale_factor_changed_events = app
            .world()
            .resource::<Events<WindowBackendScaleFactorChanged>>();
        assert_eq!(window_backend_scale_factor_changed_events.len(), 1);

        let mut window_backend_scale_factor_changed_events_iter =
            window_backend_scale_factor_changed_events.iter_current_update_events();
        assert_eq!(
            window_backend_scale_factor_changed_events_iter.next(),
            Some(&WindowBackendScaleFactorChanged {
                window: window_entity,
                scale_factor: 2.0,
            })
        );
        assert_eq!(window_backend_scale_factor_changed_events_iter.next(), None);

        let window_scale_factor_changed_events =
            app.world().resource::<Events<WindowScaleFactorChanged>>();
        assert_eq!(window_scale_factor_changed_events.len(), 1);

        let mut window_scale_factor_changed_events_iter =
            window_scale_factor_changed_events.iter_current_update_events();
        assert_eq!(
            window_scale_factor_changed_events_iter.next(),
            Some(&WindowScaleFactorChanged {
                window: window_entity,
                scale_factor: 2.0,
            })