blob: 84995e1dc0e5e6599097e5b248dd1c17a63ca064 [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"
[email protected]57999812013-02-24 05:40:5215#include "base/files/file_path.h"
[email protected]be052212011-12-14 18:40:4016#include "base/logging.h"
avie4d7b6f2015-12-26 00:59:1817#include "base/macros.h"
aviaa346b0d2016-10-28 14:52:5118#include "base/memory/ptr_util.h"
[email protected]340f55d7c2013-06-10 20:49:1119#include "base/strings/string16.h"
[email protected]41a17c52013-06-28 00:27:5320#include "base/time/time.h"
[email protected]be052212011-12-14 18:40:4021#include "base/values.h"
avie4d7b6f2015-12-26 00:59:1822#include "build/build_config.h"
[email protected]b7d000b2012-06-02 22:18:2123#include "content/public/test/mock_download_item.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;
[email protected]e582fdd2011-12-20 16:48:1732using content::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) {
aviaa346b0d2016-10-28 14:52:5166 owned_mocks_.push_back(base::MakeUnique<content::MockDownloadItem>());
67 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));
[email protected]be052212011-12-14 18:40:4070 }
71 }
72
[email protected]75e51b52012-02-04 16:57:5473 content::MockDownloadItem& mock(int index) { return *mocks_[index]; }
[email protected]be052212011-12-14 18:40:4074
75 DownloadQuery* query() { return &query_; }
76
77 template<typename ValueType> void AddFilter(
78 DownloadQuery::FilterType name, ValueType value);
79
80 void Search() {
81 query_.Search(mocks_.begin(), mocks_.end(), &results_);
82 }
83
84 DownloadVector* results() { return &results_; }
85
[email protected]c5899b252013-01-13 00:42:1086 // Filter tests generally contain 2 items. mock(0) matches the filter, mock(1)
87 // does not.
88 void ExpectStandardFilterResults() {
89 Search();
90 ASSERT_EQ(1U, results()->size());
[email protected]530047e2013-07-12 17:02:2591 ASSERT_EQ(0U, results()->at(0)->GetId());
[email protected]c5899b252013-01-13 00:42:1092 }
93
94 // If no sorters distinguish between two items, then DownloadQuery sorts by ID
95 // ascending. In order to test that a sorter distinguishes between two items,
96 // the sorter must sort them by ID descending.
97 void ExpectSortInverted() {
98 Search();
99 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25100 ASSERT_EQ(1U, results()->at(0)->GetId());
101 ASSERT_EQ(0U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:10102 }
103
[email protected]be052212011-12-14 18:40:40104 private:
aviaa346b0d2016-10-28 14:52:51105 // These two vectors hold the MockDownloadItems. |mocks_| contains just the
106 // pointers, but is necessary because DownloadQuery processes vectors of
107 // unowned pointers. |owned_mocks_| holds the ownership of the mock objects.
[email protected]75e51b52012-02-04 16:57:54108 std::vector<content::MockDownloadItem*> mocks_;
aviaa346b0d2016-10-28 14:52:51109 std::vector<std::unique_ptr<content::MockDownloadItem>> owned_mocks_;
[email protected]be052212011-12-14 18:40:40110 DownloadQuery query_;
111 DownloadVector results_;
112
113 DISALLOW_COPY_AND_ASSIGN(DownloadQueryTest);
114};
115
116template<> void DownloadQueryTest::AddFilter(
117 DownloadQuery::FilterType name, bool cpp_value) {
jdoerrie239723572017-03-02 12:09:19118 std::unique_ptr<base::Value> value(new base::Value(cpp_value));
[email protected]be052212011-12-14 18:40:40119 CHECK(query_.AddFilter(name, *value.get()));
120}
121
lazyboya61f420b2016-06-27 23:48:18122template <>
123void DownloadQueryTest::AddFilter(DownloadQuery::FilterType name,
124 double cpp_value) {
jdoerrie239723572017-03-02 12:09:19125 std::unique_ptr<base::Value> value(new base::Value(cpp_value));
[email protected]be052212011-12-14 18:40:40126 CHECK(query_.AddFilter(name, *value.get()));
127}
128
129template<> void DownloadQueryTest::AddFilter(
130 DownloadQuery::FilterType name, const char* cpp_value) {
[email protected]df287e12014-03-24 18:33:39131 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
[email protected]be052212011-12-14 18:40:40132}
133
134template<> void DownloadQueryTest::AddFilter(
[email protected]d5e60e192013-01-04 00:09:50135 DownloadQuery::FilterType name, std::string cpp_value) {
[email protected]df287e12014-03-24 18:33:39136 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
[email protected]d5e60e192013-01-04 00:09:50137}
138
139template<> void DownloadQueryTest::AddFilter(
[email protected]b6775d782013-12-25 20:04:53140 DownloadQuery::FilterType name, const base::char16* cpp_value) {
[email protected]df287e12014-03-24 18:33:39141 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
[email protected]be052212011-12-14 18:40:40142}
143
[email protected]f1d784d62013-07-28 18:36:09144template<> void DownloadQueryTest::AddFilter(
[email protected]96920152013-12-04 21:00:16145 DownloadQuery::FilterType name, std::vector<base::string16> cpp_value) {
dchengabbf44652016-04-07 22:23:39146 std::unique_ptr<base::ListValue> list(new base::ListValue());
[email protected]96920152013-12-04 21:00:16147 for (std::vector<base::string16>::const_iterator it = cpp_value.begin();
[email protected]f1d784d62013-07-28 18:36:09148 it != cpp_value.end(); ++it) {
dchengd9ea63862016-06-03 02:27:18149 list->AppendString(*it);
[email protected]f1d784d62013-07-28 18:36:09150 }
151 CHECK(query_.AddFilter(name, *list.get()));
152}
153
154template<> void DownloadQueryTest::AddFilter(
155 DownloadQuery::FilterType name, std::vector<std::string> cpp_value) {
dchengabbf44652016-04-07 22:23:39156 std::unique_ptr<base::ListValue> list(new base::ListValue());
[email protected]f1d784d62013-07-28 18:36:09157 for (std::vector<std::string>::const_iterator it = cpp_value.begin();
158 it != cpp_value.end(); ++it) {
dchengd9ea63862016-06-03 02:27:18159 list->AppendString(*it);
[email protected]f1d784d62013-07-28 18:36:09160 }
161 CHECK(query_.AddFilter(name, *list.get()));
162}
163
[email protected]c5899b252013-01-13 00:42:10164#if defined(OS_WIN)
165template<> void DownloadQueryTest::AddFilter(
166 DownloadQuery::FilterType name, std::wstring cpp_value) {
[email protected]df287e12014-03-24 18:33:39167 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
[email protected]c5899b252013-01-13 00:42:10168}
169#endif
170
171TEST_F(DownloadQueryTest, DownloadQueryTest_ZeroItems) {
[email protected]be052212011-12-14 18:40:40172 Search();
173 EXPECT_EQ(0U, results()->size());
174}
175
[email protected]c5899b252013-01-13 00:42:10176TEST_F(DownloadQueryTest, DownloadQueryTest_InvalidFilter) {
jdoerrie239723572017-03-02 12:09:19177 std::unique_ptr<base::Value> value(new base::Value(0));
avid0181f32015-12-10 19:41:47178 EXPECT_FALSE(query()->AddFilter(static_cast<DownloadQuery::FilterType>(
179 std::numeric_limits<int32_t>::max()),
180 *value.get()));
[email protected]be052212011-12-14 18:40:40181}
182
[email protected]c5899b252013-01-13 00:42:10183TEST_F(DownloadQueryTest, DownloadQueryTest_EmptyQuery) {
184 CreateMocks(2);
185 Search();
186 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25187 ASSERT_EQ(0U, results()->at(0)->GetId());
188 ASSERT_EQ(1U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:10189}
190
191TEST_F(DownloadQueryTest, DownloadQueryTest_Limit) {
[email protected]be052212011-12-14 18:40:40192 CreateMocks(2);
193 query()->Limit(1);
[email protected]c5899b252013-01-13 00:42:10194 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40195}
196
[email protected]c5899b252013-01-13 00:42:10197TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilename) {
198 CreateMocks(2);
199 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
200 static_cast<content::BrowserContext*>(NULL)));
201 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
202 static_cast<content::BrowserContext*>(NULL)));
[email protected]650b2d52013-02-10 03:41:45203 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10204 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
205 match_filename));
[email protected]650b2d52013-02-10 03:41:45206 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10207 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
208 fail_filename));
[email protected]9dccd8062012-09-17 17:19:12209 GURL fail_url("http://example.com/fail");
[email protected]c5899b252013-01-13 00:42:10210 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
211 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03212 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
213 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
214 std::vector<std::string> query_terms;
215 query_terms.push_back("query");
216 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
217 ExpectStandardFilterResults();
218}
219
220TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryOriginalUrl) {
221 CreateMocks(2);
222 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
223 static_cast<content::BrowserContext*>(NULL)));
224 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
225 static_cast<content::BrowserContext*>(NULL)));
226 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
227 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
228 fail_filename));
229 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
230 fail_filename));
231 GURL match_url("http://query.com/query");
232 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
233 GURL fail_url("http://example.com/fail");
234 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
235 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
236 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
237 std::vector<std::string> query_terms;
238 query_terms.push_back("query");
239 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
240 ExpectStandardFilterResults();
241}
242
243TEST_F(DownloadQueryTest,
244 DownloadQueryTest_FilterGenericQueryOriginalUrlUnescaping) {
245 CreateMocks(2);
246 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
247 static_cast<content::BrowserContext*>(NULL)));
248 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
249 static_cast<content::BrowserContext*>(NULL)));
250 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
251 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
252 fail_filename));
253 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
254 fail_filename));
255 GURL match_url("http://q%75%65%72y.c%6Fm/%71uer%79");
256 GURL fail_url("http://%65xampl%65.com/%66ai%6C");
257 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
258 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
259 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
260 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]f1d784d62013-07-28 18:36:09261 std::vector<std::string> query_terms;
262 query_terms.push_back("query");
263 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10264 ExpectStandardFilterResults();
265}
266
267TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryUrl) {
268 CreateMocks(2);
269 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
270 static_cast<content::BrowserContext*>(NULL)));
271 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
272 static_cast<content::BrowserContext*>(NULL)));
[email protected]650b2d52013-02-10 03:41:45273 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10274 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
275 fail_filename));
276 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
277 fail_filename));
[email protected]9dccd8062012-09-17 17:19:12278 GURL match_url("http://query.com/query");
[email protected]be052212011-12-14 18:40:40279 GURL fail_url("http://example.com/fail");
mharanczyk235b8cbc2016-07-11 09:05:03280 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
[email protected]c5899b252013-01-13 00:42:10281 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03282 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
283 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
284 std::vector<std::string> query_terms;
285 query_terms.push_back("query");
286 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
287 ExpectStandardFilterResults();
288}
289
290TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryUrlUnescaping) {
291 CreateMocks(2);
292 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
293 static_cast<content::BrowserContext*>(NULL)));
294 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
295 static_cast<content::BrowserContext*>(NULL)));
296 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
297 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
298 fail_filename));
299 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
300 fail_filename));
301 GURL match_url("http://%71uer%79.com/qu%65ry");
302 GURL fail_url("http://e%78am%70le.com/f%61il");
303 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
304 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
305 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
306 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]f1d784d62013-07-28 18:36:09307 std::vector<std::string> query_terms;
308 query_terms.push_back("query");
309 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10310 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40311}
312
[email protected]c5899b252013-01-13 00:42:10313TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilenameI18N) {
314 CreateMocks(2);
315 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
316 static_cast<content::BrowserContext*>(NULL)));
317 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
318 static_cast<content::BrowserContext*>(NULL)));
[email protected]650b2d52013-02-10 03:41:45319 const base::FilePath::StringType kTestString(
[email protected]c5899b252013-01-13 00:42:10320#if defined(OS_POSIX)
321 "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd"
322#elif defined(OS_WIN)
323 L"/\x4f60\x597d\x4f60\x597d"
324#endif
325 );
[email protected]650b2d52013-02-10 03:41:45326 base::FilePath match_filename(kTestString);
[email protected]c5899b252013-01-13 00:42:10327 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
328 match_filename));
[email protected]650b2d52013-02-10 03:41:45329 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10330 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
331 fail_filename));
332 GURL fail_url("http://example.com/fail");
333 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
334 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03335 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url));
336 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]f1d784d62013-07-28 18:36:09337 std::vector<base::FilePath::StringType> query_terms;
338 query_terms.push_back(kTestString);
339 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10340 ExpectStandardFilterResults();
341}
342
343TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilenameRegex) {
344 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45345 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10346 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
347 match_filename));
[email protected]650b2d52013-02-10 03:41:45348 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10349 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
350 fail_filename));
351 AddFilter(DownloadQuery::FILTER_FILENAME_REGEX, "y");
352 ExpectStandardFilterResults();
353}
354
355TEST_F(DownloadQueryTest, DownloadQueryTest_SortFilename) {
356 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45357 base::FilePath b_filename(FILE_PATH_LITERAL("b"));
[email protected]c5899b252013-01-13 00:42:10358 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
359 b_filename));
[email protected]650b2d52013-02-10 03:41:45360 base::FilePath a_filename(FILE_PATH_LITERAL("a"));
[email protected]c5899b252013-01-13 00:42:10361 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
362 a_filename));
363 query()->AddSorter(DownloadQuery::SORT_FILENAME, DownloadQuery::ASCENDING);
364 ExpectSortInverted();
365}
366
367TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilename) {
368 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45369 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10370 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
371 match_filename));
[email protected]650b2d52013-02-10 03:41:45372 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10373 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
374 fail_filename));
375 AddFilter(DownloadQuery::FILTER_FILENAME, match_filename.value().c_str());
376 ExpectStandardFilterResults();
377}
378
mharanczyk235b8cbc2016-07-11 09:05:03379TEST_F(DownloadQueryTest, DownloadQueryTest_FilterOriginalUrlRegex) {
[email protected]c5899b252013-01-13 00:42:10380 CreateMocks(2);
381 GURL match_url("http://query.com/query");
382 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
383 GURL fail_url("http://example.com/fail");
384 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
mharanczyk235b8cbc2016-07-11 09:05:03385 AddFilter(DownloadQuery::FILTER_ORIGINAL_URL_REGEX, "query");
386 ExpectStandardFilterResults();
387}
388
389TEST_F(DownloadQueryTest, DownloadQueryTest_SortOriginalUrl) {
390 CreateMocks(2);
391 GURL b_url("http://example.com/b");
392 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(b_url));
393 GURL a_url("http://example.com/a");
394 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(a_url));
395 query()->AddSorter(
396 DownloadQuery::SORT_ORIGINAL_URL, DownloadQuery::ASCENDING);
397 ExpectSortInverted();
398}
399
400TEST_F(DownloadQueryTest, DownloadQueryTest_FilterOriginalUrl) {
401 CreateMocks(2);
402 GURL match_url("http://query.com/query");
403 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
404 GURL fail_url("http://example.com/fail");
405 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
406 AddFilter(DownloadQuery::FILTER_ORIGINAL_URL, match_url.spec().c_str());
407 ExpectStandardFilterResults();
408}
409
410TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrlRegex) {
411 CreateMocks(2);
412 GURL match_url("http://query.com/query");
413 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
414 GURL fail_url("http://example.com/fail");
415 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]c5899b252013-01-13 00:42:10416 AddFilter(DownloadQuery::FILTER_URL_REGEX, "query");
417 ExpectStandardFilterResults();
418}
419
420TEST_F(DownloadQueryTest, DownloadQueryTest_SortUrl) {
421 CreateMocks(2);
422 GURL b_url("http://example.com/b");
mharanczyk235b8cbc2016-07-11 09:05:03423 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(b_url));
[email protected]c5899b252013-01-13 00:42:10424 GURL a_url("http://example.com/a");
mharanczyk235b8cbc2016-07-11 09:05:03425 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(a_url));
[email protected]c5899b252013-01-13 00:42:10426 query()->AddSorter(DownloadQuery::SORT_URL, DownloadQuery::ASCENDING);
427 ExpectSortInverted();
428}
429
430TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrl) {
431 CreateMocks(2);
432 GURL match_url("http://query.com/query");
mharanczyk235b8cbc2016-07-11 09:05:03433 EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url));
[email protected]c5899b252013-01-13 00:42:10434 GURL fail_url("http://example.com/fail");
mharanczyk235b8cbc2016-07-11 09:05:03435 EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url));
[email protected]c5899b252013-01-13 00:42:10436 AddFilter(DownloadQuery::FILTER_URL, match_url.spec().c_str());
437 ExpectStandardFilterResults();
438}
439
440TEST_F(DownloadQueryTest, DownloadQueryTest_FilterCallback) {
441 CreateMocks(2);
442 CHECK(query()->AddFilter(base::Bind(&IdNotEqual, 1)));
443 ExpectStandardFilterResults();
444}
445
446TEST_F(DownloadQueryTest, DownloadQueryTest_FilterBytesReceived) {
[email protected]be052212011-12-14 18:40:40447 CreateMocks(2);
448 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
449 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1));
lazyboya61f420b2016-06-27 23:48:18450 AddFilter(DownloadQuery::FILTER_BYTES_RECEIVED, 0.0);
[email protected]c5899b252013-01-13 00:42:10451 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40452}
453
[email protected]c5899b252013-01-13 00:42:10454TEST_F(DownloadQueryTest, DownloadQueryTest_SortBytesReceived) {
[email protected]be052212011-12-14 18:40:40455 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10456 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
457 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1));
458 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
459 DownloadQuery::DESCENDING);
460 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40461}
462
[email protected]c5899b252013-01-13 00:42:10463TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDangerAccepted) {
[email protected]be052212011-12-14 18:40:40464 CreateMocks(2);
[email protected]cda79062013-01-17 01:50:52465 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
466 content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
467 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
468 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
[email protected]c5899b252013-01-13 00:42:10469 AddFilter(DownloadQuery::FILTER_DANGER_ACCEPTED, true);
470 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40471}
472
[email protected]c5899b252013-01-13 00:42:10473TEST_F(DownloadQueryTest, DownloadQueryTest_SortDangerAccepted) {
[email protected]be052212011-12-14 18:40:40474 CreateMocks(2);
[email protected]cda79062013-01-17 01:50:52475 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
476 content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
477 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
478 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
[email protected]c5899b252013-01-13 00:42:10479 query()->AddSorter(DownloadQuery::SORT_DANGER_ACCEPTED,
480 DownloadQuery::ASCENDING);
481 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40482}
483
[email protected]3291a4982013-01-14 00:58:52484TEST_F(DownloadQueryTest, DownloadQueryTest_FilterExists) {
485 CreateMocks(2);
486 EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return(
487 false));
488 EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return(
489 true));
490 AddFilter(DownloadQuery::FILTER_EXISTS, true);
491 ExpectStandardFilterResults();
492}
493
494TEST_F(DownloadQueryTest, DownloadQueryTest_SortExists) {
495 CreateMocks(2);
496 EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return(
497 false));
498 EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return(
499 true));
500 query()->AddSorter(DownloadQuery::SORT_EXISTS,
501 DownloadQuery::ASCENDING);
502 ExpectSortInverted();
503}
504
[email protected]c5899b252013-01-13 00:42:10505TEST_F(DownloadQueryTest, DownloadQueryTest_FilterMime) {
[email protected]be052212011-12-14 18:40:40506 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10507 EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("text"));
508 EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("image"));
509 AddFilter(DownloadQuery::FILTER_MIME, "text");
510 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40511}
512
[email protected]c5899b252013-01-13 00:42:10513TEST_F(DownloadQueryTest, DownloadQueryTest_SortMime) {
514 CreateMocks(2);
515 EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("b"));
516 EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("a"));
517 query()->AddSorter(DownloadQuery::SORT_MIME, DownloadQuery::ASCENDING);
518 ExpectSortInverted();
519}
520
521TEST_F(DownloadQueryTest, DownloadQueryTest_FilterPaused) {
[email protected]be052212011-12-14 18:40:40522 CreateMocks(2);
523 EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true));
524 EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false));
[email protected]c5899b252013-01-13 00:42:10525 AddFilter(DownloadQuery::FILTER_PAUSED, true);
526 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40527}
528
[email protected]c5899b252013-01-13 00:42:10529TEST_F(DownloadQueryTest, DownloadQueryTest_SortPaused) {
[email protected]d5e60e192013-01-04 00:09:50530 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10531 EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true));
532 EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false));
533 query()->AddSorter(DownloadQuery::SORT_PAUSED, DownloadQuery::ASCENDING);
534 ExpectSortInverted();
[email protected]d5e60e192013-01-04 00:09:50535}
536
[email protected]c5899b252013-01-13 00:42:10537TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedAfter) {
[email protected]be052212011-12-14 18:40:40538 CreateMocks(2);
539 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10540 base::Time::FromTimeT(kSomeKnownTime + 2)));
[email protected]be052212011-12-14 18:40:40541 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10542 base::Time::FromTimeT(kSomeKnownTime + 1)));
543 AddFilter(DownloadQuery::FILTER_STARTED_AFTER,
544 std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix));
545 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40546}
547
[email protected]c5899b252013-01-13 00:42:10548TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedBefore) {
549 CreateMocks(2);
550 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
551 base::Time::FromTimeT(kSomeKnownTime + 2)));
552 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
553 base::Time::FromTimeT(kSomeKnownTime + 4)));
554 AddFilter(DownloadQuery::FILTER_STARTED_BEFORE,
555 std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix));
556 ExpectStandardFilterResults();
557}
558
559TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartTime) {
560 CreateMocks(2);
561 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
562 base::Time::FromTimeT(kSomeKnownTime + 2)));
563 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
564 base::Time::FromTimeT(kSomeKnownTime + 4)));
565 AddFilter(DownloadQuery::FILTER_START_TIME,
566 std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix));
567 ExpectStandardFilterResults();
568}
569
570TEST_F(DownloadQueryTest, DownloadQueryTest_SortStartTime) {
571 CreateMocks(2);
572 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
573 base::Time::FromTimeT(kSomeKnownTime + 2)));
574 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
575 base::Time::FromTimeT(kSomeKnownTime + 4)));
576 query()->AddSorter(DownloadQuery::SORT_START_TIME, DownloadQuery::DESCENDING);
577 ExpectSortInverted();
578}
579
580TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedAfter) {
581 CreateMocks(2);
582 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
583 base::Time::FromTimeT(kSomeKnownTime + 2)));
584 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
585 base::Time::FromTimeT(kSomeKnownTime + 1)));
586 AddFilter(DownloadQuery::FILTER_ENDED_AFTER,
587 std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix));
588 ExpectStandardFilterResults();
589}
590
591TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedBefore) {
592 CreateMocks(2);
593 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
594 base::Time::FromTimeT(kSomeKnownTime + 2)));
595 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
596 base::Time::FromTimeT(kSomeKnownTime + 4)));
597 AddFilter(DownloadQuery::FILTER_ENDED_BEFORE,
598 std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix));
599 ExpectStandardFilterResults();
600}
601
602TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndTime) {
603 CreateMocks(2);
604 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
605 base::Time::FromTimeT(kSomeKnownTime + 2)));
606 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
607 base::Time::FromTimeT(kSomeKnownTime + 4)));
608 AddFilter(DownloadQuery::FILTER_END_TIME,
609 std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix));
610 ExpectStandardFilterResults();
611}
612
613TEST_F(DownloadQueryTest, DownloadQueryTest_SortEndTime) {
614 CreateMocks(2);
615 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
616 base::Time::FromTimeT(kSomeKnownTime + 2)));
617 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
618 base::Time::FromTimeT(kSomeKnownTime + 4)));
619 query()->AddSorter(DownloadQuery::SORT_END_TIME, DownloadQuery::DESCENDING);
620 ExpectSortInverted();
621}
622
lazyboya61f420b2016-06-27 23:48:18623TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater1) {
[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(1));
lazyboya61f420b2016-06-27 23:48:18627 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, 1.0);
[email protected]c5899b252013-01-13 00:42:10628 ExpectStandardFilterResults();
629}
630
lazyboya61f420b2016-06-27 23:48:18631TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater2) {
[email protected]c5899b252013-01-13 00:42:10632 CreateMocks(2);
633 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
lazyboya61f420b2016-06-27 23:48:18634 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(1));
635 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, 1.2);
[email protected]c5899b252013-01-13 00:42:10636 ExpectStandardFilterResults();
637}
638
lazyboya61f420b2016-06-27 23:48:18639TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater3) {
640 CreateMocks(2);
641 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
642 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
643 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, kNineGBDouble);
644 ExpectStandardFilterResults();
645}
646
647TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater4) {
648 CreateMocks(2);
649 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
650 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
651 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, kEightGBDouble + 1.0);
652 ExpectStandardFilterResults();
653}
654
655TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess1) {
[email protected]c5899b252013-01-13 00:42:10656 CreateMocks(2);
657 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
658 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
lazyboya61f420b2016-06-27 23:48:18659 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, 4.0);
660 ExpectStandardFilterResults();
661}
662
663TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess2) {
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_LESS, 1.2);
668 ExpectStandardFilterResults();
669}
670
671TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess3) {
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_LESS, kEightGBDouble + 1.0);
676 ExpectStandardFilterResults();
677}
678
679TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess4) {
680 CreateMocks(2);
681 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
682 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
683 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, kNineGBDouble);
684 ExpectStandardFilterResults();
685}
686
687TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes1) {
688 CreateMocks(2);
689 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
690 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
691 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, 2.0);
692 ExpectStandardFilterResults();
693}
694
695TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes2) {
696 CreateMocks(2);
697 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(1));
698 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(2));
699 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, 1.0);
700 ExpectStandardFilterResults();
701}
702
703TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes3) {
704 CreateMocks(2);
705 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kEightGB));
706 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB));
707 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, kEightGBDouble);
[email protected]c5899b252013-01-13 00:42:10708 ExpectStandardFilterResults();
709}
710
711TEST_F(DownloadQueryTest, DownloadQueryTest_SortTotalBytes) {
712 CreateMocks(2);
713 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
714 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
715 query()->AddSorter(DownloadQuery::SORT_TOTAL_BYTES,
716 DownloadQuery::DESCENDING);
717 ExpectSortInverted();
718}
719
720TEST_F(DownloadQueryTest, DownloadQueryTest_FilterState) {
[email protected]be052212011-12-14 18:40:40721 CreateMocks(2);
722 EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10723 DownloadItem::IN_PROGRESS));
[email protected]be052212011-12-14 18:40:40724 EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10725 DownloadItem::CANCELLED));
726 query()->AddFilter(DownloadItem::IN_PROGRESS);
727 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40728}
729
[email protected]c5899b252013-01-13 00:42:10730TEST_F(DownloadQueryTest, DownloadQueryTest_SortState) {
[email protected]be052212011-12-14 18:40:40731 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10732 EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return(
733 DownloadItem::IN_PROGRESS));
734 EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return(
735 DownloadItem::CANCELLED));
736 query()->AddSorter(DownloadQuery::SORT_STATE, DownloadQuery::DESCENDING);
737 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40738}
739
[email protected]c5899b252013-01-13 00:42:10740TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDanger) {
[email protected]be052212011-12-14 18:40:40741 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10742 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
743 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
744 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
745 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
746 query()->AddFilter(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
747 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40748}
749
[email protected]c5899b252013-01-13 00:42:10750TEST_F(DownloadQueryTest, DownloadQueryTest_SortDanger) {
751 CreateMocks(2);
752 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
753 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
754 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
755 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
756 query()->AddSorter(DownloadQuery::SORT_DANGER, DownloadQuery::DESCENDING);
757 ExpectSortInverted();
758}
759
760TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById1) {
[email protected]be052212011-12-14 18:40:40761 CreateMocks(2);
762 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
763 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0));
[email protected]c5899b252013-01-13 00:42:10764 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
765 DownloadQuery::ASCENDING);
[email protected]be052212011-12-14 18:40:40766 Search();
[email protected]c5899b252013-01-13 00:42:10767 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25768 EXPECT_EQ(0U, results()->at(0)->GetId());
769 EXPECT_EQ(1U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:10770}
771
772TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById2) {
773 CreateMocks(2);
774 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
775 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0));
776 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
777 DownloadQuery::DESCENDING);
778 Search();
779 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25780 EXPECT_EQ(0U, results()->at(0)->GetId());
781 EXPECT_EQ(1U, results()->at(1)->GetId());
[email protected]be052212011-12-14 18:40:40782}
783
784TEST_F(DownloadQueryTest, DownloadQueryFilterPerformance) {
[email protected]c5899b252013-01-13 00:42:10785 static const int kNumItems = 100;
786 static const int kNumFilters = 100;
[email protected]be052212011-12-14 18:40:40787 CreateMocks(kNumItems);
788 for (size_t i = 0; i < (kNumFilters - 1); ++i) {
789 query()->AddFilter(base::Bind(&AlwaysReturn, true));
790 }
791 query()->AddFilter(base::Bind(&AlwaysReturn, false));
792 base::Time start = base::Time::Now();
793 Search();
794 base::Time end = base::Time::Now();
795 double nanos = (end - start).InMillisecondsF() * 1000.0 * 1000.0;
796 double nanos_per_item = nanos / static_cast<double>(kNumItems);
797 double nanos_per_item_per_filter = nanos_per_item
798 / static_cast<double>(kNumFilters);
799 std::cout << "Search took " << nanos_per_item_per_filter
800 << " nanoseconds per item per filter.\n";
801}