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