Make castv2 performance test work.
[chromium-blink-merge.git] / chrome / browser / external_protocol / external_protocol_handler_unittest.cc
blob1aac925915287ac0527a9c2e69d0c17390f8c694
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) override {
65 ASSERT_EQ(block_state_, ExternalProtocolHandler::UNKNOWN);
66 ASSERT_NE(os_state_, ShellIntegration::IS_DEFAULT);
67 has_prompted_ = true;
70 void LaunchUrlWithoutSecurityCheck(const GURL& url) override {
71 ASSERT_EQ(block_state_, ExternalProtocolHandler::DONT_BLOCK);
72 ASSERT_NE(os_state_, ShellIntegration::IS_DEFAULT);
73 has_launched_ = true;
76 void FinishedProcessingCheck() override {
77 base::MessageLoop::current()->Quit();
80 void set_os_state(ShellIntegration::DefaultWebClientState value) {
81 os_state_ = value;
84 void set_block_state(ExternalProtocolHandler::BlockState value) {
85 block_state_ = value;
88 bool has_launched() { return has_launched_; }
89 bool has_prompted() { return has_prompted_; }
90 bool has_blocked() { return has_blocked_; }
92 private:
93 ExternalProtocolHandler::BlockState block_state_;
94 ShellIntegration::DefaultWebClientState os_state_;
95 bool has_launched_;
96 bool has_prompted_;
97 bool has_blocked_;
100 class ExternalProtocolHandlerTest : public testing::Test {
101 protected:
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,
142 false, false, true);
145 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeNotDefault) {
146 DoTest(ExternalProtocolHandler::BLOCK,
147 ShellIntegration::NOT_DEFAULT,
148 false, false, true);
151 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeUnknown) {
152 DoTest(ExternalProtocolHandler::BLOCK,
153 ShellIntegration::UNKNOWN_DEFAULT,
154 false, false, true);
157 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeDefault) {
158 DoTest(ExternalProtocolHandler::DONT_BLOCK,
159 ShellIntegration::IS_DEFAULT,
160 false, false, true);
163 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeNotDefault) {
164 DoTest(ExternalProtocolHandler::DONT_BLOCK,
165 ShellIntegration::NOT_DEFAULT,
166 false, true, false);
169 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnBlockedChromeUnknown) {
170 DoTest(ExternalProtocolHandler::DONT_BLOCK,
171 ShellIntegration::UNKNOWN_DEFAULT,
172 false, true, false);
175 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeDefault) {
176 DoTest(ExternalProtocolHandler::UNKNOWN,
177 ShellIntegration::IS_DEFAULT,
178 false, false, true);
181 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeNotDefault) {
182 DoTest(ExternalProtocolHandler::UNKNOWN,
183 ShellIntegration::NOT_DEFAULT,
184 true, false, false);
187 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeUnknown) {
188 DoTest(ExternalProtocolHandler::UNKNOWN,
189 ShellIntegration::UNKNOWN_DEFAULT,
190 true, false, false);