Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: chrome/browser/external_protocol/external_protocol_handler_unittest.cc

Issue 2623033006: [Re-landing] Migrate external protocol prefs from local state to profiles (Closed)
Patch Set: a Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/external_protocol/external_protocol_handler.h" 5 #include "chrome/browser/external_protocol/external_protocol_handler.h"
6 6
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "chrome/browser/prefs/browser_prefs.h"
10 #include "chrome/common/pref_names.h"
11 #include "chrome/test/base/testing_browser_process.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/prefs/testing_pref_service.h"
9 #include "content/public/test/test_browser_thread.h" 14 #include "content/public/test/test_browser_thread.h"
10 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
11 16
12 using content::BrowserThread; 17 using content::BrowserThread;
13 18
14 class FakeExternalProtocolHandlerWorker 19 class FakeExternalProtocolHandlerWorker
15 : public shell_integration::DefaultProtocolClientWorker { 20 : public shell_integration::DefaultProtocolClientWorker {
16 public: 21 public:
17 FakeExternalProtocolHandlerWorker( 22 FakeExternalProtocolHandlerWorker(
18 const shell_integration::DefaultWebClientWorkerCallback& callback, 23 const shell_integration::DefaultWebClientWorkerCallback& callback,
(...skipping 27 matching lines...) Expand all
46 has_blocked_(false) {} 51 has_blocked_(false) {}
47 52
48 scoped_refptr<shell_integration::DefaultProtocolClientWorker> 53 scoped_refptr<shell_integration::DefaultProtocolClientWorker>
49 CreateShellWorker( 54 CreateShellWorker(
50 const shell_integration::DefaultWebClientWorkerCallback& callback, 55 const shell_integration::DefaultWebClientWorkerCallback& callback,
51 const std::string& protocol) override { 56 const std::string& protocol) override {
52 return new FakeExternalProtocolHandlerWorker(callback, protocol, os_state_); 57 return new FakeExternalProtocolHandlerWorker(callback, protocol, os_state_);
53 } 58 }
54 59
55 ExternalProtocolHandler::BlockState GetBlockState( 60 ExternalProtocolHandler::BlockState GetBlockState(
56 const std::string& scheme) override { 61 const std::string& scheme, Profile* profile) override {
57 return block_state_; 62 return block_state_;
58 } 63 }
59 64
60 void BlockRequest() override { 65 void BlockRequest() override {
61 ASSERT_TRUE(block_state_ == ExternalProtocolHandler::BLOCK || 66 ASSERT_TRUE(block_state_ == ExternalProtocolHandler::BLOCK ||
62 os_state_ == shell_integration::IS_DEFAULT); 67 os_state_ == shell_integration::IS_DEFAULT);
63 has_blocked_ = true; 68 has_blocked_ = true;
64 } 69 }
65 70
66 void RunExternalProtocolDialog(const GURL& url, 71 void RunExternalProtocolDialog(const GURL& url,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 bool has_prompted_; 109 bool has_prompted_;
105 bool has_blocked_; 110 bool has_blocked_;
106 }; 111 };
107 112
108 class ExternalProtocolHandlerTest : public testing::Test { 113 class ExternalProtocolHandlerTest : public testing::Test {
109 protected: 114 protected:
110 ExternalProtocolHandlerTest() 115 ExternalProtocolHandlerTest()
111 : ui_thread_(BrowserThread::UI, base::MessageLoop::current()), 116 : ui_thread_(BrowserThread::UI, base::MessageLoop::current()),
112 file_thread_(BrowserThread::FILE) {} 117 file_thread_(BrowserThread::FILE) {}
113 118
114 void SetUp() override { file_thread_.Start(); } 119 void SetUp() override {
120 file_thread_.Start();
121 local_state_.reset(new TestingPrefServiceSimple);
122 profile_.reset(new TestingProfile());
123 chrome::RegisterLocalState(local_state_->registry());
124 TestingBrowserProcess::GetGlobal()->SetLocalState(local_state_.get());
125 }
115 126
116 void TearDown() override { 127 void TearDown() override {
117 // Ensure that g_accept_requests gets set back to true after test execution. 128 // Ensure that g_accept_requests gets set back to true after test execution.
118 ExternalProtocolHandler::PermitLaunchUrl(); 129 ExternalProtocolHandler::PermitLaunchUrl();
130 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
dominickn 2017/01/19 07:01:22 Nit: use nullptr instead of NULL
ramyasharma 2017/01/20 04:04:45 Done.
131 local_state_.reset();
119 } 132 }
120 133
121 void DoTest(ExternalProtocolHandler::BlockState block_state, 134 void DoTest(ExternalProtocolHandler::BlockState block_state,
122 shell_integration::DefaultWebClientState os_state, 135 shell_integration::DefaultWebClientState os_state,
123 bool should_prompt, 136 bool should_prompt,
124 bool should_launch, 137 bool should_launch,
125 bool should_block) { 138 bool should_block) {
126 GURL url("mailto:test@test.com"); 139 GURL url("mailto:test@test.com");
127 ASSERT_FALSE(delegate_.has_prompted()); 140 ASSERT_FALSE(delegate_.has_prompted());
128 ASSERT_FALSE(delegate_.has_launched()); 141 ASSERT_FALSE(delegate_.has_launched());
129 ASSERT_FALSE(delegate_.has_blocked()); 142 ASSERT_FALSE(delegate_.has_blocked());
130 143
131 delegate_.set_block_state(block_state); 144 delegate_.set_block_state(block_state);
132 delegate_.set_os_state(os_state); 145 delegate_.set_os_state(os_state);
133 ExternalProtocolHandler::LaunchUrlWithDelegate( 146 ExternalProtocolHandler::LaunchUrlWithDelegate(
134 url, 0, 0, ui::PAGE_TRANSITION_LINK, true, &delegate_); 147 url, 0, 0, ui::PAGE_TRANSITION_LINK, true, &delegate_);
135 if (block_state != ExternalProtocolHandler::BLOCK) 148 if (block_state != ExternalProtocolHandler::BLOCK)
136 base::RunLoop().Run(); 149 base::RunLoop().Run();
137 150
138 ASSERT_EQ(should_prompt, delegate_.has_prompted()); 151 ASSERT_EQ(should_prompt, delegate_.has_prompted());
139 ASSERT_EQ(should_launch, delegate_.has_launched()); 152 ASSERT_EQ(should_launch, delegate_.has_launched());
140 ASSERT_EQ(should_block, delegate_.has_blocked()); 153 ASSERT_EQ(should_block, delegate_.has_blocked());
141 } 154 }
142 155
143 base::MessageLoopForUI ui_message_loop_; 156 base::MessageLoopForUI ui_message_loop_;
144 content::TestBrowserThread ui_thread_; 157 content::TestBrowserThread ui_thread_;
145 content::TestBrowserThread file_thread_; 158 content::TestBrowserThread file_thread_;
146 159
147 FakeExternalProtocolHandlerDelegate delegate_; 160 FakeExternalProtocolHandlerDelegate delegate_;
161
162 std::unique_ptr<TestingPrefServiceSimple> local_state_;
163 std::unique_ptr<TestingProfile> profile_;
148 }; 164 };
149 165
150 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeDefault) { 166 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeDefault) {
151 DoTest(ExternalProtocolHandler::BLOCK, shell_integration::IS_DEFAULT, false, 167 DoTest(ExternalProtocolHandler::BLOCK, shell_integration::IS_DEFAULT, false,
152 false, true); 168 false, true);
153 } 169 }
154 170
155 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeNotDefault) { 171 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeBlockedChromeNotDefault) {
156 DoTest(ExternalProtocolHandler::BLOCK, shell_integration::NOT_DEFAULT, false, 172 DoTest(ExternalProtocolHandler::BLOCK, shell_integration::NOT_DEFAULT, false,
157 false, true); 173 false, true);
(...skipping 26 matching lines...) Expand all
184 200
185 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeNotDefault) { 201 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeNotDefault) {
186 DoTest(ExternalProtocolHandler::UNKNOWN, shell_integration::NOT_DEFAULT, true, 202 DoTest(ExternalProtocolHandler::UNKNOWN, shell_integration::NOT_DEFAULT, true,
187 false, false); 203 false, false);
188 } 204 }
189 205
190 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeUnknown) { 206 TEST_F(ExternalProtocolHandlerTest, TestLaunchSchemeUnknownChromeUnknown) {
191 DoTest(ExternalProtocolHandler::UNKNOWN, shell_integration::UNKNOWN_DEFAULT, 207 DoTest(ExternalProtocolHandler::UNKNOWN, shell_integration::UNKNOWN_DEFAULT,
192 true, false, false); 208 true, false, false);
193 } 209 }
210
211 TEST_F(ExternalProtocolHandlerTest, TestGetBlockStateUnknown) {
212 ExternalProtocolHandler::BlockState block_state =
213 ExternalProtocolHandler::GetBlockState("tel", profile_.get());
214 ASSERT_EQ(ExternalProtocolHandler::UNKNOWN, block_state);
215 }
216
217 TEST_F(ExternalProtocolHandlerTest, TestGetBlockStateDefaultBlock) {
218 ExternalProtocolHandler::BlockState block_state =
219 ExternalProtocolHandler::GetBlockState("afp", profile_.get());
220 ASSERT_EQ(ExternalProtocolHandler::BLOCK, block_state);
221 }
222
223 TEST_F(ExternalProtocolHandlerTest, TestGetBlockStateDefaultDontBlock) {
224 ExternalProtocolHandler::BlockState block_state =
225 ExternalProtocolHandler::GetBlockState("mailto", profile_.get());
226 ASSERT_EQ(ExternalProtocolHandler::DONT_BLOCK, block_state);
227 }
228
229 TEST_F(ExternalProtocolHandlerTest,
230 TestGetBlockStateLocalBlockResetOnProfilePref) {
231 base::DictionaryValue prefs_local;
232 prefs_local.SetBoolean("tel", true);
233 local_state_->Set(prefs::kExcludedSchemes, prefs_local);
234 ExternalProtocolHandler::BlockState block_state =
235 ExternalProtocolHandler::GetBlockState("tel", profile_.get());
236 ASSERT_EQ(ExternalProtocolHandler::UNKNOWN, block_state);
dominickn 2017/01/19 07:01:22 Can you assert that the local_state_ pref is empty
ramyasharma 2017/01/20 04:04:45 Added a check to local state, to ensure it has not
237 }
238
239 TEST_F(ExternalProtocolHandlerTest,
240 TestGetBlockStateLocalDontBlockCopiedAsIsToProfilePref) {
241 base::DictionaryValue prefs_local;
242 prefs_local.SetBoolean("tel", false);
243 local_state_->Set(prefs::kExcludedSchemes, prefs_local);
244 ExternalProtocolHandler::BlockState block_state =
245 ExternalProtocolHandler::GetBlockState("tel", profile_.get());
246 ASSERT_EQ(ExternalProtocolHandler::DONT_BLOCK, block_state);
dominickn 2017/01/19 07:01:22 Ditto
ramyasharma 2017/01/20 04:04:45 Done.
247 }
248
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698