contextual_tooltip_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/controls/contextual_tooltip.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/controls/contextual_tooltip.h"
  7. #include "ash/session/session_controller_impl.h"
  8. #include "ash/shell.h"
  9. #include "ash/test/ash_test_base.h"
  10. #include "base/json/values_util.h"
  11. #include "base/strings/string_util.h"
  12. #include "base/test/scoped_feature_list.h"
  13. #include "base/test/simple_test_clock.h"
  14. #include "base/time/time.h"
  15. #include "base/values.h"
  16. #include "components/prefs/scoped_user_pref_update.h"
  17. #include "ui/aura/window.h"
  18. #include "ui/wm/core/window_util.h"
  19. namespace ash {
  20. namespace contextual_tooltip {
  21. class ContextualTooltipTest : public AshTestBase,
  22. public testing::WithParamInterface<bool> {
  23. public:
  24. ContextualTooltipTest() {
  25. if (GetParam()) {
  26. scoped_feature_list_.InitWithFeatures(
  27. {ash::features::kContextualNudges,
  28. ash::features::kHideShelfControlsInTabletMode},
  29. {});
  30. } else {
  31. scoped_feature_list_.InitAndDisableFeature(
  32. ash::features::kContextualNudges);
  33. }
  34. }
  35. ~ContextualTooltipTest() override = default;
  36. base::SimpleTestClock* clock() { return &test_clock_; }
  37. // AshTestBase:
  38. void SetUp() override {
  39. AshTestBase::SetUp();
  40. contextual_tooltip::OverrideClockForTesting(&test_clock_);
  41. test_clock_.Advance(base::Seconds(360));
  42. }
  43. void TearDown() override {
  44. contextual_tooltip::ClearClockOverrideForTesting();
  45. AshTestBase::TearDown();
  46. }
  47. PrefService* GetPrefService() {
  48. return Shell::Get()->session_controller()->GetLastActiveUserPrefService();
  49. }
  50. private:
  51. base::test::ScopedFeatureList scoped_feature_list_;
  52. base::SimpleTestClock test_clock_;
  53. };
  54. using ContextualTooltipDisabledTest = ContextualTooltipTest;
  55. INSTANTIATE_TEST_SUITE_P(All,
  56. ContextualTooltipDisabledTest,
  57. testing::Values(false));
  58. INSTANTIATE_TEST_SUITE_P(All, ContextualTooltipTest, testing::Values(true));
  59. // Checks that nudges are not shown when the feature flag is disabled.
  60. TEST_P(ContextualTooltipDisabledTest, FeatureFlagDisabled) {
  61. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  62. GetPrefService(), TooltipType::kInAppToHome, nullptr));
  63. }
  64. TEST_P(ContextualTooltipTest, ShouldShowPersistentDragHandleNudge) {
  65. base::TimeDelta recheck_delay;
  66. EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
  67. GetPrefService(), TooltipType::kInAppToHome, &recheck_delay));
  68. EXPECT_TRUE(recheck_delay.is_zero());
  69. EXPECT_TRUE(contextual_tooltip::GetNudgeTimeout(GetPrefService(),
  70. TooltipType::kInAppToHome)
  71. .is_zero());
  72. }
  73. // Checks that drag handle nudge has a timeout if it is not the first time it is
  74. // being shown.
  75. TEST_P(ContextualTooltipTest, NonPersistentDragHandleNudgeTimeout) {
  76. for (int shown_count = 1;
  77. shown_count < contextual_tooltip::kNotificationLimit; shown_count++) {
  78. contextual_tooltip::HandleNudgeShown(GetPrefService(),
  79. TooltipType::kInAppToHome);
  80. clock()->Advance(contextual_tooltip::kMinInterval);
  81. EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
  82. GetPrefService(), TooltipType::kInAppToHome, nullptr));
  83. EXPECT_EQ(contextual_tooltip::GetNudgeTimeout(GetPrefService(),
  84. TooltipType::kInAppToHome),
  85. contextual_tooltip::kNudgeShowDuration);
  86. }
  87. }
  88. // Checks that drag handle nudge should be shown after kMinInterval has passed
  89. // since the last time it was shown but not before the time interval has passed.
  90. TEST_P(ContextualTooltipTest, ShouldShowTimedDragHandleNudge) {
  91. contextual_tooltip::HandleNudgeShown(GetPrefService(),
  92. TooltipType::kInAppToHome);
  93. base::TimeDelta recheck_delay;
  94. for (int shown_count = 1;
  95. shown_count < contextual_tooltip::kNotificationLimit; shown_count++) {
  96. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  97. GetPrefService(), TooltipType::kInAppToHome, &recheck_delay));
  98. EXPECT_EQ(contextual_tooltip::kMinInterval, recheck_delay);
  99. clock()->Advance(contextual_tooltip::kMinInterval / 2);
  100. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  101. GetPrefService(), TooltipType::kInAppToHome, &recheck_delay));
  102. EXPECT_EQ(
  103. contextual_tooltip::kMinInterval - contextual_tooltip::kMinInterval / 2,
  104. recheck_delay);
  105. clock()->Advance(contextual_tooltip::kMinInterval / 2);
  106. EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
  107. GetPrefService(), TooltipType::kInAppToHome, nullptr));
  108. contextual_tooltip::HandleNudgeShown(GetPrefService(),
  109. TooltipType::kInAppToHome);
  110. }
  111. clock()->Advance(contextual_tooltip::kMinInterval);
  112. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  113. GetPrefService(), TooltipType::kInAppToHome, &recheck_delay));
  114. EXPECT_TRUE(recheck_delay.is_zero());
  115. EXPECT_EQ(contextual_tooltip::GetNudgeTimeout(GetPrefService(),
  116. TooltipType::kInAppToHome),
  117. contextual_tooltip::kNudgeShowDuration);
  118. }
  119. // Tests that if the user has successfully performed the gesture for at least
  120. // |kSuccessLimit|, the corresponding nudge should not be shown.
  121. TEST_P(ContextualTooltipTest, ShouldNotShowNudgeAfterSuccessLimit) {
  122. EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
  123. GetPrefService(), TooltipType::kInAppToHome, nullptr));
  124. for (int success_count = 0;
  125. success_count < contextual_tooltip::kSuccessLimitInAppToHome;
  126. success_count++) {
  127. contextual_tooltip::HandleGesturePerformed(GetPrefService(),
  128. TooltipType::kInAppToHome);
  129. }
  130. base::TimeDelta recheck_delay;
  131. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  132. GetPrefService(), TooltipType::kInAppToHome, &recheck_delay));
  133. EXPECT_TRUE(recheck_delay.is_zero());
  134. }
  135. // Should not show back gesture nudge if drag handle nudge is expected to be
  136. // shown.
  137. TEST_P(ContextualTooltipTest,
  138. DoNotShowBackGestureNudgeIfDragHandleNudgeIsExpected) {
  139. EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
  140. GetPrefService(), TooltipType::kInAppToHome, nullptr));
  141. // The drag handle nudge is expected to show, so back gesture nudge should not
  142. // be shown at the same time.
  143. base::TimeDelta recheck_delay;
  144. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  145. GetPrefService(), TooltipType::kBackGesture, &recheck_delay));
  146. EXPECT_EQ(contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge,
  147. recheck_delay);
  148. // After the nudge is shown, back gesture should remain hidden until
  149. // sufficient amount of time passes.
  150. contextual_tooltip::HandleNudgeShown(GetPrefService(),
  151. TooltipType::kInAppToHome);
  152. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  153. GetPrefService(), TooltipType::kBackGesture, &recheck_delay));
  154. EXPECT_EQ(contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge,
  155. recheck_delay);
  156. clock()->Advance(
  157. contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge / 2);
  158. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  159. GetPrefService(), TooltipType::kBackGesture, &recheck_delay));
  160. EXPECT_EQ(
  161. contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge -
  162. contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge / 2,
  163. recheck_delay);
  164. clock()->Advance(recheck_delay);
  165. EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
  166. GetPrefService(), TooltipType::kBackGesture, nullptr));
  167. // After the drag handle becomes eligible to show again, the back gesture
  168. // should be disabled.
  169. clock()->Advance(contextual_tooltip::kMinInterval);
  170. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  171. GetPrefService(), TooltipType::kBackGesture, &recheck_delay));
  172. EXPECT_EQ(contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge,
  173. recheck_delay);
  174. }
  175. // Tests that back gesture is allowed if the shelf is hidden, even if drag
  176. // handle would normally be available.
  177. TEST_P(ContextualTooltipTest, AllowBackGestureForHiddenShelf) {
  178. EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
  179. GetPrefService(), TooltipType::kInAppToHome, nullptr));
  180. // The drag handle nudge is expected to show, so back gesture nudge should not
  181. // be shown at the same time.
  182. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  183. GetPrefService(), TooltipType::kBackGesture, nullptr));
  184. // If drag handle nudge is disabled because the shelf is hidden, the back
  185. // gesture nudge should be allowed.
  186. contextual_tooltip::SetDragHandleNudgeDisabledForHiddenShelf(true);
  187. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  188. GetPrefService(), TooltipType::kInAppToHome, nullptr));
  189. EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
  190. GetPrefService(), TooltipType::kBackGesture, nullptr));
  191. // Disallow back gesture nudge if the shelf becomes visible.
  192. contextual_tooltip::SetDragHandleNudgeDisabledForHiddenShelf(false);
  193. EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
  194. GetPrefService(), TooltipType::kInAppToHome, nullptr));
  195. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  196. GetPrefService(), TooltipType::kBackGesture, nullptr));
  197. }
  198. // Tests that the drag handle nudge should not be shown while back gesture is
  199. // showing, or soon after it's been shown.
  200. TEST_P(ContextualTooltipTest,
  201. DoNotShowDragHandleNudgeIfBackGestureNudgeIsShown) {
  202. EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
  203. GetPrefService(), TooltipType::kInAppToHome, nullptr));
  204. // Drag handle nudge not allowed if back gesture is showing.
  205. contextual_tooltip::SetDragHandleNudgeDisabledForHiddenShelf(true);
  206. contextual_tooltip::SetBackGestureNudgeShowing(true);
  207. contextual_tooltip::SetDragHandleNudgeDisabledForHiddenShelf(false);
  208. base::TimeDelta recheck_delay;
  209. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  210. GetPrefService(), TooltipType::kInAppToHome, &recheck_delay));
  211. EXPECT_EQ(contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge,
  212. recheck_delay);
  213. // Allow drag handle only if sufficient amount of time passes since showing
  214. // the back gesture nudge.
  215. contextual_tooltip::SetBackGestureNudgeShowing(false);
  216. contextual_tooltip::HandleNudgeShown(GetPrefService(),
  217. TooltipType::kBackGesture);
  218. recheck_delay = base::TimeDelta();
  219. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  220. GetPrefService(), TooltipType::kInAppToHome, &recheck_delay));
  221. EXPECT_EQ(contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge,
  222. recheck_delay);
  223. clock()->Advance(
  224. contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge / 2);
  225. EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
  226. GetPrefService(), TooltipType::kInAppToHome, &recheck_delay));
  227. clock()->Advance(recheck_delay);
  228. EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
  229. GetPrefService(), TooltipType::kInAppToHome, nullptr));
  230. }
  231. } // namespace contextual_tooltip
  232. } // namespace ash