phone_status_processor_unittest.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // Copyright 2020 The Chromium 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. #include "ash/components/phonehub/phone_status_processor.h"
  5. #include <google/protobuf/repeated_field.h>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "ash/components/multidevice/remote_device_test_util.h"
  10. #include "ash/components/phonehub/fake_do_not_disturb_controller.h"
  11. #include "ash/components/phonehub/fake_feature_status_provider.h"
  12. #include "ash/components/phonehub/fake_find_my_device_controller.h"
  13. #include "ash/components/phonehub/fake_message_receiver.h"
  14. #include "ash/components/phonehub/fake_multidevice_feature_access_manager.h"
  15. #include "ash/components/phonehub/fake_notification_manager.h"
  16. #include "ash/components/phonehub/fake_recent_apps_interaction_handler.h"
  17. #include "ash/components/phonehub/fake_screen_lock_manager.h"
  18. #include "ash/components/phonehub/mutable_phone_model.h"
  19. #include "ash/components/phonehub/notification_manager.h"
  20. #include "ash/components/phonehub/notification_processor.h"
  21. #include "ash/components/phonehub/phone_model_test_util.h"
  22. #include "ash/components/phonehub/phone_status_model.h"
  23. #include "ash/components/phonehub/proto/phonehub_api.pb.h"
  24. #include "ash/constants/ash_features.h"
  25. #include "ash/services/multidevice_setup/public/cpp/fake_multidevice_setup_client.h"
  26. #include "base/strings/utf_string_conversions.h"
  27. #include "base/test/scoped_feature_list.h"
  28. #include "base/test/task_environment.h"
  29. #include "testing/gtest/include/gtest/gtest.h"
  30. #include "ui/gfx/image/image.h"
  31. namespace ash {
  32. namespace phonehub {
  33. using multidevice_setup::mojom::Feature;
  34. using multidevice_setup::mojom::FeatureState;
  35. using multidevice_setup::mojom::HostStatus;
  36. // A fake processor that immediately adds or removes notifications.
  37. class FakeNotificationProcessor : public NotificationProcessor {
  38. public:
  39. FakeNotificationProcessor(NotificationManager* notification_manager)
  40. : NotificationProcessor(notification_manager) {}
  41. void AddNotifications(
  42. const std::vector<proto::Notification>& notification_protos) override {
  43. base::flat_set<Notification> notifications;
  44. for (const auto& proto : notification_protos) {
  45. notifications.emplace(Notification(
  46. proto.id(), CreateFakeAppMetadata(), base::Time(),
  47. Notification::Importance::kDefault,
  48. Notification::Category::kConversation,
  49. {{Notification::ActionType::kInlineReply, /*action_id=*/0}},
  50. Notification::InteractionBehavior::kNone, absl::nullopt,
  51. absl::nullopt, absl::nullopt, absl::nullopt));
  52. }
  53. notification_manager_->SetNotificationsInternal(notifications);
  54. }
  55. void RemoveNotifications(
  56. const base::flat_set<int64_t>& notification_ids) override {
  57. notification_manager_->RemoveNotificationsInternal(notification_ids);
  58. }
  59. };
  60. class PhoneStatusProcessorTest : public testing::Test {
  61. protected:
  62. PhoneStatusProcessorTest()
  63. : test_remote_device_(multidevice::CreateRemoteDeviceRefForTest()) {}
  64. PhoneStatusProcessorTest(const PhoneStatusProcessorTest&) = delete;
  65. PhoneStatusProcessorTest& operator=(const PhoneStatusProcessorTest&) = delete;
  66. ~PhoneStatusProcessorTest() override = default;
  67. void SetUp() override {
  68. fake_do_not_disturb_controller_ =
  69. std::make_unique<FakeDoNotDisturbController>();
  70. fake_feature_status_provider_ =
  71. std::make_unique<FakeFeatureStatusProvider>(FeatureStatus::kDisabled);
  72. fake_message_receiver_ = std::make_unique<FakeMessageReceiver>();
  73. fake_find_my_device_controller_ =
  74. std::make_unique<FakeFindMyDeviceController>();
  75. fake_multidevice_feature_access_manager_ =
  76. std::make_unique<FakeMultideviceFeatureAccessManager>();
  77. fake_screen_lock_manager_ = std::make_unique<FakeScreenLockManager>();
  78. fake_notification_manager_ = std::make_unique<FakeNotificationManager>();
  79. fake_notification_processor_ = std::make_unique<FakeNotificationProcessor>(
  80. fake_notification_manager_.get());
  81. mutable_phone_model_ = std::make_unique<MutablePhoneModel>();
  82. fake_multidevice_setup_client_ =
  83. std::make_unique<multidevice_setup::FakeMultiDeviceSetupClient>();
  84. fake_recent_apps_interaction_handler_ =
  85. std::make_unique<FakeRecentAppsInteractionHandler>();
  86. scoped_feature_list_.InitWithFeatures(
  87. /*enabled_features=*/{features::kEcheSWA,
  88. features::kPhoneHubCameraRoll},
  89. /*disabled_features=*/{});
  90. }
  91. void CreatePhoneStatusProcessor() {
  92. phone_status_processor_ = std::make_unique<PhoneStatusProcessor>(
  93. fake_do_not_disturb_controller_.get(),
  94. fake_feature_status_provider_.get(), fake_message_receiver_.get(),
  95. fake_find_my_device_controller_.get(),
  96. fake_multidevice_feature_access_manager_.get(),
  97. fake_screen_lock_manager_.get(), fake_notification_processor_.get(),
  98. fake_multidevice_setup_client_.get(), mutable_phone_model_.get(),
  99. fake_recent_apps_interaction_handler_.get());
  100. }
  101. void InitializeNotificationProto(proto::Notification* notification,
  102. int64_t id) {
  103. auto origin_app = std::make_unique<proto::App>();
  104. origin_app->set_package_name("package");
  105. origin_app->set_visible_name("visible");
  106. origin_app->set_icon("321");
  107. notification->add_actions();
  108. proto::Action* mutable_action = notification->mutable_actions(0);
  109. mutable_action->set_id(0u);
  110. mutable_action->set_title("action title");
  111. mutable_action->set_type(proto::Action_InputType::Action_InputType_TEXT);
  112. notification->set_id(id);
  113. notification->set_epoch_time_millis(1u);
  114. notification->set_allocated_origin_app(origin_app.release());
  115. notification->set_title("title");
  116. notification->set_importance(proto::NotificationImportance::HIGH);
  117. notification->set_text_content("content");
  118. notification->set_contact_image("123");
  119. notification->set_shared_image("123");
  120. }
  121. base::test::ScopedFeatureList scoped_feature_list_;
  122. multidevice::RemoteDeviceRef test_remote_device_;
  123. std::unique_ptr<FakeDoNotDisturbController> fake_do_not_disturb_controller_;
  124. std::unique_ptr<FakeFeatureStatusProvider> fake_feature_status_provider_;
  125. std::unique_ptr<FakeMessageReceiver> fake_message_receiver_;
  126. std::unique_ptr<FakeFindMyDeviceController> fake_find_my_device_controller_;
  127. std::unique_ptr<FakeMultideviceFeatureAccessManager>
  128. fake_multidevice_feature_access_manager_;
  129. std::unique_ptr<FakeScreenLockManager> fake_screen_lock_manager_;
  130. std::unique_ptr<FakeNotificationManager> fake_notification_manager_;
  131. std::unique_ptr<FakeNotificationProcessor> fake_notification_processor_;
  132. std::unique_ptr<MutablePhoneModel> mutable_phone_model_;
  133. std::unique_ptr<multidevice_setup::FakeMultiDeviceSetupClient>
  134. fake_multidevice_setup_client_;
  135. std::unique_ptr<FakeRecentAppsInteractionHandler>
  136. fake_recent_apps_interaction_handler_;
  137. std::unique_ptr<PhoneStatusProcessor> phone_status_processor_;
  138. };
  139. TEST_F(PhoneStatusProcessorTest, PhoneStatusSnapshotUpdate_EcheDisabled) {
  140. scoped_feature_list_.Reset();
  141. scoped_feature_list_.InitWithFeatures(
  142. /*enabled_features=*/{},
  143. /*disabled_features=*/{features::kEcheSWA,
  144. features::kPhoneHubCameraRoll});
  145. fake_multidevice_setup_client_->SetHostStatusWithDevice(
  146. std::make_pair(HostStatus::kHostVerified, test_remote_device_));
  147. CreatePhoneStatusProcessor();
  148. auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
  149. expected_phone_properties->set_notification_mode(
  150. proto::NotificationMode::DO_NOT_DISTURB_ON);
  151. expected_phone_properties->set_profile_type(
  152. proto::ProfileType::DEFAULT_PROFILE);
  153. expected_phone_properties->set_notification_access_state(
  154. proto::NotificationAccessState::ACCESS_NOT_GRANTED);
  155. expected_phone_properties->set_ring_status(
  156. proto::FindMyDeviceRingStatus::RINGING);
  157. expected_phone_properties->set_battery_percentage(24u);
  158. expected_phone_properties->set_charging_state(
  159. proto::ChargingState::CHARGING_AC);
  160. expected_phone_properties->set_signal_strength(
  161. proto::SignalStrength::FOUR_BARS);
  162. expected_phone_properties->set_mobile_provider("google");
  163. expected_phone_properties->set_connection_state(
  164. proto::MobileConnectionState::SIM_WITH_RECEPTION);
  165. expected_phone_properties->set_screen_lock_state(
  166. proto::ScreenLockState::SCREEN_LOCK_UNKNOWN);
  167. proto::CameraRollAccessState* access_state =
  168. expected_phone_properties->mutable_camera_roll_access_state();
  169. access_state->set_feature_enabled(true);
  170. proto::FeatureSetupConfig* feature_setup_config =
  171. expected_phone_properties->mutable_feature_setup_config();
  172. feature_setup_config->set_feature_setup_request_supported(true);
  173. expected_phone_properties->add_user_states();
  174. proto::UserState* mutable_user_state =
  175. expected_phone_properties->mutable_user_states(0);
  176. mutable_user_state->set_user_id(1u);
  177. mutable_user_state->set_is_quiet_mode_enabled(false);
  178. proto::PhoneStatusSnapshot expected_snapshot;
  179. expected_snapshot.set_allocated_properties(
  180. expected_phone_properties.release());
  181. expected_snapshot.add_notifications();
  182. InitializeNotificationProto(expected_snapshot.mutable_notifications(0),
  183. /*id=*/0u);
  184. auto* app = expected_snapshot.mutable_streamable_apps()->add_apps();
  185. app->set_package_name("pkg1");
  186. app->set_visible_name("vis");
  187. // Simulate feature set to enabled and connected.
  188. fake_feature_status_provider_->SetStatus(FeatureStatus::kEnabledAndConnected);
  189. fake_multidevice_setup_client_->SetFeatureState(
  190. Feature::kPhoneHubNotifications, FeatureState::kEnabledByUser);
  191. // Simulate receiving a proto message.
  192. fake_message_receiver_->NotifyPhoneStatusSnapshotReceived(expected_snapshot);
  193. EXPECT_EQ(1u, fake_notification_manager_->num_notifications());
  194. EXPECT_EQ(base::UTF8ToUTF16(test_remote_device_.name()),
  195. *mutable_phone_model_->phone_name());
  196. EXPECT_TRUE(fake_do_not_disturb_controller_->IsDndEnabled());
  197. EXPECT_TRUE(fake_do_not_disturb_controller_->CanRequestNewDndState());
  198. EXPECT_EQ(FindMyDeviceController::Status::kRingingOn,
  199. fake_find_my_device_controller_->GetPhoneRingingStatus());
  200. EXPECT_EQ(
  201. MultideviceFeatureAccessManager::AccessStatus::kAvailableButNotGranted,
  202. fake_multidevice_feature_access_manager_->GetNotificationAccessStatus());
  203. EXPECT_EQ(
  204. MultideviceFeatureAccessManager::AccessStatus::kAvailableButNotGranted,
  205. fake_multidevice_feature_access_manager_->GetCameraRollAccessStatus());
  206. EXPECT_TRUE(fake_multidevice_feature_access_manager_
  207. ->GetFeatureSetupRequestSupported());
  208. EXPECT_EQ(ScreenLockManager::LockStatus::kUnknown,
  209. fake_screen_lock_manager_->GetLockStatus());
  210. absl::optional<PhoneStatusModel> phone_status_model =
  211. mutable_phone_model_->phone_status_model();
  212. EXPECT_EQ(PhoneStatusModel::ChargingState::kChargingAc,
  213. phone_status_model->charging_state());
  214. EXPECT_EQ(24u, phone_status_model->battery_percentage());
  215. EXPECT_EQ(u"google",
  216. phone_status_model->mobile_connection_metadata()->mobile_provider);
  217. EXPECT_EQ(PhoneStatusModel::SignalStrength::kFourBars,
  218. phone_status_model->mobile_connection_metadata()->signal_strength);
  219. EXPECT_EQ(PhoneStatusModel::MobileStatus::kSimWithReception,
  220. phone_status_model->mobile_status());
  221. // Change feature status to disconnected.
  222. fake_feature_status_provider_->SetStatus(
  223. FeatureStatus::kEnabledButDisconnected);
  224. EXPECT_EQ(0u, fake_notification_manager_->num_notifications());
  225. EXPECT_EQ(base::UTF8ToUTF16(test_remote_device_.name()),
  226. *mutable_phone_model_->phone_name());
  227. EXPECT_FALSE(mutable_phone_model_->phone_status_model().has_value());
  228. std::vector<RecentAppsInteractionHandler::UserState> user_states =
  229. fake_recent_apps_interaction_handler_->user_states();
  230. EXPECT_TRUE(user_states.empty());
  231. }
  232. TEST_F(PhoneStatusProcessorTest, PhoneStatusSnapshotUpdate) {
  233. fake_multidevice_setup_client_->SetHostStatusWithDevice(
  234. std::make_pair(HostStatus::kHostVerified, test_remote_device_));
  235. CreatePhoneStatusProcessor();
  236. auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
  237. expected_phone_properties->set_notification_mode(
  238. proto::NotificationMode::DO_NOT_DISTURB_ON);
  239. expected_phone_properties->set_profile_type(
  240. proto::ProfileType::DEFAULT_PROFILE);
  241. expected_phone_properties->set_notification_access_state(
  242. proto::NotificationAccessState::ACCESS_NOT_GRANTED);
  243. expected_phone_properties->set_ring_status(
  244. proto::FindMyDeviceRingStatus::RINGING);
  245. expected_phone_properties->set_battery_percentage(24u);
  246. expected_phone_properties->set_charging_state(
  247. proto::ChargingState::CHARGING_AC);
  248. expected_phone_properties->set_signal_strength(
  249. proto::SignalStrength::FOUR_BARS);
  250. expected_phone_properties->set_mobile_provider("google");
  251. expected_phone_properties->set_connection_state(
  252. proto::MobileConnectionState::SIM_WITH_RECEPTION);
  253. expected_phone_properties->set_screen_lock_state(
  254. proto::ScreenLockState::SCREEN_LOCK_UNKNOWN);
  255. proto::CameraRollAccessState* access_state =
  256. expected_phone_properties->mutable_camera_roll_access_state();
  257. access_state->set_feature_enabled(true);
  258. proto::FeatureSetupConfig* feature_setup_config =
  259. expected_phone_properties->mutable_feature_setup_config();
  260. feature_setup_config->set_feature_setup_request_supported(true);
  261. expected_phone_properties->add_user_states();
  262. proto::UserState* mutable_user_state =
  263. expected_phone_properties->mutable_user_states(0);
  264. mutable_user_state->set_user_id(1u);
  265. mutable_user_state->set_is_quiet_mode_enabled(false);
  266. proto::PhoneStatusSnapshot expected_snapshot;
  267. expected_snapshot.set_allocated_properties(
  268. expected_phone_properties.release());
  269. expected_snapshot.add_notifications();
  270. InitializeNotificationProto(expected_snapshot.mutable_notifications(0),
  271. /*id=*/0u);
  272. auto* app = expected_snapshot.mutable_streamable_apps()->add_apps();
  273. app->set_package_name("pkg1");
  274. app->set_visible_name("vis");
  275. // Simulate feature set to enabled and connected.
  276. fake_feature_status_provider_->SetStatus(FeatureStatus::kEnabledAndConnected);
  277. fake_multidevice_setup_client_->SetFeatureState(
  278. Feature::kPhoneHubNotifications, FeatureState::kEnabledByUser);
  279. // Simulate receiving a proto message.
  280. fake_message_receiver_->NotifyPhoneStatusSnapshotReceived(expected_snapshot);
  281. EXPECT_EQ(1u, fake_notification_manager_->num_notifications());
  282. EXPECT_EQ(base::UTF8ToUTF16(test_remote_device_.name()),
  283. *mutable_phone_model_->phone_name());
  284. EXPECT_TRUE(fake_do_not_disturb_controller_->IsDndEnabled());
  285. EXPECT_TRUE(fake_do_not_disturb_controller_->CanRequestNewDndState());
  286. EXPECT_EQ(FindMyDeviceController::Status::kRingingOn,
  287. fake_find_my_device_controller_->GetPhoneRingingStatus());
  288. EXPECT_EQ(
  289. MultideviceFeatureAccessManager::AccessStatus::kAvailableButNotGranted,
  290. fake_multidevice_feature_access_manager_->GetNotificationAccessStatus());
  291. EXPECT_EQ(
  292. MultideviceFeatureAccessManager::AccessStatus::kAccessGranted,
  293. fake_multidevice_feature_access_manager_->GetCameraRollAccessStatus());
  294. EXPECT_TRUE(fake_multidevice_feature_access_manager_
  295. ->GetFeatureSetupRequestSupported());
  296. EXPECT_EQ(ScreenLockManager::LockStatus::kUnknown,
  297. fake_screen_lock_manager_->GetLockStatus());
  298. absl::optional<PhoneStatusModel> phone_status_model =
  299. mutable_phone_model_->phone_status_model();
  300. EXPECT_EQ(PhoneStatusModel::ChargingState::kChargingAc,
  301. phone_status_model->charging_state());
  302. EXPECT_EQ(24u, phone_status_model->battery_percentage());
  303. EXPECT_EQ(u"google",
  304. phone_status_model->mobile_connection_metadata()->mobile_provider);
  305. EXPECT_EQ(PhoneStatusModel::SignalStrength::kFourBars,
  306. phone_status_model->mobile_connection_metadata()->signal_strength);
  307. EXPECT_EQ(PhoneStatusModel::MobileStatus::kSimWithReception,
  308. phone_status_model->mobile_status());
  309. // Change feature status to disconnected.
  310. fake_feature_status_provider_->SetStatus(
  311. FeatureStatus::kEnabledButDisconnected);
  312. EXPECT_EQ(0u, fake_notification_manager_->num_notifications());
  313. EXPECT_EQ(base::UTF8ToUTF16(test_remote_device_.name()),
  314. *mutable_phone_model_->phone_name());
  315. EXPECT_FALSE(mutable_phone_model_->phone_status_model().has_value());
  316. std::vector<RecentAppsInteractionHandler::UserState> user_states =
  317. fake_recent_apps_interaction_handler_->user_states();
  318. EXPECT_EQ(1u, user_states[0].user_id);
  319. EXPECT_EQ(true, user_states[0].is_enabled);
  320. }
  321. TEST_F(PhoneStatusProcessorTest, PhoneStatusUpdate) {
  322. fake_multidevice_setup_client_->SetHostStatusWithDevice(
  323. std::make_pair(HostStatus::kHostVerified, test_remote_device_));
  324. CreatePhoneStatusProcessor();
  325. auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
  326. expected_phone_properties->set_notification_mode(
  327. proto::NotificationMode::DO_NOT_DISTURB_ON);
  328. expected_phone_properties->set_profile_type(proto::ProfileType::WORK_PROFILE);
  329. expected_phone_properties->set_find_my_device_capability(
  330. proto::FindMyDeviceCapability::NOT_ALLOWED);
  331. expected_phone_properties->set_notification_access_state(
  332. proto::NotificationAccessState::ACCESS_GRANTED);
  333. expected_phone_properties->set_ring_status(
  334. proto::FindMyDeviceRingStatus::NOT_RINGING);
  335. expected_phone_properties->set_battery_percentage(24u);
  336. expected_phone_properties->set_charging_state(
  337. proto::ChargingState::CHARGING_AC);
  338. expected_phone_properties->set_signal_strength(
  339. proto::SignalStrength::FOUR_BARS);
  340. expected_phone_properties->set_mobile_provider("google");
  341. expected_phone_properties->set_connection_state(
  342. proto::MobileConnectionState::SIM_WITH_RECEPTION);
  343. expected_phone_properties->set_screen_lock_state(
  344. proto::ScreenLockState::SCREEN_LOCK_OFF);
  345. proto::CameraRollAccessState* access_state =
  346. expected_phone_properties->mutable_camera_roll_access_state();
  347. access_state->set_feature_enabled(false);
  348. proto::FeatureSetupConfig* feature_setup_config =
  349. expected_phone_properties->mutable_feature_setup_config();
  350. feature_setup_config->set_feature_setup_request_supported(false);
  351. expected_phone_properties->add_user_states();
  352. proto::UserState* mutable_user_state =
  353. expected_phone_properties->mutable_user_states(0);
  354. mutable_user_state->set_user_id(1u);
  355. mutable_user_state->set_is_quiet_mode_enabled(false);
  356. proto::PhoneStatusUpdate expected_update;
  357. expected_update.set_allocated_properties(expected_phone_properties.release());
  358. expected_update.add_updated_notifications();
  359. InitializeNotificationProto(expected_update.mutable_updated_notifications(0),
  360. /*id=*/0u);
  361. // Simulate feature set to enabled and connected.
  362. fake_feature_status_provider_->SetStatus(FeatureStatus::kEnabledAndConnected);
  363. fake_multidevice_setup_client_->SetFeatureState(
  364. Feature::kPhoneHubNotifications, FeatureState::kEnabledByUser);
  365. // Simulate receiving a proto message.
  366. fake_message_receiver_->NotifyPhoneStatusUpdateReceived(expected_update);
  367. EXPECT_EQ(1u, fake_notification_manager_->num_notifications());
  368. EXPECT_EQ(base::UTF8ToUTF16(test_remote_device_.name()),
  369. *mutable_phone_model_->phone_name());
  370. EXPECT_TRUE(fake_do_not_disturb_controller_->IsDndEnabled());
  371. EXPECT_FALSE(fake_do_not_disturb_controller_->CanRequestNewDndState());
  372. EXPECT_EQ(FindMyDeviceController::Status::kRingingNotAvailable,
  373. fake_find_my_device_controller_->GetPhoneRingingStatus());
  374. EXPECT_EQ(
  375. MultideviceFeatureAccessManager::AccessStatus::kProhibited,
  376. fake_multidevice_feature_access_manager_->GetNotificationAccessStatus());
  377. EXPECT_EQ(
  378. MultideviceFeatureAccessManager::AccessStatus::kAvailableButNotGranted,
  379. fake_multidevice_feature_access_manager_->GetCameraRollAccessStatus());
  380. EXPECT_FALSE(fake_multidevice_feature_access_manager_
  381. ->GetFeatureSetupRequestSupported());
  382. EXPECT_EQ(ScreenLockManager::LockStatus::kLockedOff,
  383. fake_screen_lock_manager_->GetLockStatus());
  384. absl::optional<PhoneStatusModel> phone_status_model =
  385. mutable_phone_model_->phone_status_model();
  386. EXPECT_EQ(PhoneStatusModel::ChargingState::kChargingAc,
  387. phone_status_model->charging_state());
  388. EXPECT_EQ(24u, phone_status_model->battery_percentage());
  389. EXPECT_EQ(u"google",
  390. phone_status_model->mobile_connection_metadata()->mobile_provider);
  391. EXPECT_EQ(PhoneStatusModel::SignalStrength::kFourBars,
  392. phone_status_model->mobile_connection_metadata()->signal_strength);
  393. EXPECT_EQ(PhoneStatusModel::MobileStatus::kSimWithReception,
  394. phone_status_model->mobile_status());
  395. std::vector<RecentAppsInteractionHandler::UserState> user_states =
  396. fake_recent_apps_interaction_handler_->user_states();
  397. EXPECT_EQ(1u, user_states[0].user_id);
  398. EXPECT_EQ(true, user_states[0].is_enabled);
  399. // Update with one removed notification and a default profile.
  400. expected_update.add_removed_notification_ids(0u);
  401. expected_update.mutable_properties()->set_profile_type(
  402. proto::ProfileType::DEFAULT_PROFILE);
  403. expected_update.mutable_properties()->set_find_my_device_capability(
  404. proto::FindMyDeviceCapability::NORMAL);
  405. expected_update.mutable_properties()->set_ring_status(
  406. proto::FindMyDeviceRingStatus::RINGING);
  407. expected_update.mutable_properties()->set_screen_lock_state(
  408. proto::ScreenLockState::SCREEN_LOCK_ON);
  409. fake_message_receiver_->NotifyPhoneStatusUpdateReceived(expected_update);
  410. EXPECT_EQ(0u, fake_notification_manager_->num_notifications());
  411. EXPECT_EQ(base::UTF8ToUTF16(test_remote_device_.name()),
  412. *mutable_phone_model_->phone_name());
  413. EXPECT_TRUE(fake_do_not_disturb_controller_->IsDndEnabled());
  414. EXPECT_TRUE(fake_do_not_disturb_controller_->CanRequestNewDndState());
  415. EXPECT_EQ(FindMyDeviceController::Status::kRingingOn,
  416. fake_find_my_device_controller_->GetPhoneRingingStatus());
  417. EXPECT_EQ(
  418. MultideviceFeatureAccessManager::AccessStatus::kAccessGranted,
  419. fake_multidevice_feature_access_manager_->GetNotificationAccessStatus());
  420. EXPECT_EQ(
  421. MultideviceFeatureAccessManager::AccessStatus::kAvailableButNotGranted,
  422. fake_multidevice_feature_access_manager_->GetCameraRollAccessStatus());
  423. EXPECT_EQ(ScreenLockManager::LockStatus::kLockedOn,
  424. fake_screen_lock_manager_->GetLockStatus());
  425. phone_status_model = mutable_phone_model_->phone_status_model();
  426. EXPECT_EQ(PhoneStatusModel::ChargingState::kChargingAc,
  427. phone_status_model->charging_state());
  428. EXPECT_EQ(24u, phone_status_model->battery_percentage());
  429. EXPECT_EQ(u"google",
  430. phone_status_model->mobile_connection_metadata()->mobile_provider);
  431. EXPECT_EQ(PhoneStatusModel::SignalStrength::kFourBars,
  432. phone_status_model->mobile_connection_metadata()->signal_strength);
  433. EXPECT_EQ(PhoneStatusModel::MobileStatus::kSimWithReception,
  434. phone_status_model->mobile_status());
  435. // Change feature status to disconnected.
  436. fake_feature_status_provider_->SetStatus(
  437. FeatureStatus::kEnabledButDisconnected);
  438. EXPECT_EQ(0u, fake_notification_manager_->num_notifications());
  439. EXPECT_EQ(base::UTF8ToUTF16(test_remote_device_.name()),
  440. *mutable_phone_model_->phone_name());
  441. EXPECT_FALSE(mutable_phone_model_->phone_status_model().has_value());
  442. }
  443. TEST_F(PhoneStatusProcessorTest, PhoneNotificationAccessProhibitedReason) {
  444. fake_multidevice_setup_client_->SetHostStatusWithDevice(
  445. std::make_pair(HostStatus::kHostVerified, test_remote_device_));
  446. CreatePhoneStatusProcessor();
  447. auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
  448. expected_phone_properties->set_profile_type(proto::ProfileType::WORK_PROFILE);
  449. proto::PhoneStatusUpdate expected_update;
  450. expected_update.set_allocated_properties(expected_phone_properties.release());
  451. fake_message_receiver_->NotifyPhoneStatusUpdateReceived(expected_update);
  452. fake_feature_status_provider_->SetStatus(FeatureStatus::kEnabledAndConnected);
  453. fake_multidevice_setup_client_->SetFeatureState(
  454. Feature::kPhoneHubNotifications, FeatureState::kDisabledByUser);
  455. EXPECT_FALSE(fake_do_not_disturb_controller_->CanRequestNewDndState());
  456. EXPECT_EQ(
  457. MultideviceFeatureAccessManager::AccessStatus::kProhibited,
  458. fake_multidevice_feature_access_manager_->GetNotificationAccessStatus());
  459. EXPECT_EQ(
  460. MultideviceFeatureAccessManager::AccessProhibitedReason::kWorkProfile,
  461. fake_multidevice_feature_access_manager_
  462. ->GetNotificationAccessProhibitedReason());
  463. // Verify that adding a reason properly gets processed even when the current
  464. // profile type does not change.
  465. expected_update.mutable_properties()->set_profile_disable_reason(
  466. proto::ProfileDisableReason::DISABLE_REASON_DISABLED_BY_POLICY);
  467. fake_message_receiver_->NotifyPhoneStatusUpdateReceived(expected_update);
  468. EXPECT_FALSE(fake_do_not_disturb_controller_->CanRequestNewDndState());
  469. EXPECT_EQ(
  470. MultideviceFeatureAccessManager::AccessStatus::kProhibited,
  471. fake_multidevice_feature_access_manager_->GetNotificationAccessStatus());
  472. EXPECT_EQ(
  473. MultideviceFeatureAccessManager::AccessProhibitedReason::kWorkProfile,
  474. fake_multidevice_feature_access_manager_
  475. ->GetNotificationAccessProhibitedReason());
  476. }
  477. TEST_F(PhoneStatusProcessorTest, PhoneName) {
  478. fake_multidevice_setup_client_->SetHostStatusWithDevice(
  479. std::make_pair(HostStatus::kHostVerified, absl::nullopt));
  480. CreatePhoneStatusProcessor();
  481. auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
  482. proto::PhoneStatusUpdate expected_update;
  483. expected_update.set_allocated_properties(expected_phone_properties.release());
  484. // Simulate feature set to enabled and connected.
  485. fake_feature_status_provider_->SetStatus(FeatureStatus::kEnabledAndConnected);
  486. // Simulate receiving a proto message.
  487. fake_message_receiver_->NotifyPhoneStatusUpdateReceived(expected_update);
  488. EXPECT_EQ(0u, fake_notification_manager_->num_notifications());
  489. EXPECT_EQ(absl::nullopt, mutable_phone_model_->phone_name());
  490. // Create new fake phone with name.
  491. const multidevice::RemoteDeviceRef kFakePhoneA =
  492. multidevice::RemoteDeviceRefBuilder().SetName("Phone A").Build();
  493. // Trigger a host status update and expect a new phone with new name to be
  494. // updated.
  495. fake_multidevice_setup_client_->SetHostStatusWithDevice(
  496. std::make_pair(HostStatus::kHostVerified, kFakePhoneA));
  497. EXPECT_EQ(u"Phone A", mutable_phone_model_->phone_name());
  498. }
  499. TEST_F(PhoneStatusProcessorTest, NotificationAccess) {
  500. fake_multidevice_setup_client_->SetHostStatusWithDevice(
  501. std::make_pair(HostStatus::kHostVerified, test_remote_device_));
  502. CreatePhoneStatusProcessor();
  503. auto expected_phone_properties = std::make_unique<proto::PhoneProperties>();
  504. proto::PhoneStatusUpdate expected_update;
  505. expected_update.set_allocated_properties(expected_phone_properties.release());
  506. expected_update.add_updated_notifications();
  507. InitializeNotificationProto(expected_update.mutable_updated_notifications(0),
  508. /*id=*/0u);
  509. // Simulate feature set to enabled and connected.
  510. fake_feature_status_provider_->SetStatus(FeatureStatus::kEnabledAndConnected);
  511. fake_multidevice_setup_client_->SetFeatureState(
  512. Feature::kPhoneHubNotifications, FeatureState::kEnabledByUser);
  513. // Simulate receiving a proto message.
  514. fake_message_receiver_->NotifyPhoneStatusUpdateReceived(expected_update);
  515. EXPECT_EQ(1u, fake_notification_manager_->num_notifications());
  516. // Simulate notifications feature state set as disabled.
  517. fake_multidevice_setup_client_->SetFeatureState(
  518. Feature::kPhoneHubNotifications, FeatureState::kDisabledByUser);
  519. // Update with 1 new notification, expect no notifications to be processed.
  520. expected_update.add_updated_notifications();
  521. InitializeNotificationProto(expected_update.mutable_updated_notifications(1),
  522. /*id=*/1u);
  523. fake_message_receiver_->NotifyPhoneStatusUpdateReceived(expected_update);
  524. EXPECT_EQ(1u, fake_notification_manager_->num_notifications());
  525. // Re-enable notifications and expect the previous notification to be now
  526. // added.
  527. fake_multidevice_setup_client_->SetFeatureState(
  528. Feature::kPhoneHubNotifications, FeatureState::kEnabledByUser);
  529. fake_message_receiver_->NotifyPhoneStatusUpdateReceived(expected_update);
  530. EXPECT_EQ(2u, fake_notification_manager_->num_notifications());
  531. }
  532. } // namespace phonehub
  533. } // namespace ash