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.
5 #include "chrome/browser/external_protocol/external_protocol_handler.h"
7 #include "base/message_loop/message_loop.h"
8 #include "content/public/test/test_browser_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using content::BrowserThread
;
13 class FakeExternalProtocolHandlerWorker
14 : public ShellIntegration::DefaultProtocolClientWorker
{
16 FakeExternalProtocolHandlerWorker(
17 ShellIntegration::DefaultWebClientObserver
* observer
,
18 const std::string
& protocol
,
19 ShellIntegration::DefaultWebClientState os_state
)
20 : ShellIntegration::DefaultProtocolClientWorker(observer
, protocol
),
21 os_state_(os_state
) {}
24 ~FakeExternalProtocolHandlerWorker() override
{}
26 ShellIntegration::DefaultWebClientState
CheckIsDefault() override
{
30 bool SetAsDefault(bool interactive_permitted
) override
{ return true; }
32 ShellIntegration::DefaultWebClientState os_state_
;
35 class FakeExternalProtocolHandlerDelegate
36 : public ExternalProtocolHandler::Delegate
{
38 FakeExternalProtocolHandlerDelegate()
39 : block_state_(ExternalProtocolHandler::BLOCK
),
40 os_state_(ShellIntegration::UNKNOWN_DEFAULT
),
43 has_blocked_(false) {}
45 ShellIntegration::DefaultProtocolClientWorker
* CreateShellWorker(
46 ShellIntegration::DefaultWebClientObserver
* observer
,
47 const std::string
& protocol
) override
{
48 return new FakeExternalProtocolHandlerWorker(observer
, protocol
, os_state_
);
51 ExternalProtocolHandler::BlockState
GetBlockState(
52 const std::string
& scheme
) override
{
56 void BlockRequest() override
{
57 ASSERT_TRUE(block_state_
== ExternalProtocolHandler::BLOCK
||
58 os_state_
== ShellIntegration::IS_DEFAULT
);
62 void RunExternalProtocolDialog(const GURL
& url
,
63 int render_process_host_id
,
65 ui::PageTransition page_transition
,
66 bool has_user_gesture
) override
{
67 ASSERT_EQ(block_state_
, ExternalProtocolHandler::UNKNOWN
);
68 ASSERT_NE(os_state_
, ShellIntegration::IS_DEFAULT
);
72 void LaunchUrlWithoutSecurityCheck(const GURL
& url
) override
{
73 ASSERT_EQ(block_state_
, ExternalProtocolHandler::DONT_BLOCK
);
74 ASSERT_NE(os_state_
, ShellIntegration::IS_DEFAULT
);
78 void FinishedProcessingCheck() override
{
79 base::MessageLoop::current()->Quit();
82 void set_os_state(ShellIntegration::DefaultWebClientState value
) {
86 void set_block_state(ExternalProtocolHandler::BlockState value
) {
90 bool has_launched() { return has_launched_
; }
91 bool has_prompted() { return has_prompted_
; }
92 bool has_blocked() { return has_blocked_
; }
95 ExternalProtocolHandler::BlockState block_state_
;
96 ShellIntegration::DefaultWebClientState os_state_
;
102 class ExternalProtocolHandlerTest
: public testing::Test
{
104 ExternalProtocolHandlerTest()
105 : ui_thread_(BrowserThread::UI
, base::MessageLoop::current()),
106 file_thread_(BrowserThread::FILE) {}
108 void SetUp() override
{ file_thread_
.Start(); }
110 void TearDown() override
{
111 // Ensure that g_accept_requests gets set back to true after test execution.
112 ExternalProtocolHandler::PermitLaunchUrl();
115 void DoTest(ExternalProtocolHandler::BlockState block_state
,
116 ShellIntegration::DefaultWebClientState os_state
,
117 bool should_prompt
, bool should_launch
, bool should_block
) {
118 GURL
url("mailto:test@test.com");
119 ASSERT_FALSE(delegate_
.has_prompted());
120 ASSERT_FALSE(delegate_
.has_launched());
121 ASSERT_FALSE(delegate_
.has_blocked());
123 delegate_
.set_block_state(block_state
);
124 delegate_
.set_os_state(os_state
);
125 ExternalProtocolHandler::LaunchUrlWithDelegate(
126 url
, 0, 0, ui::PAGE_TRANSITION_LINK
, true, &delegate_
);
127 if (block_state
!= ExternalProtocolHandler::BLOCK
)
128 base::MessageLoop::current()->Run();
130 ASSERT_EQ(should_prompt
, delegate_
.has_prompted());
131 ASSERT_EQ(should_launch
, delegate_
.has_launched());
132 ASSERT_EQ(should_block
, delegate_
.has_blocked());
135 base::MessageLoopForUI ui_message_loop_
;
136 content::TestBrowserThread ui_thread_
;
137 content::TestBrowserThread file_thread_
;
139 FakeExternalProtocolHandlerDelegate delegate_
;
142 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeBlockedChromeDefault
) {
143 DoTest(ExternalProtocolHandler::BLOCK
,
144 ShellIntegration::IS_DEFAULT
,
148 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeBlockedChromeNotDefault
) {
149 DoTest(ExternalProtocolHandler::BLOCK
,
150 ShellIntegration::NOT_DEFAULT
,
154 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeBlockedChromeUnknown
) {
155 DoTest(ExternalProtocolHandler::BLOCK
,
156 ShellIntegration::UNKNOWN_DEFAULT
,
160 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnBlockedChromeDefault
) {
161 DoTest(ExternalProtocolHandler::DONT_BLOCK
,
162 ShellIntegration::IS_DEFAULT
,
166 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnBlockedChromeNotDefault
) {
167 DoTest(ExternalProtocolHandler::DONT_BLOCK
,
168 ShellIntegration::NOT_DEFAULT
,
172 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnBlockedChromeUnknown
) {
173 DoTest(ExternalProtocolHandler::DONT_BLOCK
,
174 ShellIntegration::UNKNOWN_DEFAULT
,
178 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnknownChromeDefault
) {
179 DoTest(ExternalProtocolHandler::UNKNOWN
,
180 ShellIntegration::IS_DEFAULT
,
184 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnknownChromeNotDefault
) {
185 DoTest(ExternalProtocolHandler::UNKNOWN
,
186 ShellIntegration::NOT_DEFAULT
,
190 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnknownChromeUnknown
) {
191 DoTest(ExternalProtocolHandler::UNKNOWN
,
192 ShellIntegration::UNKNOWN_DEFAULT
,