Chromium Code Reviews| 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 #include "chrome/browser/metrics/desktop_engagement/audible_contents_tracker.h" | |
| 5 | |
| 6 #include "base/path_service.h" | |
| 7 #include "chrome/test/base/in_process_browser_test.h" | |
| 8 #include "chrome/test/base/ui_test_utils.h" | |
| 9 #include "content/public/test/browser_test_base.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace { | |
| 13 // Observer for testing AudibleContentsTracker. | |
| 14 class MockAudibleContentsObserver | |
| 15 : public metrics::AudibleContentsTracker::Observer { | |
| 16 public: | |
| 17 MockAudibleContentsObserver() {} | |
| 18 | |
| 19 // Initializes tracker/observer pair. | |
| 20 static void Initialize() { | |
| 21 DCHECK(!tracker_); | |
| 22 DCHECK(!observer_); | |
| 23 observer_ = new MockAudibleContentsObserver(); | |
| 24 tracker_ = new metrics::AudibleContentsTracker(observer_); | |
| 25 } | |
| 26 | |
| 27 static MockAudibleContentsObserver* GetInstance() { return observer_; } | |
| 28 | |
| 29 // AudibleContentsTracker::Observer: | |
| 30 void OnAudioStart() override { is_audio_playing_ = true; } | |
| 31 void OnAudioEnd() override { is_audio_playing_ = false; } | |
| 32 | |
| 33 bool IsAudioPlaying() { return is_audio_playing_; } | |
| 34 | |
| 35 private: | |
| 36 // Singletons for performing the tracking and observing state changes. These | |
| 37 // are deliberately leaked at shutdown. | |
| 38 static metrics::AudibleContentsTracker* tracker_; | |
| 39 static MockAudibleContentsObserver* observer_; | |
|
chrisha
2016/07/20 21:49:26
Couldn't these be owned by the test fixture, and a
gayane -on leave until 09-2017
2016/07/22 18:44:09
Done.
| |
| 40 | |
| 41 bool is_audio_playing_ = false; | |
| 42 | |
| 43 DISALLOW_COPY_AND_ASSIGN(MockAudibleContentsObserver); | |
| 44 }; | |
| 45 | |
| 46 metrics::AudibleContentsTracker* MockAudibleContentsObserver::tracker_ = | |
| 47 nullptr; | |
| 48 MockAudibleContentsObserver* MockAudibleContentsObserver::observer_ = nullptr; | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 class AudibleContentsTrackerTest : public InProcessBrowserTest {}; | |
| 53 | |
| 54 IN_PROC_BROWSER_TEST_F(AudibleContentsTrackerTest, test) { | |
| 55 MockAudibleContentsObserver::Initialize(); | |
| 56 MockAudibleContentsObserver* observer = | |
| 57 MockAudibleContentsObserver::GetInstance(); | |
| 58 EXPECT_FALSE(observer->IsAudioPlaying()); | |
| 59 | |
| 60 // For serving audio. | |
| 61 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 62 base::FilePath test_data_dir; | |
| 63 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); | |
| 64 embedded_test_server()->ServeFilesFromDirectory( | |
| 65 test_data_dir.AppendASCII("chrome/test/data/")); | |
| 66 ui_test_utils::NavigateToURL( | |
| 67 browser(), embedded_test_server()->GetURL("/autoplay_audio.html")); | |
| 68 | |
| 69 // Wait until the audio starts. | |
| 70 while (!observer->IsAudioPlaying()) { | |
| 71 base::RunLoop().RunUntilIdle(); | |
| 72 } | |
| 73 | |
| 74 // Wait until the audio stops. | |
| 75 while (observer->IsAudioPlaying()) { | |
| 76 base::RunLoop().RunUntilIdle(); | |
| 77 } | |
| 78 } | |
| OLD | NEW |