Skip to content

Commit af76cee

Browse files
Sijie ChenCommit Bot
authored andcommitted
[ffx][setui] Migrate Factory Reset
This CL adds a subcommand, factory reset of ffx_setui. Bug: 76259 Test: fx build ffx fx ffx setui factory_reset -> watch fx ffx setui factory_reset -l false -> set fx test ffx_setui_factory_reset_test -o Change-Id: I54b783590c7daa424bfd0e073963ee9c39bb6264 Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/638129 Reviewed-by: Chip Fukuhara <cfukuhara@google.com> Reviewed-by: William Xiao <wxyz@google.com> Reviewed-by: Paul Faria <paulfaria@google.com> Commit-Queue: Sijie Chen <sijiec@google.com>
1 parent b6b4b1f commit af76cee

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

src/developer/ffx/plugins/setui/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ffx_plugin("ffx_setui") {
99
edition = "2018"
1010

1111
plugin_deps = [
12+
"factory_reset:ffx_setui_factory_reset",
1213
"night_mode:ffx_setui_night_mode",
1314
"privacy:ffx_setui_privacy",
1415
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2022 The Fuchsia Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style license that can be
3+
# found in the LICENSE file.
4+
5+
import("//src/developer/ffx/build/ffx_plugin.gni")
6+
7+
ffx_plugin("ffx_setui_factory_reset") {
8+
version = "0.1.0"
9+
edition = "2018"
10+
with_unit_tests = true
11+
12+
deps = [
13+
"//sdk/fidl/fuchsia.settings:fuchsia.settings-rustc",
14+
"//src/developer/ffx/plugins/setui/utils:utils",
15+
]
16+
17+
test_deps = [ "//third_party/rust_crates:test-case" ]
18+
19+
args_sources = [ "src/args.rs" ]
20+
21+
sources = [ "src/lib.rs" ]
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2022 The Fuchsia Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
use argh::FromArgs;
6+
use ffx_core::ffx_command;
7+
8+
#[ffx_command()]
9+
#[derive(FromArgs, Debug, PartialEq)]
10+
#[argh(subcommand, name = "factory_reset")]
11+
/// get or set factory reset settings
12+
pub struct FactoryReset {
13+
/// when set to 'true', factory reset can be performed on the device
14+
#[argh(option, short = 'l')]
15+
pub is_local_reset_allowed: Option<bool>,
16+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2022 The Fuchsia Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
use anyhow::Result;
6+
use ffx_core::ffx_plugin;
7+
use ffx_setui_factory_reset_args::FactoryReset;
8+
use fidl_fuchsia_settings::{FactoryResetProxy, FactoryResetSettings};
9+
use utils::handle_mixed_result;
10+
use utils::{self, Either, WatchOrSetResult};
11+
12+
#[ffx_plugin(
13+
"setui",
14+
FactoryResetProxy = "core/setui_service:expose:fuchsia.settings.FactoryReset"
15+
)]
16+
pub async fn run_command(
17+
factory_reset_proxy: FactoryResetProxy,
18+
factory_reset: FactoryReset,
19+
) -> Result<()> {
20+
handle_mixed_result(
21+
"FactoryReset",
22+
command(factory_reset_proxy, factory_reset.is_local_reset_allowed).await,
23+
)
24+
.await?;
25+
26+
Ok(())
27+
}
28+
29+
async fn command(
30+
proxy: FactoryResetProxy,
31+
is_local_reset_allowed: Option<bool>,
32+
) -> WatchOrSetResult {
33+
let mut settings = FactoryResetSettings::EMPTY;
34+
settings.is_local_reset_allowed = is_local_reset_allowed;
35+
36+
Ok(if settings != FactoryResetSettings::EMPTY {
37+
Either::Set(if let Err(err) = proxy.set(settings.clone()).await? {
38+
format!("{:?}", err)
39+
} else {
40+
format!("Successfully set factory_reset to {:?}", settings)
41+
})
42+
} else {
43+
Either::Watch(utils::watch_to_stream(proxy, |p| p.watch()))
44+
})
45+
}
46+
47+
#[cfg(test)]
48+
mod test {
49+
use super::*;
50+
use fidl_fuchsia_settings::{FactoryResetRequest, FactoryResetSettings};
51+
use futures::prelude::*;
52+
use test_case::test_case;
53+
54+
#[test_case(
55+
true;
56+
"Test factory reset set() output with is_local_reset_allowed as true."
57+
)]
58+
#[test_case(
59+
false;
60+
"Test factory reset set() output with is_local_reset_allowed as false."
61+
)]
62+
#[fuchsia_async::run_singlethreaded(test)]
63+
async fn validate_factory_reset_set_output(
64+
expected_is_local_reset_allowed: bool,
65+
) -> Result<()> {
66+
let proxy = setup_fake_factory_reset_proxy(move |req| match req {
67+
FactoryResetRequest::Set { settings: _, responder } => {
68+
let _ = responder.send(&mut Ok(()));
69+
}
70+
FactoryResetRequest::Watch { responder: _ } => {
71+
panic!("Unexpected call to watch");
72+
}
73+
});
74+
75+
let output = utils::assert_set!(command(proxy, Some(expected_is_local_reset_allowed)));
76+
assert_eq!(
77+
output,
78+
format!(
79+
"Successfully set factory_reset to {:?}",
80+
FactoryResetSettings {
81+
is_local_reset_allowed: Some(expected_is_local_reset_allowed),
82+
..FactoryResetSettings::EMPTY
83+
}
84+
)
85+
);
86+
Ok(())
87+
}
88+
89+
#[test_case(
90+
None;
91+
"Test factory reset watch() output with is_local_reset_allowed as None."
92+
)]
93+
#[test_case(
94+
Some(false);
95+
"Test factory reset watch() output with is_local_reset_allowed as false."
96+
)]
97+
#[test_case(
98+
Some(true);
99+
"Test factory reset watch() output with is_local_reset_allowed as true."
100+
)]
101+
#[fuchsia_async::run_singlethreaded(test)]
102+
async fn validate_factory_reset_watch_output(
103+
expected_is_local_reset_allowed: Option<bool>,
104+
) -> Result<()> {
105+
let proxy = setup_fake_factory_reset_proxy(move |req| match req {
106+
FactoryResetRequest::Set { settings: _, responder: _ } => {
107+
panic!("Unexpected call to set");
108+
}
109+
FactoryResetRequest::Watch { responder } => {
110+
let _ = responder.send(FactoryResetSettings {
111+
is_local_reset_allowed: expected_is_local_reset_allowed,
112+
..FactoryResetSettings::EMPTY
113+
});
114+
}
115+
});
116+
117+
let output = utils::assert_watch!(command(proxy, None));
118+
assert_eq!(
119+
output,
120+
format!(
121+
"{:#?}",
122+
FactoryResetSettings {
123+
is_local_reset_allowed: expected_is_local_reset_allowed,
124+
..FactoryResetSettings::EMPTY
125+
}
126+
)
127+
);
128+
Ok(())
129+
}
130+
}

0 commit comments

Comments
 (0)