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_session/desktop_session_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 "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 // Mock class for |DesktopEngagementService| for testing. | 13 // Mock class for |DesktopSessionService| for testing. |
14 class MockDesktopEngagementService : public metrics::DesktopEngagementService { | 14 class MockDesktopSessionService : public metrics::DesktopSessionService { |
15 public: | 15 public: |
16 MockDesktopEngagementService() {} | 16 MockDesktopSessionService() {} |
17 | 17 |
18 bool is_timeout() const { return time_out_; } | 18 bool is_timeout() const { return time_out_; } |
19 | 19 |
20 using metrics::DesktopEngagementService::OnAudioStart; | 20 using metrics::DesktopSessionService::OnAudioStart; |
21 using metrics::DesktopEngagementService::OnAudioEnd; | 21 using metrics::DesktopSessionService::OnAudioEnd; |
22 | 22 |
23 protected: | 23 protected: |
24 void OnTimerFired() override { | 24 void OnTimerFired() override { |
25 DesktopEngagementService::OnTimerFired(); | 25 DesktopSessionService::OnTimerFired(); |
26 time_out_ = true; | 26 time_out_ = true; |
27 } | 27 } |
28 | 28 |
29 private: | 29 private: |
30 bool time_out_ = false; | 30 bool time_out_ = false; |
31 | 31 |
32 DISALLOW_COPY_AND_ASSIGN(MockDesktopEngagementService); | 32 DISALLOW_COPY_AND_ASSIGN(MockDesktopSessionService); |
33 }; | 33 }; |
34 | 34 |
35 TEST(DesktopEngagementServiceTest, TestVisibility) { | 35 TEST(DesktopSessionServiceTest, TestVisibility) { |
36 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | 36 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
37 base::HistogramTester histogram_tester; | 37 base::HistogramTester histogram_tester; |
38 | 38 |
39 MockDesktopEngagementService instance; | 39 MockDesktopSessionService instance; |
40 | 40 |
41 // The browser becomes visible but it shouldn't start the session. | 41 // The browser becomes visible but it shouldn't start the session. |
42 instance.OnVisibilityChanged(true); | 42 instance.OnVisibilityChanged(true); |
43 EXPECT_FALSE(instance.in_session()); | 43 EXPECT_FALSE(instance.in_session()); |
44 EXPECT_TRUE(instance.is_visible()); | 44 EXPECT_TRUE(instance.is_visible()); |
45 EXPECT_FALSE(instance.is_audio_playing()); | 45 EXPECT_FALSE(instance.is_audio_playing()); |
46 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 46 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
47 | 47 |
48 instance.OnUserEvent(); | 48 instance.OnUserEvent(); |
49 EXPECT_TRUE(instance.in_session()); | 49 EXPECT_TRUE(instance.in_session()); |
(...skipping 17 matching lines...) Expand all Loading... |
67 EXPECT_TRUE(instance.is_visible()); | 67 EXPECT_TRUE(instance.is_visible()); |
68 EXPECT_FALSE(instance.is_audio_playing()); | 68 EXPECT_FALSE(instance.is_audio_playing()); |
69 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 69 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
70 instance.OnVisibilityChanged(false); | 70 instance.OnVisibilityChanged(false); |
71 EXPECT_FALSE(instance.in_session()); | 71 EXPECT_FALSE(instance.in_session()); |
72 EXPECT_FALSE(instance.is_visible()); | 72 EXPECT_FALSE(instance.is_visible()); |
73 EXPECT_FALSE(instance.is_audio_playing()); | 73 EXPECT_FALSE(instance.is_audio_playing()); |
74 histogram_tester.ExpectTotalCount("Session.TotalDuration", 2); | 74 histogram_tester.ExpectTotalCount("Session.TotalDuration", 2); |
75 } | 75 } |
76 | 76 |
77 TEST(DesktopEngagementServiceTest, TestUserEvent) { | 77 TEST(DesktopSessionServiceTest, TestUserEvent) { |
78 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | 78 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
79 base::HistogramTester histogram_tester; | 79 base::HistogramTester histogram_tester; |
80 | 80 |
81 MockDesktopEngagementService instance; | 81 MockDesktopSessionService instance; |
82 instance.SetInactivityTimeoutForTesting(1); | 82 instance.SetInactivityTimeoutForTesting(1); |
83 | 83 |
84 EXPECT_FALSE(instance.in_session()); | 84 EXPECT_FALSE(instance.in_session()); |
85 EXPECT_FALSE(instance.is_visible()); | 85 EXPECT_FALSE(instance.is_visible()); |
86 EXPECT_FALSE(instance.is_audio_playing()); | 86 EXPECT_FALSE(instance.is_audio_playing()); |
87 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 87 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
88 | 88 |
89 // User event doesn't go through if nothing is visible. | 89 // User event doesn't go through if nothing is visible. |
90 instance.OnUserEvent(); | 90 instance.OnUserEvent(); |
91 EXPECT_FALSE(instance.in_session()); | 91 EXPECT_FALSE(instance.in_session()); |
(...skipping 12 matching lines...) Expand all Loading... |
104 while (!instance.is_timeout()) { | 104 while (!instance.is_timeout()) { |
105 base::RunLoop().RunUntilIdle(); | 105 base::RunLoop().RunUntilIdle(); |
106 } | 106 } |
107 | 107 |
108 EXPECT_FALSE(instance.in_session()); | 108 EXPECT_FALSE(instance.in_session()); |
109 EXPECT_TRUE(instance.is_visible()); | 109 EXPECT_TRUE(instance.is_visible()); |
110 EXPECT_FALSE(instance.is_audio_playing()); | 110 EXPECT_FALSE(instance.is_audio_playing()); |
111 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 111 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
112 } | 112 } |
113 | 113 |
114 TEST(DesktopEngagementServiceTest, TestAudioEvent) { | 114 TEST(DesktopSessionServiceTest, TestAudioEvent) { |
115 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); | 115 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
116 base::HistogramTester histogram_tester; | 116 base::HistogramTester histogram_tester; |
117 | 117 |
118 MockDesktopEngagementService instance; | 118 MockDesktopSessionService instance; |
119 instance.SetInactivityTimeoutForTesting(1); | 119 instance.SetInactivityTimeoutForTesting(1); |
120 | 120 |
121 instance.OnVisibilityChanged(true); | 121 instance.OnVisibilityChanged(true); |
122 instance.OnAudioStart(); | 122 instance.OnAudioStart(); |
123 EXPECT_TRUE(instance.in_session()); | 123 EXPECT_TRUE(instance.in_session()); |
124 EXPECT_TRUE(instance.is_visible()); | 124 EXPECT_TRUE(instance.is_visible()); |
125 EXPECT_TRUE(instance.is_audio_playing()); | 125 EXPECT_TRUE(instance.is_audio_playing()); |
126 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); | 126 histogram_tester.ExpectTotalCount("Session.TotalDuration", 0); |
127 | 127 |
128 instance.OnVisibilityChanged(false); | 128 instance.OnVisibilityChanged(false); |
(...skipping 11 matching lines...) Expand all Loading... |
140 // Wait until the session expires. | 140 // Wait until the session expires. |
141 while (!instance.is_timeout()) { | 141 while (!instance.is_timeout()) { |
142 base::RunLoop().RunUntilIdle(); | 142 base::RunLoop().RunUntilIdle(); |
143 } | 143 } |
144 | 144 |
145 EXPECT_FALSE(instance.in_session()); | 145 EXPECT_FALSE(instance.in_session()); |
146 EXPECT_FALSE(instance.is_visible()); | 146 EXPECT_FALSE(instance.is_visible()); |
147 EXPECT_FALSE(instance.is_audio_playing()); | 147 EXPECT_FALSE(instance.is_audio_playing()); |
148 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); | 148 histogram_tester.ExpectTotalCount("Session.TotalDuration", 1); |
149 } | 149 } |
OLD | NEW |