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

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

Issue 1781983002: [Downloads] Introduce GUIDs for downloads. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 4 years, 9 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 "chrome/browser/download/download_history.h" 5 #include "chrome/browser/download/download_history.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/guid.h"
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
14 #include "base/rand_util.h" 15 #include "base/rand_util.h"
15 #include "base/stl_util.h" 16 #include "base/stl_util.h"
16 #include "components/history/content/browser/download_constants_utils.h" 17 #include "components/history/content/browser/download_constants_utils.h"
17 #include "components/history/core/browser/download_constants.h" 18 #include "components/history/core/browser/download_constants.h"
18 #include "components/history/core/browser/download_row.h" 19 #include "components/history/core/browser/download_row.h"
19 #include "components/history/core/browser/history_service.h" 20 #include "components/history/core/browser/history_service.h"
20 #include "content/public/test/mock_download_item.h" 21 #include "content/public/test/mock_download_item.h"
21 #include "content/public/test/mock_download_manager.h" 22 #include "content/public/test/mock_download_manager.h"
22 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread.h"
23 #include "content/public/test/test_utils.h" 24 #include "content/public/test/test_utils.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 26
26 #if defined(ENABLE_EXTENSIONS) 27 #if defined(ENABLE_EXTENSIONS)
27 #include "chrome/browser/extensions/api/downloads/downloads_api.h" 28 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
28 #endif 29 #endif
29 30
30 using testing::DoAll; 31 using testing::DoAll;
31 using testing::Invoke; 32 using testing::Invoke;
32 using testing::Return; 33 using testing::Return;
33 using testing::ReturnRefOfCopy; 34 using testing::ReturnRefOfCopy;
34 using testing::SetArgPointee; 35 using testing::SetArgPointee;
35 using testing::WithArg; 36 using testing::WithArg;
36 using testing::_; 37 using testing::_;
37 38
38 namespace { 39 namespace {
39 40
40 void CheckInfoEqual(const history::DownloadRow& left,
41 const history::DownloadRow& right) {
42 EXPECT_EQ(left.current_path.value(), right.current_path.value());
43 EXPECT_EQ(left.target_path.value(), right.target_path.value());
44 EXPECT_EQ(left.url_chain.size(), right.url_chain.size());
45 for (unsigned int i = 0;
46 i < left.url_chain.size() && i < right.url_chain.size();
47 ++i) {
48 EXPECT_EQ(left.url_chain[i].spec(), right.url_chain[i].spec());
49 }
50 EXPECT_EQ(left.referrer_url.spec(), right.referrer_url.spec());
51 EXPECT_EQ(left.mime_type, right.mime_type);
52 EXPECT_EQ(left.original_mime_type, right.original_mime_type);
53 EXPECT_EQ(left.start_time.ToTimeT(), right.start_time.ToTimeT());
54 EXPECT_EQ(left.end_time.ToTimeT(), right.end_time.ToTimeT());
55 EXPECT_EQ(left.etag, right.etag);
56 EXPECT_EQ(left.last_modified, right.last_modified);
57 EXPECT_EQ(left.received_bytes, right.received_bytes);
58 EXPECT_EQ(left.total_bytes, right.total_bytes);
59 EXPECT_EQ(left.state, right.state);
60 EXPECT_EQ(left.danger_type, right.danger_type);
61 EXPECT_EQ(left.id, right.id);
62 EXPECT_EQ(left.opened, right.opened);
63 EXPECT_EQ(left.by_ext_id, right.by_ext_id);
64 EXPECT_EQ(left.by_ext_name, right.by_ext_name);
65 }
66
67 typedef DownloadHistory::IdSet IdSet; 41 typedef DownloadHistory::IdSet IdSet;
68 typedef std::vector<history::DownloadRow> InfoVector; 42 typedef std::vector<history::DownloadRow> InfoVector;
69 typedef testing::StrictMock<content::MockDownloadItem> StrictMockDownloadItem; 43 typedef testing::StrictMock<content::MockDownloadItem> StrictMockDownloadItem;
70 44
71 class FakeHistoryAdapter : public DownloadHistory::HistoryAdapter { 45 class FakeHistoryAdapter : public DownloadHistory::HistoryAdapter {
72 public: 46 public:
73 FakeHistoryAdapter() 47 FakeHistoryAdapter()
74 : DownloadHistory::HistoryAdapter(NULL), 48 : DownloadHistory::HistoryAdapter(NULL),
75 slow_create_download_(false), 49 slow_create_download_(false),
76 fail_create_download_(false) { 50 fail_create_download_(false) {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 113
140 void FailCreateDownload() { 114 void FailCreateDownload() {
141 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 115 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
142 fail_create_download_ = true; 116 fail_create_download_ = true;
143 } 117 }
144 118
145 void ExpectDownloadCreated( 119 void ExpectDownloadCreated(
146 const history::DownloadRow& info) { 120 const history::DownloadRow& info) {
147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
148 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); 122 content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
149 CheckInfoEqual(info, create_download_info_); 123 EXPECT_EQ(info, create_download_info_);
150 create_download_info_ = history::DownloadRow(); 124 create_download_info_ = history::DownloadRow();
151 } 125 }
152 126
153 void ExpectNoDownloadCreated() { 127 void ExpectNoDownloadCreated() {
154 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 128 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
155 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); 129 content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
156 CheckInfoEqual(history::DownloadRow(), create_download_info_); 130 EXPECT_EQ(history::DownloadRow(), create_download_info_);
157 } 131 }
158 132
159 void ExpectDownloadUpdated(const history::DownloadRow& info) { 133 void ExpectDownloadUpdated(const history::DownloadRow& info) {
160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 134 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
161 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); 135 content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
162 CheckInfoEqual(update_download_, info); 136 EXPECT_EQ(update_download_, info);
163 update_download_ = history::DownloadRow(); 137 update_download_ = history::DownloadRow();
164 } 138 }
165 139
166 void ExpectNoDownloadUpdated() { 140 void ExpectNoDownloadUpdated() {
167 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 141 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
168 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); 142 content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
169 CheckInfoEqual(history::DownloadRow(), update_download_); 143 EXPECT_EQ(history::DownloadRow(), update_download_);
170 } 144 }
171 145
172 void ExpectNoDownloadsRemoved() { 146 void ExpectNoDownloadsRemoved() {
173 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
174 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); 148 content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
175 EXPECT_EQ(0, static_cast<int>(remove_downloads_.size())); 149 EXPECT_EQ(0, static_cast<int>(remove_downloads_.size()));
176 } 150 }
177 151
178 void ExpectDownloadsRemoved(const IdSet& ids) { 152 void ExpectDownloadsRemoved(const IdSet& ids) {
179 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 153 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 void CreateDownloadHistory(scoped_ptr<InfoVector> infos) { 203 void CreateDownloadHistory(scoped_ptr<InfoVector> infos) {
230 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 204 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
231 CHECK(infos.get()); 205 CHECK(infos.get());
232 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( 206 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke(
233 this, &DownloadHistoryTest::SetManagerObserver))); 207 this, &DownloadHistoryTest::SetManagerObserver)));
234 EXPECT_CALL(manager(), RemoveObserver(_)); 208 EXPECT_CALL(manager(), RemoveObserver(_));
235 download_created_index_ = 0; 209 download_created_index_ = 0;
236 for (size_t index = 0; index < infos->size(); ++index) { 210 for (size_t index = 0; index < infos->size(); ++index) {
237 const history::DownloadRow& row = infos->at(index); 211 const history::DownloadRow& row = infos->at(index);
238 content::MockDownloadManager::CreateDownloadItemAdapter adapter( 212 content::MockDownloadManager::CreateDownloadItemAdapter adapter(
213 row.guid,
239 history::ToContentDownloadId(row.id), 214 history::ToContentDownloadId(row.id),
240 row.current_path, 215 row.current_path,
241 row.target_path, 216 row.target_path,
242 row.url_chain, 217 row.url_chain,
243 row.referrer_url, 218 row.referrer_url,
244 row.mime_type, 219 row.mime_type,
245 row.original_mime_type, 220 row.original_mime_type,
246 row.start_time, 221 row.start_time,
247 row.end_time, 222 row.end_time,
248 row.etag, 223 row.etag,
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 } 319 }
345 320
346 void InitBasicItem(const base::FilePath::CharType* path, 321 void InitBasicItem(const base::FilePath::CharType* path,
347 const char* url_string, 322 const char* url_string,
348 const char* referrer_string, 323 const char* referrer_string,
349 history::DownloadRow* info) { 324 history::DownloadRow* info) {
350 GURL url(url_string); 325 GURL url(url_string);
351 GURL referrer(referrer_string); 326 GURL referrer(referrer_string);
352 std::vector<GURL> url_chain; 327 std::vector<GURL> url_chain;
353 url_chain.push_back(url); 328 url_chain.push_back(url);
354 InitItem(static_cast<uint32_t>(items_.size() + 1), base::FilePath(path), 329 InitItem(base::GenerateGUID(),
355 base::FilePath(path), url_chain, referrer, 330 static_cast<uint32_t>(items_.size() + 1),
356 "application/octet-stream", "application/octet-stream", 331 base::FilePath(path),
332 base::FilePath(path),
333 url_chain,
334 referrer,
335 "application/octet-stream",
336 "application/octet-stream",
357 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), 337 (base::Time::Now() - base::TimeDelta::FromMinutes(10)),
358 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), "Etag", 338 (base::Time::Now() - base::TimeDelta::FromMinutes(1)),
359 "abc", 100, 100, content::DownloadItem::COMPLETE, 339 "Etag",
340 "abc",
341 100,
342 100,
343 content::DownloadItem::COMPLETE,
360 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 344 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
361 content::DOWNLOAD_INTERRUPT_REASON_NONE, false, std::string(), 345 content::DOWNLOAD_INTERRUPT_REASON_NONE,
362 std::string(), info); 346 false,
347 std::string(),
348 std::string(),
349 info);
363 } 350 }
364 351
365 void InitItem(uint32_t id, 352 void InitItem(const std::string& guid,
353 uint32_t id,
366 const base::FilePath& current_path, 354 const base::FilePath& current_path,
367 const base::FilePath& target_path, 355 const base::FilePath& target_path,
368 const std::vector<GURL>& url_chain, 356 const std::vector<GURL>& url_chain,
369 const GURL& referrer, 357 const GURL& referrer,
370 const std::string& mime_type, 358 const std::string& mime_type,
371 const std::string& original_mime_type, 359 const std::string& original_mime_type,
372 const base::Time& start_time, 360 const base::Time& start_time,
373 const base::Time& end_time, 361 const base::Time& end_time,
374 const std::string& etag, 362 const std::string& etag,
375 const std::string& last_modified, 363 const std::string& last_modified,
(...skipping 22 matching lines...) Expand all
398 info->end_time = end_time; 386 info->end_time = end_time;
399 info->etag = etag; 387 info->etag = etag;
400 info->last_modified = last_modified; 388 info->last_modified = last_modified;
401 info->received_bytes = received_bytes; 389 info->received_bytes = received_bytes;
402 info->total_bytes = total_bytes; 390 info->total_bytes = total_bytes;
403 info->state = history::ToHistoryDownloadState(state); 391 info->state = history::ToHistoryDownloadState(state);
404 info->danger_type = history::ToHistoryDownloadDangerType(danger_type); 392 info->danger_type = history::ToHistoryDownloadDangerType(danger_type);
405 info->interrupt_reason = 393 info->interrupt_reason =
406 history::ToHistoryDownloadInterruptReason(interrupt_reason); 394 history::ToHistoryDownloadInterruptReason(interrupt_reason);
407 info->id = history::ToHistoryDownloadId(id); 395 info->id = history::ToHistoryDownloadId(id);
396 info->guid = guid;
408 info->opened = opened; 397 info->opened = opened;
409 info->by_ext_id = by_extension_id; 398 info->by_ext_id = by_extension_id;
410 info->by_ext_name = by_extension_name; 399 info->by_ext_name = by_extension_name;
411 400
412 EXPECT_CALL(item(index), GetId()).WillRepeatedly(Return(id)); 401 EXPECT_CALL(item(index), GetId()).WillRepeatedly(Return(id));
402 EXPECT_CALL(item(index), GetGuid()).WillRepeatedly(ReturnRefOfCopy(guid));
413 EXPECT_CALL(item(index), GetFullPath()) 403 EXPECT_CALL(item(index), GetFullPath())
414 .WillRepeatedly(ReturnRefOfCopy(current_path)); 404 .WillRepeatedly(ReturnRefOfCopy(current_path));
415 EXPECT_CALL(item(index), GetTargetFilePath()) 405 EXPECT_CALL(item(index), GetTargetFilePath())
416 .WillRepeatedly(ReturnRefOfCopy(target_path)); 406 .WillRepeatedly(ReturnRefOfCopy(target_path));
417 DCHECK_LE(1u, url_chain.size()); 407 DCHECK_LE(1u, url_chain.size());
418 EXPECT_CALL(item(index), GetURL()) 408 EXPECT_CALL(item(index), GetURL())
419 .WillRepeatedly(ReturnRefOfCopy(url_chain[0])); 409 .WillRepeatedly(ReturnRefOfCopy(url_chain[0]));
420 EXPECT_CALL(item(index), GetUrlChain()) 410 EXPECT_CALL(item(index), GetUrlChain())
421 .WillRepeatedly(ReturnRefOfCopy(url_chain)); 411 .WillRepeatedly(ReturnRefOfCopy(url_chain));
422 EXPECT_CALL(item(index), GetMimeType()).WillRepeatedly(Return(mime_type)); 412 EXPECT_CALL(item(index), GetMimeType()).WillRepeatedly(Return(mime_type));
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
854 FinishCreateDownload(); 844 FinishCreateDownload();
855 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); 845 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0)));
856 846
857 // ItemAdded should call OnDownloadUpdated, which should detect that the item 847 // ItemAdded should call OnDownloadUpdated, which should detect that the item
858 // changed while it was being added and call UpdateDownload immediately. 848 // changed while it was being added and call UpdateDownload immediately.
859 info.opened = true; 849 info.opened = true;
860 ExpectDownloadUpdated(info); 850 ExpectDownloadUpdated(info);
861 } 851 }
862 852
863 } // anonymous namespace 853 } // anonymous namespace
OLDNEW
« no previous file with comments | « chrome/browser/download/download_history.cc ('k') | chrome/browser/download/download_ui_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698