Commit cf92082b authored by Jimmy Gong's avatar Jimmy Gong Committed by Chromium LUCI CQ

Phone Hub: Implement disabling of the Silence Phone button

This disables the Silence Phone quick action in the Phone Hub UI if
the user's phone is setup with a work profile.

Bug: 1155244
Test: local, ash_unittest
Change-Id: Ia29af3f40d69f00af74362015393d32730d2a58f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2575344
Commit-Queue: Jimmy Gong <jimmyxgong@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833991}
parent d1065844
...@@ -98,6 +98,11 @@ TEST_F(QuickActionsViewTest, EnableHotspotToggle) { ...@@ -98,6 +98,11 @@ TEST_F(QuickActionsViewTest, EnableHotspotToggle) {
} }
TEST_F(QuickActionsViewTest, SilencePhoneToggle) { TEST_F(QuickActionsViewTest, SilencePhoneToggle) {
// Allow silence phone to be toggle-able.
dnd_controller()->SetDoNotDisturbStateInternal(
/*is_dnd_enabled=*/false,
/*can_request_new_dnd_state=*/true);
// Initially, silence phone is not enabled. // Initially, silence phone is not enabled.
EXPECT_FALSE(dnd_controller()->IsDndEnabled()); EXPECT_FALSE(dnd_controller()->IsDndEnabled());
......
...@@ -58,6 +58,9 @@ QuickActionItem* SilencePhoneQuickActionController::CreateItem() { ...@@ -58,6 +58,9 @@ QuickActionItem* SilencePhoneQuickActionController::CreateItem() {
} }
void SilencePhoneQuickActionController::OnButtonPressed(bool is_now_enabled) { void SilencePhoneQuickActionController::OnButtonPressed(bool is_now_enabled) {
// Button should not be pressed if it is disabled.
DCHECK(state_ != ActionState::kDisabled);
LogQuickActionClick(is_now_enabled ? QuickAction::kToggleQuietModeOff LogQuickActionClick(is_now_enabled ? QuickAction::kToggleQuietModeOff
: QuickAction::kToggleQuietModeOn); : QuickAction::kToggleQuietModeOn);
...@@ -74,13 +77,19 @@ void SilencePhoneQuickActionController::OnButtonPressed(bool is_now_enabled) { ...@@ -74,13 +77,19 @@ void SilencePhoneQuickActionController::OnButtonPressed(bool is_now_enabled) {
} }
void SilencePhoneQuickActionController::OnDndStateChanged() { void SilencePhoneQuickActionController::OnDndStateChanged() {
state_ = if (!dnd_controller_->CanRequestNewDndState()) {
dnd_controller_->IsDndEnabled() ? ActionState::kOn : ActionState::kOff; state_ = ActionState::kDisabled;
SetItemState(state_); } else if (dnd_controller_->IsDndEnabled()) {
state_ = ActionState::kOn;
} else {
state_ = ActionState::kOff;
}
SetItemState(state_);
// If |requested_state_| correctly resembles the current state, reset it and // If |requested_state_| correctly resembles the current state, reset it and
// the timer. // the timer. Reset also if the state is |kDisabled| since we are not
if (state_ == requested_state_) { // requesting a state change.
if (state_ == requested_state_ || state_ == ActionState::kDisabled) {
check_requested_state_timer_.reset(); check_requested_state_timer_.reset();
requested_state_.reset(); requested_state_.reset();
} }
...@@ -88,21 +97,31 @@ void SilencePhoneQuickActionController::OnDndStateChanged() { ...@@ -88,21 +97,31 @@ void SilencePhoneQuickActionController::OnDndStateChanged() {
void SilencePhoneQuickActionController::SetItemState(ActionState state) { void SilencePhoneQuickActionController::SetItemState(ActionState state) {
bool icon_enabled; bool icon_enabled;
bool button_enabled;
int state_text_id; int state_text_id;
int sub_label_text; int sub_label_text;
switch (state) { switch (state) {
case ActionState::kOff: case ActionState::kOff:
icon_enabled = false; icon_enabled = false;
button_enabled = true;
state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_DISABLED_STATE_TOOLTIP; state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_DISABLED_STATE_TOOLTIP;
sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_OFF_STATE; sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_OFF_STATE;
break; break;
case ActionState::kOn: case ActionState::kOn:
icon_enabled = true; icon_enabled = true;
button_enabled = true;
state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_ENABLED_STATE_TOOLTIP; state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_ENABLED_STATE_TOOLTIP;
sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_ON_STATE; sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_ON_STATE;
break; break;
case ActionState::kDisabled:
// TODO(1155784): Update disabled view with matching toggle-state colors.
icon_enabled = dnd_controller_->IsDndEnabled();
button_enabled = false;
state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_DISABLED_STATE_TOOLTIP;
sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_NOT_AVAILABLE_STATE;
} }
item_->SetEnabled(button_enabled);
item_->SetToggled(icon_enabled); item_->SetToggled(icon_enabled);
item_->SetSubLabel(l10n_util::GetStringUTF16(sub_label_text)); item_->SetSubLabel(l10n_util::GetStringUTF16(sub_label_text));
base::string16 tooltip_state = base::string16 tooltip_state =
...@@ -125,4 +144,9 @@ void SilencePhoneQuickActionController::CheckRequestedState() { ...@@ -125,4 +144,9 @@ void SilencePhoneQuickActionController::CheckRequestedState() {
requested_state_.reset(); requested_state_.reset();
} }
SilencePhoneQuickActionController::ActionState
SilencePhoneQuickActionController::GetItemState() {
return state_;
}
} // namespace ash } // namespace ash
...@@ -51,13 +51,18 @@ class ASH_EXPORT SilencePhoneQuickActionController ...@@ -51,13 +51,18 @@ class ASH_EXPORT SilencePhoneQuickActionController
void OnDndStateChanged() override; void OnDndStateChanged() override;
private: private:
friend class SilencePhoneQuickActionControllerTest;
// All the possible states that the silence phone button can be viewed. Each // All the possible states that the silence phone button can be viewed. Each
// state has a corresponding icon, labels and tooltip view. // state has a corresponding icon, labels and tooltip view.
enum class ActionState { kOff, kOn }; enum class ActionState { kOff, kOn, kDisabled };
// Set the item (including icon, label and tooltips) to a certain state. // Set the item (including icon, label and tooltips) to a certain state.
void SetItemState(ActionState state); void SetItemState(ActionState state);
// Retrieves the current state of the QuickActionItem. Used only for testing.
ActionState GetItemState();
// Check to see if the requested state is similar to current state of the // Check to see if the requested state is similar to current state of the
// phone. Make changes to item's state if necessary. // phone. Make changes to item's state if necessary.
void CheckRequestedState(); void CheckRequestedState();
......
...@@ -47,6 +47,11 @@ class SilencePhoneQuickActionControllerTest ...@@ -47,6 +47,11 @@ class SilencePhoneQuickActionControllerTest
return dnd_controller_.get(); return dnd_controller_.get();
} }
bool IsButtonDisabled() {
return SilencePhoneQuickActionController::ActionState::kDisabled ==
controller_->GetItemState();
}
size_t GetNumObserverCalls() { return num_calls_; } size_t GetNumObserverCalls() { return num_calls_; }
private: private:
...@@ -63,16 +68,21 @@ TEST_F(SilencePhoneQuickActionControllerTest, ItemStateChanged) { ...@@ -63,16 +68,21 @@ TEST_F(SilencePhoneQuickActionControllerTest, ItemStateChanged) {
// Initially, there's one observer call during initiation. // Initially, there's one observer call during initiation.
EXPECT_EQ(1u, GetNumObserverCalls()); EXPECT_EQ(1u, GetNumObserverCalls());
// Allow the button to be enabled.
dnd_controller()->SetDoNotDisturbStateInternal(
/*is_dnd_enabled=*/false, /*can_request_new_dnd_state=*/true);
EXPECT_EQ(2u, GetNumObserverCalls());
// Press the button to enabled state will trigger observer. // Press the button to enabled state will trigger observer.
controller()->OnButtonPressed(false /* is_now_enabled */); controller()->OnButtonPressed(false /* is_now_enabled */);
EXPECT_EQ(2u, GetNumObserverCalls()); EXPECT_EQ(3u, GetNumObserverCalls());
// Item state changed to enabled. // Item state changed to enabled.
EXPECT_TRUE(controller()->IsItemEnabled()); EXPECT_TRUE(controller()->IsItemEnabled());
// Press the button to disabled state will trigger observer. // Press the button to disabled state will trigger observer.
controller()->OnButtonPressed(true /* is_now_enabled */); controller()->OnButtonPressed(true /* is_now_enabled */);
EXPECT_EQ(3u, GetNumObserverCalls()); EXPECT_EQ(4u, GetNumObserverCalls());
// Item state changed to disabled. // Item state changed to disabled.
EXPECT_FALSE(controller()->IsItemEnabled()); EXPECT_FALSE(controller()->IsItemEnabled());
...@@ -80,4 +90,31 @@ TEST_F(SilencePhoneQuickActionControllerTest, ItemStateChanged) { ...@@ -80,4 +90,31 @@ TEST_F(SilencePhoneQuickActionControllerTest, ItemStateChanged) {
dnd_controller()->SetShouldRequestFail(false); dnd_controller()->SetShouldRequestFail(false);
} }
TEST_F(SilencePhoneQuickActionControllerTest, CanRequestNewDndState) {
// Set DoNotDisturbState to not allow any new request.
dnd_controller()->SetDoNotDisturbStateInternal(
/*is_dnd_enabled=*/false, /*can_request_new_dnd_state=*/false);
EXPECT_EQ(1u, GetNumObserverCalls());
// Since no new state requests are allowed, expect the button to be disabled.
EXPECT_FALSE(controller()->IsItemEnabled());
EXPECT_TRUE(IsButtonDisabled());
// Simulate that the phone updated its DoNotDisturb state, but is still in a
// work profile.
dnd_controller()->SetDoNotDisturbStateInternal(
/*is_dnd_enabled=*/true, /*can_request_new_dnd_state=*/false);
EXPECT_EQ(2u, GetNumObserverCalls());
// The button should still be disabled despite the phone has DoNotDisturb mode
// enabled. However, the underlying toggle has been flipped to enabled.
EXPECT_TRUE(controller()->IsItemEnabled());
EXPECT_TRUE(IsButtonDisabled());
// Flip toggle state back to enabled, but still in a work profile.
dnd_controller()->SetDoNotDisturbStateInternal(
/*is_dnd_enabled=*/false, /*can_request_new_dnd_state=*/false);
EXPECT_EQ(3u, GetNumObserverCalls());
EXPECT_FALSE(controller()->IsItemEnabled());
EXPECT_TRUE(IsButtonDisabled());
}
} // namespace ash } // namespace ash
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment