ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / chromeos / login / signin / merge_session_load_page_unittest.cc
blob31572465e3b50dbc78a8c882778865132bdb4160
1 // Copyright 2014 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.
5 #include "base/run_loop.h"
6 #include "base/time/time.h"
7 #include "chrome/browser/chromeos/login/signin/merge_session_load_page.h"
8 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager.h"
9 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager_factory.h"
10 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
11 #include "chrome/browser/chromeos/settings/cros_settings.h"
12 #include "chrome/browser/chromeos/settings/device_settings_service.h"
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
14 #include "content/public/browser/interstitial_page.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/test/web_contents_tester.h"
19 using content::InterstitialPage;
20 using content::WebContents;
21 using content::WebContentsTester;
23 namespace {
25 const char kURL1[] = "http://www.google.com/";
26 const char kURL2[] = "http://mail.google.com/";
28 const int64 kSessionMergeTimeout = 60;
30 } // namespace
32 namespace chromeos {
34 // An MergeSessionLoadPage class that does not create windows.
35 class TestMergeSessionLoadPage : public MergeSessionLoadPage {
36 public:
37 TestMergeSessionLoadPage(WebContents* web_contents,
38 const GURL& url)
39 : MergeSessionLoadPage(web_contents,
40 url,
41 MergeSessionThrottle::CompletionCallback()) {
42 interstitial_page_->DontCreateViewForTesting();
45 private:
46 DISALLOW_COPY_AND_ASSIGN(TestMergeSessionLoadPage);
49 class MergeSessionLoadPageTest : public ChromeRenderViewHostTestHarness {
50 protected:
51 void SetUp() override {
52 ChromeRenderViewHostTestHarness::SetUp();
53 #if defined OS_CHROMEOS
54 test_user_manager_.reset(new chromeos::ScopedTestUserManager());
55 #endif
58 void TearDown() override {
59 #if defined OS_CHROMEOS
60 // Clean up pending tasks that might depend on the user manager.
61 base::RunLoop().RunUntilIdle();
62 test_user_manager_.reset();
63 #endif
64 ChromeRenderViewHostTestHarness::TearDown();
67 void Navigate(const char* url, int page_id) {
68 WebContentsTester::For(web_contents())->TestDidNavigate(
69 web_contents()->GetMainFrame(), page_id, GURL(url),
70 ui::PAGE_TRANSITION_TYPED);
73 void ShowInterstitial(const char* url) {
74 (new TestMergeSessionLoadPage(web_contents(), GURL(url)))->Show();
77 // Returns the MergeSessionLoadPage currently showing or NULL if none is
78 // showing.
79 InterstitialPage* GetMergeSessionLoadPage() {
80 return InterstitialPage::GetInterstitialPage(web_contents());
83 OAuth2LoginManager* GetOAuth2LoginManager() {
84 content::BrowserContext* browser_context =
85 web_contents()->GetBrowserContext();
86 if (!browser_context)
87 return NULL;
89 Profile* profile = Profile::FromBrowserContext(browser_context);
90 if (!profile)
91 return NULL;
93 OAuth2LoginManager* login_manager =
94 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
95 profile);
96 return login_manager;
99 void SetMergeSessionState(OAuth2LoginManager::SessionRestoreState state) {
100 OAuth2LoginManager* login_manager = GetOAuth2LoginManager();
101 ASSERT_TRUE(login_manager);
102 login_manager->SetSessionRestoreState(state);
105 void SetSessionRestoreStart(const base::Time& time) {
106 OAuth2LoginManager* login_manager = GetOAuth2LoginManager();
107 ASSERT_TRUE(login_manager);
108 login_manager->SetSessionRestoreStartForTesting(time);
111 private:
112 ScopedTestDeviceSettingsService test_device_settings_service_;
113 ScopedTestCrosSettings test_cros_settings_;
114 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
117 TEST_F(MergeSessionLoadPageTest, MergeSessionPageNotShown) {
118 SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_DONE);
119 // Start a load.
120 Navigate(kURL1, 1);
121 // Load next page.
122 controller().LoadURL(GURL(kURL2), content::Referrer(),
123 ui::PAGE_TRANSITION_TYPED, std::string());
125 // Simulate the load causing an merge session interstitial page
126 // to be shown.
127 InterstitialPage* interstitial = GetMergeSessionLoadPage();
128 EXPECT_FALSE(interstitial);
131 TEST_F(MergeSessionLoadPageTest, MergeSessionPageNotShownOnTimeout) {
132 SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_IN_PROGRESS);
133 SetSessionRestoreStart(
134 base::Time::Now() +
135 base::TimeDelta::FromSeconds(kSessionMergeTimeout + 1));
137 // Start a load.
138 Navigate(kURL1, 1);
139 // Load next page.
140 controller().LoadURL(GURL(kURL2), content::Referrer(),
141 ui::PAGE_TRANSITION_TYPED, std::string());
143 // Simulate the load causing an merge session interstitial page
144 // to be shown.
145 InterstitialPage* interstitial = GetMergeSessionLoadPage();
146 EXPECT_FALSE(interstitial);
149 TEST_F(MergeSessionLoadPageTest, MergeSessionPageShown) {
150 SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_IN_PROGRESS);
152 // Start a load.
153 Navigate(kURL1, 1);
154 // Load next page.
155 controller().LoadURL(GURL(kURL2), content::Referrer(),
156 ui::PAGE_TRANSITION_TYPED, std::string());
158 // Simulate the load causing an merge session interstitial page
159 // to be shown.
160 ShowInterstitial(kURL2);
161 InterstitialPage* interstitial = GetMergeSessionLoadPage();
162 ASSERT_TRUE(interstitial);
163 base::RunLoop().RunUntilIdle();
165 // Simulate merge session completion.
166 SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_DONE);
167 base::RunLoop().RunUntilIdle();
169 // The URL remains to be URL2.
170 EXPECT_EQ(kURL2, web_contents()->GetVisibleURL().spec());
172 // Commit navigation and the interstitial page is gone.
173 Navigate(kURL2, 2);
174 EXPECT_FALSE(GetMergeSessionLoadPage());
177 } // namespace chromeos