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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! Supporting types for the `simctl status_bar` subcommand.
use super::{Device, Result, Validate};
/// Controls the battery state that is shown in the status bar.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BatteryState {
    /// Indicates that the battery is charging.
    Charging,
    /// Indicates that the battery is fully charged.
    Charged,
    /// Indicates that the battery is discharging (i.e. disconnected from an
    /// external power source).
    Discharging,
}
/// Controls the cellular mode that is shown in the status bar.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum CellularMode {
    /// Indicates that this device does not support cellular connectivity.
    NotSupported,
    /// Indicates that the device is currently searching for a cellular network.
    Searching,
    /// Indicates that the device has failed to find a cellular network.
    Failed,
    /// Indicates that the device is currently connected to a cellular network.
    Active,
}
/// Controls the data network that is shown in the status bar.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum DataNetworkType {
    /// Indicates that the device is connected to a Wi-Fi network.
    Wifi,
    /// Indicates that the device is connected to a 3G cellular network.
    Cell3G,
    /// Indicates that the device is connected to a 4G cellular network.
    Cell4G,
    /// Indicates that the device is connected to a LTE cellular network.
    CellLte,
    /// Indicates that the device is connected to a LTE-Advanced cellular
    /// network.
    CellLteA,
    /// Indicates that the device is connected to a LTE+ cellular network.
    CellLtePlus,
}
/// Controls the Wi-Fi mode that is shown in the status bar.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum WifiMode {
    /// Indicates that the device is searching for a Wi-Fi network.
    Searching,
    /// Indicates that the device failed to find a Wi-Fi network.
    Failed,
    /// Indicates that the device is currently connected to a Wi-Fi network.
    Active,
}
/// Wrapper around the `simctl status_bar` subcommand.
pub struct StatusBar {
    device: Device,
}
impl StatusBar {
    /// Clears any previous override.
    pub fn clear(&self) -> Result<()> {
        self.device
            .simctl()
            .command("status_bar")
            .arg(&self.device.udid)
            .arg("clear")
            .output()?
            .validate()
    }
    /// Creates a new empty override that can be applied to this status bar.
    pub fn empty_override(&self) -> StatusBarOverride {
        StatusBarOverride {
            device: self.device.clone(),
            time: None,
            data_network: None,
            wifi_mode: None,
            wifi_bars: None,
            cellular_mode: None,
            cellular_bars: None,
            operator_name: None,
            battery_state: None,
            battery_level: None,
        }
    }
}
/// Builder that can be used to customize the status bar override before
/// applying it.
pub struct StatusBarOverride {
    device: Device,
    time: Option<String>,
    data_network: Option<DataNetworkType>,
    wifi_mode: Option<WifiMode>,
    wifi_bars: Option<usize>,
    cellular_mode: Option<CellularMode>,
    cellular_bars: Option<usize>,
    operator_name: Option<String>,
    battery_state: Option<BatteryState>,
    battery_level: Option<usize>,
}
impl StatusBarOverride {
    /// Updates the time that is shown in the status bar.
    pub fn time(&mut self, time: &str) -> &mut StatusBarOverride {
        self.time = Some(time.to_owned());
        self
    }
    /// Updates the data network type that is shown in the status bar (e.g. 3G
    /// or 4G).
    pub fn data_network(&mut self, data_network: DataNetworkType) -> &mut StatusBarOverride {
        self.data_network = Some(data_network);
        self
    }
    /// Updates the wifi mode that is shown in the status bar (i.e. whether it's
    /// active or not).
    pub fn wifi_mode(&mut self, wifi_mode: WifiMode) -> &mut StatusBarOverride {
        self.wifi_mode = Some(wifi_mode);
        self
    }
    /// Updates the number of wifi bars that are shown in the status bar. This
    /// is only applicable if the wifi mode is [`WifiMode::Active`].
    pub fn wifi_bars(&mut self, wifi_bars: usize) -> &mut StatusBarOverride {
        self.wifi_bars = Some(wifi_bars);
        self
    }
    /// Updates the cellular mode that is shown in the status bar (i.e. whether
    /// it's active or not).
    pub fn cellular_mode(&mut self, cellular_mode: CellularMode) -> &mut StatusBarOverride {
        self.cellular_mode = Some(cellular_mode);
        self
    }
    /// Updates the number of cellular bars that are shown in the status bar.
    /// This is only applicable if the cellular mode is
    /// [`CellularMode::Active`].
    pub fn cellular_bars(&mut self, cellular_bars: usize) -> &mut StatusBarOverride {
        self.cellular_bars = Some(cellular_bars);
        self
    }
    /// Updates the operator name that is shown in the status bar. This is only
    /// applicable if the cellular mode is [`CellularMode::Active`].
    pub fn operator_name(&mut self, name: &str) -> &mut StatusBarOverride {
        self.operator_name = Some(name.to_owned());
        self
    }
    /// Updates the battery state that is shown in the status bar.
    pub fn battery_state(&mut self, state: BatteryState) -> &mut StatusBarOverride {
        self.battery_state = Some(state);
        self
    }
    /// Updates the battery state that is shown in the status bar. This is only
    /// applicable if the battery state is [`BatteryState::Discharging`].
    pub fn battery_level(&mut self, level: usize) -> &mut StatusBarOverride {
        self.battery_level = Some(level);
        self
    }
    /// Applies this override to the status bar.
    pub fn apply(&self) -> Result<()> {
        let mut command = self.device.simctl().command("status_bar");
        command.arg(&self.device.udid).arg("override");
        if let Some(time) = self.time.as_ref() {
            command.arg("--time").arg(time);
        }
        if let Some(network) = self.data_network.as_ref() {
            command.arg("--dataNetwork").arg(match network {
                DataNetworkType::Wifi => "wifi",
                DataNetworkType::Cell3G => "3g",
                DataNetworkType::Cell4G => "4g",
                DataNetworkType::CellLte => "lte",
                DataNetworkType::CellLteA => "lte-a",
                DataNetworkType::CellLtePlus => "lte+",
            });
        }
        if let Some(mode) = self.wifi_mode.as_ref() {
            command.arg("--wifiMode").arg(match mode {
                WifiMode::Searching => "searching",
                WifiMode::Failed => "failed",
                WifiMode::Active => "active",
            });
        }
        if let Some(bars) = self.wifi_bars.as_ref() {
            command.arg("--wifiBars").arg(bars.to_string());
        }
        if let Some(mode) = self.cellular_mode.as_ref() {
            command.arg("--cellularMode").arg(match mode {
                CellularMode::NotSupported => "notSupported",
                CellularMode::Searching => "searching",
                CellularMode::Failed => "failed",
                CellularMode::Active => "active",
            });
        }
        if let Some(bars) = self.cellular_bars.as_ref() {
            command.arg("--cellularBars").arg(bars.to_string());
        }
        if let Some(name) = self.operator_name.as_ref() {
            command.arg("--operatorName").arg(&name);
        }
        if let Some(state) = self.battery_state.as_ref() {
            command.arg("--batteryState").arg(match state {
                BatteryState::Charging => "charging",
                BatteryState::Charged => "charged",
                BatteryState::Discharging => "discharging",
            });
        }
        if let Some(level) = self.battery_level.as_ref() {
            command.arg("--batteryLevel").arg(level.to_string());
        }
        command.output()?.validate()
    }
}
impl Device {
    /// Returns a wrapper around the `simctl status_bar` subcommand.
    pub fn status_bar(&self) -> StatusBar {
        StatusBar {
            device: self.clone(),
        }
    }
}
#[cfg(test)]
mod tests {
    use serial_test::serial;
    use super::*;
    use crate::mock;
    #[test]
    #[serial]
    fn test_status_bar() -> Result<()> {
        mock::device()?.boot()?;
        mock::device()?
            .status_bar()
            .empty_override()
            .time("00:00")
            .data_network(DataNetworkType::Cell4G)
            .cellular_mode(CellularMode::Active)
            .cellular_bars(3)
            .operator_name("Babel")
            .battery_state(BatteryState::Discharging)
            .battery_level(42)
            .apply()?;
        mock::device()?.status_bar().clear()?;
        mock::device()?.shutdown()?;
        Ok(())
    }
}