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