Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / download / download_query_unittest.cc
blob61e68c9318eca8320d59419189317e69fb41554b
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
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string16.h"
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "chrome/browser/download/download_query.h"
16 #include "content/public/test/mock_download_item.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using ::testing::Return;
21 using ::testing::ReturnRef;
22 using ::testing::_;
23 using base::Time;
24 using base::Value;
25 using content::DownloadItem;
26 typedef DownloadQuery::DownloadVector DownloadVector;
28 namespace {
30 static const int kSomeKnownTime = 1355864160;
31 static const char kSomeKnownTime8601[] = "2012-12-18T20:56:0";
32 static const char k8601Suffix[] = ".000Z";
34 bool IdNotEqual(uint32 not_id, const DownloadItem& item) {
35 return item.GetId() != not_id;
38 bool AlwaysReturn(bool result, const DownloadItem& item) {
39 return result;
42 } // anonymous namespace
44 class DownloadQueryTest : public testing::Test {
45 public:
46 DownloadQueryTest() {}
48 ~DownloadQueryTest() override {}
50 void TearDown() override { STLDeleteElements(&mocks_); }
52 void CreateMocks(int count) {
53 for (int i = 0; i < count; ++i) {
54 mocks_.push_back(new content::MockDownloadItem());
55 EXPECT_CALL(mock(mocks_.size() - 1), GetId()).WillRepeatedly(Return(
56 mocks_.size() - 1));
60 content::MockDownloadItem& mock(int index) { return *mocks_[index]; }
62 DownloadQuery* query() { return &query_; }
64 template<typename ValueType> void AddFilter(
65 DownloadQuery::FilterType name, ValueType value);
67 void Search() {
68 query_.Search(mocks_.begin(), mocks_.end(), &results_);
71 DownloadVector* results() { return &results_; }
73 // Filter tests generally contain 2 items. mock(0) matches the filter, mock(1)
74 // does not.
75 void ExpectStandardFilterResults() {
76 Search();
77 ASSERT_EQ(1U, results()->size());
78 ASSERT_EQ(0U, results()->at(0)->GetId());
81 // If no sorters distinguish between two items, then DownloadQuery sorts by ID
82 // ascending. In order to test that a sorter distinguishes between two items,
83 // the sorter must sort them by ID descending.
84 void ExpectSortInverted() {
85 Search();
86 ASSERT_EQ(2U, results()->size());
87 ASSERT_EQ(1U, results()->at(0)->GetId());
88 ASSERT_EQ(0U, results()->at(1)->GetId());
91 private:
92 std::vector<content::MockDownloadItem*> mocks_;
93 DownloadQuery query_;
94 DownloadVector results_;
96 DISALLOW_COPY_AND_ASSIGN(DownloadQueryTest);
99 template<> void DownloadQueryTest::AddFilter(
100 DownloadQuery::FilterType name, bool cpp_value) {
101 scoped_ptr<base::Value> value(new base::FundamentalValue(cpp_value));
102 CHECK(query_.AddFilter(name, *value.get()));
105 template<> void DownloadQueryTest::AddFilter(
106 DownloadQuery::FilterType name, int cpp_value) {
107 scoped_ptr<base::Value> value(new base::FundamentalValue(cpp_value));
108 CHECK(query_.AddFilter(name, *value.get()));
111 template<> void DownloadQueryTest::AddFilter(
112 DownloadQuery::FilterType name, const char* cpp_value) {
113 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
116 template<> void DownloadQueryTest::AddFilter(
117 DownloadQuery::FilterType name, std::string cpp_value) {
118 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
121 template<> void DownloadQueryTest::AddFilter(
122 DownloadQuery::FilterType name, const base::char16* cpp_value) {
123 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
126 template<> void DownloadQueryTest::AddFilter(
127 DownloadQuery::FilterType name, std::vector<base::string16> cpp_value) {
128 scoped_ptr<base::ListValue> list(new base::ListValue());
129 for (std::vector<base::string16>::const_iterator it = cpp_value.begin();
130 it != cpp_value.end(); ++it) {
131 list->Append(new base::StringValue(*it));
133 CHECK(query_.AddFilter(name, *list.get()));
136 template<> void DownloadQueryTest::AddFilter(
137 DownloadQuery::FilterType name, std::vector<std::string> cpp_value) {
138 scoped_ptr<base::ListValue> list(new base::ListValue());
139 for (std::vector<std::string>::const_iterator it = cpp_value.begin();
140 it != cpp_value.end(); ++it) {
141 list->Append(new base::StringValue(*it));
143 CHECK(query_.AddFilter(name, *list.get()));
146 #if defined(OS_WIN)
147 template<> void DownloadQueryTest::AddFilter(
148 DownloadQuery::FilterType name, std::wstring cpp_value) {
149 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
151 #endif
153 TEST_F(DownloadQueryTest, DownloadQueryTest_ZeroItems) {
154 Search();
155 EXPECT_EQ(0U, results()->size());
158 TEST_F(DownloadQueryTest, DownloadQueryTest_InvalidFilter) {
159 scoped_ptr<base::Value> value(new base::FundamentalValue(0));
160 EXPECT_FALSE(query()->AddFilter(
161 static_cast<DownloadQuery::FilterType>(kint32max),
162 *value.get()));
165 TEST_F(DownloadQueryTest, DownloadQueryTest_EmptyQuery) {
166 CreateMocks(2);
167 Search();
168 ASSERT_EQ(2U, results()->size());
169 ASSERT_EQ(0U, results()->at(0)->GetId());
170 ASSERT_EQ(1U, results()->at(1)->GetId());
173 TEST_F(DownloadQueryTest, DownloadQueryTest_Limit) {
174 CreateMocks(2);
175 query()->Limit(1);
176 ExpectStandardFilterResults();
179 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilename) {
180 CreateMocks(2);
181 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
182 static_cast<content::BrowserContext*>(NULL)));
183 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
184 static_cast<content::BrowserContext*>(NULL)));
185 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
186 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
187 match_filename));
188 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
189 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
190 fail_filename));
191 GURL fail_url("http://example.com/fail");
192 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
193 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
194 std::vector<std::string> query_terms;
195 query_terms.push_back("query");
196 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
197 ExpectStandardFilterResults();
200 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryUrl) {
201 CreateMocks(2);
202 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
203 static_cast<content::BrowserContext*>(NULL)));
204 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
205 static_cast<content::BrowserContext*>(NULL)));
206 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
207 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
208 fail_filename));
209 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
210 fail_filename));
211 GURL match_url("http://query.com/query");
212 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
213 GURL fail_url("http://example.com/fail");
214 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
215 std::vector<std::string> query_terms;
216 query_terms.push_back("query");
217 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
218 ExpectStandardFilterResults();
221 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilenameI18N) {
222 CreateMocks(2);
223 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
224 static_cast<content::BrowserContext*>(NULL)));
225 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
226 static_cast<content::BrowserContext*>(NULL)));
227 const base::FilePath::StringType kTestString(
228 #if defined(OS_POSIX)
229 "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd"
230 #elif defined(OS_WIN)
231 L"/\x4f60\x597d\x4f60\x597d"
232 #endif
234 base::FilePath match_filename(kTestString);
235 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
236 match_filename));
237 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
238 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
239 fail_filename));
240 GURL fail_url("http://example.com/fail");
241 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
242 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
243 std::vector<base::FilePath::StringType> query_terms;
244 query_terms.push_back(kTestString);
245 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
246 ExpectStandardFilterResults();
249 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilenameRegex) {
250 CreateMocks(2);
251 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
252 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
253 match_filename));
254 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
255 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
256 fail_filename));
257 AddFilter(DownloadQuery::FILTER_FILENAME_REGEX, "y");
258 ExpectStandardFilterResults();
261 TEST_F(DownloadQueryTest, DownloadQueryTest_SortFilename) {
262 CreateMocks(2);
263 base::FilePath b_filename(FILE_PATH_LITERAL("b"));
264 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
265 b_filename));
266 base::FilePath a_filename(FILE_PATH_LITERAL("a"));
267 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
268 a_filename));
269 query()->AddSorter(DownloadQuery::SORT_FILENAME, DownloadQuery::ASCENDING);
270 ExpectSortInverted();
273 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilename) {
274 CreateMocks(2);
275 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
276 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
277 match_filename));
278 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
279 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
280 fail_filename));
281 AddFilter(DownloadQuery::FILTER_FILENAME, match_filename.value().c_str());
282 ExpectStandardFilterResults();
285 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrlRegex) {
286 CreateMocks(2);
287 GURL match_url("http://query.com/query");
288 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
289 GURL fail_url("http://example.com/fail");
290 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
291 AddFilter(DownloadQuery::FILTER_URL_REGEX, "query");
292 ExpectStandardFilterResults();
295 TEST_F(DownloadQueryTest, DownloadQueryTest_SortUrl) {
296 CreateMocks(2);
297 GURL b_url("http://example.com/b");
298 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(b_url));
299 GURL a_url("http://example.com/a");
300 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(a_url));
301 query()->AddSorter(DownloadQuery::SORT_URL, DownloadQuery::ASCENDING);
302 ExpectSortInverted();
305 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrl) {
306 CreateMocks(2);
307 GURL match_url("http://query.com/query");
308 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
309 GURL fail_url("http://example.com/fail");
310 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
311 AddFilter(DownloadQuery::FILTER_URL, match_url.spec().c_str());
312 ExpectStandardFilterResults();
315 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterCallback) {
316 CreateMocks(2);
317 CHECK(query()->AddFilter(base::Bind(&IdNotEqual, 1)));
318 ExpectStandardFilterResults();
321 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterBytesReceived) {
322 CreateMocks(2);
323 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
324 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1));
325 AddFilter(DownloadQuery::FILTER_BYTES_RECEIVED, 0);
326 ExpectStandardFilterResults();
329 TEST_F(DownloadQueryTest, DownloadQueryTest_SortBytesReceived) {
330 CreateMocks(2);
331 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
332 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1));
333 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
334 DownloadQuery::DESCENDING);
335 ExpectSortInverted();
338 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDangerAccepted) {
339 CreateMocks(2);
340 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
341 content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
342 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
343 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
344 AddFilter(DownloadQuery::FILTER_DANGER_ACCEPTED, true);
345 ExpectStandardFilterResults();
348 TEST_F(DownloadQueryTest, DownloadQueryTest_SortDangerAccepted) {
349 CreateMocks(2);
350 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
351 content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
352 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
353 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
354 query()->AddSorter(DownloadQuery::SORT_DANGER_ACCEPTED,
355 DownloadQuery::ASCENDING);
356 ExpectSortInverted();
359 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterExists) {
360 CreateMocks(2);
361 EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return(
362 false));
363 EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return(
364 true));
365 AddFilter(DownloadQuery::FILTER_EXISTS, true);
366 ExpectStandardFilterResults();
369 TEST_F(DownloadQueryTest, DownloadQueryTest_SortExists) {
370 CreateMocks(2);
371 EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return(
372 false));
373 EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return(
374 true));
375 query()->AddSorter(DownloadQuery::SORT_EXISTS,
376 DownloadQuery::ASCENDING);
377 ExpectSortInverted();
380 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterMime) {
381 CreateMocks(2);
382 EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("text"));
383 EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("image"));
384 AddFilter(DownloadQuery::FILTER_MIME, "text");
385 ExpectStandardFilterResults();
388 TEST_F(DownloadQueryTest, DownloadQueryTest_SortMime) {
389 CreateMocks(2);
390 EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("b"));
391 EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("a"));
392 query()->AddSorter(DownloadQuery::SORT_MIME, DownloadQuery::ASCENDING);
393 ExpectSortInverted();
396 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterPaused) {
397 CreateMocks(2);
398 EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true));
399 EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false));
400 AddFilter(DownloadQuery::FILTER_PAUSED, true);
401 ExpectStandardFilterResults();
404 TEST_F(DownloadQueryTest, DownloadQueryTest_SortPaused) {
405 CreateMocks(2);
406 EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true));
407 EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false));
408 query()->AddSorter(DownloadQuery::SORT_PAUSED, DownloadQuery::ASCENDING);
409 ExpectSortInverted();
412 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedAfter) {
413 CreateMocks(2);
414 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
415 base::Time::FromTimeT(kSomeKnownTime + 2)));
416 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
417 base::Time::FromTimeT(kSomeKnownTime + 1)));
418 AddFilter(DownloadQuery::FILTER_STARTED_AFTER,
419 std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix));
420 ExpectStandardFilterResults();
423 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedBefore) {
424 CreateMocks(2);
425 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
426 base::Time::FromTimeT(kSomeKnownTime + 2)));
427 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
428 base::Time::FromTimeT(kSomeKnownTime + 4)));
429 AddFilter(DownloadQuery::FILTER_STARTED_BEFORE,
430 std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix));
431 ExpectStandardFilterResults();
434 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartTime) {
435 CreateMocks(2);
436 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
437 base::Time::FromTimeT(kSomeKnownTime + 2)));
438 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
439 base::Time::FromTimeT(kSomeKnownTime + 4)));
440 AddFilter(DownloadQuery::FILTER_START_TIME,
441 std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix));
442 ExpectStandardFilterResults();
445 TEST_F(DownloadQueryTest, DownloadQueryTest_SortStartTime) {
446 CreateMocks(2);
447 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
448 base::Time::FromTimeT(kSomeKnownTime + 2)));
449 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
450 base::Time::FromTimeT(kSomeKnownTime + 4)));
451 query()->AddSorter(DownloadQuery::SORT_START_TIME, DownloadQuery::DESCENDING);
452 ExpectSortInverted();
455 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedAfter) {
456 CreateMocks(2);
457 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
458 base::Time::FromTimeT(kSomeKnownTime + 2)));
459 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
460 base::Time::FromTimeT(kSomeKnownTime + 1)));
461 AddFilter(DownloadQuery::FILTER_ENDED_AFTER,
462 std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix));
463 ExpectStandardFilterResults();
466 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedBefore) {
467 CreateMocks(2);
468 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
469 base::Time::FromTimeT(kSomeKnownTime + 2)));
470 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
471 base::Time::FromTimeT(kSomeKnownTime + 4)));
472 AddFilter(DownloadQuery::FILTER_ENDED_BEFORE,
473 std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix));
474 ExpectStandardFilterResults();
477 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndTime) {
478 CreateMocks(2);
479 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
480 base::Time::FromTimeT(kSomeKnownTime + 2)));
481 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
482 base::Time::FromTimeT(kSomeKnownTime + 4)));
483 AddFilter(DownloadQuery::FILTER_END_TIME,
484 std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix));
485 ExpectStandardFilterResults();
488 TEST_F(DownloadQueryTest, DownloadQueryTest_SortEndTime) {
489 CreateMocks(2);
490 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
491 base::Time::FromTimeT(kSomeKnownTime + 2)));
492 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
493 base::Time::FromTimeT(kSomeKnownTime + 4)));
494 query()->AddSorter(DownloadQuery::SORT_END_TIME, DownloadQuery::DESCENDING);
495 ExpectSortInverted();
498 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater) {
499 CreateMocks(2);
500 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
501 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(1));
502 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, 1);
503 ExpectStandardFilterResults();
506 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess) {
507 CreateMocks(2);
508 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
509 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
510 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, 4);
511 ExpectStandardFilterResults();
514 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes) {
515 CreateMocks(2);
516 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
517 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
518 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, 2);
519 ExpectStandardFilterResults();
522 TEST_F(DownloadQueryTest, DownloadQueryTest_SortTotalBytes) {
523 CreateMocks(2);
524 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
525 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
526 query()->AddSorter(DownloadQuery::SORT_TOTAL_BYTES,
527 DownloadQuery::DESCENDING);
528 ExpectSortInverted();
531 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterState) {
532 CreateMocks(2);
533 EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return(
534 DownloadItem::IN_PROGRESS));
535 EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return(
536 DownloadItem::CANCELLED));
537 query()->AddFilter(DownloadItem::IN_PROGRESS);
538 ExpectStandardFilterResults();
541 TEST_F(DownloadQueryTest, DownloadQueryTest_SortState) {
542 CreateMocks(2);
543 EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return(
544 DownloadItem::IN_PROGRESS));
545 EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return(
546 DownloadItem::CANCELLED));
547 query()->AddSorter(DownloadQuery::SORT_STATE, DownloadQuery::DESCENDING);
548 ExpectSortInverted();
551 TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDanger) {
552 CreateMocks(2);
553 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
554 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
555 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
556 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
557 query()->AddFilter(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
558 ExpectStandardFilterResults();
561 TEST_F(DownloadQueryTest, DownloadQueryTest_SortDanger) {
562 CreateMocks(2);
563 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
564 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
565 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
566 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
567 query()->AddSorter(DownloadQuery::SORT_DANGER, DownloadQuery::DESCENDING);
568 ExpectSortInverted();
571 TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById1) {
572 CreateMocks(2);
573 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
574 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0));
575 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
576 DownloadQuery::ASCENDING);
577 Search();
578 ASSERT_EQ(2U, results()->size());
579 EXPECT_EQ(0U, results()->at(0)->GetId());
580 EXPECT_EQ(1U, results()->at(1)->GetId());
583 TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById2) {
584 CreateMocks(2);
585 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
586 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0));
587 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
588 DownloadQuery::DESCENDING);
589 Search();
590 ASSERT_EQ(2U, results()->size());
591 EXPECT_EQ(0U, results()->at(0)->GetId());
592 EXPECT_EQ(1U, results()->at(1)->GetId());
595 TEST_F(DownloadQueryTest, DownloadQueryFilterPerformance) {
596 static const int kNumItems = 100;
597 static const int kNumFilters = 100;
598 CreateMocks(kNumItems);
599 for (size_t i = 0; i < (kNumFilters - 1); ++i) {
600 query()->AddFilter(base::Bind(&AlwaysReturn, true));
602 query()->AddFilter(base::Bind(&AlwaysReturn, false));
603 base::Time start = base::Time::Now();
604 Search();
605 base::Time end = base::Time::Now();
606 double nanos = (end - start).InMillisecondsF() * 1000.0 * 1000.0;
607 double nanos_per_item = nanos / static_cast<double>(kNumItems);
608 double nanos_per_item_per_filter = nanos_per_item
609 / static_cast<double>(kNumFilters);
610 std::cout << "Search took " << nanos_per_item_per_filter
611 << " nanoseconds per item per filter.\n";