| 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_engagement/desktop_engagement_service.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 "base/threading/platform_thread.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 14 namespace { |
| 15 |
| 16 const base::TimeDelta kZeroTime = base::TimeDelta::FromSeconds(0); |
| 17 |
| 18 } // namespace |
| 19 |
| 20 |
| 13 // Mock class for |DesktopEngagementService| for testing. | 21 // Mock class for |DesktopEngagementService| for testing. |
| 14 class MockDesktopEngagementService : public metrics::DesktopEngagementService { | 22 class MockDesktopEngagementService : public metrics::DesktopEngagementService { |
| 15 public: | 23 public: |
| 16 MockDesktopEngagementService() {} | 24 MockDesktopEngagementService() {} |
| 17 | 25 |
| 18 bool is_timeout() const { return time_out_; } | 26 bool is_timeout() const { return time_out_; } |
| 19 | 27 |
| 20 using metrics::DesktopEngagementService::OnAudioStart; | 28 using metrics::DesktopEngagementService::OnAudioStart; |
| 21 using metrics::DesktopEngagementService::OnAudioEnd; | 29 using metrics::DesktopEngagementService::OnAudioEnd; |
| 22 | 30 |
| 23 protected: | 31 protected: |
| 24 void OnTimerFired() override { | 32 void OnTimerFired() override { |
| 25 DesktopEngagementService::OnTimerFired(); | 33 DesktopEngagementService::OnTimerFired(); |
| 26 time_out_ = true; | 34 time_out_ = true; |
| 27 } | 35 } |
| 28 | 36 |
| 29 private: | 37 private: |
| 30 bool time_out_ = false; | 38 bool time_out_ = false; |
| 31 | 39 |
| 32 DISALLOW_COPY_AND_ASSIGN(MockDesktopEngagementService); | 40 DISALLOW_COPY_AND_ASSIGN(MockDesktopEngagementService); |
| 33 }; | 41 }; |
| 34 | 42 |
| 35 TEST(DesktopEngagementServiceTest, TestVisibility) { | 43 class DesktopEngagementServiceTest : public testing::Test { |
| 36 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | 44 public: |
| 37 base::HistogramTester histogram_tester; | 45 DesktopEngagementServiceTest() |
| 46 : loop_(base::MessageLoop::TYPE_DEFAULT) {} |
| 38 | 47 |
| 39 MockDesktopEngagementService instance; | 48 void ExpectTotalDuration(base::TimeDelta duration) { |
| 49 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 1); |
| 50 base::Bucket bucket = |
| 51 histogram_tester_.GetAllSamples("Session.TotalDuration")[0]; |
| 52 int max_expected_value = duration.InMilliseconds(); |
| 53 EXPECT_LE(bucket.min, max_expected_value); |
| 54 } |
| 40 | 55 |
| 56 base::HistogramTester histogram_tester_; |
| 57 MockDesktopEngagementService instance_; |
| 58 private: |
| 59 base::MessageLoop loop_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(DesktopEngagementServiceTest); |
| 62 }; |
| 63 |
| 64 TEST_F(DesktopEngagementServiceTest, TestVisibility) { |
| 41 // The browser becomes visible but it shouldn't start the session. | 65 // The browser becomes visible but it shouldn't start the session. |
| 42 instance.OnVisibilityChanged(true); | 66 instance_.OnVisibilityChanged(true, kZeroTime); |
| 43 EXPECT_FALSE(instance.in_session()); | 67 EXPECT_FALSE(instance_.in_session()); |
| 44 EXPECT_TRUE(instance.is_visible()); | 68 EXPECT_TRUE(instance_.is_visible()); |
| 45 EXPECT_FALSE(instance.is_audio_playing()); | 69 EXPECT_FALSE(instance_.is_audio_playing()); |
| 46 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 70 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 0); |
| 47 | 71 |
| 48 instance.OnUserEvent(); | 72 instance_.OnUserEvent(); |
| 49 EXPECT_TRUE(instance.in_session()); | 73 EXPECT_TRUE(instance_.in_session()); |
| 50 EXPECT_TRUE(instance.is_visible()); | 74 EXPECT_TRUE(instance_.is_visible()); |
| 51 EXPECT_FALSE(instance.is_audio_playing()); | 75 EXPECT_FALSE(instance_.is_audio_playing()); |
| 52 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 76 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 0); |
| 53 | 77 |
| 54 // Even if there is a recent user event visibility change should end the | 78 // Even if there is a recent user event visibility change should end the |
| 55 // session. | 79 // session. |
| 56 instance.OnUserEvent(); | 80 instance_.OnUserEvent(); |
| 57 instance.OnUserEvent(); | 81 instance_.OnUserEvent(); |
| 58 instance.OnVisibilityChanged(false); | 82 instance_.OnVisibilityChanged(false, kZeroTime); |
| 59 EXPECT_FALSE(instance.in_session()); | 83 EXPECT_FALSE(instance_.in_session()); |
| 60 EXPECT_FALSE(instance.is_visible()); | 84 EXPECT_FALSE(instance_.is_visible()); |
| 61 EXPECT_FALSE(instance.is_audio_playing()); | 85 EXPECT_FALSE(instance_.is_audio_playing()); |
| 62 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 86 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 1); |
| 63 | 87 |
| 64 // For the second time only visibility change should start the session. | 88 // For the second time only visibility change should start the session. |
| 65 instance.OnVisibilityChanged(true); | 89 instance_.OnVisibilityChanged(true, kZeroTime); |
| 66 EXPECT_TRUE(instance.in_session()); | 90 EXPECT_TRUE(instance_.in_session()); |
| 67 EXPECT_TRUE(instance.is_visible()); | 91 EXPECT_TRUE(instance_.is_visible()); |
| 68 EXPECT_FALSE(instance.is_audio_playing()); | 92 EXPECT_FALSE(instance_.is_audio_playing()); |
| 69 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 93 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 1); |
| 70 instance.OnVisibilityChanged(false); | 94 instance_.OnVisibilityChanged(false, kZeroTime); |
| 71 EXPECT_FALSE(instance.in_session()); | 95 EXPECT_FALSE(instance_.in_session()); |
| 72 EXPECT_FALSE(instance.is_visible()); | 96 EXPECT_FALSE(instance_.is_visible()); |
| 73 EXPECT_FALSE(instance.is_audio_playing()); | 97 EXPECT_FALSE(instance_.is_audio_playing()); |
| 74 histogram_tester.ExpectTotalCount("Session.TotalDuration", 2); | 98 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 2); |
| 75 } | 99 } |
| 76 | 100 |
| 77 TEST(DesktopEngagementServiceTest, TestUserEvent) { | 101 TEST_F(DesktopEngagementServiceTest, TestUserEvent) { |
| 78 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | 102 instance_.SetInactivityTimeoutForTesting(1); |
| 79 base::HistogramTester histogram_tester; | |
| 80 | 103 |
| 81 MockDesktopEngagementService instance; | 104 EXPECT_FALSE(instance_.in_session()); |
| 82 instance.SetInactivityTimeoutForTesting(1); | 105 EXPECT_FALSE(instance_.is_visible()); |
| 83 | 106 EXPECT_FALSE(instance_.is_audio_playing()); |
| 84 EXPECT_FALSE(instance.in_session()); | 107 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 0); |
| 85 EXPECT_FALSE(instance.is_visible()); | |
| 86 EXPECT_FALSE(instance.is_audio_playing()); | |
| 87 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | |
| 88 | 108 |
| 89 // User event doesn't go through if nothing is visible. | 109 // User event doesn't go through if nothing is visible. |
| 90 instance.OnUserEvent(); | 110 instance_.OnUserEvent(); |
| 91 EXPECT_FALSE(instance.in_session()); | 111 EXPECT_FALSE(instance_.in_session()); |
| 92 EXPECT_FALSE(instance.is_visible()); | 112 EXPECT_FALSE(instance_.is_visible()); |
| 93 EXPECT_FALSE(instance.is_audio_playing()); | 113 EXPECT_FALSE(instance_.is_audio_playing()); |
| 94 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 114 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 0); |
| 95 | 115 |
| 96 instance.OnVisibilityChanged(true); | 116 instance_.OnVisibilityChanged(true, kZeroTime); |
| 97 instance.OnUserEvent(); | 117 instance_.OnUserEvent(); |
| 98 EXPECT_TRUE(instance.in_session()); | 118 EXPECT_TRUE(instance_.in_session()); |
| 99 EXPECT_TRUE(instance.is_visible()); | 119 EXPECT_TRUE(instance_.is_visible()); |
| 100 EXPECT_FALSE(instance.is_audio_playing()); | 120 EXPECT_FALSE(instance_.is_audio_playing()); |
| 101 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 121 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 0); |
| 102 | 122 |
| 103 // Wait until the session expires. | 123 // Wait until the session expires. |
| 104 while (!instance.is_timeout()) { | 124 while (!instance_.is_timeout()) { |
| 105 base::RunLoop().RunUntilIdle(); | 125 base::RunLoop().RunUntilIdle(); |
| 106 } | 126 } |
| 107 | 127 |
| 108 EXPECT_FALSE(instance.in_session()); | 128 EXPECT_FALSE(instance_.in_session()); |
| 109 EXPECT_TRUE(instance.is_visible()); | 129 EXPECT_TRUE(instance_.is_visible()); |
| 110 EXPECT_FALSE(instance.is_audio_playing()); | 130 EXPECT_FALSE(instance_.is_audio_playing()); |
| 111 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 131 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 1); |
| 112 } | 132 } |
| 113 | 133 |
| 114 TEST(DesktopEngagementServiceTest, TestAudioEvent) { | 134 TEST_F(DesktopEngagementServiceTest, TestAudioEvent) { |
| 115 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | 135 instance_.SetInactivityTimeoutForTesting(1); |
| 116 base::HistogramTester histogram_tester; | |
| 117 | 136 |
| 118 MockDesktopEngagementService instance; | 137 instance_.OnVisibilityChanged(true, kZeroTime); |
| 119 instance.SetInactivityTimeoutForTesting(1); | 138 instance_.OnAudioStart(); |
| 139 EXPECT_TRUE(instance_.in_session()); |
| 140 EXPECT_TRUE(instance_.is_visible()); |
| 141 EXPECT_TRUE(instance_.is_audio_playing()); |
| 142 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 0); |
| 120 | 143 |
| 121 instance.OnVisibilityChanged(true); | 144 instance_.OnVisibilityChanged(false, kZeroTime); |
| 122 instance.OnAudioStart(); | 145 EXPECT_TRUE(instance_.in_session()); |
| 123 EXPECT_TRUE(instance.in_session()); | 146 EXPECT_FALSE(instance_.is_visible()); |
| 124 EXPECT_TRUE(instance.is_visible()); | 147 EXPECT_TRUE(instance_.is_audio_playing()); |
| 125 EXPECT_TRUE(instance.is_audio_playing()); | 148 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 0); |
| 126 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | |
| 127 | 149 |
| 128 instance.OnVisibilityChanged(false); | 150 instance_.OnAudioEnd(); |
| 129 EXPECT_TRUE(instance.in_session()); | 151 EXPECT_TRUE(instance_.in_session()); |
| 130 EXPECT_FALSE(instance.is_visible()); | 152 EXPECT_FALSE(instance_.is_visible()); |
| 131 EXPECT_TRUE(instance.is_audio_playing()); | 153 EXPECT_FALSE(instance_.is_audio_playing()); |
| 132 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 154 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 | 155 |
| 140 // Wait until the session expires. | 156 // Wait until the session expires. |
| 141 while (!instance.is_timeout()) { | 157 while (!instance_.is_timeout()) { |
| 142 base::RunLoop().RunUntilIdle(); | 158 base::RunLoop().RunUntilIdle(); |
| 143 } | 159 } |
| 144 | 160 |
| 145 EXPECT_FALSE(instance.in_session()); | 161 EXPECT_FALSE(instance_.in_session()); |
| 146 EXPECT_FALSE(instance.is_visible()); | 162 EXPECT_FALSE(instance_.is_visible()); |
| 147 EXPECT_FALSE(instance.is_audio_playing()); | 163 EXPECT_FALSE(instance_.is_audio_playing()); |
| 148 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 164 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 1); |
| 149 } | 165 } |
| 166 |
| 167 TEST_F(DesktopEngagementServiceTest, TestInputTimeoutDiscount) { |
| 168 int inactivity_interval_seconds = 2; |
| 169 instance_.SetInactivityTimeoutForTesting(inactivity_interval_seconds); |
| 170 |
| 171 instance_.OnVisibilityChanged(true, kZeroTime); |
| 172 base::TimeTicks before_session_start = base::TimeTicks::Now(); |
| 173 instance_.OnUserEvent(); // This should start the session |
| 174 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 0); |
| 175 |
| 176 // Wait until the session expires. |
| 177 while (!instance_.is_timeout()) { |
| 178 base::RunLoop().RunUntilIdle(); |
| 179 } |
| 180 base::TimeTicks after_session_end = base::TimeTicks::Now(); |
| 181 |
| 182 ExpectTotalDuration( |
| 183 after_session_end - before_session_start - |
| 184 base::TimeDelta::FromSeconds(inactivity_interval_seconds)); |
| 185 } |
| 186 |
| 187 TEST_F(DesktopEngagementServiceTest, TestVisibilityTimeoutDiscount) { |
| 188 instance_.OnVisibilityChanged(true, kZeroTime); |
| 189 base::TimeTicks before_session_start = base::TimeTicks::Now(); |
| 190 instance_.OnUserEvent(); // This should start the session |
| 191 histogram_tester_.ExpectTotalCount("Session.TotalDuration", 0); |
| 192 |
| 193 // Sleep a little while. |
| 194 base::TimeDelta kDelay = base::TimeDelta::FromSeconds(2); |
| 195 while (true) { |
| 196 base::TimeDelta elapsed = base::TimeTicks::Now() - before_session_start; |
| 197 if (elapsed >= kDelay) |
| 198 break; |
| 199 base::PlatformThread::Sleep(kDelay); |
| 200 } |
| 201 |
| 202 // End the session via visibility change. |
| 203 instance_.OnVisibilityChanged(false, kDelay); |
| 204 base::TimeTicks after_session_end = base::TimeTicks::Now(); |
| 205 |
| 206 ExpectTotalDuration(after_session_end - before_session_start - kDelay); |
| 207 } |
| OLD | NEW |