gesture_education_notification_controller_unittest.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/system/gesture_education/gesture_education_notification_controller.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/session/session_controller_impl.h"
  7. #include "ash/shell.h"
  8. #include "ash/test/ash_test_base.h"
  9. #include "ash/wm/tablet_mode/tablet_mode_controller_test_api.h"
  10. #include "base/test/scoped_feature_list.h"
  11. #include "ui/message_center/message_center.h"
  12. #include "ui/message_center/public/cpp/notification.h"
  13. #include "ui/message_center/public/cpp/notification_types.h"
  14. namespace ash {
  15. class GestureEducationNotificationControllerTest : public AshTestBase {
  16. public:
  17. GestureEducationNotificationControllerTest() {
  18. scoped_feature_list_.InitWithFeatures(
  19. {ash::features::kHideShelfControlsInTabletMode}, {});
  20. }
  21. ~GestureEducationNotificationControllerTest() override = default;
  22. // AshTestBase:
  23. void SetUp() override {
  24. AshTestBase::SetUp();
  25. controller_ = std::make_unique<GestureEducationNotificationController>();
  26. controller_->OnActiveUserPrefServiceChanged(
  27. Shell::Get()->session_controller()->GetPrimaryUserPrefService());
  28. controller_->ResetPrefForTest();
  29. test_api_ = std::make_unique<TabletModeControllerTestApi>();
  30. }
  31. void TearDown() override {
  32. controller_.reset();
  33. test_api_.reset();
  34. AshTestBase::TearDown();
  35. }
  36. void SetTabletMode(bool on) {
  37. if (on)
  38. test_api_->EnterTabletMode();
  39. else
  40. test_api_->LeaveTabletMode();
  41. }
  42. protected:
  43. message_center::Notification* GetNotification() {
  44. const message_center::NotificationList::Notifications& notifications =
  45. message_center::MessageCenter::Get()->GetVisibleNotifications();
  46. for (message_center::NotificationList::Notifications::const_iterator iter =
  47. notifications.begin();
  48. iter != notifications.end(); ++iter) {
  49. if ((*iter)->id() ==
  50. GestureEducationNotificationController::kNotificationId)
  51. return *iter;
  52. }
  53. return nullptr;
  54. }
  55. void RemoveNotification() {
  56. message_center::MessageCenter::Get()->RemoveNotification(
  57. GestureEducationNotificationController::kNotificationId,
  58. false /* by_user */);
  59. }
  60. private:
  61. std::unique_ptr<GestureEducationNotificationController> controller_;
  62. std::unique_ptr<TabletModeControllerTestApi> test_api_;
  63. base::test::ScopedFeatureList scoped_feature_list_;
  64. };
  65. TEST_F(GestureEducationNotificationControllerTest, Notification) {
  66. // No notification initially.
  67. EXPECT_FALSE(GetNotification());
  68. SetTabletMode(true);
  69. // Notification generated after first time tablet mode entered.
  70. EXPECT_TRUE(GetNotification());
  71. RemoveNotification();
  72. SetTabletMode(false);
  73. SetTabletMode(true);
  74. // No notification should be generated once it has been shown once.
  75. EXPECT_FALSE(GetNotification());
  76. }
  77. } // namespace ash