blob: ddf41a76fd411f52f741885e759930bb2de8ac3b [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"
avie4d7b6f2015-12-26 00:59:1817#include "base/macros.h"
[email protected]340f55d7c2013-06-10 20:49:1118#include "base/strings/string16.h"
[email protected]41a17c52013-06-28 00:27:5319#include "base/time/time.h"
[email protected]be052212011-12-14 18:40:4020#include "base/values.h"
avie4d7b6f2015-12-26 00:59:1821#include "build/build_config.h"
Min Qinaddcd162018-03-28 23:25:4722#include "components/download/public/common/mock_download_item.h"
Min Qina9f487872018-02-09 20:43:2323#include "content/public/browser/download_item_utils.h"
[email protected]be052212011-12-14 18:40:4024#include "testing/gmock/include/gmock/gmock.h"
25#include "testing/gtest/include/gtest/gtest.h"
26
27using ::testing::Return;
28using ::testing::ReturnRef;
29using ::testing::_;
30using base::Time;
31using base::Value;
Min Qina9f487872018-02-09 20:43:2332using download::DownloadItem;
[email protected]be052212011-12-14 18:40:4033typedef DownloadQuery::DownloadVector DownloadVector;
34
35namespace {
36
[email protected]c5899b252013-01-13 00:42:1037static const int kSomeKnownTime = 1355864160;
38static const char kSomeKnownTime8601[] = "2012-12-18T20:56:0";
39static const char k8601Suffix[] = ".000Z";
40
lazyboya61f420b2016-06-27 23:48:1841static const int64_t kEightGB = 1LL << 33;
42static const int64_t kSixteenGB = 1LL << 34;
43static const double kEightGBDouble = 8.0 * (1LL << 30);
44static const double kNineGBDouble = 9.0 * (1LL << 30);
45
avid0181f32015-12-10 19:41:4746bool IdNotEqual(uint32_t not_id, const DownloadItem& item) {
[email protected]be052212011-12-14 18:40:4047 return item.GetId() != not_id;
48}
49
50bool AlwaysReturn(bool result, const DownloadItem& item) {
51 return result;
52}
53
54} // anonymous namespace
55
56class DownloadQueryTest : public testing::Test {
57 public:
58 DownloadQueryTest() {}
59
dchenge1bc7982014-10-30 00:32:4060 ~DownloadQueryTest() override {}
[email protected]be052212011-12-14 18:40:4061
aviaa346b0d2016-10-28 14:52:5162 void TearDown() override {}
[email protected]be052212011-12-14 18:40:4063
64 void CreateMocks(int count) {
65 for (int i = 0; i < count; ++i) {
Min Qinaddcd162018-03-28 23:25:4766 owned_mocks_.push_back(std::make_unique<download::MockDownloadItem>());
aviaa346b0d2016-10-28 14:52:5167 mocks_.push_back(owned_mocks_.back().get());
[email protected]c5899b252013-01-13 00:42:1068 EXPECT_CALL(mock(mocks_.size() - 1), GetId()).WillRepeatedly(Return(
69 mocks_.size() - 1));
Min Qina9f487872018-02-09 20:43:2370 content::DownloadItemUtils::AttachInfo(mocks_.back(), nullptr, nullptr);
[email protected]be052212011-12-14 18:40:4071 }
72 }
73
Min Qinaddcd162018-03-28 23:25:4774 download::MockDownloadItem& mock(int index) { return *mocks_[index]; }
[email protected]be052212011-12-14 18:40:4075
76 DownloadQuery* query() { return &query_; }
77
78 template<typename ValueType> void AddFilter(
79 DownloadQuery::FilterType name, ValueType value);
80
81 void Search() {
82 query_.Search(mocks_.begin(), mocks_.end(), &results_);
83 }
84
85 DownloadVector* results() { return &results_; }
86
[email protected]c5899b252013-01-13 00:42:1087 // Filter tests generally contain 2 items. mock(0) matches the filter, mock(1)
88 // does not.
89 void ExpectStandardFilterResults() {
90 Search();
91 ASSERT_EQ(1U, results()->size());
[email protected]530047e2013-07-12 17:02:2592 ASSERT_EQ(0U, results()->at(0)->GetId());
[email protected]c5899b252013-01-13 00:42:1093 }
94
95 // If no sorters distinguish between two items, then DownloadQuery sorts by ID
96 // ascending. In order to test that a sorter distinguishes between two items,
97 // the sorter must sort them by ID descending.
98 void ExpectSortInverted() {
99 Search();
100 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25101 ASSERT_EQ(1U, results()->at(0)->GetId());
102 ASSERT_EQ(0U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:10103 }
104
[email protected]be052212011-12-14 18:40:40105 private:
aviaa346b0d2016-10-28 14:52:51106 // These two vectors hold the MockDownloadItems. |mocks_| contains just the
107 // pointers, but is necessary because DownloadQuery processes vectors of
108 // unowned pointers. |owned_mocks_| holds the ownership of the mock objects.
Min Qinaddcd162018-03-28 23:25:47109 std::vector<download::MockDownloadItem*> mocks_;
110 std::vector<std::unique_ptr<download::MockDownloadItem>> owned_mocks_;
[email protected]be052212011-12-14 18:40:40111 DownloadQuery query_;
112 DownloadVector results_;
113
114 DISALLOW_COPY_AND_ASSIGN(DownloadQueryTest);
115};
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
140template<> void DownloadQueryTest::AddFilter(
[email protected]b6775d782013-12-25 20:04:53141 DownloadQuery::FilterType name, const base::char16* cpp_value) {
jdoerrie122c4da2017-03-06 11:12:04142 CHECK(query_.AddFilter(name, base::Value(cpp_value)));
[email protected]be052212011-12-14 18:40:40143}
144
[email protected]f1d784d62013-07-28 18:36:09145template<> void DownloadQueryTest::AddFilter(
[email protected]96920152013-12-04 21:00:16146 DownloadQuery::FilterType name, std::vector<base::string16> cpp_value) {
dchengabbf44652016-04-07 22:23:39147 std::unique_ptr<base::ListValue> list(new base::ListValue());
[email protected]96920152013-12-04 21:00:16148 for (std::vector<base::string16>::const_iterator it = cpp_value.begin();
[email protected]f1d784d62013-07-28 18:36:09149 it != cpp_value.end(); ++it) {
dchengd9ea63862016-06-03 02:27:18150 list->AppendString(*it);
[email protected]f1d784d62013-07-28 18:36:09151 }
152 CHECK(query_.AddFilter(name, *list.get()));
153}
154
155template<> void DownloadQueryTest::AddFilter(
156 DownloadQuery::FilterType name, std::vector<std::string> cpp_value) {
dchengabbf44652016-04-07 22:23:39157 std::unique_ptr<base::ListValue> list(new base::ListValue());
[email protected]f1d784d62013-07-28 18:36:09158 for (std::vector<std::string>::const_iterator it = cpp_value.begin();
159 it != cpp_value.end(); ++it) {
dchengd9ea63862016-06-03 02:27:18160 list->AppendString(*it);
[email protected]f1d784d62013-07-28 18:36:09161 }
162 CHECK(query_.AddFilter(name, *list.get()));
163}
164
[email protected]c5899b252013-01-13 00:42:10165TEST_F(DownloadQueryTest, DownloadQueryTest_ZeroItems) {
[email protected]be052212011-12-14 18:40:40166 Search();
167 EXPECT_EQ(0U, results()->size());
168}
169
[email protected]c5899b252013-01-13 00:42:10170TEST_F(DownloadQueryTest, DownloadQueryTest_InvalidFilter) {
jdoerrie239723572017-03-02 12:09:19171 std::unique_ptr<base::Value> value(new base::Value(0));
avid0181f32015-12-10 19:41:47172 EXPECT_FALSE(query()->AddFilter(static_cast<DownloadQuery::FilterType>(
173 std::numeric_limits<int32_t>::max()),
174 *value.get()));
[email protected]be052212011-12-14 18:40:40175}
176
[email protected]c5899b252013-01-13 00:42:10177TEST_F(DownloadQueryTest, DownloadQueryTest_EmptyQuery) {
178 CreateMocks(2);
179 Search();
180 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25181 ASSERT_EQ(0U, results()->at(0)->GetId());
182 ASSERT_EQ(1U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:10183}
184
185TEST_F(DownloadQueryTest, DownloadQueryTest_Limit) {
[email protected]be052212011-12-14 18:40:40186 CreateMocks(2);
187 query()->Limit(1);
[email protected]c5899b252013-01-13 00:42:10188 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40189}
190
[email protected]c5899b252013-01-13 00:42:10191TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilename) {
192 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45193 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10194 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
195 match_filename));
[email protected]650b2d52013-02-10 03:41:45196 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10197 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
198 fail_filename));
[email protected]9dccd8062012-09-17 17:19:12199 GURL fail_url("http://example.com/fail");
[email protected]c5899b252013-01-13 00:42:10200 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
201 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03202 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
203 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
204 std::vector<std::string> query_terms;
205 query_terms.push_back("query");
206 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
207 ExpectStandardFilterResults();
208}
209
210TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryOriginalUrl) {
211 CreateMocks(2);
mharanczyk235b8cbc2016-07-11 09:05:03212 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
213 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
214 fail_filename));
215 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
216 fail_filename));
217 GURL match_url("http://query.com/query");
218 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
219 GURL fail_url("http://example.com/fail");
220 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
221 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
222 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
223 std::vector<std::string> query_terms;
224 query_terms.push_back("query");
225 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
226 ExpectStandardFilterResults();
227}
228
229TEST_F(DownloadQueryTest,
230 DownloadQueryTest_FilterGenericQueryOriginalUrlUnescaping) {
231 CreateMocks(2);
mharanczyk235b8cbc2016-07-11 09:05:03232 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
233 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
234 fail_filename));
235 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
236 fail_filename));
237 GURL match_url("http://q%75%65%72y.c%6Fm/%71uer%79");
238 GURL fail_url("http://%65xampl%65.com/%66ai%6C");
239 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
240 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
241 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
242 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]f1d784d62013-07-28 18:36:09243 std::vector<std::string> query_terms;
244 query_terms.push_back("query");
245 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10246 ExpectStandardFilterResults();
247}
248
249TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryUrl) {
250 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45251 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10252 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
253 fail_filename));
254 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
255 fail_filename));
[email protected]9dccd8062012-09-17 17:19:12256 GURL match_url("http://query.com/query");
[email protected]be052212011-12-14 18:40:40257 GURL fail_url("http://example.com/fail");
mharanczyk235b8cbc2016-07-11 09:05:03258 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
[email protected]c5899b252013-01-13 00:42:10259 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03260 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
261 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
262 std::vector<std::string> query_terms;
263 query_terms.push_back("query");
264 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
265 ExpectStandardFilterResults();
266}
267
268TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryUrlUnescaping) {
269 CreateMocks(2);
mharanczyk235b8cbc2016-07-11 09:05:03270 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
271 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
272 fail_filename));
273 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
274 fail_filename));
275 GURL match_url("http://%71uer%79.com/qu%65ry");
276 GURL fail_url("http://e%78am%70le.com/f%61il");
277 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
278 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
279 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
280 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]f1d784d62013-07-28 18:36:09281 std::vector<std::string> query_terms;
282 query_terms.push_back("query");
283 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10284 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40285}
286
[email protected]c5899b252013-01-13 00:42:10287TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilenameI18N) {
288 CreateMocks(2);
Peter Kasting690f8c82021-02-13 18:09:54289 const std::string kTestString(
290 "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd");
291 base::FilePath match_filename = base::FilePath::FromUTF8Unsafe(kTestString);
[email protected]c5899b252013-01-13 00:42:10292 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
293 match_filename));
[email protected]650b2d52013-02-10 03:41:45294 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10295 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
296 fail_filename));
297 GURL fail_url("http://example.com/fail");
298 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
299 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03300 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
301 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
Peter Kasting690f8c82021-02-13 18:09:54302 std::vector<std::string> query_terms;
[email protected]f1d784d62013-07-28 18:36:09303 query_terms.push_back(kTestString);
304 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10305 ExpectStandardFilterResults();
306}
307
308TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilenameRegex) {
309 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45310 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10311 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
312 match_filename));
[email protected]650b2d52013-02-10 03:41:45313 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10314 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
315 fail_filename));
316 AddFilter(DownloadQuery::FILTER_FILENAME_REGEX, "y");
317 ExpectStandardFilterResults();
318}
319
320TEST_F(DownloadQueryTest, DownloadQueryTest_SortFilename) {
321 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45322 base::FilePath b_filename(FILE_PATH_LITERAL("b"));
[email protected]c5899b252013-01-13 00:42:10323 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
324 b_filename));
[email protected]650b2d52013-02-10 03:41:45325 base::FilePath a_filename(FILE_PATH_LITERAL("a"));
[email protected]c5899b252013-01-13 00:42:10326 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
327 a_filename));
328 query()->AddSorter(DownloadQuery::SORT_FILENAME, DownloadQuery::ASCENDING);
329 ExpectSortInverted();
330}
331
332TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilename) {
333 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45334 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10335 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
336 match_filename));
[email protected]650b2d52013-02-10 03:41:45337 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10338 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
339 fail_filename));
Peter Kasting690f8c82021-02-13 18:09:54340 AddFilter(DownloadQuery::FILTER_FILENAME,
341 match_filename.AsUTF8Unsafe().c_str());
[email protected]c5899b252013-01-13 00:42:10342 ExpectStandardFilterResults();
343}
344
mharanczyk235b8cbc2016-07-11 09:05:03345TEST_F(DownloadQueryTest, DownloadQueryTest_FilterOriginalUrlRegex) {
[email protected]c5899b252013-01-13 00:42:10346 CreateMocks(2);
347 GURL match_url("http://query.com/query");
348 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
349 GURL fail_url("http://example.com/fail");
350 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03351 AddFilter(DownloadQuery::FILTER_ORIGINAL_URL_REGEX, "query");
352 ExpectStandardFilterResults();
353}
354
355TEST_F(DownloadQueryTest, DownloadQueryTest_SortOriginalUrl) {
356 CreateMocks(2);
357 GURL b_url("http://example.com/b");
358 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(b_url));
359 GURL a_url("http://example.com/a");
360 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(a_url));
361 query()->AddSorter(
362 DownloadQuery::SORT_ORIGINAL_URL, DownloadQuery::ASCENDING);
363 ExpectSortInverted();
364}
365
366TEST_F(DownloadQueryTest, DownloadQueryTest_FilterOriginalUrl) {
367 CreateMocks(2);
368 GURL match_url("http://query.com/query");
369 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
370 GURL fail_url("http://example.com/fail");
371 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
372 AddFilter(DownloadQuery::FILTER_ORIGINAL_URL, match_url.spec().c_str());
373 ExpectStandardFilterResults();
374}
375
376TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrlRegex) {
377 CreateMocks(2);
378 GURL match_url("http://query.com/query");
379 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
380 GURL fail_url("http://example.com/fail");
381 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]c5899b252013-01-13 00:42:10382 AddFilter(DownloadQuery::FILTER_URL_REGEX, "query");
383 ExpectStandardFilterResults();
384}
385
386TEST_F(DownloadQueryTest, DownloadQueryTest_SortUrl) {
387 CreateMocks(2);
388 GURL b_url("http://example.com/b");
mharanczyk235b8cbc2016-07-11 09:05:03389 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(b_url));
[email protected]c5899b252013-01-13 00:42:10390 GURL a_url("http://example.com/a");
mharanczyk235b8cbc2016-07-11 09:05:03391 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(a_url));
[email protected]c5899b252013-01-13 00:42:10392 query()->AddSorter(DownloadQuery::SORT_URL, DownloadQuery::ASCENDING);
393 ExpectSortInverted();
394}
395
396TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrl) {
397 CreateMocks(2);
398 GURL match_url("http://query.com/query");
mharanczyk235b8cbc2016-07-11 09:05:03399 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
[email protected]c5899b252013-01-13 00:42:10400 GURL fail_url("http://example.com/fail");
mharanczyk235b8cbc2016-07-11 09:05:03401 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]c5899b252013-01-13 00:42:10402 AddFilter(DownloadQuery::FILTER_URL, match_url.spec().c_str());
403 ExpectStandardFilterResults();
404}
405
406TEST_F(DownloadQueryTest, DownloadQueryTest_FilterCallback) {
407 CreateMocks(2);
Evan Stadebe3a1582020-12-11 23:08:51408 CHECK(query()->AddFilter(base::BindRepeating(&IdNotEqual, 1)));
[email protected]c5899b252013-01-13 00:42:10409 ExpectStandardFilterResults();
410}
411
412TEST_F(DownloadQueryTest, DownloadQueryTest_FilterBytesReceived) {
[email protected]be052212011-12-14 18:40:40413 CreateMocks(2);
414 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
415 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1));
lazyboya61f420b2016-06-27 23:48:18416 AddFilter(DownloadQuery::FILTER_BYTES_RECEIVED, 0.0);
[email protected]c5899b252013-01-13 00:42:10417 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40418}
419
[email protected]c5899b252013-01-13 00:42:10420TEST_F(DownloadQueryTest, DownloadQueryTest_SortBytesReceived) {
[email protected]be052212011-12-14 18:40:40421 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10422 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
423 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1));
424 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
425 DownloadQuery::DESCENDING);
426 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40427}
428
[email protected]c5899b252013-01-13 00:42:10429TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDangerAccepted) {
[email protected]be052212011-12-14 18:40:40430 CreateMocks(2);
Min Qin0ca8e1ee2018-01-31 00:49:35431 EXPECT_CALL(mock(0), GetDangerType())
432 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
433 EXPECT_CALL(mock(1), GetDangerType())
434 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
[email protected]c5899b252013-01-13 00:42:10435 AddFilter(DownloadQuery::FILTER_DANGER_ACCEPTED, true);
436 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40437}
438
[email protected]c5899b252013-01-13 00:42:10439TEST_F(DownloadQueryTest, DownloadQueryTest_SortDangerAccepted) {
[email protected]be052212011-12-14 18:40:40440 CreateMocks(2);
Min Qin0ca8e1ee2018-01-31 00:49:35441 EXPECT_CALL(mock(0), GetDangerType())
442 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
443 EXPECT_CALL(mock(1), GetDangerType())
444 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
[email protected]c5899b252013-01-13 00:42:10445 query()->AddSorter(DownloadQuery::SORT_DANGER_ACCEPTED,
446 DownloadQuery::ASCENDING);
447 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40448}
449
[email protected]3291a4982013-01-14 00:58:52450TEST_F(DownloadQueryTest, DownloadQueryTest_FilterExists) {
451 CreateMocks(2);
452 EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return(
453 false));
454 EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return(
455 true));
456 AddFilter(DownloadQuery::FILTER_EXISTS, true);
457 ExpectStandardFilterResults();
458}
459
460TEST_F(DownloadQueryTest, DownloadQueryTest_SortExists) {
461 CreateMocks(2);
462 EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return(
463 false));
464 EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return(
465 true));
466 query()->AddSorter(DownloadQuery::SORT_EXISTS,
467 DownloadQuery::ASCENDING);
468 ExpectSortInverted();
469}
470
[email protected]c5899b252013-01-13 00:42:10471TEST_F(DownloadQueryTest, DownloadQueryTest_FilterMime) {
[email protected]be052212011-12-14 18:40:40472 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10473 EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("text"));
474 EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("image"));
475 AddFilter(DownloadQuery::FILTER_MIME, "text");
476 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40477}
478
[email protected]c5899b252013-01-13 00:42:10479TEST_F(DownloadQueryTest, DownloadQueryTest_SortMime) {
480 CreateMocks(2);
481 EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("b"));
482 EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("a"));
483 query()->AddSorter(DownloadQuery::SORT_MIME, DownloadQuery::ASCENDING);
484 ExpectSortInverted();
485}
486
487TEST_F(DownloadQueryTest, DownloadQueryTest_FilterPaused) {
[email protected]be052212011-12-14 18:40:40488 CreateMocks(2);
489 EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true));
490 EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false));
[email protected]c5899b252013-01-13 00:42:10491 AddFilter(DownloadQuery::FILTER_PAUSED, true);
492 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40493}
494
[email protected]c5899b252013-01-13 00:42:10495TEST_F(DownloadQueryTest, DownloadQueryTest_SortPaused) {
[email protected]d5e60e192013-01-04 00:09:50496 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10497 EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true));
498 EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false));
499 query()->AddSorter(DownloadQuery::SORT_PAUSED, DownloadQuery::ASCENDING);
500 ExpectSortInverted();
[email protected]d5e60e192013-01-04 00:09:50501}
502
[email protected]c5899b252013-01-13 00:42:10503TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedAfter) {
[email protected]be052212011-12-14 18:40:40504 CreateMocks(2);
505 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10506 base::Time::FromTimeT(kSomeKnownTime + 2)));
[email protected]be052212011-12-14 18:40:40507 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10508 base::Time::FromTimeT(kSomeKnownTime + 1)));
509 AddFilter(DownloadQuery::FILTER_STARTED_AFTER,
510 std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix));
511 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40512}
513
[email protected]c5899b252013-01-13 00:42:10514TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedBefore) {
515 CreateMocks(2);
516 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
517 base::Time::FromTimeT(kSomeKnownTime + 2)));
518 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
519 base::Time::FromTimeT(kSomeKnownTime + 4)));
520 AddFilter(DownloadQuery::FILTER_STARTED_BEFORE,
521 std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix));
522 ExpectStandardFilterResults();
523}
524
525TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartTime) {
526 CreateMocks(2);
527 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
528 base::Time::FromTimeT(kSomeKnownTime + 2)));
529 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
530 base::Time::FromTimeT(kSomeKnownTime + 4)));
531 AddFilter(DownloadQuery::FILTER_START_TIME,
532 std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix));
533 ExpectStandardFilterResults();
534}
535
536TEST_F(DownloadQueryTest, DownloadQueryTest_SortStartTime) {
537 CreateMocks(2);
538 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
539 base::Time::FromTimeT(kSomeKnownTime + 2)));
540 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
541 base::Time::FromTimeT(kSomeKnownTime + 4)));
542 query()->AddSorter(DownloadQuery::SORT_START_TIME, DownloadQuery::DESCENDING);
543 ExpectSortInverted();
544}
545
546TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedAfter) {
547 CreateMocks(2);
548 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
549 base::Time::FromTimeT(kSomeKnownTime + 2)));
550 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
551 base::Time::FromTimeT(kSomeKnownTime + 1)));
552 AddFilter(DownloadQuery::FILTER_ENDED_AFTER,
553 std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix));
554 ExpectStandardFilterResults();
555}
556
557TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedBefore) {
558 CreateMocks(2);
559 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
560 base::Time::FromTimeT(kSomeKnownTime + 2)));
561 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
562 base::Time::FromTimeT(kSomeKnownTime + 4)));
563 AddFilter(DownloadQuery::FILTER_ENDED_BEFORE,
564 std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix));
565 ExpectStandardFilterResults();
566}
567
568TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndTime) {
569 CreateMocks(2);
570 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
571 base::Time::FromTimeT(kSomeKnownTime + 2)));
572 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
573 base::Time::FromTimeT(kSomeKnownTime + 4)));
574 AddFilter(DownloadQuery::FILTER_END_TIME,
575 std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix));
576 ExpectStandardFilterResults();
577}
578
579TEST_F(DownloadQueryTest, DownloadQueryTest_SortEndTime) {
580 CreateMocks(2);
581 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
582 base::Time::FromTimeT(kSomeKnownTime + 2)));
583 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
584 base::Time::FromTimeT(kSomeKnownTime + 4)));
585 query()->AddSorter(DownloadQuery::SORT_END_TIME, DownloadQuery::DESCENDING);
586 ExpectSortInverted();
587}
588
lazyboya61f420b2016-06-27 23:48:18589TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater1) {
[email protected]c5899b252013-01-13 00:42:10590 CreateMocks(2);
591 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
592 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(1));
lazyboya61f420b2016-06-27 23:48:18593 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, 1.0);
[email protected]c5899b252013-01-13 00:42:10594 ExpectStandardFilterResults();
595}
596
lazyboya61f420b2016-06-27 23:48:18597TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater2) {
[email protected]c5899b252013-01-13 00:42:10598 CreateMocks(2);
599 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
lazyboya61f420b2016-06-27 23:48:18600 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(1));
601 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, 1.2);
[email protected]c5899b252013-01-13 00:42:10602 ExpectStandardFilterResults();
603}
604
lazyboya61f420b2016-06-27 23:48:18605TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater3) {
606 CreateMocks(2);
607 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
608 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
609 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, kNineGBDouble);
610 ExpectStandardFilterResults();
611}
612
613TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater4) {
614 CreateMocks(2);
615 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
616 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
617 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, kEightGBDouble + 1.0);
618 ExpectStandardFilterResults();
619}
620
621TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess1) {
[email protected]c5899b252013-01-13 00:42:10622 CreateMocks(2);
623 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
624 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
lazyboya61f420b2016-06-27 23:48:18625 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, 4.0);
626 ExpectStandardFilterResults();
627}
628
629TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess2) {
630 CreateMocks(2);
631 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(1));
632 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(2));
633 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, 1.2);
634 ExpectStandardFilterResults();
635}
636
637TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess3) {
638 CreateMocks(2);
639 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
640 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
641 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, kEightGBDouble + 1.0);
642 ExpectStandardFilterResults();
643}
644
645TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess4) {
646 CreateMocks(2);
647 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
648 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
649 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, kNineGBDouble);
650 ExpectStandardFilterResults();
651}
652
653TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes1) {
654 CreateMocks(2);
655 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
656 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
657 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, 2.0);
658 ExpectStandardFilterResults();
659}
660
661TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes2) {
662 CreateMocks(2);
663 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(1));
664 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(2));
665 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, 1.0);
666 ExpectStandardFilterResults();
667}
668
669TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes3) {
670 CreateMocks(2);
671 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
672 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
673 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, kEightGBDouble);
[email protected]c5899b252013-01-13 00:42:10674 ExpectStandardFilterResults();
675}
676
677TEST_F(DownloadQueryTest, DownloadQueryTest_SortTotalBytes) {
678 CreateMocks(2);
679 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
680 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
681 query()->AddSorter(DownloadQuery::SORT_TOTAL_BYTES,
682 DownloadQuery::DESCENDING);
683 ExpectSortInverted();
684}
685
686TEST_F(DownloadQueryTest, DownloadQueryTest_FilterState) {
[email protected]be052212011-12-14 18:40:40687 CreateMocks(2);
688 EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10689 DownloadItem::IN_PROGRESS));
[email protected]be052212011-12-14 18:40:40690 EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10691 DownloadItem::CANCELLED));
692 query()->AddFilter(DownloadItem::IN_PROGRESS);
693 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40694}
695
[email protected]c5899b252013-01-13 00:42:10696TEST_F(DownloadQueryTest, DownloadQueryTest_SortState) {
[email protected]be052212011-12-14 18:40:40697 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10698 EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return(
699 DownloadItem::IN_PROGRESS));
700 EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return(
701 DownloadItem::CANCELLED));
702 query()->AddSorter(DownloadQuery::SORT_STATE, DownloadQuery::DESCENDING);
703 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40704}
705
[email protected]c5899b252013-01-13 00:42:10706TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDanger) {
[email protected]be052212011-12-14 18:40:40707 CreateMocks(2);
Min Qin0ca8e1ee2018-01-31 00:49:35708 EXPECT_CALL(mock(0), GetDangerType())
709 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
710 EXPECT_CALL(mock(1), GetDangerType())
711 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
712 query()->AddFilter(download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
[email protected]c5899b252013-01-13 00:42:10713 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40714}
715
[email protected]c5899b252013-01-13 00:42:10716TEST_F(DownloadQueryTest, DownloadQueryTest_SortDanger) {
717 CreateMocks(2);
Min Qin0ca8e1ee2018-01-31 00:49:35718 EXPECT_CALL(mock(0), GetDangerType())
719 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
720 EXPECT_CALL(mock(1), GetDangerType())
721 .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
[email protected]c5899b252013-01-13 00:42:10722 query()->AddSorter(DownloadQuery::SORT_DANGER, DownloadQuery::DESCENDING);
723 ExpectSortInverted();
724}
725
726TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById1) {
[email protected]be052212011-12-14 18:40:40727 CreateMocks(2);
728 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
729 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0));
[email protected]c5899b252013-01-13 00:42:10730 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
731 DownloadQuery::ASCENDING);
[email protected]be052212011-12-14 18:40:40732 Search();
[email protected]c5899b252013-01-13 00:42:10733 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25734 EXPECT_EQ(0U, results()->at(0)->GetId());
735 EXPECT_EQ(1U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:10736}
737
738TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById2) {
739 CreateMocks(2);
740 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
741 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0));
742 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
743 DownloadQuery::DESCENDING);
744 Search();
745 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25746 EXPECT_EQ(0U, results()->at(0)->GetId());
747 EXPECT_EQ(1U, results()->at(1)->GetId());
[email protected]be052212011-12-14 18:40:40748}
749
750TEST_F(DownloadQueryTest, DownloadQueryFilterPerformance) {
[email protected]c5899b252013-01-13 00:42:10751 static const int kNumItems = 100;
752 static const int kNumFilters = 100;
[email protected]be052212011-12-14 18:40:40753 CreateMocks(kNumItems);
754 for (size_t i = 0; i < (kNumFilters - 1); ++i) {
Evan Stadebe3a1582020-12-11 23:08:51755 query()->AddFilter(base::BindRepeating(&AlwaysReturn, true));
[email protected]be052212011-12-14 18:40:40756 }
Evan Stadebe3a1582020-12-11 23:08:51757 query()->AddFilter(base::BindRepeating(&AlwaysReturn, false));
[email protected]be052212011-12-14 18:40:40758 base::Time start = base::Time::Now();
759 Search();
760 base::Time end = base::Time::Now();
761 double nanos = (end - start).InMillisecondsF() * 1000.0 * 1000.0;
762 double nanos_per_item = nanos / static_cast<double>(kNumItems);
763 double nanos_per_item_per_filter = nanos_per_item
764 / static_cast<double>(kNumFilters);
765 std::cout << "Search took " << nanos_per_item_per_filter
766 << " nanoseconds per item per filter.\n";
767}