ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / chromeos / login / signin / merge_session_load_page_unittest.cc
blob0ff4379c6583ee1ea6f731836f8df05267f05ef4
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/navigation_entry.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/web_contents_tester.h"
20 using content::InterstitialPage;
21 using content::WebContents;
22 using content::WebContentsTester;
24 namespace {
26 const char kURL1[] = "http://www.google.com/";
27 const char kURL2[] = "http://mail.google.com/";
29 const int64 kSessionMergeTimeout = 60;
31 } // namespace
33 namespace chromeos {
35 // An MergeSessionLoadPage class that does not create windows.
36 class TestMergeSessionLoadPage : public MergeSessionLoadPage {
37 public:
38 TestMergeSessionLoadPage(WebContents* web_contents,
39 const GURL& url)
40 : MergeSessionLoadPage(web_contents,
41 url,
42 MergeSessionThrottle::CompletionCallback()) {
43 interstitial_page_->DontCreateViewForTesting();
46 private:
47 DISALLOW_COPY_AND_ASSIGN(TestMergeSessionLoadPage);
50 class MergeSessionLoadPageTest : public ChromeRenderViewHostTestHarness {
51 protected:
52 void SetUp() override {
53 ChromeRenderViewHostTestHarness::SetUp();
54 #if defined OS_CHROMEOS
55 test_user_manager_.reset(new chromeos::ScopedTestUserManager());
56 #endif
59 void TearDown() override {
60 #if defined OS_CHROMEOS
61 // Clean up pending tasks that might depend on the user manager.
62 base::RunLoop().RunUntilIdle();
63 test_user_manager_.reset();
64 #endif
65 ChromeRenderViewHostTestHarness::TearDown();
68 void Navigate(const char* url,
69 int page_id,
70 int nav_entry_id,
71 bool did_create_new_entry) {
72 WebContentsTester::For(web_contents())
73 ->TestDidNavigate(web_contents()->GetMainFrame(), page_id, nav_entry_id,
74 did_create_new_entry, GURL(url),
75 ui::PAGE_TRANSITION_TYPED);
78 void ShowInterstitial(const char* url) {
79 (new TestMergeSessionLoadPage(web_contents(), GURL(url)))->Show();
82 // Returns the MergeSessionLoadPage currently showing or NULL if none is
83 // showing.
84 InterstitialPage* GetMergeSessionLoadPage() {
85 return InterstitialPage::GetInterstitialPage(web_contents());
88 OAuth2LoginManager* GetOAuth2LoginManager() {
89 content::BrowserContext* browser_context =
90 web_contents()->GetBrowserContext();
91 if (!browser_context)
92 return NULL;
94 Profile* profile = Profile::FromBrowserContext(browser_context);
95 if (!profile)
96 return NULL;
98 OAuth2LoginManager* login_manager =
99 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
100 profile);
101 return login_manager;
104 void SetMergeSessionState(OAuth2LoginManager::SessionRestoreState state) {
105 OAuth2LoginManager* login_manager = GetOAuth2LoginManager();
106 ASSERT_TRUE(login_manager);
107 login_manager->SetSessionRestoreState(state);
110 void SetSessionRestoreStart(const base::Time& time) {
111 OAuth2LoginManager* login_manager = GetOAuth2LoginManager();
112 ASSERT_TRUE(login_manager);
113 login_manager->SetSessionRestoreStartForTesting(time);
116 private:
117 ScopedTestDeviceSettingsService test_device_settings_service_;
118 ScopedTestCrosSettings test_cros_settings_;
119 scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
122 TEST_F(MergeSessionLoadPageTest, MergeSessionPageNotShown) {
123 SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_DONE);
124 // Start a load.
125 Navigate(kURL1, 1, 0, true);
126 // Load next page.
127 controller().LoadURL(GURL(kURL2), content::Referrer(),
128 ui::PAGE_TRANSITION_TYPED, std::string());
130 // Simulate the load causing an merge session interstitial page
131 // to be shown.
132 InterstitialPage* interstitial = GetMergeSessionLoadPage();
133 EXPECT_FALSE(interstitial);
136 TEST_F(MergeSessionLoadPageTest, MergeSessionPageNotShownOnTimeout) {
137 SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_IN_PROGRESS);
138 SetSessionRestoreStart(
139 base::Time::Now() +
140 base::TimeDelta::FromSeconds(kSessionMergeTimeout + 1));
142 // Start a load.
143 Navigate(kURL1, 1, 0, true);
144 // Load next page.
145 controller().LoadURL(GURL(kURL2), content::Referrer(),
146 ui::PAGE_TRANSITION_TYPED, std::string());
148 // Simulate the load causing an merge session interstitial page
149 // to be shown.
150 InterstitialPage* interstitial = GetMergeSessionLoadPage();
151 EXPECT_FALSE(interstitial);
154 TEST_F(MergeSessionLoadPageTest, MergeSessionPageShown) {
155 SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_IN_PROGRESS);
157 // Start a load.
158 Navigate(kURL1, 1, 0, true);
159 // Load next page.
160 controller().LoadURL(GURL(kURL2), content::Referrer(),
161 ui::PAGE_TRANSITION_TYPED, std::string());
162 int pending_id = controller().GetPendingEntry()->GetUniqueID();
164 // Simulate the load causing an merge session interstitial page
165 // to be shown.
166 ShowInterstitial(kURL2);
167 InterstitialPage* interstitial = GetMergeSessionLoadPage();
168 ASSERT_TRUE(interstitial);
169 base::RunLoop().RunUntilIdle();
171 // Simulate merge session completion.
172 SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_DONE);
173 base::RunLoop().RunUntilIdle();
175 // The URL remains to be URL2.
176 EXPECT_EQ(kURL2, web_contents()->GetVisibleURL().spec());
178 // Commit navigation and the interstitial page is gone.
179 Navigate(kURL2, 2, pending_id, true);
180 EXPECT_FALSE(GetMergeSessionLoadPage());
183 } // namespace chromeos