Skip to main content

easyofd_core/action/
movie.rs

1//! 视频播放动作。
2//!
3//! 对应 Java: org.ofdrw.core.action.actionType.Movie
4
5use super::{OfdAction, PlayType};
6
7/// 视频播放动作。
8///
9/// 控制视频资源的播放,对应 GB/T 33190 第 15 章的 Movie 动作。
10///
11/// 对应 Java: org.ofdrw.core.action.actionType.Movie
12#[derive(Debug, Clone)]
13pub struct Movie {
14    /// 视频资源的引用 ID。
15    ///
16    /// 对应 Java: Movie.mediaRef (String)
17    pub media_ref: String,
18
19    /// 播放类型。
20    ///
21    /// 对应 Java: Movie.type (PlayType)
22    pub play_type: PlayType,
23}
24
25impl Movie {
26    /// 创建一个新的视频播放动作。
27    ///
28    /// 对应 Java: new Movie(String mediaRef, PlayType type)
29    #[must_use]
30    pub fn new(media_ref: impl Into<String>, play_type: PlayType) -> Self {
31        Self {
32            media_ref: media_ref.into(),
33            play_type,
34        }
35    }
36}
37
38impl OfdAction for Movie {
39    fn to_xml_string(&self) -> String {
40        format!(
41            "<ofd:Movie MediaRef=\"{}\" Type=\"{}\"/>",
42            self.media_ref, self.play_type
43        )
44    }
45
46    fn clone_box(&self) -> Box<dyn OfdAction> {
47        Box::new(self.clone())
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_movie_new() {
57        let movie = Movie::new("video_001", PlayType::Play);
58        assert_eq!(movie.media_ref, "video_001");
59        assert_eq!(movie.play_type, PlayType::Play);
60    }
61
62    #[test]
63    fn test_movie_to_xml_play() {
64        let movie = Movie::new("vid_1", PlayType::Play);
65        let xml = movie.to_xml_string();
66        assert!(xml.contains("MediaRef=\"vid_1\""));
67        assert!(xml.contains("Type=\"Play\""));
68        assert!(xml.contains("<ofd:Movie"));
69        assert!(xml.ends_with("/>"));
70    }
71
72    #[test]
73    fn test_movie_to_xml_stop() {
74        let movie = Movie::new("vid_2", PlayType::Stop);
75        let xml = movie.to_xml_string();
76        assert!(xml.contains("Type=\"Stop\""));
77    }
78
79    #[test]
80    fn test_movie_clone_debug() {
81        let movie = Movie::new("m1", PlayType::Pause);
82        let movie2 = movie.clone();
83        assert_eq!(movie2.media_ref, "m1");
84        assert_eq!(movie2.play_type, PlayType::Pause);
85        let dbg = format!("{movie:?}");
86        assert!(dbg.contains("Movie"));
87    }
88}