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) {
}
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.
EXPECT_FALSE(dnd_controller()->IsDndEnabled());
......
......@@ -58,6 +58,9 @@ QuickActionItem* SilencePhoneQuickActionController::CreateItem() {
}
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
: QuickAction::kToggleQuietModeOn);
......@@ -74,13 +77,19 @@ void SilencePhoneQuickActionController::OnButtonPressed(bool is_now_enabled) {
}
void SilencePhoneQuickActionController::OnDndStateChanged() {
state_ =
dnd_controller_->IsDndEnabled() ? ActionState::kOn : ActionState::kOff;
SetItemState(state_);
if (!dnd_controller_->CanRequestNewDndState()) {
state_ = ActionState::kDisabled;
} 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
// the timer.
if (state_ == requested_state_) {
// the timer. Reset also if the state is |kDisabled| since we are not
// requesting a state change.
if (state_ == requested_state_ || state_ == ActionState::kDisabled) {
check_requested_state_timer_.reset();
requested_state_.reset();
}
......@@ -88,21 +97,31 @@ void SilencePhoneQuickActionController::OnDndStateChanged() {
void SilencePhoneQuickActionController::SetItemState(ActionState state) {
bool icon_enabled;
bool button_enabled;
int state_text_id;
int sub_label_text;
switch (state) {
case ActionState::kOff:
icon_enabled = false;
button_enabled = true;
state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_DISABLED_STATE_TOOLTIP;
sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_OFF_STATE;
break;
case ActionState::kOn:
icon_enabled = true;
button_enabled = true;
state_text_id = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_ENABLED_STATE_TOOLTIP;
sub_label_text = IDS_ASH_PHONE_HUB_QUICK_ACTIONS_ON_STATE;
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_->SetSubLabel(l10n_util::GetStringUTF16(sub_label_text));
base::string16 tooltip_state =
......@@ -125,4 +144,9 @@ void SilencePhoneQuickActionController::CheckRequestedState() {
requested_state_.reset();
}
SilencePhoneQuickActionController::ActionState
SilencePhoneQuickActionController::GetItemState() {
return state_;
}
} // namespace ash
......@@ -51,13 +51,18 @@ class ASH_EXPORT SilencePhoneQuickActionController
void OnDndStateChanged() override;
private:
friend class SilencePhoneQuickActionControllerTest;
// All the possible states that the silence phone button can be viewed. Each
// 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.
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
// phone. Make changes to item's state if necessary.
void CheckRequestedState();
......
......@@ -47,6 +47,11 @@ class SilencePhoneQuickActionControllerTest
return dnd_controller_.get();
}
bool IsButtonDisabled() {
return SilencePhoneQuickActionController::ActionState::kDisabled ==
controller_->GetItemState();
}
size_t GetNumObserverCalls() { return num_calls_; }
private:
......@@ -63,16 +68,21 @@ TEST_F(SilencePhoneQuickActionControllerTest, ItemStateChanged) {
// Initially, there's one observer call during initiation.
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.
controller()->OnButtonPressed(false /* is_now_enabled */);
EXPECT_EQ(2u, GetNumObserverCalls());
EXPECT_EQ(3u, GetNumObserverCalls());
// Item state changed to enabled.
EXPECT_TRUE(controller()->IsItemEnabled());
// Press the button to disabled state will trigger observer.
controller()->OnButtonPressed(true /* is_now_enabled */);
EXPECT_EQ(3u, GetNumObserverCalls());
EXPECT_EQ(4u, GetNumObserverCalls());
// Item state changed to disabled.
EXPECT_FALSE(controller()->IsItemEnabled());
......@@ -80,4 +90,31 @@ TEST_F(SilencePhoneQuickActionControllerTest, ItemStateChanged) {
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
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