This source file includes following definitions.
- mock_reloader_
- SetUp
- TearDown
- SimulateSuccess
- SimulateTimeout
- SimulateAbort
- SimulateAbortTimeout
- tab_helper
- ObservePortalResult
- OnRedirect
- mock_reloader
- SetIsLoginTab
- render_view_host1
- render_view_host2
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/captive_portal/captive_portal_tab_helper.h"
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/captive_portal/captive_portal_service.h"
#include "chrome/browser/captive_portal/captive_portal_tab_reloader.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "net/base/net_errors.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace captive_portal {
namespace {
const char* const kHttpUrl = "http://whatever.com/";
const char* const kHttpsUrl = "https://whatever.com/";
const char* const kHttpsUrl2 = "https://cross_process.com/";
const char* const kErrorPageUrl = "data:blah";
enum NavigationType {
kSameProcess,
kCrossProcess,
};
}
class MockCaptivePortalTabReloader : public CaptivePortalTabReloader {
public:
MockCaptivePortalTabReloader()
: CaptivePortalTabReloader(NULL, NULL, base::Callback<void()>()) {
}
MOCK_METHOD1(OnLoadStart, void(bool));
MOCK_METHOD1(OnLoadCommitted, void(int));
MOCK_METHOD0(OnAbort, void());
MOCK_METHOD1(OnRedirect, void(bool));
MOCK_METHOD2(OnCaptivePortalResults, void(Result, Result));
};
class CaptivePortalTabHelperTest : public ChromeRenderViewHostTestHarness {
public:
CaptivePortalTabHelperTest()
: tab_helper_(NULL),
mock_reloader_(new testing::StrictMock<MockCaptivePortalTabReloader>) {
tab_helper_.SetTabReloaderForTest(mock_reloader_);
}
virtual ~CaptivePortalTabHelperTest() {}
virtual void SetUp() OVERRIDE {
ChromeRenderViewHostTestHarness::SetUp();
web_contents1_.reset(CreateTestWebContents());
web_contents2_.reset(CreateTestWebContents());
}
virtual void TearDown() OVERRIDE {
web_contents2_.reset(NULL);
web_contents1_.reset(NULL);
ChromeRenderViewHostTestHarness::TearDown();
}
void SimulateSuccess(const GURL& url,
content::RenderViewHost* render_view_host) {
EXPECT_CALL(mock_reloader(), OnLoadStart(url.SchemeIsSecure())).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, url, false, false, render_view_host);
EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::OK)).Times(1);
tab_helper().DidCommitProvisionalLoadForFrame(
1, base::string16(), true, url, content::PAGE_TRANSITION_LINK,
render_view_host);
}
void SimulateTimeout(const GURL& url,
content::RenderViewHost* render_view_host) {
EXPECT_CALL(mock_reloader(), OnLoadStart(url.SchemeIsSecure())).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, url, false, false, render_view_host);
tab_helper().DidFailProvisionalLoad(
1, base::string16(), true, url, net::ERR_TIMED_OUT, base::string16(),
render_view_host);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, GURL(kErrorPageUrl), true, false, render_view_host);
EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::ERR_TIMED_OUT)).Times(1);
tab_helper().DidCommitProvisionalLoadForFrame(
1, base::string16(), true, GURL(kErrorPageUrl),
content::PAGE_TRANSITION_LINK, render_view_host);
}
void SimulateAbort(const GURL& url,
content::RenderViewHost* render_view_host,
NavigationType navigation_type) {
EXPECT_CALL(mock_reloader(), OnLoadStart(url.SchemeIsSecure())).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, url, false, false, render_view_host);
EXPECT_CALL(mock_reloader(), OnAbort()).Times(1);
if (navigation_type == kSameProcess) {
tab_helper().DidFailProvisionalLoad(
1, base::string16(), true, url, net::ERR_ABORTED, base::string16(),
render_view_host);
} else {
tab_helper().RenderViewDeleted(render_view_host);
}
EXPECT_CALL(mock_reloader(), OnAbort()).Times(0);
}
void SimulateAbortTimeout(const GURL& url,
content::RenderViewHost* render_view_host,
NavigationType navigation_type) {
EXPECT_CALL(mock_reloader(), OnLoadStart(url.SchemeIsSecure())).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, url, false, false, render_view_host);
tab_helper().DidFailProvisionalLoad(
1, base::string16(), true, url, net::ERR_TIMED_OUT, base::string16(),
render_view_host);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, url, true, false, render_view_host);
EXPECT_CALL(mock_reloader(), OnAbort()).Times(1);
if (navigation_type == kSameProcess) {
tab_helper().DidFailProvisionalLoad(
1, base::string16(), true, url, net::ERR_ABORTED, base::string16(),
render_view_host);
} else {
tab_helper().RenderViewDeleted(render_view_host);
}
EXPECT_CALL(mock_reloader(), OnAbort()).Times(0);
}
CaptivePortalTabHelper& tab_helper() {
return tab_helper_;
}
void ObservePortalResult(Result previous_result, Result result) {
content::Source<Profile> source_profile(NULL);
CaptivePortalService::Results results;
results.previous_result = previous_result;
results.result = result;
content::Details<CaptivePortalService::Results> details_results(&results);
EXPECT_CALL(mock_reloader(), OnCaptivePortalResults(previous_result,
result)).Times(1);
tab_helper().Observe(chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT,
source_profile,
details_results);
}
void OnRedirect(ResourceType::Type type, const GURL& new_url, int child_id) {
tab_helper().OnRedirect(child_id, type, new_url);
}
MockCaptivePortalTabReloader& mock_reloader() { return *mock_reloader_; }
void SetIsLoginTab() {
tab_helper().SetIsLoginTab();
}
content::RenderViewHost* render_view_host1() {
return web_contents1_->GetRenderViewHost();
}
content::RenderViewHost* render_view_host2() {
return web_contents2_->GetRenderViewHost();
}
private:
scoped_ptr<content::WebContents> web_contents1_;
scoped_ptr<content::WebContents> web_contents2_;
CaptivePortalTabHelper tab_helper_;
testing::StrictMock<MockCaptivePortalTabReloader>* mock_reloader_;
DISALLOW_COPY_AND_ASSIGN(CaptivePortalTabHelperTest);
};
TEST_F(CaptivePortalTabHelperTest, HttpSuccess) {
SimulateSuccess(GURL(kHttpUrl), render_view_host1());
tab_helper().DidStopLoading(render_view_host1());
}
TEST_F(CaptivePortalTabHelperTest, HttpTimeout) {
SimulateTimeout(GURL(kHttpUrl), render_view_host1());
tab_helper().DidStopLoading(render_view_host1());
}
TEST_F(CaptivePortalTabHelperTest, HttpTimeoutLinkDoctor) {
SimulateTimeout(GURL(kHttpUrl), render_view_host1());
EXPECT_CALL(mock_reloader(), OnLoadStart(false)).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, GURL(kErrorPageUrl), true, false, render_view_host1());
EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::OK)).Times(1);
tab_helper().DidCommitProvisionalLoadForFrame(
1, base::string16(), true, GURL(kErrorPageUrl),
content::PAGE_TRANSITION_LINK, render_view_host1());
tab_helper().DidStopLoading(render_view_host1());
}
TEST_F(CaptivePortalTabHelperTest, HttpsSuccess) {
SimulateSuccess(GURL(kHttpsUrl), render_view_host1());
tab_helper().DidStopLoading(render_view_host1());
EXPECT_FALSE(tab_helper().IsLoginTab());
}
TEST_F(CaptivePortalTabHelperTest, HttpsTimeout) {
SimulateTimeout(GURL(kHttpsUrl), render_view_host1());
SimulateSuccess(GURL(kHttpsUrl), render_view_host1());
EXPECT_FALSE(tab_helper().IsLoginTab());
}
TEST_F(CaptivePortalTabHelperTest, HttpsAbort) {
SimulateAbort(GURL(kHttpsUrl), render_view_host1(), kSameProcess);
SimulateSuccess(GURL(kHttpsUrl), render_view_host1());
EXPECT_FALSE(tab_helper().IsLoginTab());
}
TEST_F(CaptivePortalTabHelperTest, AbortCrossProcess) {
SimulateAbort(GURL(kHttpsUrl2), render_view_host2(), kCrossProcess);
SimulateSuccess(GURL(kHttpUrl), render_view_host1());
EXPECT_FALSE(tab_helper().IsLoginTab());
}
TEST_F(CaptivePortalTabHelperTest, HttpsAbortTimeout) {
SimulateAbortTimeout(GURL(kHttpsUrl), render_view_host1(), kSameProcess);
SimulateSuccess(GURL(kHttpsUrl), render_view_host1());
EXPECT_FALSE(tab_helper().IsLoginTab());
}
TEST_F(CaptivePortalTabHelperTest, AbortTimeoutCrossProcess) {
SimulateAbortTimeout(GURL(kHttpsUrl2), render_view_host2(),
kCrossProcess);
SimulateSuccess(GURL(kHttpsUrl), render_view_host1());
EXPECT_FALSE(tab_helper().IsLoginTab());
}
TEST_F(CaptivePortalTabHelperTest, HttpsAbortTimeoutForCrossProcess) {
SimulateAbortTimeout(GURL(kHttpsUrl), render_view_host1(), kSameProcess);
SimulateSuccess(GURL(kHttpsUrl2), render_view_host2());
EXPECT_FALSE(tab_helper().IsLoginTab());
}
TEST_F(CaptivePortalTabHelperTest, UnexpectedProvisionalLoad) {
GURL same_site_url = GURL(kHttpUrl);
GURL cross_process_url = GURL(kHttpsUrl2);
EXPECT_CALL(mock_reloader(),
OnLoadStart(same_site_url.SchemeIsSecure())).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, same_site_url, false, false, render_view_host1());
EXPECT_CALL(mock_reloader(), OnAbort()).Times(1);
EXPECT_CALL(mock_reloader(),
OnLoadStart(cross_process_url.SchemeIsSecure())).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, cross_process_url, false, false, render_view_host2());
tab_helper().DidFailProvisionalLoad(
1, base::string16(), true, cross_process_url, net::ERR_FAILED,
base::string16(), render_view_host2());
tab_helper().DidFailProvisionalLoad(
1, base::string16(), true, same_site_url, net::ERR_ABORTED,
base::string16(), render_view_host1());
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, GURL(kErrorPageUrl), true, false, render_view_host2());
EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::ERR_FAILED)).Times(1);
tab_helper().DidCommitProvisionalLoadForFrame(
1, base::string16(), true, GURL(kErrorPageUrl),
content::PAGE_TRANSITION_TYPED, render_view_host2());
}
TEST_F(CaptivePortalTabHelperTest, UnexpectedCommit) {
GURL same_site_url = GURL(kHttpUrl);
GURL cross_process_url = GURL(kHttpsUrl2);
EXPECT_CALL(mock_reloader(),
OnLoadStart(same_site_url.SchemeIsSecure())).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, same_site_url, false, false, render_view_host1());
EXPECT_CALL(mock_reloader(), OnAbort()).Times(1);
EXPECT_CALL(mock_reloader(),
OnLoadStart(cross_process_url.SchemeIsSecure())).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, cross_process_url, false, false, render_view_host2());
tab_helper().DidFailProvisionalLoad(
1, base::string16(), true, cross_process_url, net::ERR_FAILED,
base::string16(),
render_view_host2());
EXPECT_CALL(mock_reloader(), OnAbort()).Times(1);
EXPECT_CALL(mock_reloader(),
OnLoadStart(same_site_url.SchemeIsSecure())).Times(1);
EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::OK)).Times(1);
tab_helper().DidCommitProvisionalLoadForFrame(
1, base::string16(), true, same_site_url, content::PAGE_TRANSITION_LINK,
render_view_host1());
}
TEST_F(CaptivePortalTabHelperTest, HttpsSubframe) {
GURL url = GURL(kHttpsUrl);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, false, url, false, false, render_view_host1());
tab_helper().DidCommitProvisionalLoadForFrame(
1, base::string16(), false, url, content::PAGE_TRANSITION_LINK,
render_view_host1());
tab_helper().DidStartProvisionalLoadForFrame(
2, -1, false, url, false, false, render_view_host1());
tab_helper().DidFailProvisionalLoad(
2, base::string16(), false, url, net::ERR_TIMED_OUT, base::string16(),
render_view_host1());
tab_helper().DidStartProvisionalLoadForFrame(
2, -1, false, url, true, false, render_view_host1());
tab_helper().DidFailProvisionalLoad(
2, base::string16(), false, url, net::ERR_ABORTED, base::string16(),
render_view_host1());
tab_helper().DidStartProvisionalLoadForFrame(
3, -1, false, url, false, false, render_view_host1());
tab_helper().DidFailProvisionalLoad(
3, base::string16(), false, url, net::ERR_ABORTED, base::string16(),
render_view_host1());
}
TEST_F(CaptivePortalTabHelperTest, HttpsSubframeParallelError) {
GURL url = GURL(kHttpsUrl);
int frame_id = 2;
int subframe_id = 1;
EXPECT_CALL(mock_reloader(), OnLoadStart(url.SchemeIsSecure())).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
frame_id, -1, true, url, false, false, render_view_host1());
tab_helper().DidStartProvisionalLoadForFrame(
subframe_id, frame_id, false, url, false, false, render_view_host1());
tab_helper().DidFailProvisionalLoad(
frame_id, base::string16(), true, url, net::ERR_UNEXPECTED,
base::string16(), render_view_host1());
tab_helper().DidFailProvisionalLoad(
subframe_id, base::string16(), false, url, net::ERR_TIMED_OUT,
base::string16(), render_view_host1());
tab_helper().DidStartProvisionalLoadForFrame(
frame_id, -1, true, url, true, false, render_view_host1());
tab_helper().DidStartProvisionalLoadForFrame(
subframe_id, frame_id, false, url, true, false, render_view_host1());
tab_helper().DidCommitProvisionalLoadForFrame(
subframe_id, base::string16(), false, url,
content::PAGE_TRANSITION_AUTO_SUBFRAME, render_view_host1());
EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::ERR_UNEXPECTED)).Times(1);
tab_helper().DidCommitProvisionalLoadForFrame(
frame_id, base::string16(), true, url, content::PAGE_TRANSITION_LINK,
render_view_host1());
}
TEST_F(CaptivePortalTabHelperTest, HttpToHttpsRedirectTimeout) {
GURL http_url(kHttpUrl);
EXPECT_CALL(mock_reloader(), OnLoadStart(false)).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, http_url, false, false, render_view_host1());
GURL https_url(kHttpsUrl);
EXPECT_CALL(mock_reloader(), OnRedirect(true)).Times(1);
OnRedirect(ResourceType::MAIN_FRAME, https_url,
render_view_host1()->GetProcess()->GetID());
tab_helper().DidFailProvisionalLoad(
1, base::string16(), true, https_url, net::ERR_TIMED_OUT,
base::string16(), render_view_host1());
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, GURL(kErrorPageUrl), true, false, render_view_host1());
EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::ERR_TIMED_OUT)).Times(1);
tab_helper().DidCommitProvisionalLoadForFrame(
1, base::string16(), true, GURL(kErrorPageUrl),
content::PAGE_TRANSITION_LINK, render_view_host1());
}
TEST_F(CaptivePortalTabHelperTest, HttpsToHttpRedirect) {
GURL https_url(kHttpsUrl);
EXPECT_CALL(mock_reloader(),
OnLoadStart(https_url.SchemeIsSecure())).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(1, -1, true, https_url, false,
false, render_view_host1());
GURL http_url(kHttpUrl);
EXPECT_CALL(mock_reloader(), OnRedirect(http_url.SchemeIsSecure())).Times(1);
OnRedirect(ResourceType::MAIN_FRAME, http_url,
render_view_host1()->GetProcess()->GetID());
EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::OK)).Times(1);
tab_helper().DidCommitProvisionalLoadForFrame(
1, base::string16(), true, http_url, content::PAGE_TRANSITION_LINK,
render_view_host1());
}
TEST_F(CaptivePortalTabHelperTest, HttpToHttpRedirect) {
GURL http_url(kHttpUrl);
EXPECT_CALL(mock_reloader(),
OnLoadStart(http_url.SchemeIsSecure())).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, http_url, false, false, render_view_host1());
EXPECT_CALL(mock_reloader(), OnRedirect(http_url.SchemeIsSecure())).Times(1);
OnRedirect(ResourceType::MAIN_FRAME, http_url,
render_view_host1()->GetProcess()->GetID());
EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::OK)).Times(1);
tab_helper().DidCommitProvisionalLoadForFrame(
1, base::string16(), true, http_url, content::PAGE_TRANSITION_LINK,
render_view_host1());
}
TEST_F(CaptivePortalTabHelperTest, SubframeRedirect) {
GURL http_url(kHttpUrl);
EXPECT_CALL(mock_reloader(), OnLoadStart(false)).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, http_url, false, false, render_view_host1());
GURL https_url(kHttpsUrl);
OnRedirect(ResourceType::SUB_FRAME, https_url,
render_view_host1()->GetProcess()->GetID());
EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::OK)).Times(1);
tab_helper().DidCommitProvisionalLoadForFrame(
1, base::string16(), true, GURL(kErrorPageUrl),
content::PAGE_TRANSITION_LINK, render_view_host1());
}
TEST_F(CaptivePortalTabHelperTest, OtherRenderViewHostRedirect) {
GURL http_url(kHttpUrl);
EXPECT_CALL(mock_reloader(), OnLoadStart(false)).Times(1);
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, http_url, false, false, render_view_host1());
GURL https_url(kHttpsUrl);
OnRedirect(ResourceType::MAIN_FRAME, https_url,
render_view_host2()->GetProcess()->GetID());
tab_helper().DidFailProvisionalLoad(
1, base::string16(), true, https_url, net::ERR_TIMED_OUT,
base::string16(), render_view_host1());
tab_helper().DidStartProvisionalLoadForFrame(
1, -1, true, GURL(kErrorPageUrl), true, false, render_view_host1());
EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::ERR_TIMED_OUT)).Times(1);
tab_helper().DidCommitProvisionalLoadForFrame(
1, base::string16(), true, GURL(kErrorPageUrl),
content::PAGE_TRANSITION_LINK, render_view_host1());
}
TEST_F(CaptivePortalTabHelperTest, LoginTabLogin) {
EXPECT_FALSE(tab_helper().IsLoginTab());
SetIsLoginTab();
EXPECT_TRUE(tab_helper().IsLoginTab());
ObservePortalResult(RESULT_INTERNET_CONNECTED, RESULT_INTERNET_CONNECTED);
EXPECT_FALSE(tab_helper().IsLoginTab());
}
TEST_F(CaptivePortalTabHelperTest, LoginTabError) {
EXPECT_FALSE(tab_helper().IsLoginTab());
SetIsLoginTab();
EXPECT_TRUE(tab_helper().IsLoginTab());
ObservePortalResult(RESULT_INTERNET_CONNECTED, RESULT_NO_RESPONSE);
EXPECT_FALSE(tab_helper().IsLoginTab());
}
TEST_F(CaptivePortalTabHelperTest, LoginTabMultipleResultsBeforeLogin) {
EXPECT_FALSE(tab_helper().IsLoginTab());
SetIsLoginTab();
EXPECT_TRUE(tab_helper().IsLoginTab());
ObservePortalResult(RESULT_INTERNET_CONNECTED, RESULT_BEHIND_CAPTIVE_PORTAL);
EXPECT_TRUE(tab_helper().IsLoginTab());
ObservePortalResult(RESULT_BEHIND_CAPTIVE_PORTAL,
RESULT_BEHIND_CAPTIVE_PORTAL);
EXPECT_TRUE(tab_helper().IsLoginTab());
ObservePortalResult(RESULT_NO_RESPONSE, RESULT_INTERNET_CONNECTED);
EXPECT_FALSE(tab_helper().IsLoginTab());
}
TEST_F(CaptivePortalTabHelperTest, NoLoginTab) {
EXPECT_FALSE(tab_helper().IsLoginTab());
ObservePortalResult(RESULT_INTERNET_CONNECTED, RESULT_BEHIND_CAPTIVE_PORTAL);
EXPECT_FALSE(tab_helper().IsLoginTab());
ObservePortalResult(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_NO_RESPONSE);
EXPECT_FALSE(tab_helper().IsLoginTab());
ObservePortalResult(RESULT_NO_RESPONSE, RESULT_INTERNET_CONNECTED);
EXPECT_FALSE(tab_helper().IsLoginTab());
}
}