blob: 0135229ea1fd9ff2001d5fda9aac8ff24629a724 [file] [log] [blame]
[email protected]33d22102012-01-25 17:46:531// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]be052212011-12-14 18:40:402// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dchengabbf44652016-04-07 22:23:395#include "chrome/browser/download/download_query.h"
6
avie4d7b6f2015-12-26 00:59:187#include <stddef.h>
avid0181f32015-12-10 19:41:478#include <stdint.h>
9
10#include <limits>
dchengabbf44652016-04-07 22:23:3911#include <memory>
[email protected]be052212011-12-14 18:40:4012#include <string>
13
14#include "base/bind.h"
Hans Wennborg1790e6b2020-04-24 19:10:3315#include "base/check.h"
[email protected]57999812013-02-24 05:40:5216#include "base/files/file_path.h"
[email protected]41a17c52013-06-28 00:27:5317#include "base/time/time.h"
[email protected]be052212011-12-14 18:40:4018#include "base/values.h"
avie4d7b6f2015-12-26 00:59:1819#include "build/build_config.h"
Min Qinaddcd162018-03-28 23:25:4720#include "components/download/public/common/mock_download_item.h"
Min Qina9f487872018-02-09 20:43:2321#include "content/public/browser/download_item_utils.h"
[email protected]be052212011-12-14 18:40:4022#include "testing/gmock/include/gmock/gmock.h"
23#include "testing/gtest/include/gtest/gtest.h"
24
25using ::testing::Return;
26using ::testing::ReturnRef;
27using ::testing::_;
28using base::Time;
29using base::Value;
Min Qina9f487872018-02-09 20:43:2330using download::DownloadItem;
[email protected]be052212011-12-14 18:40:4031typedef DownloadQuery::DownloadVector DownloadVector;
32
33namespace {
34
[email protected]c5899b252013-01-13 00:42:1035static const int kSomeKnownTime = 1355864160;
36static const char kSomeKnownTime8601[] = "2012-12-18T20:56:0";
37static const char k8601Suffix[] = ".000Z";
38
lazyboya61f420b2016-06-27 23:48:1839static const int64_t kEightGB = 1LL << 33;
40static const int64_t kSixteenGB = 1LL << 34;
41static const double kEightGBDouble = 8.0 * (1LL << 30);
42static const double kNineGBDouble = 9.0 * (1LL << 30);
43
avid0181f32015-12-10 19:41:4744bool IdNotEqual(uint32_t not_id, const DownloadItem& item) {
[email protected]be052212011-12-14 18:40:4045 return item.GetId() != not_id;
46}
47
48bool AlwaysReturn(bool result, const DownloadItem& item) {
49 return result;
50}
51
52} // anonymous namespace
53
54class DownloadQueryTest : public testing::Test {
55 public:
56 DownloadQueryTest() {}
57
Peter Boström53c6c5952021-09-17 09:41:2658 DownloadQueryTest(const DownloadQueryTest&) = delete;
59 DownloadQueryTest& operator=(const DownloadQueryTest&) = delete;
60
dchenge1bc7982014-10-30 00:32:4061 ~DownloadQueryTest() override {}
[email protected]be052212011-12-14 18:40:4062
aviaa346b0d2016-10-28 14:52:5163 void TearDown() override {}
[email protected]be052212011-12-14 18:40:4064
65 void CreateMocks(int count) {
66 for (int i = 0; i < count; ++i) {
Min Qinaddcd162018-03-28 23:25:4767 owned_mocks_.push_back(std::make_unique<download::MockDownloadItem>());
aviaa346b0d2016-10-28 14:52:5168 mocks_.push_back(owned_mocks_.back().get());
[email protected]c5899b252013-01-13 00:42:1069 EXPECT_CALL(mock(mocks_.size() - 1), GetId()).WillRepeatedly(Return(
70 mocks_.size() - 1));
Ian Vollick8f082322021-12-11 14:27:5671 content::DownloadItemUtils::AttachInfoForTesting(mocks_.back(), nullptr,
72 nullptr);
[email protected]be052212011-12-14 18:40:4073 }
74 }
75
Min Qinaddcd162018-03-28 23:25:4776 download::MockDownloadItem& mock(int index) { return *mocks_[index]; }
[email protected]be052212011-12-14 18:40:4077
78 DownloadQuery* query() { return &query_; }
79
80 template<typename ValueType> void AddFilter(
81 DownloadQuery::FilterType name, ValueType value);
82
83 void Search() {
84 query_.Search(mocks_.begin(), mocks_.end(), &results_);
85 }
86
87 DownloadVector* results() { return &results_; }
88
[email protected]c5899b252013-01-13 00:42:1089 // Filter tests generally contain 2 items. mock(0) matches the filter, mock(1)
90 // does not.
91 void ExpectStandardFilterResults() {
92 Search();
93 ASSERT_EQ(1U, results()->size());
[email protected]530047e2013-07-12 17:02:2594 ASSERT_EQ(0U, results()->at(0)->GetId());
[email protected]c5899b252013-01-13 00:42:1095 }
96
97 // If no sorters distinguish between two items, then DownloadQuery sorts by ID
98 // ascending. In order to test that a sorter distinguishes between two items,
99 // the sorter must sort them by ID descending.
100 void ExpectSortInverted() {
101 Search();
102 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25103 ASSERT_EQ(1U, results()->at(0)->GetId());
104 ASSERT_EQ(0U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:10105 }
106
[email protected]be052212011-12-14 18:40:40107 private:
aviaa346b0d2016-10-28 14:52:51108 // These two vectors hold the MockDownloadItems. |mocks_| contains just the
109 // pointers, but is necessary because DownloadQuery processes vectors of
110 // unowned pointers. |owned_mocks_| holds the ownership of the mock objects.
Min Qinaddcd162018-03-28 23:25:47111 std::vector<download::MockDownloadItem*> mocks_;
112 std::vector<std::unique_ptr<download::MockDownloadItem>> owned_mocks_;
[email protected]be052212011-12-14 18:40:40113 DownloadQuery query_;
114 DownloadVector results_;
[email protected]be052212011-12-14 18:40:40115};
116
117template<> void DownloadQueryTest::AddFilter(
118 DownloadQuery::FilterType name, bool cpp_value) {
jdoerrie239723572017-03-02 12:09:19119 std::unique_ptr<base::Value> value(new base::Value(cpp_value));
[email protected]be052212011-12-14 18:40:40120 CHECK(query_.AddFilter(name, *value.get()));
121}
122
lazyboya61f420b2016-06-27 23:48:18123template <>
124void DownloadQueryTest::AddFilter(DownloadQuery::FilterType name,
125 double cpp_value) {
jdoerrie239723572017-03-02 12:09:19126 std::unique_ptr<base::Value> value(new base::Value(cpp_value));
[email protected]be052212011-12-14 18:40:40127 CHECK(query_.AddFilter(name, *value.get()));
128}
129
130template<> void DownloadQueryTest::AddFilter(
131 DownloadQuery::FilterType name, const char* cpp_value) {
jdoerrie122c4da2017-03-06 11:12:04132 CHECK(query_.AddFilter(name, base::Value(cpp_value)));
[email protected]be052212011-12-14 18:40:40133}
134
135template<> void DownloadQueryTest::AddFilter(
[email protected]d5e60e192013-01-04 00:09:50136 DownloadQuery::FilterType name, std::string cpp_value) {
jdoerrie122c4da2017-03-06 11:12:04137 CHECK(query_.AddFilter(name, base::Value(cpp_value)));
[email protected]d5e60e192013-01-04 00:09:50138}
139
Jan Wilken Dörrie362098d2021-03-09 08:47:09140template <>
141void DownloadQueryTest::AddFilter(DownloadQuery::FilterType name,
142 const char16_t* cpp_value) {
jdoerrie122c4da2017-03-06 11:12:04143 CHECK(query_.AddFilter(name, base::Value(cpp_value)));
[email protected]be052212011-12-14 18:40:40144}
145
Jan Wilken Dörrief27844b2021-03-11 23:18:48146template <>
147void DownloadQueryTest::AddFilter(DownloadQuery::FilterType name,
148 std::vector<std::u16string> cpp_value) {
dchengabbf44652016-04-07 22:23:39149 std::unique_ptr<base::ListValue> list(new base::ListValue());
Jan Wilken Dörrief27844b2021-03-11 23:18:48150 for (std::vector<std::u16string>::const_iterator it = cpp_value.begin();
[email protected]f1d784d62013-07-28 18:36:09151 it != cpp_value.end(); ++it) {
Clark DuValle5ee6df2021-08-31 19:39:41152 list->Append(*it);
[email protected]f1d784d62013-07-28 18:36:09153 }
154 CHECK(query_.AddFilter(name, *list.get()));
155}
156
157template<> void DownloadQueryTest::AddFilter(
158 DownloadQuery::FilterType name, std::vector<std::string> cpp_value) {
dchengabbf44652016-04-07 22:23:39159 std::unique_ptr<base::ListValue> list(new base::ListValue());
[email protected]f1d784d62013-07-28 18:36:09160 for (std::vector<std::string>::const_iterator it = cpp_value.begin();
161 it != cpp_value.end(); ++it) {
Clark DuValle5ee6df2021-08-31 19:39:41162 list->Append(*it);
[email protected]f1d784d62013-07-28 18:36:09163 }
164 CHECK(query_.AddFilter(name, *list.get()));
165}
166
[email protected]c5899b252013-01-13 00:42:10167TEST_F(DownloadQueryTest, DownloadQueryTest_ZeroItems) {
[email protected]be052212011-12-14 18:40:40168 Search();
169 EXPECT_EQ(0U, results()->size());
170}
171
[email protected]c5899b252013-01-13 00:42:10172TEST_F(DownloadQueryTest, DownloadQueryTest_InvalidFilter) {
jdoerrie239723572017-03-02 12:09:19173 std::unique_ptr<base::Value> value(new base::Value(0));
avid0181f32015-12-10 19:41:47174 EXPECT_FALSE(query()->AddFilter(static_cast<DownloadQuery::FilterType>(
175 std::numeric_limits<int32_t>::max()),
176 *value.get()));
[email protected]be052212011-12-14 18:40:40177}
178
[email protected]c5899b252013-01-13 00:42:10179TEST_F(DownloadQueryTest, DownloadQueryTest_EmptyQuery) {
180 CreateMocks(2);
181 Search();
182 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25183 ASSERT_EQ(0U, results()->at(0)->GetId());
184 ASSERT_EQ(1U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:10185}
186
187TEST_F(DownloadQueryTest, DownloadQueryTest_Limit) {
[email protected]be052212011-12-14 18:40:40188 CreateMocks(2);
189 query()->Limit(1);
[email protected]c5899b252013-01-13 00:42:10190 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40191}
192
[email protected]c5899b252013-01-13 00:42:10193TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilename) {
194 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45195 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10196 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
197 match_filename));
[email protected]650b2d52013-02-10 03:41:45198 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10199 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
200 fail_filename));
[email protected]9dccd8062012-09-17 17:19:12201 GURL fail_url("http://example.com/fail");
[email protected]c5899b252013-01-13 00:42:10202 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
203 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03204 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
205 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
206 std::vector<std::string> query_terms;
207 query_terms.push_back("query");
208 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
209 ExpectStandardFilterResults();
210}
211
212TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryOriginalUrl) {
213 CreateMocks(2);
mharanczyk235b8cbc2016-07-11 09:05:03214 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
215 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
216 fail_filename));
217 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
218 fail_filename));
219 GURL match_url("http://query.com/query");
220 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
221 GURL fail_url("http://example.com/fail");
222 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
223 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
224 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
225 std::vector<std::string> query_terms;
226 query_terms.push_back("query");
227 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
228 ExpectStandardFilterResults();
229}
230
231TEST_F(DownloadQueryTest,
232 DownloadQueryTest_FilterGenericQueryOriginalUrlUnescaping) {
233 CreateMocks(2);
mharanczyk235b8cbc2016-07-11 09:05:03234 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
235 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
236 fail_filename));
237 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
238 fail_filename));
239 GURL match_url("http://q%75%65%72y.c%6Fm/%71uer%79");
240 GURL fail_url("http://%65xampl%65.com/%66ai%6C");
241 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
242 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
243 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
244 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]f1d784d62013-07-28 18:36:09245 std::vector<std::string> query_terms;
246 query_terms.push_back("query");
247 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10248 ExpectStandardFilterResults();
249}
250
251TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryUrl) {
252 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45253 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10254 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
255 fail_filename));
256 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
257 fail_filename));
[email protected]9dccd8062012-09-17 17:19:12258 GURL match_url("http://query.com/query");
[email protected]be052212011-12-14 18:40:40259 GURL fail_url("http://example.com/fail");
mharanczyk235b8cbc2016-07-11 09:05:03260 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
[email protected]c5899b252013-01-13 00:42:10261 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03262 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
263 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
264 std::vector<std::string> query_terms;
265 query_terms.push_back("query");
266 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
267 ExpectStandardFilterResults();
268}
269
270TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryUrlUnescaping) {
271 CreateMocks(2);
mharanczyk235b8cbc2016-07-11 09:05:03272 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
273 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
274 fail_filename));
275 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
276 fail_filename));
277 GURL match_url("http://%71uer%79.com/qu%65ry");
278 GURL fail_url("http://e%78am%70le.com/f%61il");
279 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
280 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
281 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
282 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]f1d784d62013-07-28 18:36:09283 std::vector<std::string> query_terms;
284 query_terms.push_back("query");
285 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10286 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40287}
288
[email protected]c5899b252013-01-13 00:42:10289TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilenameI18N) {
290 CreateMocks(2);
Peter Kasting690f8c82021-02-13 18:09:54291 const std::string kTestString(
292 "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd");
293 base::FilePath match_filename = base::FilePath::FromUTF8Unsafe(kTestString);
[email protected]c5899b252013-01-13 00:42:10294 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
295 match_filename));
[email protected]650b2d52013-02-10 03:41:45296 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10297 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
298 fail_filename));
299 GURL fail_url("http://example.com/fail");
300 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
301 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03302 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
303 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
Peter Kasting690f8c82021-02-13 18:09:54304 std::vector<std::string> query_terms;
[email protected]f1d784d62013-07-28 18:36:09305 query_terms.push_back(kTestString);
306 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10307 ExpectStandardFilterResults();
308}
309
310TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilenameRegex) {
311 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45312 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10313 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
314 match_filename));
[email protected]650b2d52013-02-10 03:41:45315 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10316 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
317 fail_filename));
318 AddFilter(DownloadQuery::FILTER_FILENAME_REGEX, "y");
319 ExpectStandardFilterResults();
320}
321
322TEST_F(DownloadQueryTest, DownloadQueryTest_SortFilename) {
323 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45324 base::FilePath b_filename(FILE_PATH_LITERAL("b"));
[email protected]c5899b252013-01-13 00:42:10325 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
326 b_filename));
[email protected]650b2d52013-02-10 03:41:45327 base::FilePath a_filename(FILE_PATH_LITERAL("a"));
[email protected]c5899b252013-01-13 00:42:10328 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
329 a_filename));
330 query()->AddSorter(DownloadQuery::SORT_FILENAME, DownloadQuery::ASCENDING);
331 ExpectSortInverted();
332}
333
334TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilename) {
335 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45336 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10337 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
338 match_filename));
[email protected]650b2d52013-02-10 03:41:45339 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10340 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
341 fail_filename));
Peter Kasting690f8c82021-02-13 18:09:54342 AddFilter(DownloadQuery::FILTER_FILENAME,
343 match_filename.AsUTF8Unsafe().c_str());
[email protected]c5899b252013-01-13 00:42:10344 ExpectStandardFilterResults();
345}
346
mharanczyk235b8cbc2016-07-11 09:05:03347TEST_F(DownloadQueryTest, DownloadQueryTest_FilterOriginalUrlRegex) {
[email protected]c5899b252013-01-13 00:42:10348 CreateMocks(2);
349 GURL match_url("http://query.com/query");
350 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
351 GURL fail_url("http://example.com/fail");
352 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03353 AddFilter(DownloadQuery::FILTER_ORIGINAL_URL_REGEX, "query");
354 ExpectStandardFilterResults();
355}
356
357TEST_F(DownloadQueryTest, DownloadQueryTest_SortOriginalUrl) {
358 CreateMocks(2);
359 GURL b_url("http://example.com/b");
360 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(b_url));
361 GURL a_url("http://example.com/a");
362 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(a_url));
363 query()->AddSorter(
364 DownloadQuery::SORT_ORIGINAL_URL, DownloadQuery::ASCENDING);
365 ExpectSortInverted();
366}
367
368TEST_F(DownloadQueryTest, DownloadQueryTest_FilterOriginalUrl) {
369 CreateMocks(2);
370 GURL match_url("http://query.com/query");
371 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
372 GURL fail_url("http://example.com/fail");
373 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
374 AddFilter(DownloadQuery::FILTER_ORIGINAL_URL, match_url.spec().c_str());
375 ExpectStandardFilterResults();
376}
377
378TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrlRegex) {
379 CreateMocks(2);
380 GURL match_url("http://query.com/query");
381 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
382 GURL fail_url("http://example.com/fail");
383 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]c5899b252013-01-13 00:42:10384 AddFilter(DownloadQuery::FILTER_URL_REGEX, "query");
385 ExpectStandardFilterResults();
386}
387
388TEST_F(DownloadQueryTest, DownloadQueryTest_SortUrl) {
389 CreateMocks(2);
390 GURL b_url("http://example.com/b");
mharanczyk235b8cbc2016-07-11 09:05:03391 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(b_url));
[email protected]c5899b252013-01-13 00:42:10392 GURL a_url("http://example.com/a");
mharanczyk235b8cbc2016-07-11 09:05:03393 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(a_url));
[email protected]c5899b252013-01-13 00:42:10394 query()->AddSorter(DownloadQuery::SORT_URL, DownloadQuery::ASCENDING);
395 ExpectSortInverted();
396}
397
398TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrl) {
399 CreateMocks(2);
400 GURL match_url("http://query.com/query");
mharanczyk235b8cbc2016-07-11 09:05:03401 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
[email protected]c5899b252013-01-13 00:42:10402 GURL fail_url("http://example.com/fail");
mharanczyk235b8cbc2016-07-11 09:05:03403 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]c5899b252013-01-13 00:42:10404 AddFilter(DownloadQuery::FILTER_URL, match_url.spec().c_str());
405 ExpectStandardFilterResults();
406}
407
408TEST_F(DownloadQueryTest, DownloadQueryTest_FilterCallback) {
409 CreateMocks(2);
Evan Stadebe3a1582020-12-11 23:08:51410 CHECK(query()->AddFilter(base::BindRepeating(&IdNotEqual, 1)));
[email protected]c5899b252013-01-13 00:42:10411 ExpectStandardFilterResults();
412}
413
414TEST_F(DownloadQueryTest, DownloadQueryTest_FilterBytesReceived) {
[email protected]be052212011-12-14 18:40:40415 CreateMocks(2);
416 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
417 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1));
lazyboya61f420b2016-06-27 23:48:18418 AddFilter(DownloadQuery::FILTER_BYTES_RECEIVED, 0.0);
[email protected]c5899b252013-01-13 00:42:10419 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40420}
421
[email protected]c5899b252013-01-13 00:42:10422TEST_F(DownloadQueryTest, DownloadQueryTest_SortBytesReceived) {
[email protected]be052212011-12-14 18:40:40423 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10424 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
425 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1));
426 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
427 DownloadQuery::DESCENDING);
428 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40429}
430
[email protected]c5899b252013-01-13 00:42:10431TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDangerAccepted) {
[email protected]be052212011-12-14 18:40:40432 CreateMocks(2);
Min Qin0ca8e1ee2018-01-31 00:49:35433 EXPECT_CALL(mock(0), GetDangerType())
434 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
435 EXPECT_CALL(mock(1), GetDangerType())
436 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
[email protected]c5899b252013-01-13 00:42:10437 AddFilter(DownloadQuery::FILTER_DANGER_ACCEPTED, true);
438 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40439}
440
[email protected]c5899b252013-01-13 00:42:10441TEST_F(DownloadQueryTest, DownloadQueryTest_SortDangerAccepted) {
[email protected]be052212011-12-14 18:40:40442 CreateMocks(2);
Min Qin0ca8e1ee2018-01-31 00:49:35443 EXPECT_CALL(mock(0), GetDangerType())
444 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
445 EXPECT_CALL(mock(1), GetDangerType())
446 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
[email protected]c5899b252013-01-13 00:42:10447 query()->AddSorter(DownloadQuery::SORT_DANGER_ACCEPTED,
448 DownloadQuery::ASCENDING);
449 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40450}
451
[email protected]3291a4982013-01-14 00:58:52452TEST_F(DownloadQueryTest, DownloadQueryTest_FilterExists) {
453 CreateMocks(2);
454 EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return(
455 false));
456 EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return(
457 true));
458 AddFilter(DownloadQuery::FILTER_EXISTS, true);
459 ExpectStandardFilterResults();
460}
461
462TEST_F(DownloadQueryTest, DownloadQueryTest_SortExists) {
463 CreateMocks(2);
464 EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return(
465 false));
466 EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return(
467 true));
468 query()->AddSorter(DownloadQuery::SORT_EXISTS,
469 DownloadQuery::ASCENDING);
470 ExpectSortInverted();
471}
472
[email protected]c5899b252013-01-13 00:42:10473TEST_F(DownloadQueryTest, DownloadQueryTest_FilterMime) {
[email protected]be052212011-12-14 18:40:40474 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10475 EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("text"));
476 EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("image"));
477 AddFilter(DownloadQuery::FILTER_MIME, "text");
478 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40479}
480
[email protected]c5899b252013-01-13 00:42:10481TEST_F(DownloadQueryTest, DownloadQueryTest_SortMime) {
482 CreateMocks(2);
483 EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("b"));
484 EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("a"));
485 query()->AddSorter(DownloadQuery::SORT_MIME, DownloadQuery::ASCENDING);
486 ExpectSortInverted();
487}
488
489TEST_F(DownloadQueryTest, DownloadQueryTest_FilterPaused) {
[email protected]be052212011-12-14 18:40:40490 CreateMocks(2);
491 EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true));
492 EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false));
[email protected]c5899b252013-01-13 00:42:10493 AddFilter(DownloadQuery::FILTER_PAUSED, true);
494 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40495}
496
[email protected]c5899b252013-01-13 00:42:10497TEST_F(DownloadQueryTest, DownloadQueryTest_SortPaused) {
[email protected]d5e60e192013-01-04 00:09:50498 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10499 EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true));
500 EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false));
501 query()->AddSorter(DownloadQuery::SORT_PAUSED, DownloadQuery::ASCENDING);
502 ExpectSortInverted();
[email protected]d5e60e192013-01-04 00:09:50503}
504
[email protected]c5899b252013-01-13 00:42:10505TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedAfter) {
[email protected]be052212011-12-14 18:40:40506 CreateMocks(2);
507 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10508 base::Time::FromTimeT(kSomeKnownTime + 2)));
[email protected]be052212011-12-14 18:40:40509 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10510 base::Time::FromTimeT(kSomeKnownTime + 1)));
511 AddFilter(DownloadQuery::FILTER_STARTED_AFTER,
512 std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix));
513 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40514}
515
[email protected]c5899b252013-01-13 00:42:10516TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedBefore) {
517 CreateMocks(2);
518 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
519 base::Time::FromTimeT(kSomeKnownTime + 2)));
520 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
521 base::Time::FromTimeT(kSomeKnownTime + 4)));
522 AddFilter(DownloadQuery::FILTER_STARTED_BEFORE,
523 std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix));
524 ExpectStandardFilterResults();
525}
526
527TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartTime) {
528 CreateMocks(2);
529 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
530 base::Time::FromTimeT(kSomeKnownTime + 2)));
531 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
532 base::Time::FromTimeT(kSomeKnownTime + 4)));
533 AddFilter(DownloadQuery::FILTER_START_TIME,
534 std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix));
535 ExpectStandardFilterResults();
536}
537
538TEST_F(DownloadQueryTest, DownloadQueryTest_SortStartTime) {
539 CreateMocks(2);
540 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
541 base::Time::FromTimeT(kSomeKnownTime + 2)));
542 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
543 base::Time::FromTimeT(kSomeKnownTime + 4)));
544 query()->AddSorter(DownloadQuery::SORT_START_TIME, DownloadQuery::DESCENDING);
545 ExpectSortInverted();
546}
547
548TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedAfter) {
549 CreateMocks(2);
550 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
551 base::Time::FromTimeT(kSomeKnownTime + 2)));
552 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
553 base::Time::FromTimeT(kSomeKnownTime + 1)));
554 AddFilter(DownloadQuery::FILTER_ENDED_AFTER,
555 std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix));
556 ExpectStandardFilterResults();
557}
558
559TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedBefore) {
560 CreateMocks(2);
561 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
562 base::Time::FromTimeT(kSomeKnownTime + 2)));
563 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
564 base::Time::FromTimeT(kSomeKnownTime + 4)));
565 AddFilter(DownloadQuery::FILTER_ENDED_BEFORE,
566 std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix));
567 ExpectStandardFilterResults();
568}
569
570TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndTime) {
571 CreateMocks(2);
572 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
573 base::Time::FromTimeT(kSomeKnownTime + 2)));
574 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
575 base::Time::FromTimeT(kSomeKnownTime + 4)));
576 AddFilter(DownloadQuery::FILTER_END_TIME,
577 std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix));
578 ExpectStandardFilterResults();
579}
580
581TEST_F(DownloadQueryTest, DownloadQueryTest_SortEndTime) {
582 CreateMocks(2);
583 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
584 base::Time::FromTimeT(kSomeKnownTime + 2)));
585 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
586 base::Time::FromTimeT(kSomeKnownTime + 4)));
587 query()->AddSorter(DownloadQuery::SORT_END_TIME, DownloadQuery::DESCENDING);
588 ExpectSortInverted();
589}
590
lazyboya61f420b2016-06-27 23:48:18591TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater1) {
[email protected]c5899b252013-01-13 00:42:10592 CreateMocks(2);
593 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
594 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(1));
lazyboya61f420b2016-06-27 23:48:18595 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, 1.0);
[email protected]c5899b252013-01-13 00:42:10596 ExpectStandardFilterResults();
597}
598
lazyboya61f420b2016-06-27 23:48:18599TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater2) {
[email protected]c5899b252013-01-13 00:42:10600 CreateMocks(2);
601 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
lazyboya61f420b2016-06-27 23:48:18602 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(1));
603 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, 1.2);
[email protected]c5899b252013-01-13 00:42:10604 ExpectStandardFilterResults();
605}
606
lazyboya61f420b2016-06-27 23:48:18607TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater3) {
608 CreateMocks(2);
609 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
610 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
611 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, kNineGBDouble);
612 ExpectStandardFilterResults();
613}
614
615TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater4) {
616 CreateMocks(2);
617 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
618 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
619 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, kEightGBDouble + 1.0);
620 ExpectStandardFilterResults();
621}
622
623TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess1) {
[email protected]c5899b252013-01-13 00:42:10624 CreateMocks(2);
625 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
626 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
lazyboya61f420b2016-06-27 23:48:18627 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, 4.0);
628 ExpectStandardFilterResults();
629}
630
631TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess2) {
632 CreateMocks(2);
633 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(1));
634 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(2));
635 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, 1.2);
636 ExpectStandardFilterResults();
637}
638
639TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess3) {
640 CreateMocks(2);
641 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
642 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
643 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, kEightGBDouble + 1.0);
644 ExpectStandardFilterResults();
645}
646
647TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess4) {
648 CreateMocks(2);
649 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
650 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
651 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, kNineGBDouble);
652 ExpectStandardFilterResults();
653}
654
655TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes1) {
656 CreateMocks(2);
657 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
658 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
659 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, 2.0);
660 ExpectStandardFilterResults();
661}
662
663TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes2) {
664 CreateMocks(2);
665 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(1));
666 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(2));
667 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, 1.0);
668 ExpectStandardFilterResults();
669}
670
671TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes3) {
672 CreateMocks(2);
673 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
674 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
675 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, kEightGBDouble);
[email protected]c5899b252013-01-13 00:42:10676 ExpectStandardFilterResults();
677}
678
679TEST_F(DownloadQueryTest, DownloadQueryTest_SortTotalBytes) {
680 CreateMocks(2);
681 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
682 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
683 query()->AddSorter(DownloadQuery::SORT_TOTAL_BYTES,
684 DownloadQuery::DESCENDING);
685 ExpectSortInverted();
686}
687
688TEST_F(DownloadQueryTest, DownloadQueryTest_FilterState) {
[email protected]be052212011-12-14 18:40:40689 CreateMocks(2);
690 EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10691 DownloadItem::IN_PROGRESS));
[email protected]be052212011-12-14 18:40:40692 EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10693 DownloadItem::CANCELLED));
694 query()->AddFilter(DownloadItem::IN_PROGRESS);
695 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40696}
697
[email protected]c5899b252013-01-13 00:42:10698TEST_F(DownloadQueryTest, DownloadQueryTest_SortState) {
[email protected]be052212011-12-14 18:40:40699 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10700 EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return(
701 DownloadItem::IN_PROGRESS));
702 EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return(
703 DownloadItem::CANCELLED));
704 query()->AddSorter(DownloadQuery::SORT_STATE, DownloadQuery::DESCENDING);
705 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40706}
707
[email protected]c5899b252013-01-13 00:42:10708TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDanger) {
[email protected]be052212011-12-14 18:40:40709 CreateMocks(2);
Min Qin0ca8e1ee2018-01-31 00:49:35710 EXPECT_CALL(mock(0), GetDangerType())
711 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
712 EXPECT_CALL(mock(1), GetDangerType())
713 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
714 query()->AddFilter(download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
[email protected]c5899b252013-01-13 00:42:10715 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40716}
717
[email protected]c5899b252013-01-13 00:42:10718TEST_F(DownloadQueryTest, DownloadQueryTest_SortDanger) {
719 CreateMocks(2);
Min Qin0ca8e1ee2018-01-31 00:49:35720 EXPECT_CALL(mock(0), GetDangerType())
721 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
722 EXPECT_CALL(mock(1), GetDangerType())
723 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
[email protected]c5899b252013-01-13 00:42:10724 query()->AddSorter(DownloadQuery::SORT_DANGER, DownloadQuery::DESCENDING);
725 ExpectSortInverted();
726}
727
728TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById1) {
[email protected]be052212011-12-14 18:40:40729 CreateMocks(2);
730 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
731 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0));
[email protected]c5899b252013-01-13 00:42:10732 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
733 DownloadQuery::ASCENDING);
[email protected]be052212011-12-14 18:40:40734 Search();
[email protected]c5899b252013-01-13 00:42:10735 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25736 EXPECT_EQ(0U, results()->at(0)->GetId());
737 EXPECT_EQ(1U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:10738}
739
740TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById2) {
741 CreateMocks(2);
742 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
743 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0));
744 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
745 DownloadQuery::DESCENDING);
746 Search();
747 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25748 EXPECT_EQ(0U, results()->at(0)->GetId());
749 EXPECT_EQ(1U, results()->at(1)->GetId());
[email protected]be052212011-12-14 18:40:40750}
751
752TEST_F(DownloadQueryTest, DownloadQueryFilterPerformance) {
[email protected]c5899b252013-01-13 00:42:10753 static const int kNumItems = 100;
754 static const int kNumFilters = 100;
[email protected]be052212011-12-14 18:40:40755 CreateMocks(kNumItems);
756 for (size_t i = 0; i < (kNumFilters - 1); ++i) {
Evan Stadebe3a1582020-12-11 23:08:51757 query()->AddFilter(base::BindRepeating(&AlwaysReturn, true));
[email protected]be052212011-12-14 18:40:40758 }
Evan Stadebe3a1582020-12-11 23:08:51759 query()->AddFilter(base::BindRepeating(&AlwaysReturn, false));
[email protected]be052212011-12-14 18:40:40760 base::Time start = base::Time::Now();
761 Search();
762 base::Time end = base::Time::Now();
763 double nanos = (end - start).InMillisecondsF() * 1000.0 * 1000.0;
764 double nanos_per_item = nanos / static_cast<double>(kNumItems);
765 double nanos_per_item_per_filter = nanos_per_item
766 / static_cast<double>(kNumFilters);
767 std::cout << "Search took " << nanos_per_item_per_filter
768 << " nanoseconds per item per filter.\n";
769}