Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: chrome/browser/metrics/desktop_engagement/desktop_engagement_service_unittest.cc

Issue 2142983002: Add desktop engagement metrics. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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
5 #include "chrome/browser/metrics/desktop_engagement/desktop_engagement_service.h "
6
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/test/histogram_tester.h"
10 #include "base/test/test_mock_time_task_runner.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 // Mock class for |DesktopEngagementService| for testing.
14 class MockDesktopEngagementService : public metrics::DesktopEngagementService {
15 public:
16 MockDesktopEngagementService() {}
17
18 bool is_timeout() const { return time_out_; }
19
20 protected:
21 void OnTimerFired() override {
22 DesktopEngagementService::OnTimerFired();
23 time_out_ = true;
24 }
25
26 private:
27 bool time_out_ = false;
28
29 DISALLOW_COPY_AND_ASSIGN(MockDesktopEngagementService);
30 };
31
32 TEST(DesktopEngagementServiceTest, TestVisibility) {
33 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
34 base::HistogramTester histogram_tester;
35
36 MockDesktopEngagementService instance;
37
38 // The browser becomes visible but it shouldn't start the session.
39 instance.OnVisibilityChanged(true);
40 EXPECT_FALSE(instance.in_session());
41 EXPECT_TRUE(instance.is_visible());
42 EXPECT_FALSE(instance.is_audio_playing());
43 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
44
45 instance.OnUserEvent();
46 EXPECT_TRUE(instance.in_session());
47 EXPECT_TRUE(instance.is_visible());
48 EXPECT_FALSE(instance.is_audio_playing());
49 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
50
51 // Even if there is a recent user event visibility change should end the
52 // session.
53 instance.OnUserEvent();
54 instance.OnUserEvent();
55 instance.OnVisibilityChanged(false);
56 EXPECT_FALSE(instance.in_session());
57 EXPECT_FALSE(instance.is_visible());
58 EXPECT_FALSE(instance.is_audio_playing());
59 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1);
60
61 // For the second time only visibility change should start the session.
62 instance.OnVisibilityChanged(true);
63 EXPECT_TRUE(instance.in_session());
64 EXPECT_TRUE(instance.is_visible());
65 EXPECT_FALSE(instance.is_audio_playing());
66 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1);
67 instance.OnVisibilityChanged(false);
68 EXPECT_FALSE(instance.in_session());
69 EXPECT_FALSE(instance.is_visible());
70 EXPECT_FALSE(instance.is_audio_playing());
71 histogram_tester.ExpectTotalCount("Session.TotalDuration", 2);
72 }
73
74 TEST(DesktopEngagementServiceTest, TestUserEvent) {
75 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
76 base::HistogramTester histogram_tester;
77
78 MockDesktopEngagementService instance;
79 instance.SetInactivityTimeoutForTesting(1);
80
81 EXPECT_FALSE(instance.in_session());
82 EXPECT_FALSE(instance.is_visible());
83 EXPECT_FALSE(instance.is_audio_playing());
84 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
85
86 // User event doesn't go through if nothing is visible.
87 instance.OnUserEvent();
88 EXPECT_FALSE(instance.in_session());
89 EXPECT_FALSE(instance.is_visible());
90 EXPECT_FALSE(instance.is_audio_playing());
91 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
92
93 instance.OnVisibilityChanged(true);
94 instance.OnUserEvent();
95 EXPECT_TRUE(instance.in_session());
96 EXPECT_TRUE(instance.is_visible());
97 EXPECT_FALSE(instance.is_audio_playing());
98 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
99
100 // Wait until the session expires.
101 while (!instance.is_timeout()) {
102 base::RunLoop().RunUntilIdle();
103 }
104
105 EXPECT_FALSE(instance.in_session());
106 EXPECT_TRUE(instance.is_visible());
107 EXPECT_FALSE(instance.is_audio_playing());
108 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1);
109 }
110
111 TEST(DesktopEngagementServiceTest, TestAudioEvent) {
112 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT);
113 base::HistogramTester histogram_tester;
114
115 MockDesktopEngagementService instance;
116 instance.SetInactivityTimeoutForTesting(1);
117
118 instance.OnVisibilityChanged(true);
119 instance.OnAudioStart();
120 EXPECT_TRUE(instance.in_session());
121 EXPECT_TRUE(instance.is_visible());
122 EXPECT_TRUE(instance.is_audio_playing());
123 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
124
125 instance.OnVisibilityChanged(false);
126 EXPECT_TRUE(instance.in_session());
127 EXPECT_FALSE(instance.is_visible());
128 EXPECT_TRUE(instance.is_audio_playing());
129 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
130
131 instance.OnAudioEnd();
132 EXPECT_TRUE(instance.in_session());
133 EXPECT_FALSE(instance.is_visible());
134 EXPECT_FALSE(instance.is_audio_playing());
135 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0);
136
137 // Wait until the session expires.
138 while (!instance.is_timeout()) {
139 base::RunLoop().RunUntilIdle();
140 }
141
142 EXPECT_FALSE(instance.in_session());
143 EXPECT_FALSE(instance.is_visible());
144 EXPECT_FALSE(instance.is_audio_playing());
145 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1);
146 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698