offline_load_page_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 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 "chrome/browser/chromeos/cros/cros_library.h"
6#include "chrome/browser/chromeos/offline/offline_load_page.h"
7#include "chrome/test/base/chrome_render_view_host_test_harness.h"
8#include "content/public/browser/interstitial_page.h"
9#include "content/public/browser/navigation_controller.h"
10#include "content/public/browser/web_contents.h"
11#include "content/public/test/test_browser_thread.h"
12#include "content/public/test/web_contents_tester.h"
13
14using content::BrowserThread;
15using content::InterstitialPage;
16using content::WebContents;
17using content::WebContentsTester;
18
19static const char* kURL1 = "http://www.google.com/";
20static const char* kURL2 = "http://www.gmail.com/";
21
22namespace chromeos {
23
24class OfflineLoadPageTest;
25
26// An OfflineLoadPage class that does not create windows.
27class TestOfflineLoadPage :  public chromeos::OfflineLoadPage {
28 public:
29  TestOfflineLoadPage(WebContents* web_contents,
30                      const GURL& url,
31                      OfflineLoadPageTest* test_page)
32    : chromeos::OfflineLoadPage(web_contents, url, CompletionCallback()),
33      test_page_(test_page) {
34    interstitial_page_->DontCreateViewForTesting();
35  }
36
37  // chromeos::OfflineLoadPage override.
38  virtual void NotifyBlockingPageComplete(bool proceed) OVERRIDE;
39
40 private:
41  OfflineLoadPageTest* test_page_;
42
43  DISALLOW_COPY_AND_ASSIGN(TestOfflineLoadPage);
44};
45
46class OfflineLoadPageTest : public ChromeRenderViewHostTestHarness {
47 public:
48  // The decision the user made.
49  enum UserResponse {
50    PENDING,
51    OK,
52    CANCEL
53  };
54
55  OfflineLoadPageTest()
56      : ui_thread_(BrowserThread::UI, MessageLoop::current()),
57        file_user_blocking_thread_(
58            BrowserThread::FILE_USER_BLOCKING, MessageLoop::current()),
59        io_thread_(BrowserThread::IO, MessageLoop::current()) {
60  }
61
62  virtual void SetUp() {
63    ChromeRenderViewHostTestHarness::SetUp();
64    user_response_ = PENDING;
65  }
66
67  void OnBlockingPageComplete(bool proceed) {
68    if (proceed)
69      user_response_ = OK;
70    else
71      user_response_ = CANCEL;
72  }
73
74  void Navigate(const char* url, int page_id) {
75    WebContentsTester::For(web_contents())->TestDidNavigate(
76        web_contents()->GetRenderViewHost(), page_id, GURL(url),
77        content::PAGE_TRANSITION_TYPED);
78  }
79
80  void ShowInterstitial(const char* url) {
81    (new TestOfflineLoadPage(web_contents(), GURL(url), this))->Show();
82  }
83
84  // Returns the OfflineLoadPage currently showing or NULL if none is
85  // showing.
86  InterstitialPage* GetOfflineLoadPage() {
87    return InterstitialPage::GetInterstitialPage(web_contents());
88  }
89
90  UserResponse user_response() const { return user_response_; }
91
92 private:
93  UserResponse user_response_;
94  content::TestBrowserThread ui_thread_;
95  content::TestBrowserThread file_user_blocking_thread_;
96  content::TestBrowserThread io_thread_;
97
98  // Initializes / shuts down a stub CrosLibrary.
99  chromeos::ScopedStubCrosEnabler stub_cros_enabler_;
100
101  DISALLOW_COPY_AND_ASSIGN(OfflineLoadPageTest);
102};
103
104void TestOfflineLoadPage::NotifyBlockingPageComplete(bool proceed) {
105  test_page_->OnBlockingPageComplete(proceed);
106}
107
108TEST_F(OfflineLoadPageTest, OfflinePageProceed) {
109  // Start a load.
110  Navigate(kURL1, 1);
111  // Load next page.
112  controller().LoadURL(GURL(kURL2), content::Referrer(),
113                       content::PAGE_TRANSITION_TYPED, std::string());
114
115  // Simulate the load causing an offline browsing interstitial page
116  // to be shown.
117  ShowInterstitial(kURL2);
118  InterstitialPage* interstitial = GetOfflineLoadPage();
119  ASSERT_TRUE(interstitial);
120  MessageLoop::current()->RunUntilIdle();
121
122  // Simulate the user clicking "proceed".
123  interstitial->Proceed();
124  MessageLoop::current()->RunUntilIdle();
125
126  EXPECT_EQ(OK, user_response());
127
128  // The URL remains to be URL2.
129  EXPECT_EQ(kURL2, web_contents()->GetURL().spec());
130
131  // Commit navigation and the interstitial page is gone.
132  Navigate(kURL2, 2);
133  EXPECT_FALSE(GetOfflineLoadPage());
134}
135
136// Tests showing an offline page and not proceeding.
137TEST_F(OfflineLoadPageTest, OfflinePageDontProceed) {
138  // Start a load.
139  Navigate(kURL1, 1);
140  controller().LoadURL(GURL(kURL2), content::Referrer(),
141                       content::PAGE_TRANSITION_TYPED, std::string());
142
143  // Simulate the load causing an offline interstitial page to be shown.
144  ShowInterstitial(kURL2);
145  InterstitialPage* interstitial = GetOfflineLoadPage();
146  ASSERT_TRUE(interstitial);
147  MessageLoop::current()->RunUntilIdle();
148
149  // Simulate the user clicking "don't proceed".
150  interstitial->DontProceed();
151
152  // The interstitial should be gone.
153  EXPECT_EQ(CANCEL, user_response());
154  EXPECT_FALSE(GetOfflineLoadPage());
155  // We did not proceed, the pending entry should be gone.
156  EXPECT_FALSE(controller().GetPendingEntry());
157  // the URL is set back to kURL1.
158  EXPECT_EQ(kURL1, web_contents()->GetURL().spec());
159}
160
161}  // namespace chromeos
162