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

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

Issue 12422012: Fix DownloadExtensionTest_OnDeterminingFilename_InterruptedResume (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r191608 Created 7 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | 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 #include <algorithm> 5 #include <algorithm>
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 18 matching lines...) Expand all
29 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
30 #include "chrome/test/base/in_process_browser_test.h" 30 #include "chrome/test/base/in_process_browser_test.h"
31 #include "chrome/test/base/ui_test_utils.h" 31 #include "chrome/test/base/ui_test_utils.h"
32 #include "content/public/browser/browser_context.h" 32 #include "content/public/browser/browser_context.h"
33 #include "content/public/browser/browser_thread.h" 33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/download_item.h" 34 #include "content/public/browser/download_item.h"
35 #include "content/public/browser/download_manager.h" 35 #include "content/public/browser/download_manager.h"
36 #include "content/public/browser/notification_service.h" 36 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/storage_partition.h" 37 #include "content/public/browser/storage_partition.h"
38 #include "content/public/browser/web_contents.h" 38 #include "content/public/browser/web_contents.h"
39 #include "content/public/common/content_switches.h"
39 #include "content/public/common/page_transition_types.h" 40 #include "content/public/common/page_transition_types.h"
40 #include "content/public/test/download_test_observer.h" 41 #include "content/public/test/download_test_observer.h"
41 #include "content/public/test/test_file_error_injector.h"
42 #include "content/test/net/url_request_slow_download_job.h" 42 #include "content/test/net/url_request_slow_download_job.h"
43 #include "net/base/data_url.h" 43 #include "net/base/data_url.h"
44 #include "net/base/net_util.h" 44 #include "net/base/net_util.h"
45 #include "net/url_request/url_request.h" 45 #include "net/url_request/url_request.h"
46 #include "net/url_request/url_request_context.h" 46 #include "net/url_request/url_request_context.h"
47 #include "net/url_request/url_request_job.h" 47 #include "net/url_request/url_request_job.h"
48 #include "net/url_request/url_request_job_factory.h" 48 #include "net/url_request/url_request_job_factory.h"
49 #include "net/url_request/url_request_job_factory_impl.h" 49 #include "net/url_request/url_request_job_factory_impl.h"
50 #include "webkit/blob/blob_data.h" 50 #include "webkit/blob/blob_data.h"
51 #include "webkit/blob/blob_storage_controller.h" 51 #include "webkit/blob/blob_storage_controller.h"
(...skipping 27 matching lines...) Expand all
79 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT, 79 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT,
80 content::NotificationService::AllSources()); 80 content::NotificationService::AllSources());
81 } 81 }
82 82
83 virtual ~DownloadsEventsListener() { 83 virtual ~DownloadsEventsListener() {
84 registrar_.Remove(this, chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT, 84 registrar_.Remove(this, chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT,
85 content::NotificationService::AllSources()); 85 content::NotificationService::AllSources());
86 STLDeleteElements(&events_); 86 STLDeleteElements(&events_);
87 } 87 }
88 88
89 void ClearEvents() {
90 STLDeleteElements(&events_);
91 events_.clear();
92 }
93
89 class Event { 94 class Event {
90 public: 95 public:
91 Event(Profile* profile, 96 Event(Profile* profile,
92 const std::string& event_name, 97 const std::string& event_name,
93 const std::string& json_args, 98 const std::string& json_args,
94 base::Time caught) 99 base::Time caught)
95 : profile_(profile), 100 : profile_(profile),
96 event_name_(event_name), 101 event_name_(event_name),
97 json_args_(json_args), 102 json_args_(json_args),
98 args_(base::JSONReader::Read(json_args)), 103 args_(base::JSONReader::Read(json_args)),
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 return WaitFor(events::kOnDownloadChanged, 333 return WaitFor(events::kOnDownloadChanged,
329 base::StringPrintf("[{\"id\": %d," 334 base::StringPrintf("[{\"id\": %d,"
330 " \"error\": {\"current\": %d}," 335 " \"error\": {\"current\": %d},"
331 " \"state\": {" 336 " \"state\": {"
332 " \"previous\": \"in_progress\"," 337 " \"previous\": \"in_progress\","
333 " \"current\": \"interrupted\"}}]", 338 " \"current\": \"interrupted\"}}]",
334 item->GetId(), 339 item->GetId(),
335 expected_error)); 340 expected_error));
336 } 341 }
337 342
343 void ClearEvents() {
344 events_listener_->ClearEvents();
345 }
346
338 std::string GetExtensionURL() { 347 std::string GetExtensionURL() {
339 return extension_->url().spec(); 348 return extension_->url().spec();
340 } 349 }
341 std::string GetExtensionId() { 350 std::string GetExtensionId() {
342 return extension_->id(); 351 return extension_->id();
343 } 352 }
344 353
345 std::string GetFilename(const char* path) { 354 std::string GetFilename(const char* path) {
346 std::string result = 355 std::string result =
347 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe(); 356 downloads_directory_.path().AppendASCII(path).AsUTF8Unsafe();
(...skipping 2741 matching lines...) Expand 10 before | Expand all | Expand 10 after
3089 GetFilename("42 (1).txt.crdownload").c_str(), 3098 GetFilename("42 (1).txt.crdownload").c_str(),
3090 GetFilename("42 (1).txt").c_str()))); 3099 GetFilename("42 (1).txt").c_str())));
3091 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 3100 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3092 base::StringPrintf("[{\"id\": %d," 3101 base::StringPrintf("[{\"id\": %d,"
3093 " \"state\": {" 3102 " \"state\": {"
3094 " \"previous\": \"in_progress\"," 3103 " \"previous\": \"in_progress\","
3095 " \"current\": \"complete\"}}]", 3104 " \"current\": \"complete\"}}]",
3096 result_id))); 3105 result_id)));
3097 } 3106 }
3098 3107
3099 // Test download interruption while extensions determining filename, re-run 3108 class JustInProgressDownloadObserver
3100 // through fan-out and fan-in. 3109 : public content::DownloadTestObserverInProgress {
Randy Smith (Not in Mondays) 2013/04/02 21:52:34 It probably won't surprise you to hear that I don'
benjhayden 2013/04/03 15:43:00 Even if the tests all pass, wouldn't that cause ra
Randy Smith (Not in Mondays) 2013/04/03 15:56:47 Good point; you'd have to check the tests that use
3101 // TODO(rdsmith): FILE_OPERATION_INITIALIZE is not right for this test. 3110 public:
3111 JustInProgressDownloadObserver(
3112 DownloadManager* download_manager, size_t wait_count)
3113 : content::DownloadTestObserverInProgress(download_manager, wait_count) {
3114 }
3115
3116 virtual ~JustInProgressDownloadObserver() {}
3117
3118 private:
3119 virtual bool IsDownloadInFinalState(DownloadItem* item) OVERRIDE {
3120 return item->GetState() == DownloadItem::IN_PROGRESS;
3121 }
3122
3123 DISALLOW_COPY_AND_ASSIGN(JustInProgressDownloadObserver);
3124 };
3125
3126 // Test download interruption while extensions determining filename. Should not
3127 // re-dispatch onDeterminingFilename.
3102 IN_PROC_BROWSER_TEST_F( 3128 IN_PROC_BROWSER_TEST_F(
3103 DownloadExtensionTest, 3129 DownloadExtensionTest,
3104 DISABLED_DownloadExtensionTest_OnDeterminingFilename_InterruptedResume) { 3130 DownloadExtensionTest_OnDeterminingFilename_InterruptedResume) {
3131 CommandLine::ForCurrentProcess()->AppendSwitch(
3132 switches::kEnableDownloadResumption);
3105 LoadExtension("downloads_split"); 3133 LoadExtension("downloads_split");
3106 CHECK(StartTestServer()); 3134 CHECK(StartTestServer());
3107 std::string download_url = test_server()->GetURL("slow?0").spec();
3108 GoOnTheRecord(); 3135 GoOnTheRecord();
3109 AddFilenameDeterminer(); 3136 AddFilenameDeterminer();
3110 3137
3111 // TODO Interrupt the download instead of responding to onDeterminingFilename.
3112 scoped_refptr<content::TestFileErrorInjector> injector(
3113 content::TestFileErrorInjector::Create(
3114 GetCurrentManager()));
3115 content::TestFileErrorInjector::FileErrorInfo error_info = {
3116 download_url,
3117 content::TestFileErrorInjector::FILE_OPERATION_INITIALIZE,
3118 0,
3119 content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE};
3120 injector->AddError(error_info);
3121 injector->InjectErrors();
3122
3123 // Start a download. 3138 // Start a download.
3124 scoped_ptr<base::Value> result(RunFunctionAndReturnResult( 3139 DownloadItem* item = NULL;
3125 new DownloadsDownloadFunction(), base::StringPrintf( 3140 {
3126 "[{\"url\": \"%s\"}]", download_url.c_str()))); 3141 DownloadManager* manager = GetCurrentManager();
3127 ASSERT_TRUE(result.get()); 3142 scoped_ptr<content::DownloadTestObserver> observer(
3128 int result_id = -1; 3143 new JustInProgressDownloadObserver(manager, 1));
3129 ASSERT_TRUE(result->GetAsInteger(&result_id)); 3144 ASSERT_EQ(0, manager->InProgressCount());
3130 DownloadItem* item = GetCurrentManager()->GetDownload(result_id); 3145 ui_test_utils::NavigateToURLWithDisposition(
3131 ASSERT_TRUE(item); 3146 current_browser(),
3147 GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl),
3148 CURRENT_TAB,
Randy Smith (Not in Mondays) 2013/04/02 21:52:34 Please comment this innocuous looking enum :-}.
benjhayden 2013/04/03 15:43:00 Done.
3149 ui_test_utils::BROWSER_TEST_NONE);
3150 observer->WaitForFinished();
3151 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::IN_PROGRESS));
3152 DownloadManager::DownloadVector items;
3153 manager->GetAllDownloads(&items);
3154 for (DownloadManager::DownloadVector::iterator iter = items.begin();
3155 iter != items.end(); ++iter) {
3156 if ((*iter)->GetState() == DownloadItem::IN_PROGRESS) {
3157 // There should be only one IN_PROGRESS item.
3158 EXPECT_EQ(NULL, item);
3159 item = *iter;
3160 }
3161 }
3162 ASSERT_TRUE(item);
3163 }
3132 ScopedCancellingItem canceller(item); 3164 ScopedCancellingItem canceller(item);
3133 ASSERT_EQ(download_url, item->GetOriginalUrl().spec());
3134 3165
3135 // Wait for the onCreated and onDeterminingFilename event. 3166 // Wait for the onCreated and onDeterminingFilename event.
3136 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated, 3167 ASSERT_TRUE(WaitFor(events::kOnDownloadCreated,
3137 base::StringPrintf("[{\"danger\": \"safe\"," 3168 base::StringPrintf("[{\"danger\": \"safe\","
3138 " \"incognito\": false," 3169 " \"incognito\": false,"
3139 " \"id\": %d," 3170 " \"id\": %d,"
3140 " \"mime\": \"text/plain\"," 3171 " \"mime\": \"application/octet-stream\","
3141 " \"paused\": false," 3172 " \"paused\": false}]",
3142 " \"url\": \"%s\"}]", 3173 item->GetId())));
3143 result_id,
3144 download_url.c_str())));
3145 ASSERT_TRUE(WaitFor( 3174 ASSERT_TRUE(WaitFor(
3146 events::kOnDownloadDeterminingFilename, 3175 events::kOnDownloadDeterminingFilename,
3147 base::StringPrintf("[{\"id\": %d," 3176 base::StringPrintf("[{\"id\": %d,"
3148 " \"incognito\": false," 3177 " \"incognito\": false,"
3149 " \"filename\":\"slow.txt\"}]", 3178 " \"filename\":\"download-unknown-size\"}]",
3150 result_id))); 3179 item->GetId())));
3151 ASSERT_TRUE(item->GetTargetFilePath().empty()); 3180 ASSERT_TRUE(item->GetTargetFilePath().empty());
3152 ASSERT_TRUE(item->IsInProgress()); 3181 ASSERT_TRUE(item->IsInProgress());
3153 3182
3154 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 3183 ClearEvents();
3155 base::StringPrintf("[{\"id\": %d," 3184 ui_test_utils::NavigateToURLWithDisposition(
3156 " \"state\": {" 3185 current_browser(),
3157 " \"previous\": \"in_progress\"," 3186 GURL(URLRequestSlowDownloadJob::kErrorDownloadUrl),
3158 " \"current\": \"interrupted\"}}]", 3187 NEW_BACKGROUND_TAB,
3159 result_id))); 3188 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
3160 ASSERT_TRUE(item->IsInterrupted());
3161 item->ResumeInterruptedDownload();
3162 3189
3163 // Wait for and respond to the onDeterminingFilename event. 3190 // Errors caught before filename determination are delayed until after
3164 ASSERT_TRUE(WaitFor( 3191 // filename determination.
3165 events::kOnDownloadDeterminingFilename,
3166 base::StringPrintf("[{\"id\": %d,"
3167 " \"incognito\": false,"
3168 " \"filename\":\"slow.txt\"}]",
3169 result_id)));
3170 ASSERT_TRUE(item->GetTargetFilePath().empty());
3171 ASSERT_TRUE(item->IsInProgress());
3172 std::string error; 3192 std::string error;
3173 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename( 3193 ASSERT_TRUE(ExtensionDownloadsEventRouter::DetermineFilename(
3174 current_browser()->profile(), 3194 current_browser()->profile(),
3175 false, 3195 false,
3176 GetExtensionId(), 3196 GetExtensionId(),
3177 result_id, 3197 item->GetId(),
3178 base::FilePath(FILE_PATH_LITERAL("42.txt")), 3198 base::FilePath(FILE_PATH_LITERAL("42.txt")),
3179 false, 3199 false,
3180 &error)); 3200 &error)) << error;
3181 EXPECT_EQ("", error); 3201 EXPECT_EQ("", error);
3182 3202
3203 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3204 base::StringPrintf("[{\"id\": %d,"
3205 " \"error\":{\"current\":20},"
3206 " \"state\":{"
3207 " \"previous\":\"in_progress\","
3208 " \"current\":\"interrupted\"}}]",
3209 item->GetId())));
3210 EXPECT_EQ(item->GetState(), DownloadItem::INTERRUPTED);
3211 ASSERT_TRUE(item->IsInterrupted());
3212
3213 ClearEvents();
3214 item->ResumeInterruptedDownload();
3215
3216 // Errors caught before filename determination is complete are delayed until
3217 // after filename determination so that, on resumption, filename determination
3218 // does not need to be re-done. So, there will not be a second
3219 // onDeterminingFilename event.
3220
3221 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3222 base::StringPrintf("[{\"id\": %d,"
3223 " \"error\":{\"previous\":20},"
3224 " \"state\":{"
3225 " \"previous\":\"interrupted\","
3226 " \"current\":\"in_progress\"}}]",
3227 item->GetId())));
3228
3229 ClearEvents();
3230 FinishPendingSlowDownloads();
3231
3183 // The download should complete successfully. 3232 // The download should complete successfully.
3184 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged, 3233 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3185 base::StringPrintf("[{\"id\": %d," 3234 base::StringPrintf("[{\"id\": %d,"
3186 " \"filename\": {" 3235 " \"filename\": {"
3187 " \"previous\": \"%s\"," 3236 " \"previous\": \"%s\","
3188 " \"current\": \"%s\"}," 3237 " \"current\": \"%s\"},"
3189 " \"state\": {"
3190 " \"previous\": \"in_progress\","
3191 " \"current\": \"complete\"}}]",
3192 result_id,
3193 GetFilename("42.txt.crdownload").c_str(),
3194 GetFilename("42.txt").c_str())));
3195 ASSERT_TRUE(WaitFor(events::kOnDownloadChanged,
3196 base::StringPrintf("[{\"id\": %d,"
3197 " \"state\": {" 3238 " \"state\": {"
3198 " \"previous\": \"in_progress\"," 3239 " \"previous\": \"in_progress\","
3199 " \"current\": \"complete\"}}]", 3240 " \"current\": \"complete\"}}]",
3200 result_id))); 3241 item->GetId(),
3242 GetFilename("42.txt.crdownload").c_str(),
3243 GetFilename("42.txt").c_str())));
3201 } 3244 }
3202 3245
3203 // TODO(benjhayden) Figure out why DisableExtension() does not fire 3246 // TODO(benjhayden) Figure out why DisableExtension() does not fire
3204 // OnListenerRemoved. 3247 // OnListenerRemoved.
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698