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/audible_contents_tracker.h" | |
6 | |
7 #include "base/path_service.h" | |
8 #include "chrome/test/base/in_process_browser_test.h" | |
9 #include "chrome/test/base/ui_test_utils.h" | |
10 #include "content/public/test/browser_test_base.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 namespace { | |
14 | |
15 // Observer for testing AudibleContentsTracker. | |
16 class MockAudibleContentsObserver | |
17 : public metrics::AudibleContentsTracker::Observer { | |
18 public: | |
19 MockAudibleContentsObserver() {} | |
20 | |
21 // AudibleContentsTracker::Observer: | |
22 void OnAudioStart() override { is_audio_playing_ = true; } | |
23 void OnAudioEnd() override { is_audio_playing_ = false; } | |
24 | |
25 bool is_audio_playing() const { return is_audio_playing_; } | |
26 | |
27 private: | |
28 bool is_audio_playing_ = false; | |
29 | |
30 DISALLOW_COPY_AND_ASSIGN(MockAudibleContentsObserver); | |
31 }; | |
32 | |
33 } // namespace | |
34 | |
35 class AudibleContentsTrackerTest : public InProcessBrowserTest { | |
36 public: | |
37 AudibleContentsTrackerTest() {} | |
38 | |
39 void SetUp() override { | |
40 observer_.reset(new MockAudibleContentsObserver()); | |
41 tracker_.reset(new metrics::AudibleContentsTracker(observer())); | |
42 InProcessBrowserTest::SetUp(); | |
43 } | |
44 | |
45 void TearDown() override { | |
46 InProcessBrowserTest::TearDown(); | |
47 tracker_.reset(); | |
48 observer_.reset(); | |
49 } | |
50 | |
51 MockAudibleContentsObserver* observer() const { return observer_.get(); } | |
52 | |
53 private: | |
54 std::unique_ptr<MockAudibleContentsObserver> observer_ = nullptr; | |
55 std::unique_ptr<metrics::AudibleContentsTracker> tracker_ = nullptr; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(AudibleContentsTrackerTest); | |
58 }; | |
59 | |
60 IN_PROC_BROWSER_TEST_F(AudibleContentsTrackerTest, TestAudioNotifications) { | |
61 MockAudibleContentsObserver* audio_observer = observer(); | |
62 EXPECT_FALSE(audio_observer->is_audio_playing()); | |
63 | |
64 // For serving audio. | |
65 ASSERT_TRUE(embedded_test_server()->Start()); | |
66 base::FilePath test_data_dir; | |
67 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); | |
68 embedded_test_server()->ServeFilesFromDirectory( | |
69 test_data_dir.AppendASCII("chrome/test/data/")); | |
70 ui_test_utils::NavigateToURL( | |
71 browser(), embedded_test_server()->GetURL("/autoplay_audio.html")); | |
72 | |
73 // Wait until the audio starts. | |
74 while (!audio_observer->is_audio_playing()) { | |
75 base::RunLoop().RunUntilIdle(); | |
76 } | |
77 | |
78 // Wait until the audio stops. | |
79 while (audio_observer->is_audio_playing()) { | |
80 base::RunLoop().RunUntilIdle(); | |
81 } | |
82 } | |
OLD | NEW |