1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// Copyright (c) 2012 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 "chrome/browser/external_protocol/external_protocol_handler.h"
#include "base/message_loop/message_loop.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
class FakeExternalProtocolHandlerWorker
: public ShellIntegration::DefaultProtocolClientWorker {
public:
FakeExternalProtocolHandlerWorker(
ShellIntegration::DefaultWebClientObserver* observer,
const std::string& protocol,
ShellIntegration::DefaultWebClientState os_state)
: ShellIntegration::DefaultProtocolClientWorker(observer, protocol),
os_state_(os_state) {}
private:
~FakeExternalProtocolHandlerWorker() override {}
ShellIntegration::DefaultWebClientState CheckIsDefault() override {
return os_state_;
}
bool SetAsDefault(bool interactive_permitted) override { return true; }
ShellIntegration::DefaultWebClientState os_state_;
};
class FakeExternalProtocolHandlerDelegate
: public ExternalProtocolHandler::Delegate {
public:
FakeExternalProtocolHandlerDelegate()
: block_state_(ExternalProtocolHandler::BLOCK),
os_state_(ShellIntegration::UNKNOWN_DEFAULT),
has_launched_(false),
has_prompted_(false),
has_blocked_ (false) {}
ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker(
ShellIntegration::DefaultWebClientObserver* observer,
const std::string& protocol) override {
return new FakeExternalProtocolHandlerWorker(observer, protocol, os_state_);
}
ExternalProtocolHandler::BlockState GetBlockState(
const std::string& scheme) override {
return block_state_;
}
void BlockRequest() override {
ASSERT_TRUE(block_state_ == ExternalProtocolHandler::BLOCK ||
os_state_ == ShellIntegration::IS_DEFAULT);
has_blocked_ = true;
}
void RunExternalProtocolDialog(const GURL& url,
int render_process_host_id,
int routing_id) override {
ASSERT_EQ(block_state_, ExternalProtocolHandler::UNKNOWN);
ASSERT_NE(os_state_, ShellIntegration::IS_DEFAULT);
has_prompted_ = true;
}
void LaunchUrlWithoutSecurityCheck(const GURL& url) override {
ASSERT_EQ(block_state_, ExternalProtocolHandler::DONT_BLOCK);
ASSERT_NE(os_state_, ShellIntegration::IS_DEFAULT);
has_launched_ = true;
}
void FinishedProcessingCheck() override {
base::MessageLoop::current()->Quit();
}
void set_os_state(ShellIntegration::DefaultWebClientState value) {
os_state_ = value;
}
void set_block_state(ExternalProtocolHandler::BlockState value) {
block_state_ = value;
}
bool has_launched() { return has_launched_; }
bool has_prompted() { return has_prompted_; }
bool has_blocked() { return has_blocked_; }
private:
ExternalProtocolHandler::BlockState block_state_;
ShellIntegration::DefaultWebClientState os_state_;
bool has_launched_;
bool has_prompted_;
bool has_blocked_;
};
class ExternalProtocolHandlerTest : public testing::Test {
protected:
ExternalProtocolHandlerTest()
: ui_thread_(BrowserThread::UI, base::MessageLoop::current()),
file_thread_(BrowserThread::FILE) {}
void SetUp() override { file_thread_.Start(); }
void TearDown() override {
// Ensure that g_accept_requests gets set back to true after test execution.
ExternalProtocolHandler::PermitLaunchUrl();
}
void DoTest(ExternalProtocolHandler::BlockState block_state,
ShellIntegration::DefaultWebClientState os_state,
bool should_prompt, bool should_launch, bool should_block) {
GURL url("mailto:test@test.com");
ASSERT_FALSE(delegate_.has_prompted());
ASSERT_FALSE(delegate_.has_launched());
ASSERT_FALSE(delegate_.has_blocked());
delegate_.set_block_state(block_state);
delegate_.set_os_state(os_state);
ExternalProtocolHandler::LaunchUrlWithDelegate(url, 0, 0, &delegate_);
if (block_state != ExternalProtocolHandler::BLOCK)
base::MessageLoop::current()->Run();
ASSERT_EQ(should_prompt, delegate_.has_prompted());
ASSERT_EQ(should_launch, delegate_.has_launched());
ASSERT_EQ(should_block, delegate_.has_blocked());
}
base::MessageLoopForUI ui_message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
FakeExternalProtocolHandlerDelegate delegate_;
};
TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeDefault) {
DoTest(ExternalProtocolHandler::BLOCK,
ShellIntegration::IS_DEFAULT,
false, false, true);
}
TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeNotDefault) {
DoTest(ExternalProtocolHandler::BLOCK,
ShellIntegration::NOT_DEFAULT,
false, false, true);
}
TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeUnknown) {
DoTest(ExternalProtocolHandler::BLOCK,
ShellIntegration::UNKNOWN_DEFAULT,
false, false, true);
}
TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeDefault) {
DoTest(ExternalProtocolHandler::DONT_BLOCK,
ShellIntegration::IS_DEFAULT,
false, false, true);
}
TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeNotDefault) {
DoTest(ExternalProtocolHandler::DONT_BLOCK,
ShellIntegration::NOT_DEFAULT,
false, true, false);
}
TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeUnknown) {
DoTest(ExternalProtocolHandler::DONT_BLOCK,
ShellIntegration::UNKNOWN_DEFAULT,
false, true, false);
}
TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeDefault) {
DoTest(ExternalProtocolHandler::UNKNOWN,
ShellIntegration::IS_DEFAULT,
false, false, true);
}
TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeNotDefault) {
DoTest(ExternalProtocolHandler::UNKNOWN,
ShellIntegration::NOT_DEFAULT,
true, false, false);
}
TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeUnknown) {
DoTest(ExternalProtocolHandler::UNKNOWN,
ShellIntegration::UNKNOWN_DEFAULT,
true, false, false);
}
|