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

Side by Side Diff: chrome/browser/download/download_target_determiner_unittest.cc

Issue 1217223006: Prompt user for file access permission before download starts (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 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 "base/at_exit.h" 5 #include "base/at_exit.h"
6 #include "base/files/file_path.h" 6 #include "base/files/file_path.h"
7 #include "base/files/scoped_temp_dir.h" 7 #include "base/files/scoped_temp_dir.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/observer_list.h" 9 #include "base/observer_list.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
(...skipping 29 matching lines...) Expand all
40 #if defined(ENABLE_PLUGINS) 40 #if defined(ENABLE_PLUGINS)
41 #include "content/public/browser/plugin_service.h" 41 #include "content/public/browser/plugin_service.h"
42 #include "content/public/browser/plugin_service_filter.h" 42 #include "content/public/browser/plugin_service_filter.h"
43 #include "content/public/common/webplugininfo.h" 43 #include "content/public/common/webplugininfo.h"
44 #endif 44 #endif
45 45
46 #if defined(ENABLE_EXTENSIONS) 46 #if defined(ENABLE_EXTENSIONS)
47 #include "extensions/common/extension.h" 47 #include "extensions/common/extension.h"
48 #endif 48 #endif
49 49
50 #if defined(OS_ANDROID)
51 #include "content/public/browser/android/download_controller_android.h"
52 #include "content/public/common/context_menu_params.h"
53 using content::WebContents;
54 using content::ContextMenuParams;
55 #endif
56
50 using ::testing::AnyNumber; 57 using ::testing::AnyNumber;
51 using ::testing::Invoke; 58 using ::testing::Invoke;
52 using ::testing::Ref; 59 using ::testing::Ref;
53 using ::testing::Return; 60 using ::testing::Return;
54 using ::testing::ReturnRef; 61 using ::testing::ReturnRef;
55 using ::testing::ReturnRefOfCopy; 62 using ::testing::ReturnRefOfCopy;
56 using ::testing::Truly; 63 using ::testing::Truly;
57 using ::testing::WithArg; 64 using ::testing::WithArg;
58 using ::testing::_; 65 using ::testing::_;
59 using content::DownloadItem; 66 using content::DownloadItem;
60 67
61 namespace { 68 namespace {
62 69
63 // No-op delegate. 70 // No-op delegate.
64 class NullWebContentsDelegate : public content::WebContentsDelegate { 71 class NullWebContentsDelegate : public content::WebContentsDelegate {
65 public: 72 public:
66 NullWebContentsDelegate() {} 73 NullWebContentsDelegate() {}
67 ~NullWebContentsDelegate() override {} 74 ~NullWebContentsDelegate() override {}
68 }; 75 };
69 76
77 #if defined(OS_ANDROID)
78 class MockDownloadControllerAndroid
79 : public content::DownloadControllerAndroid {
80 public:
81 MockDownloadControllerAndroid()
82 : require_user_permission_prompt_(false),
83 approve_user_request_(true) {}
84 ~MockDownloadControllerAndroid() override {}
85
86 void CreateGETDownload(int render_process_id, int render_view_id,
87 int request_id) override {};
88 void OnDownloadStarted(DownloadItem* download_item) override {};
89 void StartContextMenuDownload(
90 const ContextMenuParams& params, WebContents* web_contents,
91 bool is_link, const std::string& extra_headers) override {};
92 void DangerousDownloadValidated(
93 WebContents* web_contents, int download_id, bool accept) override {};
94 bool RequireUserPermissionPrompt(WebContents* web_contents) override {
95 return require_user_permission_prompt_;
96 }
97 void PromptUserForPermission(
98 WebContents* web_contents,
99 const UserPermissionPromptCallback& callback) override {
100 ASSERT_TRUE(require_user_permission_prompt_);
101 base::ThreadTaskRunnerHandle::Get()->PostTask(
102 FROM_HERE, base::Bind(callback, approve_user_request_));
103 }
104
105 void set_require_user_permission_prompt(bool require_user_permission_prompt) {
106 require_user_permission_prompt_ = require_user_permission_prompt;
107 }
108 void set_approve_user_request(bool approve_user_request) {
109 approve_user_request_ = approve_user_request;
110 }
111 private:
112 bool require_user_permission_prompt_;
113 bool approve_user_request_;
114 };
115 #endif
116
70 // Google Mock action that posts a task to the current message loop that invokes 117 // Google Mock action that posts a task to the current message loop that invokes
71 // the first argument of the mocked method as a callback. Said argument must be 118 // the first argument of the mocked method as a callback. Said argument must be
72 // a base::Callback<void(ParamType)>. |result| must be of |ParamType| and is 119 // a base::Callback<void(ParamType)>. |result| must be of |ParamType| and is
73 // bound as that parameter. 120 // bound as that parameter.
74 // Example: 121 // Example:
75 // class FooClass { 122 // class FooClass {
76 // public: 123 // public:
77 // virtual void Foo(base::Callback<void(bool)> callback); 124 // virtual void Foo(base::Callback<void(bool)> callback);
78 // }; 125 // };
79 // ... 126 // ...
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 } 297 }
251 298
252 MockDownloadTargetDeterminerDelegate* delegate() { 299 MockDownloadTargetDeterminerDelegate* delegate() {
253 return &delegate_; 300 return &delegate_;
254 } 301 }
255 302
256 DownloadPrefs* download_prefs() { 303 DownloadPrefs* download_prefs() {
257 return download_prefs_.get(); 304 return download_prefs_.get();
258 } 305 }
259 306
307 #if defined(OS_ANDROID)
308 MockDownloadControllerAndroid* download_controller() {
309 return &download_controller_;
310 }
311 #endif
312
260 private: 313 private:
261 scoped_ptr<DownloadPrefs> download_prefs_; 314 scoped_ptr<DownloadPrefs> download_prefs_;
262 ::testing::NiceMock<MockDownloadTargetDeterminerDelegate> delegate_; 315 ::testing::NiceMock<MockDownloadTargetDeterminerDelegate> delegate_;
263 NullWebContentsDelegate web_contents_delegate_; 316 NullWebContentsDelegate web_contents_delegate_;
264 base::ScopedTempDir test_download_dir_; 317 base::ScopedTempDir test_download_dir_;
265 base::FilePath test_virtual_dir_; 318 base::FilePath test_virtual_dir_;
319 #if defined(OS_ANDROID)
320 MockDownloadControllerAndroid download_controller_;
321 #endif
266 }; 322 };
267 323
268 void DownloadTargetDeterminerTest::SetUp() { 324 void DownloadTargetDeterminerTest::SetUp() {
269 ChromeRenderViewHostTestHarness::SetUp(); 325 ChromeRenderViewHostTestHarness::SetUp();
270 CHECK(profile()); 326 CHECK(profile());
271 download_prefs_.reset(new DownloadPrefs(profile())); 327 download_prefs_.reset(new DownloadPrefs(profile()));
272 web_contents()->SetDelegate(&web_contents_delegate_); 328 web_contents()->SetDelegate(&web_contents_delegate_);
273 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); 329 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir());
274 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual")); 330 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual"));
275 download_prefs_->SetDownloadPath(test_download_dir()); 331 download_prefs_->SetDownloadPath(test_download_dir());
276 delegate_.SetupDefaults(); 332 delegate_.SetupDefaults();
333 #if defined(OS_ANDROID)
334 DownloadTargetDeterminer::SetDownloadControllerAndroidForTestings(
335 &download_controller_);
asanka 2015/07/06 21:09:41 You are setting a global pointer to a temporary va
qinmin 2015/07/07 00:54:22 Done.
336 #endif
277 } 337 }
278 338
279 void DownloadTargetDeterminerTest::TearDown() { 339 void DownloadTargetDeterminerTest::TearDown() {
280 download_prefs_.reset(); 340 download_prefs_.reset();
281 ChromeRenderViewHostTestHarness::TearDown(); 341 ChromeRenderViewHostTestHarness::TearDown();
282 } 342 }
283 343
284 content::MockDownloadItem* 344 content::MockDownloadItem*
285 DownloadTargetDeterminerTest::CreateActiveDownloadItem( 345 DownloadTargetDeterminerTest::CreateActiveDownloadItem(
286 int32 id, 346 int32 id,
(...skipping 860 matching lines...) Expand 10 before | Expand all | Expand 10 after
1147 }, 1207 },
1148 }; 1208 };
1149 1209
1150 SetPromptForDownload(true); 1210 SetPromptForDownload(true);
1151 EnableAutoOpenBasedOnExtension( 1211 EnableAutoOpenBasedOnExtension(
1152 base::FilePath(FILE_PATH_LITERAL("dummy.dummy"))); 1212 base::FilePath(FILE_PATH_LITERAL("dummy.dummy")));
1153 RunTestCasesWithActiveItem(kPromptingTestCases, 1213 RunTestCasesWithActiveItem(kPromptingTestCases,
1154 arraysize(kPromptingTestCases)); 1214 arraysize(kPromptingTestCases));
1155 } 1215 }
1156 1216
1217 #if defined(OS_ANDROID)
1218 TEST_F(DownloadTargetDeterminerTest,
1219 TargetDeterminer_ApprovePromptForUserPermission) {
1220 const DownloadTestCase kUserPermissionTestCases[] = {
1221 {
1222 // 0: Automatic Safe
1223 AUTOMATIC,
1224 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1225 "http://example.com/foo.txt", "text/plain",
1226 FILE_PATH_LITERAL(""),
1227
1228 FILE_PATH_LITERAL("foo.txt"),
1229 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1230
1231 EXPECT_CRDOWNLOAD
1232 },
1233 };
1234 download_controller()->set_require_user_permission_prompt(true);
1235 RunTestCasesWithActiveItem(kUserPermissionTestCases,
1236 arraysize(kUserPermissionTestCases));
1237 }
1238
1239 TEST_F(DownloadTargetDeterminerTest,
1240 TargetDeterminer_DisapprovePromptForUserPermission) {
1241 const DownloadTestCase kUserPermissionTestCases[] = {
1242 {
1243 // 0: Automatic Safe
1244 AUTOMATIC,
1245 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1246 "http://example.com/foo.txt", "text/plain",
1247 FILE_PATH_LITERAL(""),
1248
1249 FILE_PATH_LITERAL(""),
1250 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1251
1252 EXPECT_LOCAL_PATH
1253 },
1254 };
1255 download_controller()->set_require_user_permission_prompt(true);
1256 download_controller()->set_approve_user_request(false);
1257 RunTestCasesWithActiveItem(kUserPermissionTestCases,
1258 arraysize(kUserPermissionTestCases));
1259 }
1260 #endif
1261
1157 #if defined(ENABLE_EXTENSIONS) 1262 #if defined(ENABLE_EXTENSIONS)
1158 // These test cases are run with "Prompt for download" user preference set to 1263 // These test cases are run with "Prompt for download" user preference set to
1159 // true. Automatic extension downloads shouldn't cause prompting. 1264 // true. Automatic extension downloads shouldn't cause prompting.
1160 // Android doesn't support extensions. 1265 // Android doesn't support extensions.
1161 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_PromptAlways_Extension) { 1266 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_PromptAlways_Extension) {
1162 const DownloadTestCase kPromptingTestCases[] = { 1267 const DownloadTestCase kPromptingTestCases[] = {
1163 { 1268 {
1164 // 0: Automatic Browser Extension download. - Shouldn't prompt for browser 1269 // 0: Automatic Browser Extension download. - Shouldn't prompt for browser
1165 // extension downloads even if "Prompt for download" preference is set. 1270 // extension downloads even if "Prompt for download" preference is set.
1166 AUTOMATIC, 1271 AUTOMATIC,
(...skipping 1072 matching lines...) Expand 10 before | Expand all | Expand 10 after
2239 EXPECT_CALL(mock_plugin_filter_, MockPluginAvailable(npapi_plugin.path())) 2344 EXPECT_CALL(mock_plugin_filter_, MockPluginAvailable(npapi_plugin.path()))
2240 .WillRepeatedly(Return(true)); 2345 .WillRepeatedly(Return(true));
2241 2346
2242 target_info = RunDownloadTargetDeterminer( 2347 target_info = RunDownloadTargetDeterminer(
2243 GetPathInDownloadDir(kInitialPath), item.get()); 2348 GetPathInDownloadDir(kInitialPath), item.get());
2244 EXPECT_FALSE(target_info->is_filetype_handled_safely); 2349 EXPECT_FALSE(target_info->is_filetype_handled_safely);
2245 } 2350 }
2246 #endif // defined(ENABLE_PLUGINS) 2351 #endif // defined(ENABLE_PLUGINS)
2247 2352
2248 } // namespace 2353 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698