| 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_session_duration/chrome_visibility_obse
rver.h" | 5 #include "chrome/browser/metrics/desktop_session_duration/chrome_visibility_obse
rver.h" |
| 6 | 6 |
| 7 #include "base/memory/singleton.h" | |
| 8 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" | |
| 10 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_list.h" | 9 #include "chrome/browser/ui/browser_window.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" | 10 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "chrome/test/base/ui_test_utils.h" | 11 #include "chrome/test/base/ui_test_utils.h" |
| 14 #include "content/public/browser/notification_service.h" | |
| 15 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
| 16 | 13 |
| 17 // Mock class for |ChromeVisibilityObserver| for testing. | 14 // Mock class for |ChromeVisibilityObserver| for testing. |
| 18 class MockChromeVisibilityObserver : public metrics::ChromeVisibilityObserver { | 15 class MockChromeVisibilityObserver : public metrics::ChromeVisibilityObserver { |
| 19 public: | 16 public: |
| 20 MockChromeVisibilityObserver() : is_active_(false) { | 17 MockChromeVisibilityObserver() : is_active_(false) {} |
| 21 ResetRunLoop(); | |
| 22 } | |
| 23 | |
| 24 void Wait() { | |
| 25 run_loop_->Run(); | |
| 26 } | |
| 27 | |
| 28 void ResetRunLoop() { | |
| 29 run_loop_.reset(new base::RunLoop()); | |
| 30 } | |
| 31 | 18 |
| 32 bool is_active() const { return is_active_; } | 19 bool is_active() const { return is_active_; } |
| 33 void set_is_active(bool is_active) { is_active_ = is_active; } | |
| 34 | 20 |
| 35 private: | 21 private: |
| 36 void SendVisibilityChangeEvent(bool active, | 22 void SendVisibilityChangeEvent(bool active, |
| 37 base::TimeDelta time_ago) override { | 23 base::TimeDelta time_ago) override { |
| 38 is_active_ = active; | 24 is_active_ = active; |
| 39 // In this test browser became inactive after browser is removed, thus | |
| 40 // in this case quit runloop in OnBrowserRemoved(). | |
| 41 if (is_active_) | |
| 42 run_loop_->Quit(); | |
| 43 } | |
| 44 | |
| 45 void OnBrowserRemoved(Browser* browser) override { | |
| 46 metrics::ChromeVisibilityObserver::OnBrowserRemoved(browser); | |
| 47 run_loop_->Quit(); | |
| 48 } | 25 } |
| 49 | 26 |
| 50 bool is_active_; | 27 bool is_active_; |
| 51 std::unique_ptr<base::RunLoop> run_loop_; | |
| 52 | 28 |
| 53 DISALLOW_COPY_AND_ASSIGN(MockChromeVisibilityObserver); | 29 DISALLOW_COPY_AND_ASSIGN(MockChromeVisibilityObserver); |
| 54 }; | 30 }; |
| 55 | 31 |
| 56 class ChromeVisibilityObserverBrowserTest : public InProcessBrowserTest {}; | 32 class ChromeVisibilityObserverBrowserTest : public InProcessBrowserTest {}; |
| 57 | 33 |
| 58 IN_PROC_BROWSER_TEST_F(ChromeVisibilityObserverBrowserTest, VisibilityTest) { | 34 IN_PROC_BROWSER_TEST_F(ChromeVisibilityObserverBrowserTest, VisibilityTest) { |
| 35 // Deactivate the initial browser window, to make observer start inactive. |
| 36 browser()->window()->Deactivate(); |
| 37 base::RunLoop().RunUntilIdle(); |
| 38 |
| 59 MockChromeVisibilityObserver observer; | 39 MockChromeVisibilityObserver observer; |
| 60 | 40 |
| 61 EXPECT_FALSE(observer.is_active()); | 41 EXPECT_FALSE(observer.is_active()); |
| 62 Browser* new_browser = CreateBrowser(browser()->profile()); | 42 Browser* new_browser = CreateBrowser(browser()->profile()); |
| 63 observer.Wait(); | 43 base::RunLoop().RunUntilIdle(); |
| 64 EXPECT_TRUE(observer.is_active()); | 44 EXPECT_TRUE(observer.is_active()); |
| 65 | 45 |
| 66 observer.ResetRunLoop(); | |
| 67 observer.set_is_active(false); | |
| 68 Browser* incognito_browser = CreateIncognitoBrowser(); | 46 Browser* incognito_browser = CreateIncognitoBrowser(); |
| 69 observer.Wait(); | 47 base::RunLoop().RunUntilIdle(); |
| 70 EXPECT_TRUE(observer.is_active()); | 48 EXPECT_TRUE(observer.is_active()); |
| 71 | 49 |
| 72 observer.ResetRunLoop(); | |
| 73 CloseBrowserSynchronously(incognito_browser); | 50 CloseBrowserSynchronously(incognito_browser); |
| 74 observer.Wait(); | 51 base::RunLoop().RunUntilIdle(); |
| 75 EXPECT_TRUE(observer.is_active()); | 52 EXPECT_TRUE(observer.is_active()); |
| 76 | 53 |
| 77 observer.ResetRunLoop(); | |
| 78 CloseBrowserSynchronously(new_browser); | 54 CloseBrowserSynchronously(new_browser); |
| 79 observer.Wait(); | 55 base::RunLoop().RunUntilIdle(); |
| 80 EXPECT_TRUE(observer.is_active()); | 56 EXPECT_TRUE(observer.is_active()); |
| 81 | 57 |
| 82 observer.ResetRunLoop(); | |
| 83 CloseBrowserSynchronously(browser()); | 58 CloseBrowserSynchronously(browser()); |
| 84 observer.Wait(); | 59 base::RunLoop().RunUntilIdle(); |
| 85 EXPECT_FALSE(observer.is_active()); | 60 EXPECT_FALSE(observer.is_active()); |
| 86 } | 61 } |
| OLD | NEW |