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