top_shortcuts_view_unittest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2018 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/unified/top_shortcuts_view.h"
  5. #include "ash/constants/ash_pref_names.h"
  6. #include "ash/session/test_session_controller_client.h"
  7. #include "ash/style/icon_button.h"
  8. #include "ash/system/unified/collapse_button.h"
  9. #include "ash/system/unified/unified_system_tray_controller.h"
  10. #include "ash/system/unified/unified_system_tray_model.h"
  11. #include "ash/test/ash_test_base.h"
  12. #include "base/memory/scoped_refptr.h"
  13. #include "components/prefs/pref_registry_simple.h"
  14. namespace ash {
  15. // Tests manually control their session visibility.
  16. class TopShortcutsViewTest : public NoSessionAshTestBase {
  17. public:
  18. TopShortcutsViewTest() = default;
  19. TopShortcutsViewTest(const TopShortcutsViewTest&) = delete;
  20. TopShortcutsViewTest& operator=(const TopShortcutsViewTest&) = delete;
  21. ~TopShortcutsViewTest() override = default;
  22. void SetUp() override {
  23. NoSessionAshTestBase::SetUp();
  24. model_ = base::MakeRefCounted<UnifiedSystemTrayModel>(nullptr);
  25. controller_ = std::make_unique<UnifiedSystemTrayController>(model_.get());
  26. }
  27. void TearDown() override {
  28. controller_.reset();
  29. top_shortcuts_view_.reset();
  30. model_.reset();
  31. NoSessionAshTestBase::TearDown();
  32. }
  33. protected:
  34. void SetUpView() {
  35. top_shortcuts_view_ = std::make_unique<TopShortcutsView>(controller_.get());
  36. }
  37. views::View* GetUserAvatar() {
  38. return top_shortcuts_view_->user_avatar_button_;
  39. }
  40. views::Button* GetSignOutButton() {
  41. return top_shortcuts_view_->sign_out_button_;
  42. }
  43. views::Button* GetLockButton() { return top_shortcuts_view_->lock_button_; }
  44. views::Button* GetSettingsButton() {
  45. return top_shortcuts_view_->settings_button_;
  46. }
  47. views::Button* GetPowerButton() { return top_shortcuts_view_->power_button_; }
  48. views::Button* GetCollapseButton() {
  49. return top_shortcuts_view_->collapse_button_;
  50. }
  51. void Layout() { top_shortcuts_view_->Layout(); }
  52. private:
  53. scoped_refptr<UnifiedSystemTrayModel> model_;
  54. std::unique_ptr<UnifiedSystemTrayController> controller_;
  55. std::unique_ptr<TopShortcutsView> top_shortcuts_view_;
  56. };
  57. // Settings button and lock button are hidden before login.
  58. TEST_F(TopShortcutsViewTest, ButtonStatesNotLoggedIn) {
  59. SetUpView();
  60. EXPECT_EQ(nullptr, GetUserAvatar());
  61. EXPECT_EQ(nullptr, GetSignOutButton());
  62. EXPECT_EQ(nullptr, GetLockButton());
  63. EXPECT_EQ(nullptr, GetSettingsButton());
  64. EXPECT_TRUE(GetPowerButton()->GetVisible());
  65. EXPECT_TRUE(GetCollapseButton()->GetVisible());
  66. }
  67. // All buttons are shown after login.
  68. TEST_F(TopShortcutsViewTest, ButtonStatesLoggedIn) {
  69. CreateUserSessions(1);
  70. SetUpView();
  71. EXPECT_TRUE(GetUserAvatar()->GetVisible());
  72. EXPECT_TRUE(GetSignOutButton()->GetVisible());
  73. EXPECT_TRUE(GetLockButton()->GetVisible());
  74. EXPECT_TRUE(GetSettingsButton()->GetVisible());
  75. EXPECT_TRUE(GetPowerButton()->GetVisible());
  76. EXPECT_TRUE(GetCollapseButton()->GetVisible());
  77. }
  78. // Settings button and lock button are hidden at the lock screen.
  79. TEST_F(TopShortcutsViewTest, ButtonStatesLockScreen) {
  80. BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
  81. SetUpView();
  82. EXPECT_TRUE(GetUserAvatar()->GetVisible());
  83. EXPECT_TRUE(GetSignOutButton()->GetVisible());
  84. EXPECT_EQ(nullptr, GetLockButton());
  85. EXPECT_EQ(nullptr, GetSettingsButton());
  86. EXPECT_TRUE(GetPowerButton()->GetVisible());
  87. EXPECT_TRUE(GetCollapseButton()->GetVisible());
  88. }
  89. // Settings button and lock button are hidden when adding a second
  90. // multiprofile user.
  91. TEST_F(TopShortcutsViewTest, ButtonStatesAddingUser) {
  92. CreateUserSessions(1);
  93. SetUserAddingScreenRunning(true);
  94. SetUpView();
  95. EXPECT_TRUE(GetUserAvatar()->GetVisible());
  96. EXPECT_TRUE(GetSignOutButton()->GetVisible());
  97. EXPECT_EQ(nullptr, GetLockButton());
  98. EXPECT_EQ(nullptr, GetSettingsButton());
  99. EXPECT_TRUE(GetPowerButton()->GetVisible());
  100. EXPECT_TRUE(GetCollapseButton()->GetVisible());
  101. }
  102. // Try to layout buttons before login.
  103. TEST_F(TopShortcutsViewTest, ButtonLayoutNotLoggedIn) {
  104. SetUpView();
  105. Layout();
  106. }
  107. // Try to layout buttons after login.
  108. TEST_F(TopShortcutsViewTest, ButtonLayoutLoggedIn) {
  109. CreateUserSessions(1);
  110. SetUpView();
  111. Layout();
  112. }
  113. // Try to layout buttons at the lock screen.
  114. TEST_F(TopShortcutsViewTest, ButtonLayoutLockScreen) {
  115. BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
  116. SetUpView();
  117. Layout();
  118. }
  119. // Try to layout buttons when adding a second multiprofile user.
  120. TEST_F(TopShortcutsViewTest, ButtonLayoutAddingUser) {
  121. CreateUserSessions(1);
  122. SetUserAddingScreenRunning(true);
  123. SetUpView();
  124. Layout();
  125. }
  126. // Settings button is disabled when kSettingsIconDisabled is set.
  127. TEST_F(TopShortcutsViewTest, DisableSettingsIconPolicy) {
  128. GetSessionControllerClient()->AddUserSession("foo@example.com",
  129. user_manager::USER_TYPE_REGULAR);
  130. GetSessionControllerClient()->SetSessionState(
  131. session_manager::SessionState::ACTIVE);
  132. SetUpView();
  133. EXPECT_EQ(views::Button::STATE_NORMAL, GetSettingsButton()->GetState());
  134. local_state()->SetBoolean(prefs::kOsSettingsEnabled, false);
  135. EXPECT_EQ(views::Button::STATE_DISABLED, GetSettingsButton()->GetState());
  136. local_state()->SetBoolean(prefs::kOsSettingsEnabled, true);
  137. EXPECT_EQ(views::Button::STATE_NORMAL, GetSettingsButton()->GetState());
  138. }
  139. } // namespace ash