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