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

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: . Created 8 years, 3 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
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/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/scoped_temp_dir.h" 11 #include "base/scoped_temp_dir.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_persistent_store_info.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_impl.h" 49 #include "net/url_request/url_request_job_factory_impl.h"
49 #include "net/url_request/url_request_job_factory.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 61
63 namespace events = extensions::event_names; 62 namespace events = extensions::event_names;
64 63
65 namespace { 64 namespace {
66 65
67 // Comparator that orders download items by their ID. Can be used with 66 // Comparator that orders download items by their ID. Can be used with
68 // std::sort. 67 // std::sort.
69 struct DownloadIdComparator { 68 struct DownloadIdComparator {
70 bool operator() (DownloadItem* first, DownloadItem* second) { 69 bool operator() (DownloadItem* first, DownloadItem* second) {
71 return first->GetId() < second->GetId(); 70 return first->GetId() < second->GetId();
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 348
350 // Creates a set of history downloads based on the provided |history_info| 349 // Creates a set of history downloads based on the provided |history_info|
351 // array. |count| is the number of elements in |history_info|. On success, 350 // array. |count| is the number of elements in |history_info|. On success,
352 // |items| will contain |count| DownloadItems in the order that they were 351 // |items| will contain |count| DownloadItems in the order that they were
353 // specified in |history_info|. Returns true on success and false otherwise. 352 // specified in |history_info|. Returns true on success and false otherwise.
354 bool CreateHistoryDownloads(const HistoryDownloadInfo* history_info, 353 bool CreateHistoryDownloads(const HistoryDownloadInfo* history_info,
355 size_t count, 354 size_t count,
356 DownloadManager::DownloadVector* items) { 355 DownloadManager::DownloadVector* items) {
357 DownloadIdComparator download_id_comparator; 356 DownloadIdComparator download_id_comparator;
358 base::Time current = base::Time::Now(); 357 base::Time current = base::Time::Now();
359 std::vector<DownloadPersistentStoreInfo> entries; 358 items->clear();
360 entries.reserve(count); 359 GetOnRecordManager()->GetAllDownloads(items);
360 CHECK_EQ(0, static_cast<int>(items->size()));
361 for (size_t i = 0; i < count; ++i) { 361 for (size_t i = 0; i < count; ++i) {
362 DownloadPersistentStoreInfo entry( 362 DownloadItem* item = GetOnRecordManager()->CreateDownloadItem(
363 downloads_directory().Append(history_info[i].filename), 363 downloads_directory().Append(history_info[i].filename),
364 GURL(), GURL(), // URL, referrer 364 GURL(), GURL(), // URL, referrer
365 current, current, // start_time, end_time 365 current, current, // start_time, end_time
366 1, 1, // received_bytes, total_bytes 366 1, 1, // received_bytes, total_bytes
367 history_info[i].state, // state 367 history_info[i].state, // state
368 i + 1, // db_handle
369 false); // opened 368 false); // opened
370 entries.push_back(entry); 369 items->push_back(item);
371 } 370 }
372 GetOnRecordManager()->OnPersistentStoreQueryComplete(&entries);
373 GetOnRecordManager()->GetAllDownloads(items);
374 EXPECT_EQ(count, items->size());
375 if (count != items->size())
376 return false;
377 371
378 // Order by ID so that they are in the order that we created them. 372 // Order by ID so that they are in the order that we created them.
379 std::sort(items->begin(), items->end(), download_id_comparator); 373 std::sort(items->begin(), items->end(), download_id_comparator);
380 // Set the danger type if necessary. 374 // Set the danger type if necessary.
381 for (size_t i = 0; i < count; ++i) { 375 for (size_t i = 0; i < count; ++i) {
382 if (history_info[i].danger_type != 376 if (history_info[i].danger_type !=
383 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { 377 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) {
384 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT, 378 EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT,
385 history_info[i].danger_type); 379 history_info[i].danger_type);
386 items->at(i)->OnContentCheckCompleted(history_info[i].danger_type); 380 items->at(i)->OnContentCheckCompleted(history_info[i].danger_type);
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 expected_path, icon_size, response)); 883 expected_path, icon_size, response));
890 return function.release(); 884 return function.release();
891 } 885 }
892 886
893 // Test downloads.getFileIcon() on in-progress, finished, cancelled and deleted 887 // Test downloads.getFileIcon() on in-progress, finished, cancelled and deleted
894 // download items. 888 // download items.
895 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest, 889 IN_PROC_BROWSER_TEST_F(DownloadExtensionTest,
896 DownloadExtensionTest_FileIcon_Active) { 890 DownloadExtensionTest_FileIcon_Active) {
897 DownloadItem* download_item = CreateSlowTestDownload(); 891 DownloadItem* download_item = CreateSlowTestDownload();
898 ASSERT_TRUE(download_item); 892 ASSERT_TRUE(download_item);
893 EXPECT_FALSE(download_item->GetTargetFilePath().empty());
899 std::string args32(base::StringPrintf("[%d, {\"size\": 32}]", 894 std::string args32(base::StringPrintf("[%d, {\"size\": 32}]",
900 download_item->GetId())); 895 download_item->GetId()));
901 std::string result_string; 896 std::string result_string;
902 897
903 // Get the icon for the in-progress download. This call should succeed even 898 // Get the icon for the in-progress download. This call should succeed even
904 // if the file type isn't registered. 899 // if the file type isn't registered.
905 // Test whether the correct path is being pased into the icon extractor. 900 // Test whether the correct path is being pased into the icon extractor.
906 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction( 901 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction(
907 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"), 902 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"),
908 base::StringPrintf("[%d, {}]", download_item->GetId()), &result_string)); 903 base::StringPrintf("[%d, {}]", download_item->GetId()), &result_string));
(...skipping 16 matching lines...) Expand all
925 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"), 920 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"),
926 args32, &result_string)); 921 args32, &result_string));
927 922
928 // Check the path passed to the icon extractor post-completion. 923 // Check the path passed to the icon extractor post-completion.
929 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction( 924 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction(
930 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"), 925 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"),
931 args32, &result_string)); 926 args32, &result_string));
932 927
933 // Now create another download. 928 // Now create another download.
934 download_item = CreateSlowTestDownload(); 929 download_item = CreateSlowTestDownload();
930 ASSERT_TRUE(download_item);
931 EXPECT_FALSE(download_item->GetTargetFilePath().empty());
935 args32 = base::StringPrintf("[%d, {\"size\": 32}]", download_item->GetId()); 932 args32 = base::StringPrintf("[%d, {\"size\": 32}]", download_item->GetId());
936 ASSERT_TRUE(download_item);
937 933
938 // Cancel the download. As long as the download has a target path, we should 934 // Cancel the download. As long as the download has a target path, we should
939 // be able to query the file icon. 935 // be able to query the file icon.
940 download_item->Cancel(true); 936 download_item->Cancel(true);
937 EXPECT_FALSE(download_item->GetTargetFilePath().empty());
941 // Let cleanup complete on the FILE thread. 938 // Let cleanup complete on the FILE thread.
942 content::RunAllPendingInMessageLoop(BrowserThread::FILE); 939 content::RunAllPendingInMessageLoop(BrowserThread::FILE);
943 // Check the path passed to the icon extractor post-cancellation. 940 // Check the path passed to the icon extractor post-cancellation.
944 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction( 941 EXPECT_TRUE(RunFunctionAndReturnString(MockedGetFileIconFunction(
945 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"), 942 download_item->GetTargetFilePath(), IconLoader::NORMAL, "foo"),
946 args32, 943 args32,
947 &result_string)); 944 &result_string));
948 945
949 // Simulate an error during icon load by invoking the mock with an empty 946 // Simulate an error during icon load by invoking the mock with an empty
950 // result string. 947 // result string.
(...skipping 1136 matching lines...) Expand 10 before | Expand all | Expand 10 after
2087 " \"state\": {" 2084 " \"state\": {"
2088 " \"previous\": \"in_progress\"," 2085 " \"previous\": \"in_progress\","
2089 " \"current\": \"complete\"}}]", 2086 " \"current\": \"complete\"}}]",
2090 result_id, 2087 result_id,
2091 GetFilename("on_record.txt.crdownload").c_str(), 2088 GetFilename("on_record.txt.crdownload").c_str(),
2092 GetFilename("on_record.txt").c_str()))); 2089 GetFilename("on_record.txt").c_str())));
2093 std::string disk_data; 2090 std::string disk_data;
2094 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data)); 2091 EXPECT_TRUE(file_util::ReadFileToString(item->GetFullPath(), &disk_data));
2095 EXPECT_STREQ(kPayloadData, disk_data.c_str()); 2092 EXPECT_STREQ(kPayloadData, disk_data.c_str());
2096 } 2093 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698