Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / external_protocol / external_protocol_handler_unittest.cc
blob53cfd6633c2a8082b30a4f915820805abb46137c
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 {
15 public:
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) {}
23 private:
24 ~FakeExternalProtocolHandlerWorker() override {}
26 ShellIntegration::DefaultWebClientState CheckIsDefault() override {
27 return os_state_;
30 bool SetAsDefault(bool interactive_permitted) override { return true; }
32 ShellIntegration::DefaultWebClientState os_state_;
35 class FakeExternalProtocolHandlerDelegate
36 : public ExternalProtocolHandler::Delegate {
37 public:
38 FakeExternalProtocolHandlerDelegate()
39 : block_state_(ExternalProtocolHandler::BLOCK),
40 os_state_(ShellIntegration::UNKNOWN_DEFAULT),
41 has_launched_(false),
42 has_prompted_(false),
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 {
53 return block_state_;
56 void BlockRequest() override {
57 ASSERT_TRUE(block_state_ == ExternalProtocolHandler::BLOCK ||
58 os_state_ == ShellIntegration::IS_DEFAULT);
59 has_blocked_ = true;
62 void RunExternalProtocolDialog(const GURL& url,
63 int render_process_host_id,
64 int routing_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);
69 has_prompted_ = true;
72 void LaunchUrlWithoutSecurityCheck(const GURL& url) override {
73 ASSERT_EQ(block_state_, ExternalProtocolHandler::DONT_BLOCK);
74 ASSERT_NE(os_state_, ShellIntegration::IS_DEFAULT);
75 has_launched_ = true;
78 void FinishedProcessingCheck() override {
79 base::MessageLoop::current()->Quit();
82 void set_os_state(ShellIntegration::DefaultWebClientState value) {
83 os_state_ = value;
86 void set_block_state(ExternalProtocolHandler::BlockState value) {
87 block_state_ = value;
90 bool has_launched() { return has_launched_; }
91 bool has_prompted() { return has_prompted_; }
92 bool has_blocked() { return has_blocked_; }
94 private:
95 ExternalProtocolHandler::BlockState block_state_;
96 ShellIntegration::DefaultWebClientState os_state_;
97 bool has_launched_;
98 bool has_prompted_;
99 bool has_blocked_;
102 class ExternalProtocolHandlerTest : public testing::Test {
103 protected:
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,
145 false, false, true);
148 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeNotDefault) {
149 DoTest(ExternalProtocolHandler::BLOCK,
150 ShellIntegration::NOT_DEFAULT,
151 false, false, true);
154 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeUnknown) {
155 DoTest(ExternalProtocolHandler::BLOCK,
156 ShellIntegration::UNKNOWN_DEFAULT,
157 false, false, true);
160 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeDefault) {
161 DoTest(ExternalProtocolHandler::DONT_BLOCK,
162 ShellIntegration::IS_DEFAULT,
163 false, false, true);
166 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeNotDefault) {
167 DoTest(ExternalProtocolHandler::DONT_BLOCK,
168 ShellIntegration::NOT_DEFAULT,
169 false, true, false);
172 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeUnknown) {
173 DoTest(ExternalProtocolHandler::DONT_BLOCK,
174 ShellIntegration::UNKNOWN_DEFAULT,
175 false, true, false);
178 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeDefault) {
179 DoTest(ExternalProtocolHandler::UNKNOWN,
180 ShellIntegration::IS_DEFAULT,
181 false, false, true);
184 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeNotDefault) {
185 DoTest(ExternalProtocolHandler::UNKNOWN,
186 ShellIntegration::NOT_DEFAULT,
187 true, false, false);
190 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeUnknown) {
191 DoTest(ExternalProtocolHandler::UNKNOWN,
192 ShellIntegration::UNKNOWN_DEFAULT,
193 true, false, false);