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 "chrome/browser/chromeos/cros/network_library.h"
      7 #include "chrome/browser/chromeos/login/merge_session_load_page.h"
      8 #include "chrome/browser/chromeos/login/user_manager.h"
      9 #include "chrome/browser/chromeos/settings/cros_settings.h"
     10 #include "chrome/browser/chromeos/settings/device_settings_service.h"
     11 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
     12 #include "content/public/browser/interstitial_page.h"
     13 #include "content/public/browser/navigation_controller.h"
     14 #include "content/public/browser/web_contents.h"
     15 #include "content/public/test/web_contents_tester.h"
     16 
     17 using content::InterstitialPage;
     18 using content::WebContents;
     19 using content::WebContentsTester;
     20 
     21 namespace {
     22 
     23 const char kURL1[] = "http://www.google.com/";
     24 const char kURL2[] = "http://mail.google.com/";
     25 
     26 }  // namespace
     27 
     28 namespace chromeos {
     29 
     30 class MergeSessionLoadPageTest;
     31 
     32 // An MergeSessionLoadPage class that does not create windows.
     33 class TestMergeSessionLoadPage :  public MergeSessionLoadPage {
     34  public:
     35   TestMergeSessionLoadPage(WebContents* web_contents,
     36                            const GURL& url,
     37                            MergeSessionLoadPageTest* test_page)
     38     : MergeSessionLoadPage(web_contents, url, CompletionCallback()),
     39       test_page_(test_page) {
     40     interstitial_page_->DontCreateViewForTesting();
     41   }
     42 
     43  private:
     44   MergeSessionLoadPageTest* test_page_;
     45 
     46   DISALLOW_COPY_AND_ASSIGN(TestMergeSessionLoadPage);
     47 };
     48 
     49 class MergeSessionLoadPageTest : public ChromeRenderViewHostTestHarness {
     50  protected:
     51   virtual void SetUp() OVERRIDE {
     52     ChromeRenderViewHostTestHarness::SetUp();
     53 #if defined OS_CHROMEOS
     54   test_user_manager_.reset(new chromeos::ScopedTestUserManager());
     55 #endif
     56   }
     57 
     58   virtual 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();
     65   }
     66 
     67   void Navigate(const char* url, int page_id) {
     68     WebContentsTester::For(web_contents())->TestDidNavigate(
     69         web_contents()->GetRenderViewHost(), page_id, GURL(url),
     70         content::PAGE_TRANSITION_TYPED);
     71   }
     72 
     73   void ShowInterstitial(const char* url) {
     74     (new TestMergeSessionLoadPage(web_contents(), GURL(url), this))->Show();
     75   }
     76 
     77   // Returns the MergeSessionLoadPage currently showing or NULL if none is
     78   // showing.
     79   InterstitialPage* GetMergeSessionLoadPage() {
     80     return InterstitialPage::GetInterstitialPage(web_contents());
     81   }
     82 
     83  private:
     84   ScopedStubNetworkLibraryEnabler stub_network_library_enabler_;
     85   ScopedTestDeviceSettingsService test_device_settings_service_;
     86   ScopedTestCrosSettings test_cros_settings_;
     87   scoped_ptr<chromeos::ScopedTestUserManager> test_user_manager_;
     88 };
     89 
     90 TEST_F(MergeSessionLoadPageTest, MergeSessionPageNotShown) {
     91   UserManager::Get()->SetMergeSessionState(
     92       UserManager::MERGE_STATUS_DONE);
     93   // Start a load.
     94   Navigate(kURL1, 1);
     95   // Load next page.
     96   controller().LoadURL(GURL(kURL2), content::Referrer(),
     97                        content::PAGE_TRANSITION_TYPED, std::string());
     98 
     99   // Simulate the load causing an merge session interstitial page
    100   // to be shown.
    101   InterstitialPage* interstitial = GetMergeSessionLoadPage();
    102   EXPECT_FALSE(interstitial);
    103 }
    104 
    105 TEST_F(MergeSessionLoadPageTest, MergeSessionPageShown) {
    106   UserManager::Get()->SetMergeSessionState(
    107       UserManager::MERGE_STATUS_IN_PROCESS);
    108   // Start a load.
    109   Navigate(kURL1, 1);
    110   // Load next page.
    111   controller().LoadURL(GURL(kURL2), content::Referrer(),
    112                        content::PAGE_TRANSITION_TYPED, std::string());
    113 
    114   // Simulate the load causing an merge session interstitial page
    115   // to be shown.
    116   ShowInterstitial(kURL2);
    117   InterstitialPage* interstitial = GetMergeSessionLoadPage();
    118   ASSERT_TRUE(interstitial);
    119   base::RunLoop().RunUntilIdle();
    120 
    121   // Simulate merge session completion.
    122   UserManager::Get()->SetMergeSessionState(
    123       UserManager::MERGE_STATUS_DONE);
    124   base::RunLoop().RunUntilIdle();
    125 
    126   // The URL remains to be URL2.
    127   EXPECT_EQ(kURL2, web_contents()->GetVisibleURL().spec());
    128 
    129   // Commit navigation and the interstitial page is gone.
    130   Navigate(kURL2, 2);
    131   EXPECT_FALSE(GetMergeSessionLoadPage());
    132 }
    133 
    134 }  // namespace chromeos
    135