|
| 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