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
,
64 int routing_id
) override
{
65 ASSERT_EQ(block_state_
, ExternalProtocolHandler::UNKNOWN
);
66 ASSERT_NE(os_state_
, ShellIntegration::IS_DEFAULT
);
70 void LaunchUrlWithoutSecurityCheck(const GURL
& url
) override
{
71 ASSERT_EQ(block_state_
, ExternalProtocolHandler::DONT_BLOCK
);
72 ASSERT_NE(os_state_
, ShellIntegration::IS_DEFAULT
);
76 void FinishedProcessingCheck() override
{
77 base::MessageLoop::current()->Quit();
80 void set_os_state(ShellIntegration::DefaultWebClientState value
) {
84 void set_block_state(ExternalProtocolHandler::BlockState value
) {
88 bool has_launched() { return has_launched_
; }
89 bool has_prompted() { return has_prompted_
; }
90 bool has_blocked() { return has_blocked_
; }
93 ExternalProtocolHandler::BlockState block_state_
;
94 ShellIntegration::DefaultWebClientState os_state_
;
100 class ExternalProtocolHandlerTest
: public testing::Test
{
102 ExternalProtocolHandlerTest()
103 : ui_thread_(BrowserThread::UI
, base::MessageLoop::current()),
104 file_thread_(BrowserThread::FILE) {}
106 void SetUp() override
{ file_thread_
.Start(); }
108 void TearDown() override
{
109 // Ensure that g_accept_requests gets set back to true after test execution.
110 ExternalProtocolHandler::PermitLaunchUrl();
113 void DoTest(ExternalProtocolHandler::BlockState block_state
,
114 ShellIntegration::DefaultWebClientState os_state
,
115 bool should_prompt
, bool should_launch
, bool should_block
) {
116 GURL
url("mailto:test@test.com");
117 ASSERT_FALSE(delegate_
.has_prompted());
118 ASSERT_FALSE(delegate_
.has_launched());
119 ASSERT_FALSE(delegate_
.has_blocked());
121 delegate_
.set_block_state(block_state
);
122 delegate_
.set_os_state(os_state
);
123 ExternalProtocolHandler::LaunchUrlWithDelegate(url
, 0, 0, &delegate_
);
124 if (block_state
!= ExternalProtocolHandler::BLOCK
)
125 base::MessageLoop::current()->Run();
127 ASSERT_EQ(should_prompt
, delegate_
.has_prompted());
128 ASSERT_EQ(should_launch
, delegate_
.has_launched());
129 ASSERT_EQ(should_block
, delegate_
.has_blocked());
132 base::MessageLoopForUI ui_message_loop_
;
133 content::TestBrowserThread ui_thread_
;
134 content::TestBrowserThread file_thread_
;
136 FakeExternalProtocolHandlerDelegate delegate_
;
139 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeBlockedChromeDefault
) {
140 DoTest(ExternalProtocolHandler::BLOCK
,
141 ShellIntegration::IS_DEFAULT
,
145 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeBlockedChromeNotDefault
) {
146 DoTest(ExternalProtocolHandler::BLOCK
,
147 ShellIntegration::NOT_DEFAULT
,
151 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeBlockedChromeUnknown
) {
152 DoTest(ExternalProtocolHandler::BLOCK
,
153 ShellIntegration::UNKNOWN_DEFAULT
,
157 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnBlockedChromeDefault
) {
158 DoTest(ExternalProtocolHandler::DONT_BLOCK
,
159 ShellIntegration::IS_DEFAULT
,
163 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnBlockedChromeNotDefault
) {
164 DoTest(ExternalProtocolHandler::DONT_BLOCK
,
165 ShellIntegration::NOT_DEFAULT
,
169 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnBlockedChromeUnknown
) {
170 DoTest(ExternalProtocolHandler::DONT_BLOCK
,
171 ShellIntegration::UNKNOWN_DEFAULT
,
175 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnknownChromeDefault
) {
176 DoTest(ExternalProtocolHandler::UNKNOWN
,
177 ShellIntegration::IS_DEFAULT
,
181 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnknownChromeNotDefault
) {
182 DoTest(ExternalProtocolHandler::UNKNOWN
,
183 ShellIntegration::NOT_DEFAULT
,
187 TEST_F(ExternalProtocolHandlerTest
, TestLaunchSchemeUnknownChromeUnknown
) {
188 DoTest(ExternalProtocolHandler::UNKNOWN
,
189 ShellIntegration::UNKNOWN_DEFAULT
,