date_tray_unittest.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2022 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/date_tray.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/system/status_area_widget.h"
  7. #include "ash/system/status_area_widget_test_helper.h"
  8. #include "ash/system/time/time_tray_item_view.h"
  9. #include "ash/system/time/time_view.h"
  10. #include "ash/system/unified/unified_system_tray_bubble.h"
  11. #include "ash/test/ash_test_base.h"
  12. #include "base/test/metrics/histogram_tester.h"
  13. #include "base/test/scoped_feature_list.h"
  14. #include "base/time/time.h"
  15. #include "base/time/time_override.h"
  16. #include "ui/events/keycodes/keyboard_codes_posix.h"
  17. namespace ash {
  18. class DateTrayTest : public AshTestBase {
  19. public:
  20. DateTrayTest() = default;
  21. DateTrayTest(const DateTrayTest&) = delete;
  22. DateTrayTest& operator=(const DateTrayTest&) = delete;
  23. ~DateTrayTest() override = default;
  24. void SetUp() override {
  25. // Enable calendar view feature.
  26. scoped_feature_list_.InitWithFeatures({ash::features::kCalendarView}, {});
  27. // Set time override.
  28. base::subtle::ScopedTimeClockOverrides time_override(
  29. []() {
  30. base::Time date;
  31. bool result = base::Time::FromString("24 Aug 2021 10:00 GMT", &date);
  32. DCHECK(result);
  33. return date;
  34. },
  35. /*time_ticks_override=*/nullptr,
  36. /*thread_ticks_override=*/nullptr);
  37. AshTestBase::SetUp();
  38. widget_ = CreateFramelessTestWidget();
  39. widget_->SetFullscreen(true);
  40. date_tray_ = StatusAreaWidgetTestHelper::GetStatusAreaWidget()->date_tray();
  41. widget_->SetContentsView(date_tray_);
  42. date_tray_->SetVisiblePreferred(true);
  43. date_tray_->unified_system_tray_->SetVisiblePreferred(true);
  44. }
  45. void TearDown() override {
  46. widget_.reset();
  47. date_tray_ = nullptr;
  48. AshTestBase::TearDown();
  49. }
  50. DateTray* GetDateTray() { return date_tray_; }
  51. UnifiedSystemTray* GetUnifiedSystemTray() {
  52. return date_tray_->unified_system_tray_;
  53. }
  54. std::u16string GetTimeViewText() {
  55. return date_tray_->time_view_->time_view()
  56. ->horizontal_label_date_for_test()
  57. ->GetText();
  58. }
  59. private:
  60. base::test::ScopedFeatureList scoped_feature_list_;
  61. std::unique_ptr<views::Widget> widget_;
  62. // Owned by `widget_`.
  63. DateTray* date_tray_ = nullptr;
  64. };
  65. // Test the initial state.
  66. TEST_F(DateTrayTest, InitialState) {
  67. // Show the mock time now Month and day.
  68. EXPECT_EQ(u"Aug 24", GetTimeViewText());
  69. // Initial state: not showing the calendar bubble.
  70. EXPECT_FALSE(GetUnifiedSystemTray()->IsBubbleShown());
  71. EXPECT_FALSE(GetUnifiedSystemTray()->IsShowingCalendarView());
  72. }
  73. // Tests clicking/tapping the DateTray shows/closes the calendar bubble.
  74. TEST_F(DateTrayTest, ShowCalendarBubble) {
  75. base::HistogramTester histogram_tester;
  76. // Clicking on the `DateTray` -> show the calendar bubble.
  77. LeftClickOn(GetDateTray());
  78. base::RunLoop().RunUntilIdle();
  79. EXPECT_TRUE(GetUnifiedSystemTray()->IsBubbleShown());
  80. EXPECT_TRUE(GetUnifiedSystemTray()->IsShowingCalendarView());
  81. EXPECT_FALSE(GetUnifiedSystemTray()->is_active());
  82. EXPECT_TRUE(GetDateTray()->is_active());
  83. histogram_tester.ExpectTotalCount("Ash.Calendar.ShowSource.TimeView", 1);
  84. // Clicking on the `DateTray` again -> close the calendar bubble.
  85. LeftClickOn(GetDateTray());
  86. base::RunLoop().RunUntilIdle();
  87. EXPECT_FALSE(GetUnifiedSystemTray()->IsShowingCalendarView());
  88. EXPECT_FALSE(GetUnifiedSystemTray()->is_active());
  89. EXPECT_FALSE(GetDateTray()->is_active());
  90. // Tapping on the `DateTray` again -> open the calendar bubble.
  91. GestureTapOn(GetDateTray());
  92. base::RunLoop().RunUntilIdle();
  93. EXPECT_TRUE(GetUnifiedSystemTray()->IsBubbleShown());
  94. EXPECT_TRUE(GetUnifiedSystemTray()->IsShowingCalendarView());
  95. EXPECT_FALSE(GetUnifiedSystemTray()->is_active());
  96. EXPECT_TRUE(GetDateTray()->is_active());
  97. histogram_tester.ExpectTotalCount("Ash.Calendar.ShowSource.TimeView", 2);
  98. // Tapping on the `DateTray` again -> close the calendar bubble.
  99. GestureTapOn(GetDateTray());
  100. base::RunLoop().RunUntilIdle();
  101. EXPECT_FALSE(GetUnifiedSystemTray()->IsBubbleShown());
  102. EXPECT_FALSE(GetUnifiedSystemTray()->IsShowingCalendarView());
  103. EXPECT_FALSE(GetUnifiedSystemTray()->is_active());
  104. EXPECT_FALSE(GetDateTray()->is_active());
  105. }
  106. // Tests the behavior when clicking on different areas.
  107. TEST_F(DateTrayTest, ClickingArea) {
  108. // Clicking on the `DateTray` -> show the calendar bubble.
  109. LeftClickOn(GetDateTray());
  110. base::RunLoop().RunUntilIdle();
  111. EXPECT_TRUE(GetUnifiedSystemTray()->IsShowingCalendarView());
  112. EXPECT_FALSE(GetUnifiedSystemTray()->is_active());
  113. EXPECT_TRUE(GetDateTray()->is_active());
  114. // Clicking on the bubble area -> not close the calendar bubble.
  115. LeftClickOn(GetUnifiedSystemTray()->bubble()->GetBubbleView());
  116. base::RunLoop().RunUntilIdle();
  117. EXPECT_TRUE(GetUnifiedSystemTray()->IsShowingCalendarView());
  118. EXPECT_FALSE(GetUnifiedSystemTray()->is_active());
  119. EXPECT_TRUE(GetDateTray()->is_active());
  120. // Clicking on the `UnifiedSystemTray` -> switch to QS bubble.
  121. LeftClickOn(GetUnifiedSystemTray());
  122. base::RunLoop().RunUntilIdle();
  123. EXPECT_TRUE(GetUnifiedSystemTray()->IsBubbleShown());
  124. EXPECT_FALSE(GetUnifiedSystemTray()->IsShowingCalendarView());
  125. EXPECT_TRUE(GetUnifiedSystemTray()->is_active());
  126. EXPECT_FALSE(GetDateTray()->is_active());
  127. // Clicking on the `DateTray` -> switch to the calendar bubble.
  128. LeftClickOn(GetDateTray());
  129. base::RunLoop().RunUntilIdle();
  130. EXPECT_TRUE(GetUnifiedSystemTray()->IsShowingCalendarView());
  131. EXPECT_FALSE(GetUnifiedSystemTray()->is_active());
  132. EXPECT_TRUE(GetDateTray()->is_active());
  133. // Clicking on the gap between `DateTray` and `UnifiedSystemTray`-> close the
  134. // bubble.
  135. auto* event_generator = GetEventGenerator();
  136. int date_tray_right = GetDateTray()->GetBoundsInScreen().right();
  137. int unigied_tray_left = GetUnifiedSystemTray()->GetBoundsInScreen().x();
  138. event_generator->MoveMouseTo(
  139. gfx::Point((date_tray_right + unigied_tray_left) / 2,
  140. GetDateTray()->GetBoundsInScreen().CenterPoint().y()));
  141. event_generator->ClickLeftButton();
  142. LeftClickOn(GetDateTray());
  143. base::RunLoop().RunUntilIdle();
  144. EXPECT_FALSE(GetUnifiedSystemTray()->IsBubbleShown());
  145. EXPECT_FALSE(GetUnifiedSystemTray()->is_active());
  146. EXPECT_FALSE(GetDateTray()->is_active());
  147. }
  148. TEST_F(DateTrayTest, EscapeKeyForClose) {
  149. base::HistogramTester histogram_tester;
  150. // Clicking on the `DateTray` -> show the calendar bubble.
  151. LeftClickOn(GetDateTray());
  152. base::RunLoop().RunUntilIdle();
  153. EXPECT_TRUE(GetUnifiedSystemTray()->IsBubbleShown());
  154. EXPECT_TRUE(GetUnifiedSystemTray()->IsShowingCalendarView());
  155. EXPECT_FALSE(GetUnifiedSystemTray()->is_active());
  156. EXPECT_TRUE(GetDateTray()->is_active());
  157. histogram_tester.ExpectTotalCount("Ash.Calendar.ShowSource.TimeView", 1);
  158. // Hitting escape key -> close and deactivate the calendar bubble.
  159. PressAndReleaseKey(ui::KeyboardCode::VKEY_ESCAPE);
  160. base::RunLoop().RunUntilIdle();
  161. EXPECT_FALSE(GetUnifiedSystemTray()->IsShowingCalendarView());
  162. EXPECT_FALSE(GetUnifiedSystemTray()->is_active());
  163. EXPECT_FALSE(GetDateTray()->is_active());
  164. }
  165. } // namespace ash