summaryrefslogtreecommitdiff
path: root/rust/crates/commands/tests/framework.rs
blob: e3defbad07fb06aeced39ad0300d867723a606f5 (plain) (blame)
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
mod common;

use std::cell::RefCell;
use std::rc::Rc;

use commands::{
    BatchCommand, Command, CommandError, CommandHistory, CommandResult, DeleteElementsCommand,
    PreviewTracker,
};
use common::{VIDEO_SECONDS, seconds, seed_project_with_video};
use scene::{ElementRef, Project};

struct RecordingCommand {
    label: &'static str,
    log: Rc<RefCell<Vec<&'static str>>>,
    selection: Option<Vec<ElementRef>>,
}

impl RecordingCommand {
    fn new(label: &'static str, log: &Rc<RefCell<Vec<&'static str>>>) -> Self {
        Self {
            label,
            log: Rc::clone(log),
            selection: None,
        }
    }

    fn with_selection(mut self, selection: Vec<ElementRef>) -> Self {
        self.selection = Some(selection);
        self
    }
}

impl Command for RecordingCommand {
    fn execute(&mut self, _project: &mut Project) -> Result<Option<CommandResult>, CommandError> {
        self.log.borrow_mut().push(self.label);
        Ok(self
            .selection
            .clone()
            .map(CommandResult::element_selection))
    }

    fn undo(&mut self, _project: &mut Project) {
        self.log.borrow_mut().push(self.label);
    }
}

#[test]
fn batch_executes_in_order_and_undoes_in_reverse() {
    let (mut project, _) = seed_project_with_video();
    let log = Rc::new(RefCell::new(Vec::new()));

    let mut batch = BatchCommand::new(vec![
        Box::new(RecordingCommand::new("first", &log)),
        Box::new(RecordingCommand::new("second", &log)),
        Box::new(RecordingCommand::new("third", &log)),
    ]);

    batch.execute(&mut project).expect("execute");
    assert_eq!(*log.borrow(), vec!["first", "second", "third"]);

    log.borrow_mut().clear();
    batch.undo(&mut project);
    assert_eq!(*log.borrow(), vec!["third", "second", "first"]);
}

#[test]
fn batch_returns_latest_selection_result() {
    let (mut project, _) = seed_project_with_video();
    let log = Rc::new(RefCell::new(Vec::new()));

    let selected = vec![ElementRef {
        track_id: "main-track-1".to_string(),
        element_id: "video-1".to_string(),
    }];
    let mut batch = BatchCommand::new(vec![
        Box::new(
            RecordingCommand::new("selecting", &log).with_selection(selected.clone()),
        ),
        Box::new(RecordingCommand::new("plain", &log)),
    ]);

    let result = batch.execute(&mut project).expect("execute");
    let selection = result
        .and_then(|result| result.selection)
        .expect("selection result");
    assert_eq!(selection.selected_elements, selected);
}

#[test]
fn history_undo_redo_roundtrip() {
    let (mut project, track_id) = seed_project_with_video();
    let mut history = CommandHistory::new();

    assert!(!history.can_undo());
    assert!(!history.can_redo());

    history
        .execute(
            Box::new(DeleteElementsCommand::new(vec![ElementRef {
                track_id: track_id.clone(),
                element_id: "video-1".to_string(),
            }])),
            &mut project,
        )
        .expect("execute");

    let scene = common::active_scene;
    assert!(scene(&project).tracks.main.elements().is_empty());
    assert!(history.can_undo());
    assert!(!history.can_redo());

    history.undo(&mut project);
    assert_eq!(scene(&project).tracks.main.elements().len(), 1);
    assert!(!history.can_undo());
    assert!(history.can_redo());

    history.redo(&mut project).expect("redo");
    assert!(scene(&project).tracks.main.elements().is_empty());
    assert!(history.can_undo());
    assert!(!history.can_redo());
}

#[test]
fn history_new_execute_clears_redo_stack() {
    let (mut project, track_id) = seed_project_with_video();
    let mut history = CommandHistory::new();

    let delete = || {
        Box::new(DeleteElementsCommand::new(vec![ElementRef {
            track_id: track_id.clone(),
            element_id: "video-1".to_string(),
        }])) as Box<dyn Command>
    };

    history.execute(delete(), &mut project).expect("execute");
    history.undo(&mut project);
    assert!(history.can_redo());

    history.execute(delete(), &mut project).expect("execute");
    assert!(!history.can_redo());

    history.clear();
    assert!(!history.can_undo());
    assert!(!history.can_redo());
}

#[test]
fn preview_tracker_keeps_first_snapshot_until_end() {
    let mut tracker = PreviewTracker::new();
    assert!(!tracker.is_active());
    assert_eq!(tracker.end(), None);

    tracker.begin(&seconds(VIDEO_SECONDS));
    tracker.begin(&seconds(1.0));
    assert!(tracker.is_active());
    assert_eq!(tracker.snapshot(), Some(&seconds(VIDEO_SECONDS)));

    assert_eq!(tracker.end(), Some(seconds(VIDEO_SECONDS)));
    assert!(!tracker.is_active());
    assert_eq!(tracker.snapshot(), None);
}