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

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

Issue 10915180: Make DownloadHistory observe manager, items (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r168573 Created 8 years, 1 month 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
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/json/json_writer.h" 10 #include "base/json/json_writer.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "chrome/browser/download/download_file_icon_extractor.h" 14 #include "chrome/browser/download/download_file_icon_extractor.h"
15 #include "chrome/browser/download/download_service.h" 15 #include "chrome/browser/download/download_service.h"
16 #include "chrome/browser/download/download_service_factory.h" 16 #include "chrome/browser/download/download_service_factory.h"
17 #include "chrome/browser/download/download_test_file_chooser_observer.h" 17 #include "chrome/browser/download/download_test_file_chooser_observer.h"
18 #include "chrome/browser/extensions/api/downloads/downloads_api.h" 18 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
19 #include "chrome/browser/extensions/event_names.h" 19 #include "chrome/browser/extensions/event_names.h"
20 #include "chrome/browser/extensions/extension_apitest.h" 20 #include "chrome/browser/extensions/extension_apitest.h"
21 #include "chrome/browser/extensions/extension_function_test_utils.h" 21 #include "chrome/browser/extensions/extension_function_test_utils.h"
22 #include "chrome/browser/history/download_row.h"
22 #include "chrome/browser/net/url_request_mock_util.h" 23 #include "chrome/browser/net/url_request_mock_util.h"
23 #include "chrome/browser/prefs/pref_service.h" 24 #include "chrome/browser/prefs/pref_service.h"
24 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/browser.h" 26 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_tabstrip.h" 27 #include "chrome/browser/ui/browser_tabstrip.h"
27 #include "chrome/browser/ui/tab_contents/tab_contents.h" 28 #include "chrome/browser/ui/tab_contents/tab_contents.h"
28 #include "chrome/common/chrome_notification_types.h" 29 #include "chrome/common/chrome_notification_types.h"
29 #include "chrome/common/pref_names.h" 30 #include "chrome/common/pref_names.h"
30 #include "chrome/test/base/in_process_browser_test.h" 31 #include "chrome/test/base/in_process_browser_test.h"
31 #include "chrome/test/base/ui_test_utils.h" 32 #include "chrome/test/base/ui_test_utils.h"
32 #include "content/public/browser/browser_context.h" 33 #include "content/public/browser/browser_context.h"
33 #include "content/public/browser/browser_thread.h" 34 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/download_item.h" 35 #include "content/public/browser/download_item.h"
35 #include "content/public/browser/download_manager.h" 36 #include "content/public/browser/download_manager.h"
36 #include "content/public/browser/download_persistent_store_info.h"
37 #include "content/public/browser/notification_service.h" 37 #include "content/public/browser/notification_service.h"
38 #include "content/public/browser/storage_partition.h" 38 #include "content/public/browser/storage_partition.h"
39 #include "content/public/browser/web_contents.h" 39 #include "content/public/browser/web_contents.h"
40 #include "content/public/common/page_transition_types.h" 40 #include "content/public/common/page_transition_types.h"
41 #include "content/public/test/download_test_observer.h" 41 #include "content/public/test/download_test_observer.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"
52 #include "webkit/blob/blob_url_request_job.h" 52 #include "webkit/blob/blob_url_request_job.h"
53 #include "webkit/fileapi/file_system_context.h" 53 #include "webkit/fileapi/file_system_context.h"
54 #include "webkit/fileapi/file_system_operation.h" 54 #include "webkit/fileapi/file_system_operation.h"
55 #include "webkit/fileapi/file_system_url.h" 55 #include "webkit/fileapi/file_system_url.h"
56 56
57 using content::BrowserContext; 57 using content::BrowserContext;
58 using content::BrowserThread; 58 using content::BrowserThread;
59 using content::DownloadItem; 59 using content::DownloadItem;
60 using content::DownloadManager; 60 using content::DownloadManager;
61 using content::DownloadPersistentStoreInfo;
62 using content::URLRequestSlowDownloadJob; 61 using content::URLRequestSlowDownloadJob;
63 62
64 namespace events = extensions::event_names; 63 namespace events = extensions::event_names;
65 64
66 namespace { 65 namespace {
67 66
68 // Comparator that orders download items by their ID. Can be used with 67 // Comparator that orders download items by their ID. Can be used with
69 // std::sort. 68 // std::sort.
70 struct DownloadIdComparator { 69 struct DownloadIdComparator {
71 bool operator() (DownloadItem* first, DownloadItem* second) { 70 bool operator() (DownloadItem* first, DownloadItem* second) {
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 349
351 // Creates a set of history downloads based on the provided |history_info| 350 // Creates a set of history downloads based on the provided |history_info|
352 // array. |count| is the number of elements in |history_info|. On success, 351 // array. |count| is the number of elements in |history_info|. On success,
353 // |items| will contain |count| DownloadItems in the order that they were 352 // |items| will contain |count| DownloadItems in the order that they were
354 // specified in |history_info|. Returns true on success and false otherwise. 353 // specified in |history_info|. Returns true on success and false otherwise.
355 bool CreateHistoryDownloads(const HistoryDownloadInfo* history_info, 354 bool CreateHistoryDownloads(const HistoryDownloadInfo* history_info,
356 size_t count, 355 size_t count,
357 DownloadManager::DownloadVector* items) { 356 DownloadManager::DownloadVector* items) {
358 DownloadIdComparator download_id_comparator; 357 DownloadIdComparator download_id_comparator;
359 base::Time current = base::Time::Now(); 358 base::Time current = base::Time::Now();
360 std::vector<DownloadPersistentStoreInfo> entries; 359 items->clear();
361 entries.reserve(count); 360 GetOnRecordManager()->GetAllDownloads(items);
361 CHECK_EQ(0, static_cast<int>(items->size()));
362 for (size_t i = 0; i < count; ++i) { 362 for (size_t i = 0; i < count; ++i) {
363 DownloadPersistentStoreInfo entry( 363 DownloadItem* item = GetOnRecordManager()->CreateDownloadItem(
364 downloads_directory().Append(history_info[i].filename), 364 downloads_directory().Append(history_info[i].filename),
365 GURL(), GURL(), // URL, referrer 365 GURL(), GURL(), // URL, referrer
366 current, current, // start_time, end_time 366 current, current, // start_time, end_time
367 1, 1, // received_bytes, total_bytes 367 1, 1, // received_bytes, total_bytes
368 history_info[i].state, // state 368 history_info[i].state, // state
369 i + 1, // db_handle
370 false); // opened 369 false); // opened
371 entries.push_back(entry); 370 items->push_back(item);
372 } 371 }
373 GetOnRecordManager()->OnPersistentStoreQueryComplete(&entries);
374 GetOnRecordManager()->GetAllDownloads(items);
375 EXPECT_EQ(count, items->size());
376 if (count != items->size())
377 return false;
378 372
379 // Order by ID so that they are in the order that we created them. 373 // Order by ID so that they are in the order that we created them.
380 std::sort(items->begin(), items->end(), download_id_comparator); 374 std::sort(items->begin(), items->end(), download_id_comparator);
381 // Set the danger type if necessary. 375 // Set the danger type if necessary.
382 for (size_t i = 0; i < count; ++i) { 376 for (size_t i = 0; i < count; ++i) {
383 if (history_info[i].danger_type != 377 if (history_info[i].danger_type !=
384 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { 378 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) {
385 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT, 379 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT,
386 history_info[i].danger_type); 380 history_info[i].danger_type);
387 items->at(i)->OnContentCheckCompleted(history_info[i].danger_type); 381 items->at(i)->OnContentCheckCompleted(history_info[i].danger_type);
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 void RunCallback() { 570 void RunCallback() {
577 callback_.Run(response_); 571 callback_.Run(response_);
578 } 572 }
579 573
580 FilePath expected_path_; 574 FilePath expected_path_;
581 IconLoader::IconSize expected_icon_size_; 575 IconLoader::IconSize expected_icon_size_;
582 std::string response_; 576 std::string response_;
583 IconURLCallback callback_; 577 IconURLCallback callback_;
584 }; 578 };
585 579
580 bool ItemNotInProgress(DownloadItem* item) {
581 return !item->IsInProgress();
582 }
583
586 // Cancels the underlying DownloadItem when the ScopedCancellingItem goes out of 584 // Cancels the underlying DownloadItem when the ScopedCancellingItem goes out of
587 // scope. Like a scoped_ptr, but for DownloadItems. 585 // scope. Like a scoped_ptr, but for DownloadItems.
588 class ScopedCancellingItem { 586 class ScopedCancellingItem {
589 public: 587 public:
590 explicit ScopedCancellingItem(DownloadItem* item) : item_(item) {} 588 explicit ScopedCancellingItem(DownloadItem* item) : item_(item) {}
591 ~ScopedCancellingItem() { 589 ~ScopedCancellingItem() {
592 item_->Cancel(true); 590 item_->Cancel(true);
591 content::DownloadUpdatedObserver observer(
592 item_, base::Bind(&ItemNotInProgress));
593 observer.WaitForEvent();
593 } 594 }
594 DownloadItem* operator*() { return item_; } 595 DownloadItem* operator*() { return item_; }
595 DownloadItem* operator->() { return item_; } 596 DownloadItem* operator->() { return item_; }
596 DownloadItem* get() { return item_; } 597 DownloadItem* get() { return item_; }
597 private: 598 private:
598 DownloadItem* item_; 599 DownloadItem* item_;
599 DISALLOW_COPY_AND_ASSIGN(ScopedCancellingItem); 600 DISALLOW_COPY_AND_ASSIGN(ScopedCancellingItem);
600 }; 601 };
601 602
602 // Cancels all the underlying DownloadItems when the ScopedItemVectorCanceller 603 // Cancels all the underlying DownloadItems when the ScopedItemVectorCanceller
603 // goes out of scope. Generalization of ScopedCancellingItem to many 604 // goes out of scope. Generalization of ScopedCancellingItem to many
604 // DownloadItems. 605 // DownloadItems.
605 class ScopedItemVectorCanceller { 606 class ScopedItemVectorCanceller {
606 public: 607 public:
607 explicit ScopedItemVectorCanceller(DownloadManager::DownloadVector* items) 608 explicit ScopedItemVectorCanceller(DownloadManager::DownloadVector* items)
608 : items_(items) { 609 : items_(items) {
609 } 610 }
610 ~ScopedItemVectorCanceller() { 611 ~ScopedItemVectorCanceller() {
611 for (DownloadManager::DownloadVector::const_iterator item = items_->begin(); 612 for (DownloadManager::DownloadVector::const_iterator item = items_->begin();
612 item != items_->end(); ++item) { 613 item != items_->end(); ++item) {
613 if ((*item)->IsInProgress()) 614 if ((*item)->IsInProgress())
614 (*item)->Cancel(true); 615 (*item)->Cancel(true);
616 content::DownloadUpdatedObserver observer(
617 (*item), base::Bind(&ItemNotInProgress));
618 observer.WaitForEvent();
615 } 619 }
616 } 620 }
617 621
618 private: 622 private:
619 DownloadManager::DownloadVector* items_; 623 DownloadManager::DownloadVector* items_;
620 DISALLOW_COPY_AND_ASSIGN(ScopedItemVectorCanceller); 624 DISALLOW_COPY_AND_ASSIGN(ScopedItemVectorCanceller);
621 }; 625 };
622 626
623 class TestProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { 627 class TestProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
624 public: 628 public:
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 const FilePath& expected_path, 894 const FilePath& expected_path,
891 IconLoader::IconSize icon_size, 895 IconLoader::IconSize icon_size,
892 const std::string& response) { 896 const std::string& response) {
893 scoped_refptr<DownloadsGetFileIconFunction> function( 897 scoped_refptr<DownloadsGetFileIconFunction> function(
894 new DownloadsGetFileIconFunction()); 898 new DownloadsGetFileIconFunction());
895 function->SetIconExtractorForTesting(new MockIconExtractorImpl( 899 function->SetIconExtractorForTesting(new MockIconExtractorImpl(
896 expected_path, icon_size, response)); 900 expected_path, icon_size, response));
897 return function.release(); 901 return function.release();
898 } 902 }
899 903
900 bool WaitForPersisted(DownloadItem* item) {
901 return item->IsPersisted();
902 }
903
904 // Test downloads.getFileIcon() on in-progress, finished, cancelled and deleted 904 // Test downloads.getFileIcon() on in-progress, finished, cancelled and deleted
905 // download items. 905 // download items.
906 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 906 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
907 DownloadExtensionTest_FileIcon_Active) { 907 DownloadExtensionTest_FileIcon_Active) {
908 DownloadItem* download_item = CreateSlowTestDownload(); 908 DownloadItem* download_item = CreateSlowTestDownload();
909 ASSERT_TRUE(download_item); 909 ASSERT_TRUE(download_item);
910 ASSERT_FALSE(download_item->GetTargetFilePath().empty());
910 std::string args32(base::StringPrintf("[%d, {\"size\": 32}]", 911 std::string args32(base::StringPrintf("[%d, {\"size\": 32}]",
911 download_item->GetId())); 912 download_item->GetId()));
912 std::string result_string; 913 std::string result_string;
913 914
914 // Get the icon for the in-progress download. This call should succeed even 915 // Get the icon for the in-progress download. This call should succeed even
915 // if the file type isn't registered. 916 // if the file type isn't registered.
916 // Test whether the correct path is being pased into the icon extractor. 917 // Test whether the correct path is being pased into the icon extractor.
917 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction( 918 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction(
918 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"), 919 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"),
919 base::StringPrintf("[%d, {}]", download_item->GetId()), &result_string)); 920 base::StringPrintf("[%d, {}]", download_item->GetId()), &result_string));
(...skipping 16 matching lines...) Expand all
936 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"), 937 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"),
937 args32, &result_string)); 938 args32, &result_string));
938 939
939 // Check the path passed to the icon extractor post-completion. 940 // Check the path passed to the icon extractor post-completion.
940 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction( 941 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction(
941 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"), 942 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"),
942 args32, &result_string)); 943 args32, &result_string));
943 944
944 // Now create another download. 945 // Now create another download.
945 download_item = CreateSlowTestDownload(); 946 download_item = CreateSlowTestDownload();
947 ASSERT_TRUE(download_item);
948 ASSERT_FALSE(download_item->GetTargetFilePath().empty());
946 args32 = base::StringPrintf("[%d, {\"size\": 32}]", download_item->GetId()); 949 args32 = base::StringPrintf("[%d, {\"size\": 32}]", download_item->GetId());
947 ASSERT_TRUE(download_item);
948
949 // http://crbug.com/154995
950 content::DownloadUpdatedObserver persisted(
951 download_item, base::Bind(&WaitForPersisted));
952 persisted.WaitForEvent();
953 950
954 // Cancel the download. As long as the download has a target path, we should 951 // Cancel the download. As long as the download has a target path, we should
955 // be able to query the file icon. 952 // be able to query the file icon.
956 download_item->Cancel(true); 953 download_item->Cancel(true);
954 ASSERT_FALSE(download_item->GetTargetFilePath().empty());
957 // Let cleanup complete on the FILE thread. 955 // Let cleanup complete on the FILE thread.
958 content::RunAllPendingInMessageLoop(BrowserThread::FILE); 956 content::RunAllPendingInMessageLoop(BrowserThread::FILE);
959 // Check the path passed to the icon extractor post-cancellation. 957 // Check the path passed to the icon extractor post-cancellation.
960 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction( 958 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction(
961 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"), 959 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"),
962 args32, 960 args32,
963 &result_string)); 961 &result_string));
964 962
965 // Simulate an error during icon load by invoking the mock with an empty 963 // Simulate an error during icon load by invoking the mock with an empty
966 // result string. 964 // result string.
(...skipping 1116 matching lines...) Expand 10 before | Expand all | Expand 10 after
2083 " \"state\": {" 2081 " \"state\": {"
2084 " \"previous\": \"in_progress\"," 2082 " \"previous\": \"in_progress\","
2085 " \"current\": \"complete\"}}]", 2083 " \"current\": \"complete\"}}]",
2086 result_id, 2084 result_id,
2087 GetFilename("on_record.txt.crdownload").c_str(), 2085 GetFilename("on_record.txt.crdownload").c_str(),
2088 GetFilename("on_record.txt").c_str()))); 2086 GetFilename("on_record.txt").c_str())));
2089 std::string disk_data; 2087 std::string disk_data;
2090 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data)); 2088 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data));
2091 EXPECT_STREQ(kPayloadData, disk_data.c_str()); 2089 EXPECT_STREQ(kPayloadData, disk_data.c_str());
2092 } 2090 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698