File: help_bubble_view_timeout_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (164 lines) | stat: -rw-r--r-- 5,970 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/functional/callback_forward.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/test_with_browser_view.h"
#include "chrome/browser/ui/views/user_education/browser_user_education_service.h"
#include "components/user_education/common/help_bubble/help_bubble_params.h"
#include "components/user_education/views/help_bubble_view.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/widget/widget_observer.h"

using user_education::HelpBubbleArrow;
using user_education::HelpBubbleButtonParams;
using user_education::HelpBubbleParams;
using user_education::HelpBubbleView;

namespace {
class TestHelpBubbleView : public HelpBubbleView {
 public:
  using HelpBubbleView::HelpBubbleView;
  using HelpBubbleView::OnWidgetActivationChanged;
};
}  // namespace

// Testing timeouts can be flaky on some platforms without the full browser view
// and its message pump, so we do these tests here rather than in the
// user_education component.
class HelpBubbleViewTimeoutTest : public TestWithBrowserView {
 public:
  HelpBubbleViewTimeoutTest()
      : TestWithBrowserView(
            base::test::SingleThreadTaskEnvironment::TimeSource::MOCK_TIME) {}
  ~HelpBubbleViewTimeoutTest() override = default;

 protected:
  HelpBubbleParams GetBubbleParams() {
    HelpBubbleParams params;
    params.body_text = u"To X, do Y";
    params.arrow = HelpBubbleArrow::kTopRight;
    return params;
  }

  TestHelpBubbleView* CreateHelpBubbleView(HelpBubbleParams params) {
    return new TestHelpBubbleView(GetHelpBubbleDelegate(),
                                  {browser_view()->contents_container()},
                                  std::move(params));
  }
};

class MockWidgetObserver : public views::WidgetObserver {
 public:
  MOCK_METHOD(void, OnWidgetClosing, (views::Widget*), ());
};

TEST_F(HelpBubbleViewTimeoutTest, DismissOnTimeout) {
  HelpBubbleParams params = GetBubbleParams();
  params.timeout = base::Seconds(30);
  HelpBubbleView* const bubble = CreateHelpBubbleView(std::move(params));
  MockWidgetObserver dismiss_observer;
  EXPECT_CALL(dismiss_observer, OnWidgetClosing(testing::_)).Times(1);
  bubble->GetWidget()->AddObserver(&dismiss_observer);
  task_environment()->FastForwardBy(base::Minutes(1));
  task_environment()->RunUntilIdle();
}

TEST_F(HelpBubbleViewTimeoutTest, NoAutoDismissWithoutTimeout) {
  // Without a button, there is a default timeout; with a button there is none.
  HelpBubbleParams params = GetBubbleParams();
  HelpBubbleButtonParams button_params;
  button_params.text = u"button";
  params.buttons.push_back(std::move(button_params));
  HelpBubbleView* const bubble = CreateHelpBubbleView(std::move(params));
  MockWidgetObserver dismiss_observer;
  EXPECT_CALL(dismiss_observer, OnWidgetClosing(testing::_)).Times(0);
  bubble->GetWidget()->AddObserver(&dismiss_observer);
  task_environment()->FastForwardBy(base::Minutes(1));
  task_environment()->RunUntilIdle();
  // WidgetObserver checks if it is in an observer list in its destructor.
  // Need to remove it from widget manually.
  bubble->GetWidget()->RemoveObserver(&dismiss_observer);
}

TEST_F(HelpBubbleViewTimeoutTest, TimeoutCallback) {
  base::MockRepeatingClosure timeout_callback;

  HelpBubbleParams params = GetBubbleParams();
  params.timeout = base::Seconds(10);
  params.timeout_callback = timeout_callback.Get();

  CreateHelpBubbleView(std::move(params));

  EXPECT_CALL(timeout_callback, Run()).Times(1);
  task_environment()->FastForwardBy(base::Seconds(10));
}

TEST_F(HelpBubbleViewTimeoutTest, NoTimeoutIfSetToZero) {
  base::MockRepeatingClosure timeout_callback;

  HelpBubbleParams params = GetBubbleParams();
  params.timeout = base::TimeDelta();
  params.timeout_callback = timeout_callback.Get();

  CreateHelpBubbleView(std::move(params));

  EXPECT_CALL(timeout_callback, Run()).Times(0);

  // Fast forward by a long time to check bubble does not time out.
  task_environment()->FastForwardBy(base::Hours(1));
}

TEST_F(HelpBubbleViewTimeoutTest, RespectsProvidedTimeoutBeforeActivate) {
  base::MockRepeatingClosure timeout_callback;

  HelpBubbleParams params = GetBubbleParams();
  params.timeout = base::Seconds(20);
  params.timeout_callback = timeout_callback.Get();

  CreateHelpBubbleView(std::move(params));

  EXPECT_CALL(timeout_callback, Run()).Times(0);
  task_environment()->FastForwardBy(base::Seconds(19));

  EXPECT_CALL(timeout_callback, Run()).Times(1);
  task_environment()->FastForwardBy(base::Seconds(1));
}

TEST_F(HelpBubbleViewTimeoutTest, RespectsProvidedTimeoutAfterActivate) {
  base::MockRepeatingClosure timeout_callback;

  HelpBubbleParams params = GetBubbleParams();
  params.timeout = base::Seconds(10);
  params.timeout_callback = timeout_callback.Get();

  EXPECT_CALL(timeout_callback, Run()).Times(0);

  TestHelpBubbleView* const bubble = CreateHelpBubbleView(std::move(params));

  task_environment()->FastForwardBy(base::Seconds(9));

  // Simulate bubble activation. We won't actually activate the bubble since
  // bubble visibility and activation don't work well in this mock environment.
  bubble->OnWidgetActivationChanged(bubble->GetWidget(), true);

  // The bubble should not time out since it is active.
  task_environment()->FastForwardBy(base::Seconds(4));

  // Deactivating the widget should restart the timer.
  bubble->OnWidgetActivationChanged(bubble->GetWidget(), false);

  // Wait most of the timeout, but not all of it.
  task_environment()->FastForwardBy(base::Seconds(9));

  EXPECT_CALL(timeout_callback, Run()).Times(1);

  // Finishing the timeout should dismiss the bubble.
  task_environment()->FastForwardBy(base::Seconds(1));
}