OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/metrics/desktop_engagement/desktop_engagement_service.h
" | 5 #include "chrome/browser/metrics/desktop_session_duration/desktop_session_durati
on_tracker.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/test/histogram_tester.h" | 9 #include "base/test/histogram_tester.h" |
10 #include "base/test/test_mock_time_task_runner.h" | 10 #include "base/test/test_mock_time_task_runner.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 // Mock class for |DesktopEngagementService| for testing. | 13 // Mock class for |DesktopSessionDurationTracker| for testing. |
14 class MockDesktopEngagementService : public metrics::DesktopEngagementService { | 14 class MockDesktopSessionDurationTracker |
| 15 : public metrics::DesktopSessionDurationTracker { |
15 public: | 16 public: |
16 MockDesktopEngagementService() {} | 17 MockDesktopSessionDurationTracker() {} |
17 | 18 |
18 bool is_timeout() const { return time_out_; } | 19 bool is_timeout() const { return time_out_; } |
19 | 20 |
20 using metrics::DesktopEngagementService::OnAudioStart; | 21 using metrics::DesktopSessionDurationTracker::OnAudioStart; |
21 using metrics::DesktopEngagementService::OnAudioEnd; | 22 using metrics::DesktopSessionDurationTracker::OnAudioEnd; |
22 | 23 |
23 protected: | 24 protected: |
24 void OnTimerFired() override { | 25 void OnTimerFired() override { |
25 DesktopEngagementService::OnTimerFired(); | 26 DesktopSessionDurationTracker::OnTimerFired(); |
26 time_out_ = true; | 27 time_out_ = true; |
27 } | 28 } |
28 | 29 |
29 private: | 30 private: |
30 bool time_out_ = false; | 31 bool time_out_ = false; |
31 | 32 |
32 DISALLOW_COPY_AND_ASSIGN(MockDesktopEngagementService); | 33 DISALLOW_COPY_AND_ASSIGN(MockDesktopSessionDurationTracker); |
33 }; | 34 }; |
34 | 35 |
35 TEST(DesktopEngagementServiceTest, TestVisibility) { | 36 TEST(DesktopSessionDurationTrackerTest, TestVisibility) { |
36 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | 37 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
37 base::HistogramTester histogram_tester; | 38 base::HistogramTester histogram_tester; |
38 | 39 |
39 MockDesktopEngagementService instance; | 40 MockDesktopSessionDurationTracker instance; |
40 | 41 |
41 // The browser becomes visible but it shouldn't start the session. | 42 // The browser becomes visible but it shouldn't start the session. |
42 instance.OnVisibilityChanged(true); | 43 instance.OnVisibilityChanged(true); |
43 EXPECT_FALSE(instance.in_session()); | 44 EXPECT_FALSE(instance.in_session()); |
44 EXPECT_TRUE(instance.is_visible()); | 45 EXPECT_TRUE(instance.is_visible()); |
45 EXPECT_FALSE(instance.is_audio_playing()); | 46 EXPECT_FALSE(instance.is_audio_playing()); |
46 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 47 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
47 | 48 |
48 instance.OnUserEvent(); | 49 instance.OnUserEvent(); |
49 EXPECT_TRUE(instance.in_session()); | 50 EXPECT_TRUE(instance.in_session()); |
(...skipping 17 matching lines...) Expand all Loading... |
67 EXPECT_TRUE(instance.is_visible()); | 68 EXPECT_TRUE(instance.is_visible()); |
68 EXPECT_FALSE(instance.is_audio_playing()); | 69 EXPECT_FALSE(instance.is_audio_playing()); |
69 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 70 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
70 instance.OnVisibilityChanged(false); | 71 instance.OnVisibilityChanged(false); |
71 EXPECT_FALSE(instance.in_session()); | 72 EXPECT_FALSE(instance.in_session()); |
72 EXPECT_FALSE(instance.is_visible()); | 73 EXPECT_FALSE(instance.is_visible()); |
73 EXPECT_FALSE(instance.is_audio_playing()); | 74 EXPECT_FALSE(instance.is_audio_playing()); |
74 histogram_tester.ExpectTotalCount("Session.TotalDuration", 2); | 75 histogram_tester.ExpectTotalCount("Session.TotalDuration", 2); |
75 } | 76 } |
76 | 77 |
77 TEST(DesktopEngagementServiceTest, TestUserEvent) { | 78 TEST(DesktopSessionDurationTrackerTest, TestUserEvent) { |
78 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | 79 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
79 base::HistogramTester histogram_tester; | 80 base::HistogramTester histogram_tester; |
80 | 81 |
81 MockDesktopEngagementService instance; | 82 MockDesktopSessionDurationTracker instance; |
82 instance.SetInactivityTimeoutForTesting(1); | 83 instance.SetInactivityTimeoutForTesting(1); |
83 | 84 |
84 EXPECT_FALSE(instance.in_session()); | 85 EXPECT_FALSE(instance.in_session()); |
85 EXPECT_FALSE(instance.is_visible()); | 86 EXPECT_FALSE(instance.is_visible()); |
86 EXPECT_FALSE(instance.is_audio_playing()); | 87 EXPECT_FALSE(instance.is_audio_playing()); |
87 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 88 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
88 | 89 |
89 // User event doesn't go through if nothing is visible. | 90 // User event doesn't go through if nothing is visible. |
90 instance.OnUserEvent(); | 91 instance.OnUserEvent(); |
91 EXPECT_FALSE(instance.in_session()); | 92 EXPECT_FALSE(instance.in_session()); |
(...skipping 12 matching lines...) Expand all Loading... |
104 while (!instance.is_timeout()) { | 105 while (!instance.is_timeout()) { |
105 base::RunLoop().RunUntilIdle(); | 106 base::RunLoop().RunUntilIdle(); |
106 } | 107 } |
107 | 108 |
108 EXPECT_FALSE(instance.in_session()); | 109 EXPECT_FALSE(instance.in_session()); |
109 EXPECT_TRUE(instance.is_visible()); | 110 EXPECT_TRUE(instance.is_visible()); |
110 EXPECT_FALSE(instance.is_audio_playing()); | 111 EXPECT_FALSE(instance.is_audio_playing()); |
111 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 112 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
112 } | 113 } |
113 | 114 |
114 TEST(DesktopEngagementServiceTest, TestAudioEvent) { | 115 TEST(DesktopSessionDurationTrackerTest, TestAudioEvent) { |
115 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | 116 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
116 base::HistogramTester histogram_tester; | 117 base::HistogramTester histogram_tester; |
117 | 118 |
118 MockDesktopEngagementService instance; | 119 MockDesktopSessionDurationTracker instance; |
119 instance.SetInactivityTimeoutForTesting(1); | 120 instance.SetInactivityTimeoutForTesting(1); |
120 | 121 |
121 instance.OnVisibilityChanged(true); | 122 instance.OnVisibilityChanged(true); |
122 instance.OnAudioStart(); | 123 instance.OnAudioStart(); |
123 EXPECT_TRUE(instance.in_session()); | 124 EXPECT_TRUE(instance.in_session()); |
124 EXPECT_TRUE(instance.is_visible()); | 125 EXPECT_TRUE(instance.is_visible()); |
125 EXPECT_TRUE(instance.is_audio_playing()); | 126 EXPECT_TRUE(instance.is_audio_playing()); |
126 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 127 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
127 | 128 |
128 instance.OnVisibilityChanged(false); | 129 instance.OnVisibilityChanged(false); |
(...skipping 15 matching lines...) Expand all Loading... |
144 | 145 |
145 EXPECT_FALSE(instance.in_session()); | 146 EXPECT_FALSE(instance.in_session()); |
146 EXPECT_FALSE(instance.is_visible()); | 147 EXPECT_FALSE(instance.is_visible()); |
147 EXPECT_FALSE(instance.is_audio_playing()); | 148 EXPECT_FALSE(instance.is_audio_playing()); |
148 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 149 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
149 } | 150 } |
150 | 151 |
151 TEST(DesktopEngagementServiceTest, TestTimeoutDiscount) { | 152 TEST(DesktopEngagementServiceTest, TestTimeoutDiscount) { |
152 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | 153 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
153 base::HistogramTester histogram_tester; | 154 base::HistogramTester histogram_tester; |
154 MockDesktopEngagementService instance; | 155 MockDesktopSessionDurationTracker instance; |
155 | 156 |
156 int inactivity_interval = 2; | 157 int inactivity_interval = 2; |
157 instance.SetInactivityTimeoutForTesting(inactivity_interval); | 158 instance.SetInactivityTimeoutForTesting(inactivity_interval); |
158 | 159 |
159 instance.OnVisibilityChanged(true); | 160 instance.OnVisibilityChanged(true); |
160 base::TimeTicks before_session_start = base::TimeTicks::Now(); | 161 base::TimeTicks before_session_start = base::TimeTicks::Now(); |
161 instance.OnUserEvent(); // This should start the session | 162 instance.OnUserEvent(); // This should start the session |
162 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 163 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
163 | 164 |
164 // Wait until the session expires. | 165 // Wait until the session expires. |
165 while (!instance.is_timeout()) { | 166 while (!instance.is_timeout()) { |
166 base::RunLoop().RunUntilIdle(); | 167 base::RunLoop().RunUntilIdle(); |
167 } | 168 } |
168 base::TimeTicks after_session_end = base::TimeTicks::Now(); | 169 base::TimeTicks after_session_end = base::TimeTicks::Now(); |
169 | 170 |
170 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 171 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
171 // The recorded value should be shorter than the specified inactivity | 172 // The recorded value should be shorter than the specified inactivity |
172 // interval. | 173 // interval. |
173 base::Bucket bucket = | 174 base::Bucket bucket = |
174 histogram_tester.GetAllSamples("Session.TotalDuration")[0]; | 175 histogram_tester.GetAllSamples("Session.TotalDuration")[0]; |
175 EXPECT_LE( | 176 EXPECT_LE(bucket.min + inactivity_interval, |
176 bucket.min + inactivity_interval, | 177 (after_session_end - before_session_start).InSeconds()); |
177 (after_session_end - before_session_start).InSeconds()); | |
178 } | 178 } |
OLD | NEW |