Don't preload rarely seen large images
[chromium-blink-merge.git] / ash / metrics / desktop_task_switch_metric_recorder_unittest.cc
blob59ec7f9eac0ba8e12def9f655c8278df78b749a8
1 // Copyright 2015 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.
5 #include "ash/metrics/desktop_task_switch_metric_recorder.h"
7 #include "ash/shell.h"
8 #include "ash/test/ash_test_base.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/test/user_action_tester.h"
11 #include "ui/aura/test/test_window_delegate.h"
12 #include "ui/aura/window.h"
13 #include "ui/compositor/layer_type.h"
14 #include "ui/events/test/event_generator.h"
15 #include "ui/wm/public/activation_client.h"
16 #include "ui/wm/public/window_types.h"
18 using aura::client::ActivationChangeObserver;
20 namespace ash {
21 namespace {
23 const char kDesktopTaskSwitchUserAction[] = "Desktop_SwitchTask";
25 // Test fixture for the DesktopTaskSwitchMetricsRecorder class. NOTE: This
26 // fixture extends AshTestBase so that the UserMetricsRecorder instance required
27 // by the test target can be obtained through Shell::GetInstance()->metrics()
28 // and the test target is not the same instance as the one owned by the
29 // UserMetricsRecorder instance.
30 class DesktopTaskSwitchMetricRecorderTest : public test::AshTestBase {
31 public:
32 DesktopTaskSwitchMetricRecorderTest();
33 ~DesktopTaskSwitchMetricRecorderTest() override;
35 // test::AshTestBase:
36 void SetUp() override;
37 void TearDown() override;
39 // Resets the recorded user action counts.
40 void ResetActionCounts();
42 // Returns the number of times the "Desktop_SwitchTask" user action was
43 // recorded.
44 int GetActionCount() const;
46 // Creates a positionable window such that wm::IsWindowUserPositionable(...)
47 // would retun true.
48 scoped_ptr<aura::Window> CreatePositionableWindow() const;
50 // Creates a non-positionable window such that
51 // wm::IsWindowUserPositionable(...) would retun false.
52 scoped_ptr<aura::Window> CreateNonPositionableWindow() const;
54 // Wrapper to notify the test target's OnWindowActivated(...) method that
55 // |window| was activated due to an INPUT_EVENT.
56 void ActiveTaskWindowWithUserInput(aura::Window* window);
58 protected:
59 // Records UMA user action counts.
60 scoped_ptr<base::UserActionTester> user_action_tester_;
62 // The test target.
63 scoped_ptr<DesktopTaskSwitchMetricRecorder> metrics_recorder_;
65 private:
66 DISALLOW_COPY_AND_ASSIGN(DesktopTaskSwitchMetricRecorderTest);
69 DesktopTaskSwitchMetricRecorderTest::DesktopTaskSwitchMetricRecorderTest() {
72 DesktopTaskSwitchMetricRecorderTest::~DesktopTaskSwitchMetricRecorderTest() {
75 void DesktopTaskSwitchMetricRecorderTest::SetUp() {
76 test::AshTestBase::SetUp();
77 metrics_recorder_.reset(new DesktopTaskSwitchMetricRecorder);
78 user_action_tester_.reset(new base::UserActionTester);
81 void DesktopTaskSwitchMetricRecorderTest::TearDown() {
82 user_action_tester_.reset();
83 metrics_recorder_.reset();
84 test::AshTestBase::TearDown();
87 void DesktopTaskSwitchMetricRecorderTest::ActiveTaskWindowWithUserInput(
88 aura::Window* window) {
89 metrics_recorder_->OnWindowActivated(
90 ActivationChangeObserver::ActivationReason::INPUT_EVENT, window, nullptr);
93 void DesktopTaskSwitchMetricRecorderTest::ResetActionCounts() {
94 user_action_tester_->ResetCounts();
97 int DesktopTaskSwitchMetricRecorderTest::GetActionCount() const {
98 return user_action_tester_->GetActionCount(kDesktopTaskSwitchUserAction);
101 scoped_ptr<aura::Window>
102 DesktopTaskSwitchMetricRecorderTest::CreatePositionableWindow() const {
103 scoped_ptr<aura::Window> window(new aura::Window(
104 aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate()));
105 window->SetType(ui::wm::WINDOW_TYPE_NORMAL);
106 window->Init(ui::LAYER_NOT_DRAWN);
107 return window.Pass();
110 scoped_ptr<aura::Window>
111 DesktopTaskSwitchMetricRecorderTest::CreateNonPositionableWindow() const {
112 scoped_ptr<aura::Window> window(new aura::Window(
113 aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate()));
114 window->SetType(ui::wm::WINDOW_TYPE_UNKNOWN);
115 window->Init(ui::LAYER_NOT_DRAWN);
116 return window.Pass();
119 // Verify user action is recorded when a positionable window is activated given
120 // that a null window was activated last.
121 TEST_F(DesktopTaskSwitchMetricRecorderTest,
122 ActivatePositionableWindowWhenNullWindowWasActivatedLast) {
123 scoped_ptr<aura::Window> null_window;
124 scoped_ptr<aura::Window> positionable_window =
125 CreatePositionableWindow().Pass();
127 ActiveTaskWindowWithUserInput(null_window.get());
128 ResetActionCounts();
130 ActiveTaskWindowWithUserInput(positionable_window.get());
131 EXPECT_EQ(1, GetActionCount());
134 // Verify user action is recorded whena positionable window is activated given
135 // a different positionable window was activated last.
136 TEST_F(
137 DesktopTaskSwitchMetricRecorderTest,
138 ActivatePositionableWindowWhenADifferentPositionableWindowWasActivatedLast) {
139 scoped_ptr<aura::Window> positionable_window_1 =
140 CreatePositionableWindow().Pass();
141 scoped_ptr<aura::Window> positionable_window_2 =
142 CreatePositionableWindow().Pass();
144 ActiveTaskWindowWithUserInput(positionable_window_1.get());
145 ResetActionCounts();
147 ActiveTaskWindowWithUserInput(positionable_window_2.get());
148 EXPECT_EQ(1, GetActionCount());
151 // Verify user action is not recorded when a positionable window is activated
152 // given the same positionable window was activated last.
153 TEST_F(
154 DesktopTaskSwitchMetricRecorderTest,
155 ActivatePositionableWindowWhenTheSamePositionableWindowWasActivatedLast) {
156 scoped_ptr<aura::Window> positionable_window =
157 CreatePositionableWindow().Pass();
159 ActiveTaskWindowWithUserInput(positionable_window.get());
160 ResetActionCounts();
162 ActiveTaskWindowWithUserInput(positionable_window.get());
163 EXPECT_EQ(0, GetActionCount());
166 // Verify user action is recorded when a positionable window is activated given
167 // a non-positionable window was activated last.
168 TEST_F(DesktopTaskSwitchMetricRecorderTest,
169 ActivatePositionableWindowWhenANonPositionableWindowWasActivatedLast) {
170 scoped_ptr<aura::Window> non_positionable_window =
171 CreateNonPositionableWindow().Pass();
172 scoped_ptr<aura::Window> positionable_window =
173 CreatePositionableWindow().Pass();
175 ActiveTaskWindowWithUserInput(non_positionable_window.get());
176 ResetActionCounts();
178 ActiveTaskWindowWithUserInput(positionable_window.get());
179 EXPECT_EQ(1, GetActionCount());
182 // Verify user action is not recorded when a non-positionable window is
183 // activated between two activations of the same positionable window.
184 TEST_F(DesktopTaskSwitchMetricRecorderTest,
185 ActivateNonPositionableWindowBetweenTwoPositionableWindowActivations) {
186 scoped_ptr<aura::Window> positionable_window =
187 CreatePositionableWindow().Pass();
188 scoped_ptr<aura::Window> non_positionable_window =
189 CreateNonPositionableWindow().Pass();
191 ActiveTaskWindowWithUserInput(positionable_window.get());
192 ResetActionCounts();
194 ActiveTaskWindowWithUserInput(non_positionable_window.get());
195 EXPECT_EQ(0, GetActionCount());
197 ActiveTaskWindowWithUserInput(positionable_window.get());
198 EXPECT_EQ(0, GetActionCount());
201 // Verify user action is not recorded when a null window is activated.
202 TEST_F(DesktopTaskSwitchMetricRecorderTest, ActivateNullWindow) {
203 scoped_ptr<aura::Window> positionable_window =
204 CreatePositionableWindow().Pass();
205 scoped_ptr<aura::Window> null_window = nullptr;
207 ActiveTaskWindowWithUserInput(positionable_window.get());
208 ResetActionCounts();
210 ActiveTaskWindowWithUserInput(null_window.get());
211 EXPECT_EQ(0, GetActionCount());
214 // Verify user action is not recorded when a non-positionable window is
215 // activated.
216 TEST_F(DesktopTaskSwitchMetricRecorderTest, ActivateNonPositionableWindow) {
217 scoped_ptr<aura::Window> positionable_window =
218 CreatePositionableWindow().Pass();
219 scoped_ptr<aura::Window> non_positionable_window =
220 CreateNonPositionableWindow().Pass();
222 ActiveTaskWindowWithUserInput(positionable_window.get());
223 ResetActionCounts();
225 ActiveTaskWindowWithUserInput(non_positionable_window.get());
226 EXPECT_EQ(0, GetActionCount());
229 // Verify user action is not recorded when the ActivationReason is not an
230 // INPUT_EVENT.
231 TEST_F(DesktopTaskSwitchMetricRecorderTest,
232 ActivatePositionableWindowWithNonInputEventReason) {
233 scoped_ptr<aura::Window> positionable_window_1 =
234 CreatePositionableWindow().Pass();
235 scoped_ptr<aura::Window> positionable_window_2 =
236 CreatePositionableWindow().Pass();
238 ActiveTaskWindowWithUserInput(positionable_window_1.get());
239 ResetActionCounts();
241 metrics_recorder_->OnWindowActivated(
242 ActivationChangeObserver::ActivationReason::ACTIVATION_CLIENT,
243 positionable_window_2.get(), nullptr);
244 EXPECT_EQ(0, GetActionCount());
247 // Test fixture to test the integration of the DesktopTaskSwitchMetricsRecorder
248 // class with ash::Shell environment.
249 class DesktopTaskSwitchMetricRecorderWithShellIntegrationTest
250 : public test::AshTestBase {
251 public:
252 DesktopTaskSwitchMetricRecorderWithShellIntegrationTest();
253 ~DesktopTaskSwitchMetricRecorderWithShellIntegrationTest() override;
255 // test::AshTestBase:
256 void SetUp() override;
257 void TearDown() override;
259 // Returns the number of times the "Desktop_SwitchTask" user action was
260 // recorded.
261 int GetActionCount() const;
263 // Creates a positionable window with the given |bounds| such that
264 // wm::IsWindowUserPositionable(...) would retun true.
265 aura::Window* CreatePositionableWindowInShellWithBounds(
266 const gfx::Rect& bounds);
268 protected:
269 // Records UMA user action counts.
270 scoped_ptr<base::UserActionTester> user_action_tester_;
272 // Delegate used when creating new windows using the
273 // CreatePositionableWindowInShellWithBounds(...) method.
274 aura::test::TestWindowDelegate test_window_delegate_;
276 private:
277 DISALLOW_COPY_AND_ASSIGN(
278 DesktopTaskSwitchMetricRecorderWithShellIntegrationTest);
281 DesktopTaskSwitchMetricRecorderWithShellIntegrationTest::
282 DesktopTaskSwitchMetricRecorderWithShellIntegrationTest() {
285 DesktopTaskSwitchMetricRecorderWithShellIntegrationTest::
286 ~DesktopTaskSwitchMetricRecorderWithShellIntegrationTest() {
289 void DesktopTaskSwitchMetricRecorderWithShellIntegrationTest::SetUp() {
290 test::AshTestBase::SetUp();
291 user_action_tester_.reset(new base::UserActionTester);
294 void DesktopTaskSwitchMetricRecorderWithShellIntegrationTest::TearDown() {
295 user_action_tester_.reset();
296 test::AshTestBase::TearDown();
299 int DesktopTaskSwitchMetricRecorderWithShellIntegrationTest::GetActionCount()
300 const {
301 return user_action_tester_->GetActionCount(kDesktopTaskSwitchUserAction);
304 aura::Window* DesktopTaskSwitchMetricRecorderWithShellIntegrationTest::
305 CreatePositionableWindowInShellWithBounds(const gfx::Rect& bounds) {
306 return CreateTestWindowInShellWithDelegate(&test_window_delegate_, 0, bounds);
309 // Verify a user action is recorded when a positionable window is activated by
310 // a INPUT_EVENT.
311 TEST_F(DesktopTaskSwitchMetricRecorderWithShellIntegrationTest,
312 ActivatePositionableWindowWithInputEvent) {
313 aura::Window* positionable_window =
314 CreatePositionableWindowInShellWithBounds(gfx::Rect(0, 0, 10, 10));
316 ui::test::EventGenerator event_generator(Shell::GetPrimaryRootWindow());
318 event_generator.MoveMouseToCenterOf(positionable_window);
319 event_generator.ClickLeftButton();
321 EXPECT_EQ(1, GetActionCount());
324 // Verify a user action is not recorded when a positionable window is activated
325 // by a non INPUT_EVENT.
326 TEST_F(DesktopTaskSwitchMetricRecorderWithShellIntegrationTest,
327 ActivatePositionableWindowWithNonInputEvent) {
328 aura::Window* positionable_window =
329 CreatePositionableWindowInShellWithBounds(gfx::Rect(0, 0, 10, 10));
331 Shell::GetInstance()->activation_client()->ActivateWindow(
332 positionable_window);
334 EXPECT_EQ(0, GetActionCount());
337 } // namespace
338 } // namespace ash