| 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_session_duration/desktop_session_durati
on_tracker.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" |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 while (!instance.is_timeout()) { | 142 while (!instance.is_timeout()) { |
| 143 base::RunLoop().RunUntilIdle(); | 143 base::RunLoop().RunUntilIdle(); |
| 144 } | 144 } |
| 145 | 145 |
| 146 EXPECT_FALSE(instance.in_session()); | 146 EXPECT_FALSE(instance.in_session()); |
| 147 EXPECT_FALSE(instance.is_visible()); | 147 EXPECT_FALSE(instance.is_visible()); |
| 148 EXPECT_FALSE(instance.is_audio_playing()); | 148 EXPECT_FALSE(instance.is_audio_playing()); |
| 149 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 149 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
| 150 } | 150 } |
| 151 | 151 |
| 152 // Flaky on Mac Debug. See http://crbug.com/646758 | 152 TEST(DesktopSessionDurationTrackerTest, TestTimeoutDiscount) { |
| 153 #if defined(OS_MACOSX) | |
| 154 #define MAYBE_TestTimeoutDiscount DISABLED_TestTimeoutDiscount | |
| 155 #else | |
| 156 #define MAYBE_TestTimeoutDiscount TestTimeoutDiscount | |
| 157 #endif | |
| 158 TEST(DesktopEngagementServiceTest, MAYBE_TestTimeoutDiscount) { | |
| 159 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | 153 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
| 160 base::HistogramTester histogram_tester; | 154 base::HistogramTester histogram_tester; |
| 161 MockDesktopSessionDurationTracker instance; | 155 MockDesktopSessionDurationTracker instance; |
| 162 | 156 |
| 163 int inactivity_interval = 2; | 157 int inactivity_interval_seconds = 2; |
| 164 instance.SetInactivityTimeoutForTesting(inactivity_interval); | 158 instance.SetInactivityTimeoutForTesting(inactivity_interval_seconds); |
| 165 | 159 |
| 166 instance.OnVisibilityChanged(true); | 160 instance.OnVisibilityChanged(true); |
| 167 base::TimeTicks before_session_start = base::TimeTicks::Now(); | 161 base::TimeTicks before_session_start = base::TimeTicks::Now(); |
| 168 instance.OnUserEvent(); // This should start the session | 162 instance.OnUserEvent(); // This should start the session |
| 169 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 163 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
| 170 | 164 |
| 171 // Wait until the session expires. | 165 // Wait until the session expires. |
| 172 while (!instance.is_timeout()) { | 166 while (!instance.is_timeout()) { |
| 173 base::RunLoop().RunUntilIdle(); | 167 base::RunLoop().RunUntilIdle(); |
| 174 } | 168 } |
| 175 base::TimeTicks after_session_end = base::TimeTicks::Now(); | 169 base::TimeTicks after_session_end = base::TimeTicks::Now(); |
| 176 | 170 |
| 177 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 171 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
| 178 // The recorded value should be shorter than the specified inactivity | |
| 179 // interval. | |
| 180 base::Bucket bucket = | 172 base::Bucket bucket = |
| 181 histogram_tester.GetAllSamples("Session.TotalDuration")[0]; | 173 histogram_tester.GetAllSamples("Session.TotalDuration")[0]; |
| 182 EXPECT_LE(bucket.min + inactivity_interval, | 174 int max_expected_value = |
| 183 (after_session_end - before_session_start).InSeconds()); | 175 (after_session_end - before_session_start - |
| 176 base::TimeDelta::FromSeconds(inactivity_interval_seconds)) |
| 177 .InMilliseconds(); |
| 178 EXPECT_LE(bucket.min, max_expected_value); |
| 184 } | 179 } |
| OLD | NEW |