Home | History | Annotate | Download | only in login
      1 // Copyright (c) 2013 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 
      5 #include "base/run_loop.h"
      6 #include "base/time/time.h"
      7 #include "chrome/browser/chromeos/login/merge_session_load_page.h"
      8 #include "chrome/browser/chromeos/login/oauth2_login_manager.h"
      9 #include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h"
     10 #include "chrome/browser/chromeos/login/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"
     18 
     19 using content::InterstitialPage;
     20 using content::WebContents;
     21 using content::WebContentsTester;
     22 
     23 namespace {
     24 
     25 const char kURL1[] = "http://www.google.com/";
     26 const char kURL2[] = "http://mail.google.com/";
     27 
     28 const int64 kSessionMergeTimeout = 60;
     29 
     30 }  // namespace
     31 
     32 namespace chromeos {
     33 
     34 class MergeSessionLoadPageTest;
     35 
     36 // An MergeSessionLoadPage class that does not create windows.
     37 class TestMergeSessionLoadPage :  public MergeSessionLoadPage {
     38  public:
     39   TestMergeSessionLoadPage(WebContents* web_contents,
     40                            const GURL& url,
     41                            MergeSessionLoadPageTest* test_page)
     42     : MergeSessionLoadPage(web_contents, url, CompletionCallback()),
     43       test_page_(test_page) {
     44     interstitial_page_->DontCreateViewForTesting();
     45   }
     46 
     47  private:
     48   MergeSessionLoadPageTest* test_page_;
     49 
     50   DISALLOW_COPY_AND_ASSIGN(TestMergeSessionLoadPage);
     51 };
     52 
     53 class MergeSessionLoadPageTest : public ChromeRenderViewHostTestHarness {
     54  protected:
     55   virtual void SetUp() OVERRIDE {
     56     ChromeRenderViewHostTestHarness::SetUp();
     57 #if defined OS_CHROMEOS
     58   test_user_manager_.reset(new chromeos::ScopedTestUserManager());
     59 #endif
     60   }
     61 
     62   virtual void TearDown() OVERRIDE {
     63 #if defined OS_CHROMEOS
     64     // Clean up pending tasks that might depend on the user manager.
     65     base::RunLoop().RunUntilIdle();
     66     test_user_manager_.reset();
     67 #endif
     68     ChromeRenderViewHostTestHarness::TearDown();
     69   }
     70 
     71   void Navigate(const char* url, int page_id) {
     72     WebContentsTester::For(web_contents())->TestDidNavigate(
     73         web_contents()->GetRenderViewHost(), page_id, GURL(url),
     74         content::PAGE_TRANSITION_TYPED);
     75   }
     76 
     77   void ShowInterstitial(const char* url) {
     78     (new TestMergeSessionLoadPage(web_contents(), GURL(url), this))->Show();
     79   }
     80 
     81   // Returns the MergeSessionLoadPage currently showing or NULL if none is
     82   // showing.
     83   InterstitialPage* GetMergeSessionLoadPage() {
     84     return InterstitialPage::GetInterstitialPage(web_contents());
     85   }
     86 
     87   OAuth2LoginManager* GetOAuth2LoginManager() {
     88     content::BrowserContext* browser_context =
     89         web_contents()->GetBrowserContext();
     90     if (!browser_context)
     91       return NULL;
     92 
     93     Profile* profile = Profile::FromBrowserContext(browser_context);
     94     if (!profile)
     95       return NULL;
     96 
     97     OAuth2LoginManager* login_manager =
     98         OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
     99             profile);
    100     return login_manager;
    101   }
    102 
    103   void SetMergeSessionState(OAuth2LoginManager::SessionRestoreState state) {
    104     OAuth2LoginManager* login_manager = GetOAuth2LoginManager();
    105     ASSERT_TRUE(login_manager);
    106     login_manager->SetSessionRestoreState(state);
    107   }
    108 
    109   void SetSessionRestoreStart(const base::Time& time) {
    110     OAuth2LoginManager* login_manager = GetOAuth2LoginManager();
    111     ASSERT_TRUE(login_manager);
    112     login_manager->SetSessionRestoreStartForTesting(time);
    113   }
    114 
    115  private:
    116   ScopedTestDeviceSettingsService test_device_settings_service_;
    117   ScopedTestCrosSettings test_cros_settings_;
    118   scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
    119 };
    120 
    121 TEST_F(MergeSessionLoadPageTest, MergeSessionPageNotShown) {
    122   SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_DONE);
    123   // Start a load.
    124   Navigate(kURL1, 1);
    125   // Load next page.
    126   controller().LoadURL(GURL(kURL2), content::Referrer(),
    127                        content::PAGE_TRANSITION_TYPED, std::string());
    128 
    129   // Simulate the load causing an merge session interstitial page
    130   // to be shown.
    131   InterstitialPage* interstitial = GetMergeSessionLoadPage();
    132   EXPECT_FALSE(interstitial);
    133 }
    134 
    135 TEST_F(MergeSessionLoadPageTest, MergeSessionPageNotShownOnTimeout) {
    136   SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_IN_PROGRESS);
    137   SetSessionRestoreStart(
    138       base::Time::Now() +
    139       base::TimeDelta::FromSeconds(kSessionMergeTimeout + 1));
    140 
    141   // Start a load.
    142   Navigate(kURL1, 1);
    143   // Load next page.
    144   controller().LoadURL(GURL(kURL2), content::Referrer(),
    145                        content::PAGE_TRANSITION_TYPED, std::string());
    146 
    147   // Simulate the load causing an merge session interstitial page
    148   // to be shown.
    149   InterstitialPage* interstitial = GetMergeSessionLoadPage();
    150   EXPECT_FALSE(interstitial);
    151 }
    152 
    153 TEST_F(MergeSessionLoadPageTest, MergeSessionPageShown) {
    154   SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_IN_PROGRESS);
    155 
    156   // Start a load.
    157   Navigate(kURL1, 1);
    158   // Load next page.
    159   controller().LoadURL(GURL(kURL2), content::Referrer(),
    160                        content::PAGE_TRANSITION_TYPED, std::string());
    161 
    162   // Simulate the load causing an merge session interstitial page
    163   // to be shown.
    164   ShowInterstitial(kURL2);
    165   InterstitialPage* interstitial = GetMergeSessionLoadPage();
    166   ASSERT_TRUE(interstitial);
    167   base::RunLoop().RunUntilIdle();
    168 
    169   // Simulate merge session completion.
    170   SetMergeSessionState(OAuth2LoginManager::SESSION_RESTORE_DONE);
    171   base::RunLoop().RunUntilIdle();
    172 
    173   // The URL remains to be URL2.
    174   EXPECT_EQ(kURL2, web_contents()->GetVisibleURL().spec());
    175 
    176   // Commit navigation and the interstitial page is gone.
    177   Navigate(kURL2, 2);
    178   EXPECT_FALSE(GetMergeSessionLoadPage());
    179 }
    180 
    181 }  // namespace chromeos
    182