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

Side by Side Diff: chrome/browser/extensions/api/downloads/downloads_api_browsertest.cc

Issue 280853004: Use EventRouter::Get instead of ExtensionSystem::Get(browser_context)->event_router() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: tweak header Created 6 years, 7 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
« no previous file with comments | « chrome/browser/extensions/api/downloads/downloads_api.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // Disable everything on windows only. http://crbug.com/306144 5 // Disable everything on windows only. http://crbug.com/306144
6 #ifndef OS_WIN 6 #ifndef OS_WIN
7 7
8 #include <algorithm> 8 #include <algorithm>
9 9
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/json/json_reader.h" 12 #include "base/json/json_reader.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/prefs/pref_service.h" 14 #include "base/prefs/pref_service.h"
15 #include "base/stl_util.h" 15 #include "base/stl_util.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/synchronization/waitable_event.h" 17 #include "base/synchronization/waitable_event.h"
18 #include "chrome/browser/chrome_notification_types.h" 18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/download/download_file_icon_extractor.h" 19 #include "chrome/browser/download/download_file_icon_extractor.h"
20 #include "chrome/browser/download/download_service.h" 20 #include "chrome/browser/download/download_service.h"
21 #include "chrome/browser/download/download_service_factory.h" 21 #include "chrome/browser/download/download_service_factory.h"
22 #include "chrome/browser/download/download_test_file_activity_observer.h" 22 #include "chrome/browser/download/download_test_file_activity_observer.h"
23 #include "chrome/browser/extensions/api/downloads/downloads_api.h" 23 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
24 #include "chrome/browser/extensions/browser_action_test_util.h" 24 #include "chrome/browser/extensions/browser_action_test_util.h"
25 #include "chrome/browser/extensions/extension_apitest.h" 25 #include "chrome/browser/extensions/extension_apitest.h"
26 #include "chrome/browser/extensions/extension_function_test_utils.h" 26 #include "chrome/browser/extensions/extension_function_test_utils.h"
27 #include "chrome/browser/extensions/extension_service.h"
28 #include "chrome/browser/history/download_row.h" 27 #include "chrome/browser/history/download_row.h"
29 #include "chrome/browser/net/url_request_mock_util.h" 28 #include "chrome/browser/net/url_request_mock_util.h"
30 #include "chrome/browser/profiles/profile.h" 29 #include "chrome/browser/profiles/profile.h"
31 #include "chrome/browser/ui/browser.h" 30 #include "chrome/browser/ui/browser.h"
32 #include "chrome/browser/ui/browser_tabstrip.h" 31 #include "chrome/browser/ui/browser_tabstrip.h"
33 #include "chrome/common/pref_names.h" 32 #include "chrome/common/pref_names.h"
34 #include "chrome/test/base/in_process_browser_test.h" 33 #include "chrome/test/base/in_process_browser_test.h"
35 #include "chrome/test/base/ui_test_utils.h" 34 #include "chrome/test/base/ui_test_utils.h"
36 #include "content/public/browser/browser_context.h" 35 #include "content/public/browser/browser_context.h"
37 #include "content/public/browser/browser_thread.h" 36 #include "content/public/browser/browser_thread.h"
38 #include "content/public/browser/download_item.h" 37 #include "content/public/browser/download_item.h"
39 #include "content/public/browser/download_manager.h" 38 #include "content/public/browser/download_manager.h"
40 #include "content/public/browser/notification_service.h" 39 #include "content/public/browser/notification_service.h"
41 #include "content/public/browser/storage_partition.h" 40 #include "content/public/browser/storage_partition.h"
42 #include "content/public/browser/web_contents.h" 41 #include "content/public/browser/web_contents.h"
43 #include "content/public/common/content_switches.h" 42 #include "content/public/common/content_switches.h"
44 #include "content/public/common/page_transition_types.h" 43 #include "content/public/common/page_transition_types.h"
45 #include "content/public/test/download_test_observer.h" 44 #include "content/public/test/download_test_observer.h"
46 #include "content/test/net/url_request_slow_download_job.h" 45 #include "content/test/net/url_request_slow_download_job.h"
46 #include "extensions/browser/event_router.h"
47 #include "net/base/data_url.h" 47 #include "net/base/data_url.h"
48 #include "net/base/net_util.h" 48 #include "net/base/net_util.h"
49 #include "net/url_request/url_request.h" 49 #include "net/url_request/url_request.h"
50 #include "net/url_request/url_request_context.h" 50 #include "net/url_request/url_request_context.h"
51 #include "net/url_request/url_request_job.h" 51 #include "net/url_request/url_request_job.h"
52 #include "net/url_request/url_request_job_factory.h" 52 #include "net/url_request/url_request_job_factory.h"
53 #include "net/url_request/url_request_job_factory_impl.h" 53 #include "net/url_request/url_request_job_factory_impl.h"
54 #include "webkit/browser/fileapi/file_system_context.h" 54 #include "webkit/browser/fileapi/file_system_context.h"
55 #include "webkit/browser/fileapi/file_system_operation_runner.h" 55 #include "webkit/browser/fileapi/file_system_operation_runner.h"
56 #include "webkit/browser/fileapi/file_system_url.h" 56 #include "webkit/browser/fileapi/file_system_url.h"
57 57
58 using content::BrowserContext; 58 using content::BrowserContext;
59 using content::BrowserThread; 59 using content::BrowserThread;
60 using content::DownloadItem; 60 using content::DownloadItem;
61 using content::DownloadManager; 61 using content::DownloadManager;
62 using content::URLRequestSlowDownloadJob; 62 using content::URLRequestSlowDownloadJob;
63 63
64 namespace errors = download_extension_errors; 64 namespace errors = download_extension_errors;
65 65
66 namespace downloads = extensions::api::downloads;
67
68 namespace extensions { 66 namespace extensions {
67 namespace downloads = api::downloads;
69 68
70 namespace { 69 namespace {
71 70
72 // Comparator that orders download items by their ID. Can be used with 71 // Comparator that orders download items by their ID. Can be used with
73 // std::sort. 72 // std::sort.
74 struct DownloadIdComparator { 73 struct DownloadIdComparator {
75 bool operator() (DownloadItem* first, DownloadItem* second) { 74 bool operator() (DownloadItem* first, DownloadItem* second) {
76 return first->GetId() < second->GetId(); 75 return first->GetId() < second->GetId();
77 } 76 }
78 }; 77 };
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 void LoadExtension(const char* name) { 268 void LoadExtension(const char* name) {
270 // Store the created Extension object so that we can attach it to 269 // Store the created Extension object so that we can attach it to
271 // ExtensionFunctions. Also load the extension in incognito profiles for 270 // ExtensionFunctions. Also load the extension in incognito profiles for
272 // testing incognito. 271 // testing incognito.
273 extension_ = LoadExtensionIncognito(test_data_dir_.AppendASCII(name)); 272 extension_ = LoadExtensionIncognito(test_data_dir_.AppendASCII(name));
274 CHECK(extension_); 273 CHECK(extension_);
275 content::WebContents* tab = chrome::AddSelectedTabWithURL( 274 content::WebContents* tab = chrome::AddSelectedTabWithURL(
276 current_browser(), 275 current_browser(),
277 extension_->GetResourceURL("empty.html"), 276 extension_->GetResourceURL("empty.html"),
278 content::PAGE_TRANSITION_LINK); 277 content::PAGE_TRANSITION_LINK);
279 extensions::EventRouter::Get(current_browser()->profile()) 278 EventRouter::Get(current_browser()->profile())
280 ->AddEventListener(downloads::OnCreated::kEventName, 279 ->AddEventListener(downloads::OnCreated::kEventName,
281 tab->GetRenderProcessHost(), 280 tab->GetRenderProcessHost(),
282 GetExtensionId()); 281 GetExtensionId());
283 extensions::EventRouter::Get(current_browser()->profile()) 282 EventRouter::Get(current_browser()->profile())
284 ->AddEventListener(downloads::OnChanged::kEventName, 283 ->AddEventListener(downloads::OnChanged::kEventName,
285 tab->GetRenderProcessHost(), 284 tab->GetRenderProcessHost(),
286 GetExtensionId()); 285 GetExtensionId());
287 extensions::EventRouter::Get(current_browser()->profile()) 286 EventRouter::Get(current_browser()->profile())
288 ->AddEventListener(downloads::OnErased::kEventName, 287 ->AddEventListener(downloads::OnErased::kEventName,
289 tab->GetRenderProcessHost(), 288 tab->GetRenderProcessHost(),
290 GetExtensionId()); 289 GetExtensionId());
291 } 290 }
292 291
293 content::RenderProcessHost* AddFilenameDeterminer() { 292 content::RenderProcessHost* AddFilenameDeterminer() {
294 ExtensionDownloadsEventRouter::SetDetermineFilenameTimeoutSecondsForTesting( 293 ExtensionDownloadsEventRouter::SetDetermineFilenameTimeoutSecondsForTesting(
295 2); 294 2);
296 content::WebContents* tab = chrome::AddSelectedTabWithURL( 295 content::WebContents* tab = chrome::AddSelectedTabWithURL(
297 current_browser(), 296 current_browser(),
298 extension_->GetResourceURL("empty.html"), 297 extension_->GetResourceURL("empty.html"),
299 content::PAGE_TRANSITION_LINK); 298 content::PAGE_TRANSITION_LINK);
300 extensions::ExtensionSystem::Get(current_browser()->profile()) 299 EventRouter::Get(current_browser()->profile())
301 ->event_router()
302 ->AddEventListener(downloads::OnDeterminingFilename::kEventName, 300 ->AddEventListener(downloads::OnDeterminingFilename::kEventName,
303 tab->GetRenderProcessHost(), 301 tab->GetRenderProcessHost(),
304 GetExtensionId()); 302 GetExtensionId());
305 return tab->GetRenderProcessHost(); 303 return tab->GetRenderProcessHost();
306 } 304 }
307 305
308 void RemoveFilenameDeterminer(content::RenderProcessHost* host) { 306 void RemoveFilenameDeterminer(content::RenderProcessHost* host) {
309 extensions::ExtensionSystem::Get(current_browser()->profile()) 307 EventRouter::Get(current_browser()->profile())->RemoveEventListener(
310 ->event_router() 308 downloads::OnDeterminingFilename::kEventName, host, GetExtensionId());
311 ->RemoveEventListener(downloads::OnDeterminingFilename::kEventName,
312 host,
313 GetExtensionId());
314 } 309 }
315 310
316 Browser* current_browser() { return current_browser_; } 311 Browser* current_browser() { return current_browser_; }
317 312
318 // InProcessBrowserTest 313 // InProcessBrowserTest
319 virtual void SetUpOnMainThread() OVERRIDE { 314 virtual void SetUpOnMainThread() OVERRIDE {
320 ExtensionApiTest::SetUpOnMainThread(); 315 ExtensionApiTest::SetUpOnMainThread();
321 BrowserThread::PostTask( 316 BrowserThread::PostTask(
322 BrowserThread::IO, FROM_HERE, 317 BrowserThread::IO, FROM_HERE,
323 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); 318 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
596 } 591 }
597 592
598 void CreateAndSetDownloadsDirectory() { 593 void CreateAndSetDownloadsDirectory() {
599 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); 594 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir());
600 current_browser()->profile()->GetPrefs()->SetFilePath( 595 current_browser()->profile()->GetPrefs()->SetFilePath(
601 prefs::kDownloadDefaultDirectory, 596 prefs::kDownloadDefaultDirectory,
602 downloads_directory_.path()); 597 downloads_directory_.path());
603 } 598 }
604 599
605 base::ScopedTempDir downloads_directory_; 600 base::ScopedTempDir downloads_directory_;
606 const extensions::Extension* extension_; 601 const Extension* extension_;
607 Browser* incognito_browser_; 602 Browser* incognito_browser_;
608 Browser* current_browser_; 603 Browser* current_browser_;
609 scoped_ptr<DownloadsEventsListener> events_listener_; 604 scoped_ptr<DownloadsEventsListener> events_listener_;
610 605
611 DISALLOW_COPY_AND_ASSIGN(DownloadExtensionTest); 606 DISALLOW_COPY_AND_ASSIGN(DownloadExtensionTest);
612 }; 607 };
613 608
614 class MockIconExtractorImpl : public DownloadFileIconExtractor { 609 class MockIconExtractorImpl : public DownloadFileIconExtractor {
615 public: 610 public:
616 MockIconExtractorImpl(const base::FilePath& path, 611 MockIconExtractorImpl(const base::FilePath& path,
(...skipping 3414 matching lines...) Expand 10 before | Expand all | Expand 10 after
4031 #include "content/public/browser/download_interrupt_reason_values.h" 4026 #include "content/public/browser/download_interrupt_reason_values.h"
4032 #undef INTERRUPT_REASON 4027 #undef INTERRUPT_REASON
4033 } 4028 }
4034 4029
4035 TEST(ExtensionDetermineDownloadFilenameInternal, 4030 TEST(ExtensionDetermineDownloadFilenameInternal,
4036 ExtensionDetermineDownloadFilenameInternal) { 4031 ExtensionDetermineDownloadFilenameInternal) {
4037 std::string winner_id; 4032 std::string winner_id;
4038 base::FilePath filename; 4033 base::FilePath filename;
4039 downloads::FilenameConflictAction conflict_action = 4034 downloads::FilenameConflictAction conflict_action =
4040 downloads::FILENAME_CONFLICT_ACTION_UNIQUIFY; 4035 downloads::FILENAME_CONFLICT_ACTION_UNIQUIFY;
4041 extensions::ExtensionWarningSet warnings; 4036 ExtensionWarningSet warnings;
4042 4037
4043 // Empty incumbent determiner 4038 // Empty incumbent determiner
4044 warnings.clear(); 4039 warnings.clear();
4045 ExtensionDownloadsEventRouter::DetermineFilenameInternal( 4040 ExtensionDownloadsEventRouter::DetermineFilenameInternal(
4046 base::FilePath(FILE_PATH_LITERAL("a")), 4041 base::FilePath(FILE_PATH_LITERAL("a")),
4047 downloads::FILENAME_CONFLICT_ACTION_OVERWRITE, 4042 downloads::FILENAME_CONFLICT_ACTION_OVERWRITE,
4048 "suggester", 4043 "suggester",
4049 base::Time::Now(), 4044 base::Time::Now(),
4050 "", 4045 "",
4051 base::Time(), 4046 base::Time(),
(...skipping 16 matching lines...) Expand all
4068 "incumbent", 4063 "incumbent",
4069 base::Time::Now(), 4064 base::Time::Now(),
4070 &winner_id, 4065 &winner_id,
4071 &filename, 4066 &filename,
4072 &conflict_action, 4067 &conflict_action,
4073 &warnings); 4068 &warnings);
4074 EXPECT_EQ("incumbent", winner_id); 4069 EXPECT_EQ("incumbent", winner_id);
4075 EXPECT_EQ(FILE_PATH_LITERAL("a"), filename.value()); 4070 EXPECT_EQ(FILE_PATH_LITERAL("a"), filename.value());
4076 EXPECT_EQ(downloads::FILENAME_CONFLICT_ACTION_OVERWRITE, conflict_action); 4071 EXPECT_EQ(downloads::FILENAME_CONFLICT_ACTION_OVERWRITE, conflict_action);
4077 EXPECT_FALSE(warnings.empty()); 4072 EXPECT_FALSE(warnings.empty());
4078 EXPECT_EQ(extensions::ExtensionWarning::kDownloadFilenameConflict, 4073 EXPECT_EQ(ExtensionWarning::kDownloadFilenameConflict,
4079 warnings.begin()->warning_type()); 4074 warnings.begin()->warning_type());
4080 EXPECT_EQ("suggester", warnings.begin()->extension_id()); 4075 EXPECT_EQ("suggester", warnings.begin()->extension_id());
4081 4076
4082 // Suggester wins 4077 // Suggester wins
4083 warnings.clear(); 4078 warnings.clear();
4084 ExtensionDownloadsEventRouter::DetermineFilenameInternal( 4079 ExtensionDownloadsEventRouter::DetermineFilenameInternal(
4085 base::FilePath(FILE_PATH_LITERAL("b")), 4080 base::FilePath(FILE_PATH_LITERAL("b")),
4086 downloads::FILENAME_CONFLICT_ACTION_PROMPT, 4081 downloads::FILENAME_CONFLICT_ACTION_PROMPT,
4087 "suggester", 4082 "suggester",
4088 base::Time::Now(), 4083 base::Time::Now(),
4089 "incumbent", 4084 "incumbent",
4090 base::Time::Now() - base::TimeDelta::FromDays(1), 4085 base::Time::Now() - base::TimeDelta::FromDays(1),
4091 &winner_id, 4086 &winner_id,
4092 &filename, 4087 &filename,
4093 &conflict_action, 4088 &conflict_action,
4094 &warnings); 4089 &warnings);
4095 EXPECT_EQ("suggester", winner_id); 4090 EXPECT_EQ("suggester", winner_id);
4096 EXPECT_EQ(FILE_PATH_LITERAL("b"), filename.value()); 4091 EXPECT_EQ(FILE_PATH_LITERAL("b"), filename.value());
4097 EXPECT_EQ(downloads::FILENAME_CONFLICT_ACTION_PROMPT, conflict_action); 4092 EXPECT_EQ(downloads::FILENAME_CONFLICT_ACTION_PROMPT, conflict_action);
4098 EXPECT_FALSE(warnings.empty()); 4093 EXPECT_FALSE(warnings.empty());
4099 EXPECT_EQ(extensions::ExtensionWarning::kDownloadFilenameConflict, 4094 EXPECT_EQ(ExtensionWarning::kDownloadFilenameConflict,
4100 warnings.begin()->warning_type()); 4095 warnings.begin()->warning_type());
4101 EXPECT_EQ("incumbent", warnings.begin()->extension_id()); 4096 EXPECT_EQ("incumbent", warnings.begin()->extension_id());
4102 } 4097 }
4103 4098
4104 } // namespace extensions 4099 } // namespace extensions
4105 4100
4106 #endif // http://crbug.com/3061144 4101 #endif // http://crbug.com/3061144
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/downloads/downloads_api.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698