This source file includes following definitions.
- test_page_
- SetUp
- OnBlockingPageComplete
- Navigate
- ShowInterstitial
- GetOfflineLoadPage
- user_response
- NotifyBlockingPageComplete
- TEST_F
- TEST_F
#include "chrome/browser/chromeos/offline/offline_load_page.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/browser/interstitial_page.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/web_contents_tester.h"
using content::InterstitialPage;
using content::WebContents;
using content::WebContentsTester;
static const char* kURL1 = "http://www.google.com/";
static const char* kURL2 = "http://www.gmail.com/";
namespace chromeos {
class OfflineLoadPageTest;
class TestOfflineLoadPage : public chromeos::OfflineLoadPage {
public:
TestOfflineLoadPage(WebContents* web_contents,
const GURL& url,
OfflineLoadPageTest* test_page)
: chromeos::OfflineLoadPage(web_contents, url, CompletionCallback()),
test_page_(test_page) {
interstitial_page_->DontCreateViewForTesting();
}
virtual void NotifyBlockingPageComplete(bool proceed) OVERRIDE;
private:
OfflineLoadPageTest* test_page_;
DISALLOW_COPY_AND_ASSIGN(TestOfflineLoadPage);
};
class OfflineLoadPageTest : public ChromeRenderViewHostTestHarness {
public:
enum UserResponse {
PENDING,
OK,
CANCEL
};
virtual void SetUp() {
ChromeRenderViewHostTestHarness::SetUp();
user_response_ = PENDING;
}
void OnBlockingPageComplete(bool proceed) {
if (proceed)
user_response_ = OK;
else
user_response_ = CANCEL;
}
void Navigate(const char* url, int page_id) {
WebContentsTester::For(web_contents())->TestDidNavigate(
web_contents()->GetRenderViewHost(), page_id, GURL(url),
content::PAGE_TRANSITION_TYPED);
}
void ShowInterstitial(const char* url) {
(new TestOfflineLoadPage(web_contents(), GURL(url), this))->Show();
}
InterstitialPage* GetOfflineLoadPage() {
return InterstitialPage::GetInterstitialPage(web_contents());
}
UserResponse user_response() const { return user_response_; }
private:
friend class TestOfflineLoadPage;
UserResponse user_response_;
};
void TestOfflineLoadPage::NotifyBlockingPageComplete(bool proceed) {
test_page_->OnBlockingPageComplete(proceed);
}
TEST_F(OfflineLoadPageTest, OfflinePageProceed) {
Navigate(kURL1, 1);
controller().LoadURL(GURL(kURL2), content::Referrer(),
content::PAGE_TRANSITION_TYPED, std::string());
ShowInterstitial(kURL2);
InterstitialPage* interstitial = GetOfflineLoadPage();
ASSERT_TRUE(interstitial);
base::MessageLoop::current()->RunUntilIdle();
interstitial->Proceed();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(OK, user_response());
EXPECT_EQ(kURL2, web_contents()->GetVisibleURL().spec());
Navigate(kURL2, 2);
EXPECT_FALSE(GetOfflineLoadPage());
}
TEST_F(OfflineLoadPageTest, OfflinePageDontProceed) {
Navigate(kURL1, 1);
controller().LoadURL(GURL(kURL2), content::Referrer(),
content::PAGE_TRANSITION_TYPED, std::string());
ShowInterstitial(kURL2);
InterstitialPage* interstitial = GetOfflineLoadPage();
ASSERT_TRUE(interstitial);
base::MessageLoop::current()->RunUntilIdle();
interstitial->DontProceed();
EXPECT_EQ(CANCEL, user_response());
EXPECT_FALSE(GetOfflineLoadPage());
EXPECT_FALSE(controller().GetPendingEntry());
EXPECT_EQ(kURL1, web_contents()->GetLastCommittedURL().spec());
}
}