xref: /aosp_15_r20/tools/asuite/adevice/tests/common/fakes.rs (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1 use adevice::adevice::{Device, Host, Profiler};
2 use adevice::commands::{AdbAction, AdbCommand};
3 use adevice::fingerprint::FileMetadata;
4 use adevice::metrics::MetricSender;
5 use adevice::tracking::Config;
6 use anyhow::Result;
7 use std::cell::RefCell;
8 use std::collections::{HashMap, HashSet};
9 use std::path::{Path, PathBuf};
10 
11 #[derive(Default)]
12 pub struct FakeHost {
13     /// Files on the filesystem, relative to PRODUCT_OUT
14     files: HashMap<PathBuf, FileMetadata>,
15     /// Dependencies from ninja, relative to PRODUCT_OUT
16     tracked_files: Vec<String>,
17 }
18 
19 // We allow dead code here and below as not all the tests uses all the methods,
20 // and they get marked as dead in soong when compiling just one test at a time.
21 #[allow(dead_code)]
22 impl FakeHost {
new(files: &HashMap<PathBuf, FileMetadata>, tracked_files: &[String]) -> FakeHost23     pub fn new(files: &HashMap<PathBuf, FileMetadata>, tracked_files: &[String]) -> FakeHost {
24         FakeHost { files: files.clone(), tracked_files: tracked_files.to_owned() }
25     }
26 
27     /// Returns true if `path` starts with one of the `partitions`
on_a_partition(path: &Path, partitions: &[PathBuf]) -> bool28     fn on_a_partition(path: &Path, partitions: &[PathBuf]) -> bool {
29         for p in partitions {
30             if path.starts_with(p) {
31                 return true;
32             }
33         }
34         false
35     }
36 }
37 
38 #[allow(dead_code)]
39 #[derive(Default)]
40 pub struct FakeDevice {
41     /// Apks that are installed with "adb install" on the /data partition.
42     /// Used to see if we should warn the user about potential problems.
43     installed_apks: HashSet<String>,
44 
45     /// Files on the filesystem.
46     /// User passes some to start, but "push" and "clean" commands will affect it.
47     files: HashMap<PathBuf, FileMetadata>,
48 
49     // Files pushed to the device via an `adb_command`
50     pushes: RefCell<Vec<PathBuf>>,
51     // Files and directories removed from the device via `adb_command`
52     removes: RefCell<Vec<PathBuf>>,
53 
54     // Cmds that are issued with run_raw_adb_command;
55     raw_cmds: RefCell<Vec<String>>,
56 
57     // How many times has wait() beeng called on the fake.
58     wait_called: RefCell<u32>,
59 }
60 
61 #[allow(dead_code)]
62 impl FakeDevice {
new(files: &HashMap<PathBuf, FileMetadata>) -> FakeDevice63     pub fn new(files: &HashMap<PathBuf, FileMetadata>) -> FakeDevice {
64         FakeDevice { files: files.clone(), ..Default::default() }
65     }
66 
67     /// Returns the ordered list of all removed files or dirs.
removes(&self) -> Vec<PathBuf>68     pub fn removes(&self) -> Vec<PathBuf> {
69         self.removes.borrow().clone()
70     }
71 
72     /// Returns orderd list of all pushed files.
73     #[allow(dead_code)]
pushes(&self) -> Vec<PathBuf>74     pub fn pushes(&self) -> Vec<PathBuf> {
75         self.pushes.borrow().clone()
76     }
77 
78     /// Returns orderd list of all raw adb commands.
79     #[allow(dead_code)]
raw_cmds(&self) -> Vec<String>80     pub fn raw_cmds(&self) -> Vec<String> {
81         self.raw_cmds.borrow().clone()
82     }
wait_calls(&self) -> u3283     pub fn wait_calls(&self) -> u32 {
84         *self.wait_called.borrow()
85     }
86 }
87 
88 impl Host for FakeHost {
fingerprint( &self, _partition_root: &Path, partitions: &[PathBuf], ) -> Result<HashMap<PathBuf, FileMetadata>>89     fn fingerprint(
90         &self,
91         _partition_root: &Path,
92         partitions: &[PathBuf],
93     ) -> Result<HashMap<PathBuf, FileMetadata>> {
94         let mut files = self.files.clone();
95         files.retain(|path, _m| Self::on_a_partition(path, partitions));
96         Ok(files)
97     }
98 
tracked_files(&self, _config: &Config) -> Result<Vec<String>>99     fn tracked_files(&self, _config: &Config) -> Result<Vec<String>> {
100         Ok(self.tracked_files.clone())
101     }
102 }
103 
104 impl Device for FakeDevice {
105     // Convert "push" into updating the filesystem, ignore everything else.
run_adb_command(&self, cmd: &AdbCommand) -> Result<String>106     fn run_adb_command(&self, cmd: &AdbCommand) -> Result<String> {
107         match cmd.action {
108             AdbAction::Push { .. } => self.pushes.borrow_mut().push(cmd.file.clone()),
109             AdbAction::DeleteDir { .. } | AdbAction::DeleteFile => {
110                 self.removes.borrow_mut().push(cmd.file.clone())
111             }
112             _ => (),
113         }
114         Ok(String::new())
115     }
run_raw_adb_command(&self, cmds: &[String]) -> Result<String>116     fn run_raw_adb_command(&self, cmds: &[String]) -> Result<String> {
117         self.raw_cmds.borrow_mut().push(cmds.join(" "));
118         Ok(String::new())
119     }
120 
121     // No need to do anything.
reboot(&self) -> Result<String>122     fn reboot(&self) -> Result<String> {
123         Ok(String::new())
124     }
125 
126     // No need to do anything.
soft_restart(&self) -> Result<String>127     fn soft_restart(&self) -> Result<String> {
128         Ok(String::new())
129     }
130 
fingerprint(&self, _partitions: &[String]) -> Result<HashMap<PathBuf, FileMetadata>>131     fn fingerprint(&self, _partitions: &[String]) -> Result<HashMap<PathBuf, FileMetadata>> {
132         // Technically, I should filter the result to ensure it only includes `partitions`
133         Ok(self.files.clone())
134     }
135 
get_installed_apks(&self) -> Result<HashSet<String>>136     fn get_installed_apks(&self) -> Result<HashSet<String>> {
137         Ok(self.installed_apks.clone())
138     }
139 
wait(&self, _profiler: &mut Profiler) -> Result<String>140     fn wait(&self, _profiler: &mut Profiler) -> Result<String> {
141         let mut counter = self.wait_called.borrow_mut();
142         *counter += 1;
143         Ok(String::new())
144     }
prep_after_flash(&self, _profiler: &mut Profiler) -> Result<()>145     fn prep_after_flash(&self, _profiler: &mut Profiler) -> Result<()> {
146         Ok(())
147     }
148 }
149 
150 pub struct FakeMetricSender {}
151 
152 #[allow(dead_code)]
153 impl FakeMetricSender {
new() -> Self154     pub fn new() -> Self {
155         FakeMetricSender {}
156     }
157 }
158 impl MetricSender for FakeMetricSender {
159     // TODO: Capture and test metrics.
add_start_event(&mut self, _command_line: &str, _add_start_event: &str)160     fn add_start_event(&mut self, _command_line: &str, _add_start_event: &str) {}
161 
add_action_event(&mut self, _action: &str, _duration: std::time::Duration)162     fn add_action_event(&mut self, _action: &str, _duration: std::time::Duration) {}
163 
add_action_event_with_files_changed( &mut self, _action: &str, _duration: std::time::Duration, _files_changed: std::vec::Vec<String>, )164     fn add_action_event_with_files_changed(
165         &mut self,
166         _action: &str,
167         _duration: std::time::Duration,
168         _files_changed: std::vec::Vec<String>,
169     ) {
170     }
171 
add_profiler_events(&mut self, _profiler: &adevice::adevice::Profiler)172     fn add_profiler_events(&mut self, _profiler: &adevice::adevice::Profiler) {}
173 
add_exit_event(&mut self, _output: &str, _exit_code: i32)174     fn add_exit_event(&mut self, _output: &str, _exit_code: i32) {}
175 
display_survey(&mut self)176     fn display_survey(&mut self) {}
177 }
178