Added bowser test for Captive Portal Window.

BUG=244175
TEST=interactive_ui_tests:CaptivePortalWindow*

Review URL: https://chromiumcodereview.appspot.com/15780006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@202544 0039d316-1c4b-4281-b951-d872f2087c98
parent c93e0b5a
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h"
#include "chrome/browser/chromeos/login/captive_portal_window_proxy.h"
#include "chrome/browser/chromeos/login/login_display_host_impl.h"
#include "chromeos/chromeos_switches.h"
namespace chromeos {
namespace {
// Stub implementation of CaptivePortalWindowProxyDelegate, does
// nothing and used to instantiate CaptivePortalWindowProxy.
class CaptivePortalWindowProxyStubDelegate
: public CaptivePortalWindowProxyDelegate {
public:
CaptivePortalWindowProxyStubDelegate(): num_portal_notifications_(0) {
}
virtual ~CaptivePortalWindowProxyStubDelegate() {
}
virtual void OnPortalDetected() OVERRIDE {
++num_portal_notifications_;
}
int num_portal_notifications() const { return num_portal_notifications_; }
private:
int num_portal_notifications_;
};
} // namespace
class CaptivePortalWindowTest : public CrosInProcessBrowserTest {
protected:
void ShowIfRedirected() {
captive_portal_window_proxy_->ShowIfRedirected();
}
void Show() {
captive_portal_window_proxy_->Show();
}
void Close() {
captive_portal_window_proxy_->Close();
}
void OnRedirected() {
captive_portal_window_proxy_->OnRedirected();
}
void OnOriginalURLLoaded() {
captive_portal_window_proxy_->OnOriginalURLLoaded();
}
void CheckState(bool is_shown, int num_portal_notifications) {
bool actual_is_shown = (CaptivePortalWindowProxy::STATE_DISPLAYED ==
captive_portal_window_proxy_->GetState());
ASSERT_EQ(is_shown, actual_is_shown);
ASSERT_EQ(num_portal_notifications, delegate_.num_portal_notifications());
}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
command_line->AppendSwitch(chromeos::switches::kLoginManager);
}
virtual void SetUpOnMainThread() OVERRIDE {
CHECK(LoginDisplayHostImpl::default_host());
gfx::NativeWindow native_window =
LoginDisplayHostImpl::default_host()->GetNativeWindow();
captive_portal_window_proxy_.reset(
new CaptivePortalWindowProxy(&delegate_, native_window));
}
private:
scoped_ptr<CaptivePortalWindowProxy> captive_portal_window_proxy_;
CaptivePortalWindowProxyStubDelegate delegate_;
};
IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, ShowClose) {
CheckState(false, 0);
Show();
CheckState(true, 0);
Close();
CheckState(false, 0);
}
IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, OnRedirected) {
CheckState(false, 0);
ShowIfRedirected();
CheckState(false, 0);
OnRedirected();
CheckState(true, 1);
Close();
CheckState(false, 1);
}
IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, OnOriginalURLLoaded) {
CheckState(false, 0);
ShowIfRedirected();
CheckState(false, 0);
OnRedirected();
CheckState(true, 1);
OnOriginalURLLoaded();
CheckState(false, 1);
}
IN_PROC_BROWSER_TEST_F(CaptivePortalWindowTest, MultipleCalls) {
CheckState(false, 0);
ShowIfRedirected();
CheckState(false, 0);
Show();
CheckState(true, 0);
Close();
CheckState(false, 0);
OnRedirected();
CheckState(false, 1);
OnOriginalURLLoaded();
CheckState(false, 1);
Show();
CheckState(true, 1);
OnRedirected();
CheckState(true, 2);
Close();
CheckState(false, 2);
OnOriginalURLLoaded();
CheckState(false, 2);
}
} // namespace chromeos
......@@ -23,34 +23,22 @@ CaptivePortalWindowProxy::CaptivePortalWindowProxy(Delegate* delegate,
: delegate_(delegate),
widget_(NULL),
parent_(parent) {
DCHECK(GetState() == STATE_IDLE);
}
CaptivePortalWindowProxy::~CaptivePortalWindowProxy() {
if (widget_) {
widget_->RemoveObserver(this);
widget_->Close();
}
if (!widget_)
return;
DCHECK(GetState() == STATE_DISPLAYED);
widget_->RemoveObserver(this);
widget_->Close();
}
void CaptivePortalWindowProxy::ShowIfRedirected() {
if (widget_) {
// Invalid state as when widget is created (Show())
// CaptivePortalView ownership is transferred to it.
if (captive_portal_view_.get()) {
NOTREACHED();
}
// Dialog is already shown, no need to reload.
if (GetState() != STATE_IDLE)
return;
}
// Dialog is not initialized yet.
if (!captive_portal_view_.get()) {
captive_portal_view_.reset(
new CaptivePortalView(ProfileHelper::GetSigninProfile(), this));
}
// If dialog has been created (but not shown) previously, force reload.
captive_portal_view_->StartLoad();
InitCaptivePortalView();
DCHECK(GetState() == STATE_WAITING_FOR_REDIRECTION);
}
void CaptivePortalWindowProxy::Show() {
......@@ -60,10 +48,10 @@ void CaptivePortalWindowProxy::Show() {
return;
}
if (!captive_portal_view_.get() || widget_) {
// Dialog is already shown, do nothing.
if (GetState() == STATE_DISPLAYED) // Dialog is already shown, do nothing.
return;
}
InitCaptivePortalView();
CaptivePortalView* captive_portal_view = captive_portal_view_.release();
widget_ = views::Widget::CreateWindowWithParent(
......@@ -77,18 +65,18 @@ void CaptivePortalWindowProxy::Show() {
widget_->AddObserver(this);
widget_->Show();
DCHECK(GetState() == STATE_DISPLAYED);
}
void CaptivePortalWindowProxy::Close() {
if (widget_) {
if (GetState() == STATE_DISPLAYED)
widget_->Close();
} else {
captive_portal_view_.reset();
}
captive_portal_view_.reset();
}
void CaptivePortalWindowProxy::OnRedirected() {
Show();
if (GetState() == STATE_WAITING_FOR_REDIRECTION)
Show();
delegate_->OnPortalDetected();
}
......@@ -96,11 +84,39 @@ void CaptivePortalWindowProxy::OnOriginalURLLoaded() {
Close();
}
void CaptivePortalWindowProxy::OnWidgetDestroying(views::Widget* widget) {
void CaptivePortalWindowProxy::OnWidgetClosing(views::Widget* widget) {
DCHECK(GetState() == STATE_DISPLAYED);
DCHECK(widget == widget_);
DCHECK(captive_portal_view_.get() == NULL);
widget->RemoveObserver(this);
widget_ = NULL;
DCHECK(GetState() == STATE_IDLE);
}
void CaptivePortalWindowProxy::InitCaptivePortalView() {
DCHECK(GetState() == STATE_IDLE ||
GetState() == STATE_WAITING_FOR_REDIRECTION);
if (!captive_portal_view_.get()) {
captive_portal_view_.reset(
new CaptivePortalView(ProfileHelper::GetSigninProfile(), this));
}
captive_portal_view_->StartLoad();
}
CaptivePortalWindowProxy::State CaptivePortalWindowProxy::GetState() const {
if (widget_ == NULL) {
if (captive_portal_view_.get() == NULL)
return STATE_IDLE;
else
return STATE_WAITING_FOR_REDIRECTION;
} else {
if (captive_portal_view_.get() == NULL)
return STATE_DISPLAYED;
else
NOTREACHED();
}
return STATE_UNKNOWN;
}
} // namespace chromeos
......@@ -58,9 +58,35 @@ class CaptivePortalWindowProxy : public views::WidgetObserver {
void OnOriginalURLLoaded();
// Overridden from views::WidgetObserver:
virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE;
virtual void OnWidgetClosing(views::Widget* widget) OVERRIDE;
private:
friend class CaptivePortalWindowTest;
// Possible transitions between states:
//
// wp(ShowIfRedirected(), WAITING_FOR_REDIRECTION) = IDLE
// wp(Show(), DISPLAYED) = IDLE | WAITING_FOR_REDIRECTION
// wp(Close(), IDLE) = WAITING_FOR_REDIRECTION | DISPLAYED
// wp(OnRedirected(), DISPLAYED) = WAITING_FOR_REDIRECTION
// wp(OnOriginalURLLoaded(), IDLE) = WAITING_FOR_REDIRECTION | DISPLAYED
//
// where wp(E, S) is a weakest precondition (initial state) such
// that after execution of E the system will be surely in the state S.
enum State {
STATE_IDLE = 0,
STATE_WAITING_FOR_REDIRECTION,
STATE_DISPLAYED,
STATE_UNKNOWN
};
// Initializes |captive_portal_view_| if it is not initialized and
// starts loading Captive Portal redirect URL.
void InitCaptivePortalView();
// Returns symbolic state name based on internal state.
State GetState() const;
Delegate* delegate_;
views::Widget* widget_;
scoped_ptr<CaptivePortalView> captive_portal_view_;
......
......@@ -403,6 +403,7 @@
'browser/chromeos/input_method/textinput_surroundingtext_browsertest.cc',
'browser/chromeos/input_method/textinput_test_helper.cc',
'browser/chromeos/input_method/textinput_test_helper.h',
'browser/chromeos/login/captive_portal_window_browsertest.cc',
'browser/chromeos/login/eula_browsertest.cc',
'browser/chromeos/login/login_browsertest.cc',
'browser/chromeos/login/mock_authenticator.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment