unified_system_tray_controller_unittest.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/unified_system_tray_controller.h"
  5. #include "ash/constants/ash_pref_names.h"
  6. #include "ash/session/session_controller_impl.h"
  7. #include "ash/shell.h"
  8. #include "ash/system/message_center/ash_message_center_lock_screen_controller.h"
  9. #include "ash/system/unified/notification_hidden_view.h"
  10. #include "ash/system/unified/unified_system_tray.h"
  11. #include "ash/system/unified/unified_system_tray_bubble.h"
  12. #include "ash/system/unified/unified_system_tray_model.h"
  13. #include "ash/system/unified/unified_system_tray_view.h"
  14. #include "ash/test/ash_test_base.h"
  15. #include "base/memory/scoped_refptr.h"
  16. #include "chromeos/ash/components/dbus/shill/shill_clients.h"
  17. #include "chromeos/services/network_config/public/cpp/cros_network_config_test_helper.h"
  18. #include "components/prefs/testing_pref_service.h"
  19. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  20. #include "ui/gfx/animation/slide_animation.h"
  21. #include "ui/views/view_observer.h"
  22. namespace ash {
  23. namespace {
  24. void SetSessionState(const session_manager::SessionState& state) {
  25. SessionInfo info;
  26. info.state = state;
  27. Shell::Get()->session_controller()->SetSessionInfo(info);
  28. }
  29. } // anonymous namespace
  30. class UnifiedSystemTrayControllerTest : public AshTestBase,
  31. public views::ViewObserver {
  32. public:
  33. UnifiedSystemTrayControllerTest() = default;
  34. UnifiedSystemTrayControllerTest(const UnifiedSystemTrayControllerTest&) =
  35. delete;
  36. UnifiedSystemTrayControllerTest& operator=(
  37. const UnifiedSystemTrayControllerTest&) = delete;
  38. ~UnifiedSystemTrayControllerTest() override = default;
  39. // testing::Test:
  40. void SetUp() override {
  41. network_config_helper_ = std::make_unique<
  42. chromeos::network_config::CrosNetworkConfigTestHelper>();
  43. AshTestBase::SetUp();
  44. // Networking stubs may have asynchronous initialization.
  45. base::RunLoop().RunUntilIdle();
  46. model_ = base::MakeRefCounted<UnifiedSystemTrayModel>(nullptr);
  47. controller_ = std::make_unique<UnifiedSystemTrayController>(model());
  48. }
  49. void TearDown() override {
  50. DCHECK(view_) << "Must call InitializeView() during the tests";
  51. view_->RemoveObserver(this);
  52. view_.reset();
  53. controller_.reset();
  54. model_.reset();
  55. AshTestBase::TearDown();
  56. }
  57. // views::ViewObserver:
  58. void OnViewPreferredSizeChanged(views::View* observed_view) override {
  59. view_->SetBoundsRect(gfx::Rect(view_->GetPreferredSize()));
  60. view_->Layout();
  61. ++preferred_size_changed_count_;
  62. }
  63. protected:
  64. void WaitForAnimation(UnifiedSystemTrayController* controller) {
  65. while (controller->animation_->is_animating())
  66. base::RunLoop().RunUntilIdle();
  67. }
  68. int preferred_size_changed_count() const {
  69. return preferred_size_changed_count_;
  70. }
  71. void InitializeView() {
  72. view_.reset(controller_->CreateView());
  73. view_->AddObserver(this);
  74. OnViewPreferredSizeChanged(view());
  75. preferred_size_changed_count_ = 0;
  76. }
  77. UnifiedSystemTrayModel* model() { return model_.get(); }
  78. UnifiedSystemTrayController* controller() { return controller_.get(); }
  79. UnifiedSystemTrayView* view() { return view_.get(); }
  80. bool PrimarySystemTrayIsExpandedOnOpen() {
  81. return GetPrimaryUnifiedSystemTray()->model()->IsExpandedOnOpen();
  82. }
  83. private:
  84. std::unique_ptr<chromeos::network_config::CrosNetworkConfigTestHelper>
  85. network_config_helper_;
  86. scoped_refptr<UnifiedSystemTrayModel> model_;
  87. std::unique_ptr<UnifiedSystemTrayController> controller_;
  88. std::unique_ptr<UnifiedSystemTrayView> view_;
  89. int preferred_size_changed_count_ = 0;
  90. };
  91. TEST_F(UnifiedSystemTrayControllerTest, ToggleExpanded) {
  92. InitializeView();
  93. EXPECT_TRUE(model()->IsExpandedOnOpen());
  94. const int expanded_height = view()->GetPreferredSize().height();
  95. controller()->ToggleExpanded();
  96. WaitForAnimation(controller());
  97. const int collapsed_height = view()->GetPreferredSize().height();
  98. EXPECT_LT(collapsed_height, expanded_height);
  99. EXPECT_FALSE(model()->IsExpandedOnOpen());
  100. EXPECT_EQ(expanded_height, view()->GetExpandedSystemTrayHeight());
  101. }
  102. TEST_F(UnifiedSystemTrayControllerTest, EnsureExpanded_UserChooserShown) {
  103. InitializeView();
  104. EXPECT_FALSE(view()->detailed_view_for_testing()->GetVisible());
  105. // Show the user chooser view.
  106. controller()->ShowUserChooserView();
  107. EXPECT_TRUE(view()->detailed_view_for_testing()->GetVisible());
  108. // Calling EnsureExpanded() should hide the detailed view (e.g. this can
  109. // happen when changing the brightness or volume).
  110. controller()->EnsureExpanded();
  111. EXPECT_FALSE(view()->detailed_view_for_testing()->GetVisible());
  112. }
  113. TEST_F(UnifiedSystemTrayControllerTest, PreferredSizeChanged) {
  114. InitializeView();
  115. // Checks PreferredSizeChanged is not called too frequently.
  116. EXPECT_EQ(0, preferred_size_changed_count());
  117. view()->SetExpandedAmount(0.0);
  118. EXPECT_EQ(1, preferred_size_changed_count());
  119. view()->SetExpandedAmount(0.25);
  120. EXPECT_EQ(2, preferred_size_changed_count());
  121. view()->SetExpandedAmount(0.75);
  122. EXPECT_EQ(3, preferred_size_changed_count());
  123. view()->SetExpandedAmount(1.0);
  124. EXPECT_EQ(4, preferred_size_changed_count());
  125. }
  126. TEST_F(UnifiedSystemTrayControllerTest, NotificationHiddenView_ModeShow) {
  127. AshMessageCenterLockScreenController::OverrideModeForTest(
  128. AshMessageCenterLockScreenController::Mode::SHOW);
  129. SetSessionState(session_manager::SessionState::LOCKED);
  130. InitializeView();
  131. EXPECT_TRUE(AshMessageCenterLockScreenController::IsAllowed());
  132. EXPECT_TRUE(AshMessageCenterLockScreenController::IsEnabled());
  133. EXPECT_FALSE(view()->notification_hidden_view_for_testing()->GetVisible());
  134. }
  135. TEST_F(UnifiedSystemTrayControllerTest, NotificationHiddenView_ModeHide) {
  136. AshMessageCenterLockScreenController::OverrideModeForTest(
  137. AshMessageCenterLockScreenController::Mode::HIDE);
  138. SetSessionState(session_manager::SessionState::LOCKED);
  139. InitializeView();
  140. EXPECT_TRUE(AshMessageCenterLockScreenController::IsAllowed());
  141. EXPECT_FALSE(AshMessageCenterLockScreenController::IsEnabled());
  142. EXPECT_TRUE(view()->notification_hidden_view_for_testing()->GetVisible());
  143. EXPECT_NE(nullptr, view()
  144. ->notification_hidden_view_for_testing()
  145. ->change_button_for_testing());
  146. }
  147. TEST_F(UnifiedSystemTrayControllerTest,
  148. NotificationHiddenView_ModeHideSensitive) {
  149. AshMessageCenterLockScreenController::OverrideModeForTest(
  150. AshMessageCenterLockScreenController::Mode::HIDE_SENSITIVE);
  151. SetSessionState(session_manager::SessionState::LOCKED);
  152. InitializeView();
  153. EXPECT_TRUE(AshMessageCenterLockScreenController::IsAllowed());
  154. EXPECT_TRUE(AshMessageCenterLockScreenController::IsEnabled());
  155. EXPECT_FALSE(view()->notification_hidden_view_for_testing()->GetVisible());
  156. }
  157. TEST_F(UnifiedSystemTrayControllerTest, NotificationHiddenView_ModeProhibited) {
  158. AshMessageCenterLockScreenController::OverrideModeForTest(
  159. AshMessageCenterLockScreenController::Mode::PROHIBITED);
  160. SetSessionState(session_manager::SessionState::LOCKED);
  161. InitializeView();
  162. EXPECT_FALSE(AshMessageCenterLockScreenController::IsAllowed());
  163. EXPECT_FALSE(AshMessageCenterLockScreenController::IsEnabled());
  164. EXPECT_TRUE(view()->notification_hidden_view_for_testing()->GetVisible());
  165. EXPECT_EQ(nullptr, view()
  166. ->notification_hidden_view_for_testing()
  167. ->change_button_for_testing());
  168. }
  169. TEST_F(UnifiedSystemTrayControllerTest, SystemTrayCollapsePref) {
  170. InitializeView();
  171. GetPrimaryUnifiedSystemTray()->ShowBubble();
  172. UnifiedSystemTrayController* controller =
  173. GetPrimaryUnifiedSystemTray()->bubble()->unified_system_tray_controller();
  174. PrefService* prefs =
  175. Shell::Get()->session_controller()->GetLastActiveUserPrefService();
  176. // System tray is initially expanded when no pref is set.
  177. EXPECT_FALSE(prefs->HasPrefPath(prefs::kSystemTrayExpanded));
  178. EXPECT_TRUE(PrimarySystemTrayIsExpandedOnOpen());
  179. // Toggle collapsed state.
  180. controller->ToggleExpanded();
  181. WaitForAnimation(controller);
  182. EXPECT_FALSE(PrimarySystemTrayIsExpandedOnOpen());
  183. // Close bubble and assert pref has been set.
  184. GetPrimaryUnifiedSystemTray()->CloseBubble();
  185. EXPECT_TRUE(prefs->HasPrefPath(prefs::kSystemTrayExpanded));
  186. // Reopen bubble to load `kSystemTrayExpanded` pref.
  187. GetPrimaryUnifiedSystemTray()->ShowBubble();
  188. EXPECT_FALSE(PrimarySystemTrayIsExpandedOnOpen());
  189. }
  190. } // namespace ash