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