palette_welcome_bubble_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2017 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/palette/palette_welcome_bubble.h"
  5. #include "ash/constants/ash_pref_names.h"
  6. #include "ash/session/session_controller_impl.h"
  7. #include "ash/session/test_session_controller_client.h"
  8. #include "ash/shell.h"
  9. #include "ash/system/palette/palette_tray.h"
  10. #include "ash/system/status_area_widget.h"
  11. #include "ash/system/status_area_widget_test_helper.h"
  12. #include "ash/test/ash_test_base.h"
  13. #include "base/command_line.h"
  14. #include "components/prefs/pref_service.h"
  15. #include "components/session_manager/session_manager_types.h"
  16. #include "ui/events/test/event_generator.h"
  17. #include "ui/views/controls/button/image_button.h"
  18. namespace ash {
  19. namespace {
  20. constexpr char kUser1Email[] = "user1@palettewelcome.com";
  21. constexpr char kUser2Email[] = "user2@palettewelcome.com";
  22. constexpr char kGuestEmail[] = "guest@palettewelcome.com";
  23. constexpr char kPublicAccountEmail[] = "public@palettewelcome.com";
  24. } // namespace
  25. class PaletteWelcomeBubbleTest : public AshTestBase {
  26. public:
  27. PaletteWelcomeBubbleTest() = default;
  28. PaletteWelcomeBubbleTest(const PaletteWelcomeBubbleTest&) = delete;
  29. PaletteWelcomeBubbleTest& operator=(const PaletteWelcomeBubbleTest&) = delete;
  30. ~PaletteWelcomeBubbleTest() override = default;
  31. PrefService* user1_pref_service() {
  32. return Shell::Get()->session_controller()->GetUserPrefServiceForUser(
  33. AccountId::FromUserEmail(kUser1Email));
  34. }
  35. PrefService* user2_pref_service() {
  36. return Shell::Get()->session_controller()->GetUserPrefServiceForUser(
  37. AccountId::FromUserEmail(kUser2Email));
  38. }
  39. void ShowBubble() { welcome_bubble_->Show(); }
  40. void HideBubble() { welcome_bubble_->Hide(); }
  41. // AshTestBase:
  42. void SetUp() override {
  43. AshTestBase::SetUp();
  44. welcome_bubble_ = std::make_unique<PaletteWelcomeBubble>(
  45. StatusAreaWidgetTestHelper::GetStatusAreaWidget()->palette_tray());
  46. GetSessionControllerClient()->AddUserSession(kUser1Email);
  47. GetSessionControllerClient()->AddUserSession(kUser2Email);
  48. GetSessionControllerClient()->SwitchActiveUser(
  49. AccountId::FromUserEmail(kUser1Email));
  50. }
  51. void TearDown() override {
  52. welcome_bubble_.reset();
  53. AshTestBase::TearDown();
  54. }
  55. protected:
  56. std::unique_ptr<PaletteWelcomeBubble> welcome_bubble_;
  57. };
  58. // Test the basic Show/Hide functions work.
  59. TEST_F(PaletteWelcomeBubbleTest, Basic) {
  60. EXPECT_FALSE(welcome_bubble_->GetBubbleViewForTesting());
  61. ShowBubble();
  62. EXPECT_TRUE(welcome_bubble_->GetBubbleViewForTesting());
  63. HideBubble();
  64. EXPECT_FALSE(welcome_bubble_->GetBubbleViewForTesting());
  65. // Verify that the pref changes after the bubble is hidden.
  66. EXPECT_TRUE(
  67. user1_pref_service()->GetBoolean(prefs::kShownPaletteWelcomeBubble));
  68. }
  69. // Verify if the bubble has been show before, it will not be shown again.
  70. TEST_F(PaletteWelcomeBubbleTest, ShowIfNeeded) {
  71. user1_pref_service()->SetBoolean(prefs::kShownPaletteWelcomeBubble, true);
  72. welcome_bubble_->ShowIfNeeded();
  73. EXPECT_FALSE(welcome_bubble_->GetBubbleViewForTesting());
  74. }
  75. // Verify that tapping on the screen outside of the welcome bubble closes the
  76. // bubble.
  77. TEST_F(PaletteWelcomeBubbleTest, TapOutsideOfBubble) {
  78. ShowBubble();
  79. ASSERT_TRUE(welcome_bubble_->GetBubbleViewForTesting());
  80. auto bounds = welcome_bubble_->GetBubbleViewForTesting()->GetBoundsInScreen();
  81. ASSERT_FALSE(bounds.IsEmpty());
  82. // The bubble remains open if a tap occurs on the bubble.
  83. GetEventGenerator()->set_current_screen_location(bounds.CenterPoint());
  84. GetEventGenerator()->ClickLeftButton();
  85. EXPECT_TRUE(welcome_bubble_->GetBubbleViewForTesting());
  86. // Tap anywhere outside the bubble.
  87. ASSERT_FALSE(bounds.Contains(gfx::Point()));
  88. GetEventGenerator()->set_current_screen_location(gfx::Point());
  89. GetEventGenerator()->ClickLeftButton();
  90. // The Widget (and thus the contained views) is closed asynchronously. This
  91. // check ensures either the BubbleView doesn't exist or that the Widget has
  92. // been closed.
  93. EXPECT_TRUE(
  94. !welcome_bubble_->GetBubbleViewForTesting() ||
  95. welcome_bubble_->GetBubbleViewForTesting()->GetWidget()->IsClosed());
  96. }
  97. // Verify that a second user sees the bubble even after a first user has seen it
  98. // already.
  99. TEST_F(PaletteWelcomeBubbleTest, BubbleShownForSecondUser) {
  100. // Show the bubble for the first user, and verify that the pref is set when
  101. // the bubble is hidden.
  102. ShowBubble();
  103. EXPECT_TRUE(welcome_bubble_->GetBubbleViewForTesting());
  104. HideBubble();
  105. ASSERT_TRUE(
  106. user1_pref_service()->GetBoolean(prefs::kShownPaletteWelcomeBubble));
  107. // Switch to the second user, and verify that the bubble will get shown.
  108. GetSessionControllerClient()->SwitchActiveUser(
  109. AccountId::FromUserEmail(kUser2Email));
  110. EXPECT_FALSE(
  111. user2_pref_service()->GetBoolean(prefs::kShownPaletteWelcomeBubble));
  112. welcome_bubble_->ShowIfNeeded();
  113. EXPECT_TRUE(welcome_bubble_->GetBubbleViewForTesting());
  114. }
  115. TEST_F(PaletteWelcomeBubbleTest, BubbleNotShownInactiveSession) {
  116. GetSessionControllerClient()->SetSessionState(
  117. session_manager::SessionState::LOGGED_IN_NOT_ACTIVE);
  118. welcome_bubble_->ShowIfNeeded();
  119. EXPECT_FALSE(welcome_bubble_->GetBubbleViewForTesting());
  120. }
  121. using PaletteWelcomeBubbleEmphemeralAccountTest = AshTestBase;
  122. TEST_F(PaletteWelcomeBubbleEmphemeralAccountTest, BubbleNotShownForGuest) {
  123. auto welcome_bubble = std::make_unique<PaletteWelcomeBubble>(
  124. StatusAreaWidgetTestHelper::GetStatusAreaWidget()->palette_tray());
  125. GetSessionControllerClient()->AddUserSession(kGuestEmail,
  126. user_manager::USER_TYPE_GUEST);
  127. GetSessionControllerClient()->SwitchActiveUser(
  128. AccountId::FromUserEmail(kGuestEmail));
  129. welcome_bubble->ShowIfNeeded();
  130. EXPECT_FALSE(welcome_bubble->GetBubbleViewForTesting());
  131. }
  132. TEST_F(PaletteWelcomeBubbleEmphemeralAccountTest,
  133. BubbleNotShownForPublicAccount) {
  134. auto welcome_bubble = std::make_unique<PaletteWelcomeBubble>(
  135. StatusAreaWidgetTestHelper::GetStatusAreaWidget()->palette_tray());
  136. GetSessionControllerClient()->AddUserSession(
  137. kPublicAccountEmail, user_manager::USER_TYPE_PUBLIC_ACCOUNT);
  138. GetSessionControllerClient()->SwitchActiveUser(
  139. AccountId::FromUserEmail(kPublicAccountEmail));
  140. welcome_bubble->ShowIfNeeded();
  141. EXPECT_FALSE(welcome_bubble->GetBubbleViewForTesting());
  142. }
  143. } // namespace ash