| 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_visible() const { return is_visible_; } |
| 19 bool in_session() const { return in_session_; } |
| 20 bool is_audio_playing() const { return is_audio_playing_; } |
| 21 bool is_timeout() const { return time_out_; } |
| 22 |
| 23 void SetInactivityTimeout(int seconds) { |
| 24 inactivity_timeout_ = base::TimeDelta::FromSeconds(seconds); |
| 25 } |
| 26 |
| 27 protected: |
| 28 void OnTimerFired() override { |
| 29 DesktopEngagementService::OnTimerFired(); |
| 30 time_out_ = true; |
| 31 } |
| 32 |
| 33 private: |
| 34 bool time_out_ = false; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(MockDesktopEngagementService); |
| 37 }; |
| 38 |
| 39 TEST(DesktopEngagementServiceTest, TestVisibility) { |
| 40 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
| 41 base::HistogramTester histogram_tester; |
| 42 |
| 43 MockDesktopEngagementService instance; |
| 44 |
| 45 // The browser becomes visible but it shouldn't start the session. |
| 46 instance.OnVisibilityChanged(true); |
| 47 EXPECT_FALSE(instance.in_session()); |
| 48 EXPECT_TRUE(instance.is_visible()); |
| 49 EXPECT_FALSE(instance.is_audio_playing()); |
| 50 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
| 51 |
| 52 instance.OnUserEvent(); |
| 53 EXPECT_TRUE(instance.in_session()); |
| 54 EXPECT_TRUE(instance.is_visible()); |
| 55 EXPECT_FALSE(instance.is_audio_playing()); |
| 56 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
| 57 |
| 58 // Even if there is a recent user event visibility change should end the |
| 59 // session. |
| 60 instance.OnUserEvent(); |
| 61 instance.OnUserEvent(); |
| 62 instance.OnVisibilityChanged(false); |
| 63 EXPECT_FALSE(instance.in_session()); |
| 64 EXPECT_FALSE(instance.is_visible()); |
| 65 EXPECT_FALSE(instance.is_audio_playing()); |
| 66 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
| 67 |
| 68 // For the second time only visibility change should start the session. |
| 69 instance.OnVisibilityChanged(true); |
| 70 EXPECT_TRUE(instance.in_session()); |
| 71 EXPECT_TRUE(instance.is_visible()); |
| 72 EXPECT_FALSE(instance.is_audio_playing()); |
| 73 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
| 74 instance.OnVisibilityChanged(false); |
| 75 EXPECT_FALSE(instance.in_session()); |
| 76 EXPECT_FALSE(instance.is_visible()); |
| 77 EXPECT_FALSE(instance.is_audio_playing()); |
| 78 histogram_tester.ExpectTotalCount("Session.TotalDuration", 2); |
| 79 } |
| 80 |
| 81 TEST(DesktopEngagementServiceTest, TestUserEvent) { |
| 82 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
| 83 base::HistogramTester histogram_tester; |
| 84 |
| 85 MockDesktopEngagementService instance; |
| 86 instance.SetInactivityTimeout(1); |
| 87 |
| 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 // User event doesn't go through if nothing is visible. |
| 94 instance.OnUserEvent(); |
| 95 EXPECT_FALSE(instance.in_session()); |
| 96 EXPECT_FALSE(instance.is_visible()); |
| 97 EXPECT_FALSE(instance.is_audio_playing()); |
| 98 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
| 99 |
| 100 instance.OnVisibilityChanged(true); |
| 101 instance.OnUserEvent(); |
| 102 EXPECT_TRUE(instance.in_session()); |
| 103 EXPECT_TRUE(instance.is_visible()); |
| 104 EXPECT_FALSE(instance.is_audio_playing()); |
| 105 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
| 106 |
| 107 // Wait until the session expires. |
| 108 while (!instance.is_timeout()) { |
| 109 base::RunLoop().RunUntilIdle(); |
| 110 } |
| 111 |
| 112 EXPECT_FALSE(instance.in_session()); |
| 113 EXPECT_TRUE(instance.is_visible()); |
| 114 EXPECT_FALSE(instance.is_audio_playing()); |
| 115 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
| 116 } |
| 117 |
| 118 TEST(DesktopEngagementServiceTest, TestAudioEvent) { |
| 119 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
| 120 base::HistogramTester histogram_tester; |
| 121 |
| 122 MockDesktopEngagementService instance; |
| 123 instance.SetInactivityTimeout(1); |
| 124 |
| 125 instance.OnVisibilityChanged(true); |
| 126 instance.OnAudioStart(); |
| 127 EXPECT_TRUE(instance.in_session()); |
| 128 EXPECT_TRUE(instance.is_visible()); |
| 129 EXPECT_TRUE(instance.is_audio_playing()); |
| 130 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
| 131 |
| 132 instance.OnVisibilityChanged(false); |
| 133 EXPECT_TRUE(instance.in_session()); |
| 134 EXPECT_FALSE(instance.is_visible()); |
| 135 EXPECT_TRUE(instance.is_audio_playing()); |
| 136 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
| 137 |
| 138 instance.OnAudioEnd(); |
| 139 EXPECT_TRUE(instance.in_session()); |
| 140 EXPECT_FALSE(instance.is_visible()); |
| 141 EXPECT_FALSE(instance.is_audio_playing()); |
| 142 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
| 143 |
| 144 // Wait until the session expires. |
| 145 while (!instance.is_timeout()) { |
| 146 base::RunLoop().RunUntilIdle(); |
| 147 } |
| 148 |
| 149 EXPECT_FALSE(instance.in_session()); |
| 150 EXPECT_FALSE(instance.is_visible()); |
| 151 EXPECT_FALSE(instance.is_audio_playing()); |
| 152 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
| 153 } |
| OLD | NEW |