OLD | NEW |
| (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 using metrics::DesktopEngagementService::OnAudioStart; | |
21 using metrics::DesktopEngagementService::OnAudioEnd; | |
22 | |
23 protected: | |
24 void OnTimerFired() override { | |
25 DesktopEngagementService::OnTimerFired(); | |
26 time_out_ = true; | |
27 } | |
28 | |
29 private: | |
30 bool time_out_ = false; | |
31 | |
32 DISALLOW_COPY_AND_ASSIGN(MockDesktopEngagementService); | |
33 }; | |
34 | |
35 TEST(DesktopEngagementServiceTest, TestVisibility) { | |
36 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | |
37 base::HistogramTester histogram_tester; | |
38 | |
39 MockDesktopEngagementService instance; | |
40 | |
41 // The browser becomes visible but it shouldn't start the session. | |
42 instance.OnVisibilityChanged(true); | |
43 EXPECT_FALSE(instance.in_session()); | |
44 EXPECT_TRUE(instance.is_visible()); | |
45 EXPECT_FALSE(instance.is_audio_playing()); | |
46 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | |
47 | |
48 instance.OnUserEvent(); | |
49 EXPECT_TRUE(instance.in_session()); | |
50 EXPECT_TRUE(instance.is_visible()); | |
51 EXPECT_FALSE(instance.is_audio_playing()); | |
52 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | |
53 | |
54 // Even if there is a recent user event visibility change should end the | |
55 // session. | |
56 instance.OnUserEvent(); | |
57 instance.OnUserEvent(); | |
58 instance.OnVisibilityChanged(false); | |
59 EXPECT_FALSE(instance.in_session()); | |
60 EXPECT_FALSE(instance.is_visible()); | |
61 EXPECT_FALSE(instance.is_audio_playing()); | |
62 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | |
63 | |
64 // For the second time only visibility change should start the session. | |
65 instance.OnVisibilityChanged(true); | |
66 EXPECT_TRUE(instance.in_session()); | |
67 EXPECT_TRUE(instance.is_visible()); | |
68 EXPECT_FALSE(instance.is_audio_playing()); | |
69 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | |
70 instance.OnVisibilityChanged(false); | |
71 EXPECT_FALSE(instance.in_session()); | |
72 EXPECT_FALSE(instance.is_visible()); | |
73 EXPECT_FALSE(instance.is_audio_playing()); | |
74 histogram_tester.ExpectTotalCount("Session.TotalDuration", 2); | |
75 } | |
76 | |
77 TEST(DesktopEngagementServiceTest, TestUserEvent) { | |
78 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | |
79 base::HistogramTester histogram_tester; | |
80 | |
81 MockDesktopEngagementService instance; | |
82 instance.SetInactivityTimeoutForTesting(1); | |
83 | |
84 EXPECT_FALSE(instance.in_session()); | |
85 EXPECT_FALSE(instance.is_visible()); | |
86 EXPECT_FALSE(instance.is_audio_playing()); | |
87 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | |
88 | |
89 // User event doesn't go through if nothing is visible. | |
90 instance.OnUserEvent(); | |
91 EXPECT_FALSE(instance.in_session()); | |
92 EXPECT_FALSE(instance.is_visible()); | |
93 EXPECT_FALSE(instance.is_audio_playing()); | |
94 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | |
95 | |
96 instance.OnVisibilityChanged(true); | |
97 instance.OnUserEvent(); | |
98 EXPECT_TRUE(instance.in_session()); | |
99 EXPECT_TRUE(instance.is_visible()); | |
100 EXPECT_FALSE(instance.is_audio_playing()); | |
101 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | |
102 | |
103 // Wait until the session expires. | |
104 while (!instance.is_timeout()) { | |
105 base::RunLoop().RunUntilIdle(); | |
106 } | |
107 | |
108 EXPECT_FALSE(instance.in_session()); | |
109 EXPECT_TRUE(instance.is_visible()); | |
110 EXPECT_FALSE(instance.is_audio_playing()); | |
111 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | |
112 } | |
113 | |
114 TEST(DesktopEngagementServiceTest, TestAudioEvent) { | |
115 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | |
116 base::HistogramTester histogram_tester; | |
117 | |
118 MockDesktopEngagementService instance; | |
119 instance.SetInactivityTimeoutForTesting(1); | |
120 | |
121 instance.OnVisibilityChanged(true); | |
122 instance.OnAudioStart(); | |
123 EXPECT_TRUE(instance.in_session()); | |
124 EXPECT_TRUE(instance.is_visible()); | |
125 EXPECT_TRUE(instance.is_audio_playing()); | |
126 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | |
127 | |
128 instance.OnVisibilityChanged(false); | |
129 EXPECT_TRUE(instance.in_session()); | |
130 EXPECT_FALSE(instance.is_visible()); | |
131 EXPECT_TRUE(instance.is_audio_playing()); | |
132 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | |
133 | |
134 instance.OnAudioEnd(); | |
135 EXPECT_TRUE(instance.in_session()); | |
136 EXPECT_FALSE(instance.is_visible()); | |
137 EXPECT_FALSE(instance.is_audio_playing()); | |
138 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | |
139 | |
140 // Wait until the session expires. | |
141 while (!instance.is_timeout()) { | |
142 base::RunLoop().RunUntilIdle(); | |
143 } | |
144 | |
145 EXPECT_FALSE(instance.in_session()); | |
146 EXPECT_FALSE(instance.is_visible()); | |
147 EXPECT_FALSE(instance.is_audio_playing()); | |
148 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | |
149 } | |
150 | |
151 TEST(DesktopEngagementServiceTest, TestTimeoutDiscount) { | |
152 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | |
153 base::HistogramTester histogram_tester; | |
154 MockDesktopEngagementService instance; | |
155 | |
156 int inactivity_interval = 2; | |
157 instance.SetInactivityTimeoutForTesting(inactivity_interval); | |
158 | |
159 instance.OnVisibilityChanged(true); | |
160 base::TimeTicks before_session_start = base::TimeTicks::Now(); | |
161 instance.OnUserEvent(); // This should start the session | |
162 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | |
163 | |
164 // Wait until the session expires. | |
165 while (!instance.is_timeout()) { | |
166 base::RunLoop().RunUntilIdle(); | |
167 } | |
168 base::TimeTicks after_session_end = base::TimeTicks::Now(); | |
169 | |
170 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | |
171 // The recorded value should be shorter than the specified inactivity | |
172 // interval. | |
173 base::Bucket bucket = | |
174 histogram_tester.GetAllSamples("Session.TotalDuration")[0]; | |
175 EXPECT_LE( | |
176 bucket.min + inactivity_interval, | |
177 (after_session_end - before_session_start).InSeconds()); | |
178 } | |
OLD | NEW |