Commit e3cb5ba2 authored by Jimmy Gong's avatar Jimmy Gong Committed by Commit Bot

Implement DoNotDisturbController

Fully implements the DoNotDisturbController. This class is responsible
sending and receiving states in regards to the DoNotDisturb feature of
the user's phone.

Bug: 1106937
Test: chromeos_components_unittests
Change-Id: I6a4fa96de5920711a7534ae7eff8bb33b9bb35b7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2439777
Commit-Queue: Jimmy Gong <jimmyxgong@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813420}
parent 3b230450
......@@ -5,11 +5,16 @@
#include "chromeos/components/phonehub/do_not_disturb_controller_impl.h"
#include "chromeos/components/multidevice/logging/logging.h"
#include "chromeos/components/phonehub/message_sender.h"
namespace chromeos {
namespace phonehub {
DoNotDisturbControllerImpl::DoNotDisturbControllerImpl() = default;
DoNotDisturbControllerImpl::DoNotDisturbControllerImpl(
MessageSender* message_sender)
: message_sender_(message_sender) {
DCHECK(message_sender_);
}
DoNotDisturbControllerImpl::~DoNotDisturbControllerImpl() = default;
......@@ -19,11 +24,19 @@ bool DoNotDisturbControllerImpl::IsDndEnabled() const {
void DoNotDisturbControllerImpl::SetDoNotDisturbStateInternal(
bool is_dnd_enabled) {
if (is_dnd_enabled == is_dnd_enabled_)
return;
is_dnd_enabled_ = is_dnd_enabled;
NotifyDndStateChanged();
}
void DoNotDisturbControllerImpl::RequestNewDoNotDisturbState(bool enabled) {
if (enabled == is_dnd_enabled_)
return;
PA_LOG(INFO) << "Attempting to set DND state; new value: " << enabled;
message_sender_->SendUpdateNotificationModeRequest(enabled);
}
} // namespace phonehub
......
......@@ -10,19 +10,25 @@
namespace chromeos {
namespace phonehub {
// TODO(https://crbug.com/1106937): Add real implementation.
class MessageSender;
// Responsible for sending and receiving states in regards to the DoNotDisturb
// feature of the user's remote phone.
class DoNotDisturbControllerImpl : public DoNotDisturbController {
public:
DoNotDisturbControllerImpl();
DoNotDisturbControllerImpl(MessageSender* message_sender);
~DoNotDisturbControllerImpl() override;
private:
friend class DoNotDisturbControllerImplTest;
// DoNotDisturbController:
bool IsDndEnabled() const override;
void SetDoNotDisturbStateInternal(bool is_dnd_enabled) override;
void RequestNewDoNotDisturbState(bool enabled) override;
bool is_dnd_enabled_ = false;
MessageSender* message_sender_;
};
} // namespace phonehub
......
......@@ -6,6 +6,7 @@
#include <memory>
#include "chromeos/components/phonehub/fake_message_sender.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
......@@ -39,7 +40,9 @@ class DoNotDisturbControllerImplTest : public testing::Test {
// testing::Test:
void SetUp() override {
controller_ = std::make_unique<DoNotDisturbControllerImpl>();
fake_message_sender_ = std::make_unique<FakeMessageSender>();
controller_ = std::make_unique<DoNotDisturbControllerImpl>(
fake_message_sender_.get());
controller_->AddObserver(&fake_observer_);
}
......@@ -47,17 +50,65 @@ class DoNotDisturbControllerImplTest : public testing::Test {
bool IsDndEnabled() const { return controller_->IsDndEnabled(); }
void SetDoNotDisturbInternal(bool is_dnd_enabled) {
controller_->SetDoNotDisturbStateInternal(is_dnd_enabled);
}
void RequestNewDoNotDisturbState(bool enabled) {
controller_->RequestNewDoNotDisturbState(enabled);
}
bool GetRecentUpdateNotificationModeRequest() {
return fake_message_sender_->GetRecentUpdateNotificationModeRequest();
}
size_t GetUpdateNotificationModeRequestCallCount() {
return fake_message_sender_->GetUpdateNotificationModeRequestCallCount();
}
size_t GetNumObserverCalls() const { return fake_observer_.num_calls(); }
private:
FakeObserver fake_observer_;
std::unique_ptr<DoNotDisturbController> controller_;
std::unique_ptr<FakeMessageSender> fake_message_sender_;
std::unique_ptr<DoNotDisturbControllerImpl> controller_;
};
// TODO(https://crbug.com/1106937): Remove this test once we have real
// functionality to test.
TEST_F(DoNotDisturbControllerImplTest, Initialize) {
TEST_F(DoNotDisturbControllerImplTest, SetInternalStatesWithObservers) {
EXPECT_FALSE(IsDndEnabled());
SetDoNotDisturbInternal(/*is_dnd_enabled=*/true);
EXPECT_TRUE(IsDndEnabled());
EXPECT_EQ(1u, GetNumObserverCalls());
SetDoNotDisturbInternal(/*is_dnd_enabled=*/false);
EXPECT_FALSE(IsDndEnabled());
EXPECT_EQ(2u, GetNumObserverCalls());
// Setting internal state with the same previous state will not trigger an
// observer event.
SetDoNotDisturbInternal(/*is_dnd_enabled=*/false);
EXPECT_FALSE(IsDndEnabled());
EXPECT_EQ(2u, GetNumObserverCalls());
}
TEST_F(DoNotDisturbControllerImplTest, RequestNewDoNotDisturbState) {
RequestNewDoNotDisturbState(/*enabled=*/true);
EXPECT_TRUE(GetRecentUpdateNotificationModeRequest());
EXPECT_EQ(1u, GetUpdateNotificationModeRequestCallCount());
// Simulate receiving a response and setting the internal value.
SetDoNotDisturbInternal(/*is_dnd_enabled=*/true);
RequestNewDoNotDisturbState(/*enabled=*/false);
EXPECT_FALSE(GetRecentUpdateNotificationModeRequest());
EXPECT_EQ(2u, GetUpdateNotificationModeRequestCallCount());
// Simulate receiving a response and setting the internal value.
SetDoNotDisturbInternal(/*is_dnd_enabled=*/false);
// Requesting for a the same state as the currently set state is a no-op.
RequestNewDoNotDisturbState(/*enabled=*/false);
EXPECT_FALSE(GetRecentUpdateNotificationModeRequest());
EXPECT_EQ(2u, GetUpdateNotificationModeRequestCallCount());
}
} // namespace phonehub
......
......@@ -28,9 +28,7 @@ PhoneHubManagerImpl::PhoneHubManagerImpl(
multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client,
chromeos::secure_channel::SecureChannelClient* secure_channel_client,
const base::RepeatingClosure& show_multidevice_setup_dialog_callback)
: do_not_disturb_controller_(
std::make_unique<DoNotDisturbControllerImpl>()),
connection_manager_(
: connection_manager_(
std::make_unique<ConnectionManagerImpl>(multidevice_setup_client,
device_sync_client,
secure_channel_client)),
......@@ -42,6 +40,8 @@ PhoneHubManagerImpl::PhoneHubManagerImpl(
std::make_unique<MessageReceiverImpl>(connection_manager_.get())),
message_sender_(
std::make_unique<MessageSenderImpl>(connection_manager_.get())),
do_not_disturb_controller_(
std::make_unique<DoNotDisturbControllerImpl>(message_sender_.get())),
connection_scheduler_(std::make_unique<ConnectionSchedulerImpl>(
connection_manager_.get(),
feature_status_provider_.get())),
......@@ -117,11 +117,11 @@ void PhoneHubManagerImpl::Shutdown() {
notification_access_manager_.reset();
find_my_device_controller_.reset();
connection_scheduler_.reset();
do_not_disturb_controller_.reset();
message_sender_.reset();
message_receiver_.reset();
feature_status_provider_.reset();
connection_manager_.reset();
do_not_disturb_controller_.reset();
}
} // namespace phonehub
......
......@@ -61,11 +61,11 @@ class PhoneHubManagerImpl : public PhoneHubManager, public KeyedService {
// KeyedService:
void Shutdown() override;
std::unique_ptr<DoNotDisturbController> do_not_disturb_controller_;
std::unique_ptr<ConnectionManager> connection_manager_;
std::unique_ptr<FeatureStatusProvider> feature_status_provider_;
std::unique_ptr<MessageReceiver> message_receiver_;
std::unique_ptr<MessageSender> message_sender_;
std::unique_ptr<DoNotDisturbController> do_not_disturb_controller_;
std::unique_ptr<ConnectionScheduler> connection_scheduler_;
std::unique_ptr<FindMyDeviceController> find_my_device_controller_;
std::unique_ptr<NotificationAccessManager> notification_access_manager_;
......
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