Commit 6f0a6f4b authored by dominickn's avatar dominickn Committed by Commit bot

Add a browser test for the views external protocol dialog.

This CL tests that the views external protocol dialog behaves correctly
when it is Accepted, Canceled, and Closed. In particular, the status of
the dialog's checkbox is verified; when the dialog is Closed without
a definite user choice of Accept or Cancel, it is ensured that the
checkbox is always reported as unchecked. This is to prevent dismissing
the dialog from accidentally persisting a permanent block on external
protocols.

The dialog is not used on ChromeOS, so the test is only run on Linux,
Windows, and MacViews.

BUG=671658

Review-Url: https://codereview.chromium.org/2610793002
Cr-Commit-Position: refs/heads/master@{#441869}
parent ddcf5446
......@@ -13,6 +13,10 @@
class ProtocolDialogDelegate;
namespace test {
class ExternalProtocolDialogTestApi;
}
namespace views {
class MessageBoxView;
}
......@@ -40,6 +44,8 @@ class ExternalProtocolDialog : public views::DialogDelegate {
ui::ModalType GetModalType() const override;
private:
friend class test::ExternalProtocolDialogTestApi;
const std::unique_ptr<const ProtocolDialogDelegate> delegate_;
// The message box view whose commands we handle.
......
// Copyright 2017 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/macros.h"
#include "base/memory/ptr_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/external_protocol_dialog_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/external_protocol_dialog.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "ui/views/controls/message_box_view.h"
#include "url/gurl.h"
namespace test {
class ExternalProtocolDialogTestApi {
public:
explicit ExternalProtocolDialogTestApi(ExternalProtocolDialog* dialog)
: dialog_(dialog) {}
void SetCheckBoxSelected(bool checked) {
dialog_->message_box_view_->SetCheckBoxSelected(checked);
}
private:
ExternalProtocolDialog* dialog_;
DISALLOW_COPY_AND_ASSIGN(ExternalProtocolDialogTestApi);
};
} // namespace test
// Wrapper dialog delegate that sets |called|, |accept|, |cancel|, and
// |dont_block| bools based on what is called by the ExternalProtocolDialog.
class TestExternalProtocolDialogDelegate
: public ExternalProtocolDialogDelegate {
public:
TestExternalProtocolDialogDelegate(const GURL& url,
int render_process_host_id,
int routing_id,
bool* called,
bool* accept,
bool* cancel,
bool* dont_block)
: ExternalProtocolDialogDelegate(url, render_process_host_id, routing_id),
called_(called),
accept_(accept),
cancel_(cancel),
dont_block_(dont_block) {}
// ExternalProtocolDialogDelegate:
void DoAccept(const GURL& url, bool dont_block) const override {
// Don't call the base impl because it will actually launch |url|.
*called_ = true;
*accept_ = true;
*cancel_ = false;
*dont_block_ = dont_block;
}
void DoCancel(const GURL& url, bool dont_block) const override {
// Don't call the base impl because it will actually launch |url|.
*called_ = true;
*accept_ = false;
*cancel_ = true;
*dont_block_ = dont_block;
}
private:
bool* called_;
bool* accept_;
bool* cancel_;
bool* dont_block_;
DISALLOW_COPY_AND_ASSIGN(TestExternalProtocolDialogDelegate);
};
class ExternalProtocolDialogBrowserTest : public InProcessBrowserTest {
public:
ExternalProtocolDialogBrowserTest() {}
void ShowDialog() {
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
int render_process_host_id = web_contents->GetRenderProcessHost()->GetID();
int routing_id = web_contents->GetRenderViewHost()->GetRoutingID();
dialog_ = new ExternalProtocolDialog(
base::MakeUnique<TestExternalProtocolDialogDelegate>(
GURL("telnet://12345"), render_process_host_id, routing_id,
&called_, &accept_, &cancel_, &dont_block_),
render_process_host_id, routing_id);
}
void SetChecked(bool checked) {
test::ExternalProtocolDialogTestApi(dialog_).SetCheckBoxSelected(checked);
}
protected:
ExternalProtocolDialog* dialog_ = nullptr;
bool called_ = false;
bool accept_ = false;
bool cancel_ = false;
bool dont_block_ = false;
private:
DISALLOW_COPY_AND_ASSIGN(ExternalProtocolDialogBrowserTest);
};
IN_PROC_BROWSER_TEST_F(ExternalProtocolDialogBrowserTest, TestAccept) {
ShowDialog();
EXPECT_TRUE(dialog_->Accept());
EXPECT_TRUE(called_);
EXPECT_TRUE(accept_);
EXPECT_FALSE(cancel_);
EXPECT_FALSE(dont_block_);
}
IN_PROC_BROWSER_TEST_F(ExternalProtocolDialogBrowserTest,
TestAcceptWithChecked) {
ShowDialog();
SetChecked(true);
EXPECT_TRUE(dialog_->Accept());
EXPECT_TRUE(called_);
EXPECT_TRUE(accept_);
EXPECT_FALSE(cancel_);
EXPECT_TRUE(dont_block_);
}
IN_PROC_BROWSER_TEST_F(ExternalProtocolDialogBrowserTest, TestCancel) {
ShowDialog();
EXPECT_TRUE(dialog_->Cancel());
EXPECT_TRUE(called_);
EXPECT_FALSE(accept_);
EXPECT_TRUE(cancel_);
EXPECT_FALSE(dont_block_);
}
IN_PROC_BROWSER_TEST_F(ExternalProtocolDialogBrowserTest,
TestCancelWithChecked) {
ShowDialog();
SetChecked(true);
EXPECT_TRUE(dialog_->Cancel());
EXPECT_TRUE(called_);
EXPECT_FALSE(accept_);
EXPECT_TRUE(cancel_);
EXPECT_TRUE(dont_block_);
}
IN_PROC_BROWSER_TEST_F(ExternalProtocolDialogBrowserTest, TestClose) {
// Closing the dialog should always call DoCancel() with |dont_block| = false.
ShowDialog();
EXPECT_TRUE(dialog_->Close());
EXPECT_TRUE(called_);
EXPECT_FALSE(accept_);
EXPECT_TRUE(cancel_);
EXPECT_FALSE(dont_block_);
}
IN_PROC_BROWSER_TEST_F(ExternalProtocolDialogBrowserTest,
TestCloseWithChecked) {
// Closing the dialog should always call DoCancel() with |dont_block| = false.
ShowDialog();
SetChecked(true);
EXPECT_TRUE(dialog_->Close());
EXPECT_TRUE(called_);
EXPECT_FALSE(accept_);
EXPECT_TRUE(cancel_);
EXPECT_FALSE(dont_block_);
}
......@@ -2126,7 +2126,10 @@ test("browser_tests") {
]
deps += [ "//ui/views" ]
if (!is_chromeos && (!is_mac || mac_views_browser)) {
sources += [ "../browser/ui/views/profiles/profile_chooser_view_browsertest.cc" ]
sources += [
"../browser/ui/views/external_protocol_dialog_browsertest.cc",
"../browser/ui/views/profiles/profile_chooser_view_browsertest.cc",
]
}
if (!is_mac || mac_views_browser) {
sources += [
......
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