blob: 61e68c9318eca8320d59419189317e69fb41554b [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
5#include <string>
6
7#include "base/bind.h"
[email protected]57999812013-02-24 05:40:528#include "base/files/file_path.h"
[email protected]be052212011-12-14 18:40:409#include "base/logging.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/stl_util.h"
[email protected]340f55d7c2013-06-10 20:49:1112#include "base/strings/string16.h"
[email protected]41a17c52013-06-28 00:27:5313#include "base/time/time.h"
[email protected]be052212011-12-14 18:40:4014#include "base/values.h"
[email protected]0a2bafc2012-02-01 22:35:0015#include "chrome/browser/download/download_query.h"
[email protected]b7d000b2012-06-02 22:18:2116#include "content/public/test/mock_download_item.h"
[email protected]be052212011-12-14 18:40:4017#include "testing/gmock/include/gmock/gmock.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20using ::testing::Return;
21using ::testing::ReturnRef;
22using ::testing::_;
23using base::Time;
24using base::Value;
[email protected]e582fdd2011-12-20 16:48:1725using content::DownloadItem;
[email protected]be052212011-12-14 18:40:4026typedef DownloadQuery::DownloadVector DownloadVector;
27
28namespace {
29
[email protected]c5899b252013-01-13 00:42:1030static const int kSomeKnownTime = 1355864160;
31static const char kSomeKnownTime8601[] = "2012-12-18T20:56:0";
32static const char k8601Suffix[] = ".000Z";
33
[email protected]530047e2013-07-12 17:02:2534bool IdNotEqual(uint32 not_id, const DownloadItem& item) {
[email protected]be052212011-12-14 18:40:4035 return item.GetId() != not_id;
36}
37
38bool AlwaysReturn(bool result, const DownloadItem& item) {
39 return result;
40}
41
42} // anonymous namespace
43
44class DownloadQueryTest : public testing::Test {
45 public:
46 DownloadQueryTest() {}
47
dchenge1bc7982014-10-30 00:32:4048 ~DownloadQueryTest() override {}
[email protected]be052212011-12-14 18:40:4049
dchenge1bc7982014-10-30 00:32:4050 void TearDown() override { STLDeleteElements(&mocks_); }
[email protected]be052212011-12-14 18:40:4051
52 void CreateMocks(int count) {
53 for (int i = 0; i < count; ++i) {
[email protected]75e51b52012-02-04 16:57:5454 mocks_.push_back(new content::MockDownloadItem());
[email protected]c5899b252013-01-13 00:42:1055 EXPECT_CALL(mock(mocks_.size() - 1), GetId()).WillRepeatedly(Return(
56 mocks_.size() - 1));
[email protected]be052212011-12-14 18:40:4057 }
58 }
59
[email protected]75e51b52012-02-04 16:57:5460 content::MockDownloadItem& mock(int index) { return *mocks_[index]; }
[email protected]be052212011-12-14 18:40:4061
62 DownloadQuery* query() { return &query_; }
63
64 template<typename ValueType> void AddFilter(
65 DownloadQuery::FilterType name, ValueType value);
66
67 void Search() {
68 query_.Search(mocks_.begin(), mocks_.end(), &results_);
69 }
70
71 DownloadVector* results() { return &results_; }
72
[email protected]c5899b252013-01-13 00:42:1073 // Filter tests generally contain 2 items. mock(0) matches the filter, mock(1)
74 // does not.
75 void ExpectStandardFilterResults() {
76 Search();
77 ASSERT_EQ(1U, results()->size());
[email protected]530047e2013-07-12 17:02:2578 ASSERT_EQ(0U, results()->at(0)->GetId());
[email protected]c5899b252013-01-13 00:42:1079 }
80
81 // If no sorters distinguish between two items, then DownloadQuery sorts by ID
82 // ascending. In order to test that a sorter distinguishes between two items,
83 // the sorter must sort them by ID descending.
84 void ExpectSortInverted() {
85 Search();
86 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:2587 ASSERT_EQ(1U, results()->at(0)->GetId());
88 ASSERT_EQ(0U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:1089 }
90
[email protected]be052212011-12-14 18:40:4091 private:
[email protected]75e51b52012-02-04 16:57:5492 std::vector<content::MockDownloadItem*> mocks_;
[email protected]be052212011-12-14 18:40:4093 DownloadQuery query_;
94 DownloadVector results_;
95
96 DISALLOW_COPY_AND_ASSIGN(DownloadQueryTest);
97};
98
99template<> void DownloadQueryTest::AddFilter(
100 DownloadQuery::FilterType name, bool cpp_value) {
[email protected]012d1312014-07-17 06:37:40101 scoped_ptr<base::Value> value(new base::FundamentalValue(cpp_value));
[email protected]be052212011-12-14 18:40:40102 CHECK(query_.AddFilter(name, *value.get()));
103}
104
105template<> void DownloadQueryTest::AddFilter(
106 DownloadQuery::FilterType name, int cpp_value) {
[email protected]a7965a42014-07-22 02:35:56107 scoped_ptr<base::Value> value(new base::FundamentalValue(cpp_value));
[email protected]be052212011-12-14 18:40:40108 CHECK(query_.AddFilter(name, *value.get()));
109}
110
111template<> void DownloadQueryTest::AddFilter(
112 DownloadQuery::FilterType name, const char* cpp_value) {
[email protected]df287e12014-03-24 18:33:39113 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
[email protected]be052212011-12-14 18:40:40114}
115
116template<> void DownloadQueryTest::AddFilter(
[email protected]d5e60e192013-01-04 00:09:50117 DownloadQuery::FilterType name, std::string cpp_value) {
[email protected]df287e12014-03-24 18:33:39118 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
[email protected]d5e60e192013-01-04 00:09:50119}
120
121template<> void DownloadQueryTest::AddFilter(
[email protected]b6775d782013-12-25 20:04:53122 DownloadQuery::FilterType name, const base::char16* cpp_value) {
[email protected]df287e12014-03-24 18:33:39123 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
[email protected]be052212011-12-14 18:40:40124}
125
[email protected]f1d784d62013-07-28 18:36:09126template<> void DownloadQueryTest::AddFilter(
[email protected]96920152013-12-04 21:00:16127 DownloadQuery::FilterType name, std::vector<base::string16> cpp_value) {
[email protected]f1d784d62013-07-28 18:36:09128 scoped_ptr<base::ListValue> list(new base::ListValue());
[email protected]96920152013-12-04 21:00:16129 for (std::vector<base::string16>::const_iterator it = cpp_value.begin();
[email protected]f1d784d62013-07-28 18:36:09130 it != cpp_value.end(); ++it) {
[email protected]df287e12014-03-24 18:33:39131 list->Append(new base::StringValue(*it));
[email protected]f1d784d62013-07-28 18:36:09132 }
133 CHECK(query_.AddFilter(name, *list.get()));
134}
135
136template<> void DownloadQueryTest::AddFilter(
137 DownloadQuery::FilterType name, std::vector<std::string> cpp_value) {
138 scoped_ptr<base::ListValue> list(new base::ListValue());
139 for (std::vector<std::string>::const_iterator it = cpp_value.begin();
140 it != cpp_value.end(); ++it) {
[email protected]df287e12014-03-24 18:33:39141 list->Append(new base::StringValue(*it));
[email protected]f1d784d62013-07-28 18:36:09142 }
143 CHECK(query_.AddFilter(name, *list.get()));
144}
145
[email protected]c5899b252013-01-13 00:42:10146#if defined(OS_WIN)
147template<> void DownloadQueryTest::AddFilter(
148 DownloadQuery::FilterType name, std::wstring cpp_value) {
[email protected]df287e12014-03-24 18:33:39149 CHECK(query_.AddFilter(name, base::StringValue(cpp_value)));
[email protected]c5899b252013-01-13 00:42:10150}
151#endif
152
153TEST_F(DownloadQueryTest, DownloadQueryTest_ZeroItems) {
[email protected]be052212011-12-14 18:40:40154 Search();
155 EXPECT_EQ(0U, results()->size());
156}
157
[email protected]c5899b252013-01-13 00:42:10158TEST_F(DownloadQueryTest, DownloadQueryTest_InvalidFilter) {
[email protected]a7965a42014-07-22 02:35:56159 scoped_ptr<base::Value> value(new base::FundamentalValue(0));
[email protected]be052212011-12-14 18:40:40160 EXPECT_FALSE(query()->AddFilter(
161 static_cast<DownloadQuery::FilterType>(kint32max),
162 *value.get()));
163}
164
[email protected]c5899b252013-01-13 00:42:10165TEST_F(DownloadQueryTest, DownloadQueryTest_EmptyQuery) {
166 CreateMocks(2);
167 Search();
168 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25169 ASSERT_EQ(0U, results()->at(0)->GetId());
170 ASSERT_EQ(1U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:10171}
172
173TEST_F(DownloadQueryTest, DownloadQueryTest_Limit) {
[email protected]be052212011-12-14 18:40:40174 CreateMocks(2);
175 query()->Limit(1);
[email protected]c5899b252013-01-13 00:42:10176 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40177}
178
[email protected]c5899b252013-01-13 00:42:10179TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilename) {
180 CreateMocks(2);
181 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
182 static_cast<content::BrowserContext*>(NULL)));
183 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
184 static_cast<content::BrowserContext*>(NULL)));
[email protected]650b2d52013-02-10 03:41:45185 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10186 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
187 match_filename));
[email protected]650b2d52013-02-10 03:41:45188 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10189 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
190 fail_filename));
[email protected]9dccd8062012-09-17 17:19:12191 GURL fail_url("http://example.com/fail");
[email protected]c5899b252013-01-13 00:42:10192 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
193 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
[email protected]f1d784d62013-07-28 18:36:09194 std::vector<std::string> query_terms;
195 query_terms.push_back("query");
196 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10197 ExpectStandardFilterResults();
198}
199
200TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryUrl) {
201 CreateMocks(2);
202 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
203 static_cast<content::BrowserContext*>(NULL)));
204 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
205 static_cast<content::BrowserContext*>(NULL)));
[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(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
208 fail_filename));
209 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
210 fail_filename));
[email protected]9dccd8062012-09-17 17:19:12211 GURL match_url("http://query.com/query");
[email protected]c5899b252013-01-13 00:42:10212 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
[email protected]be052212011-12-14 18:40:40213 GURL fail_url("http://example.com/fail");
[email protected]c5899b252013-01-13 00:42:10214 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
[email protected]f1d784d62013-07-28 18:36:09215 std::vector<std::string> query_terms;
216 query_terms.push_back("query");
217 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10218 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40219}
220
[email protected]c5899b252013-01-13 00:42:10221TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilenameI18N) {
222 CreateMocks(2);
223 EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return(
224 static_cast<content::BrowserContext*>(NULL)));
225 EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return(
226 static_cast<content::BrowserContext*>(NULL)));
[email protected]650b2d52013-02-10 03:41:45227 const base::FilePath::StringType kTestString(
[email protected]c5899b252013-01-13 00:42:10228#if defined(OS_POSIX)
229 "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd"
230#elif defined(OS_WIN)
231 L"/\x4f60\x597d\x4f60\x597d"
232#endif
233 );
[email protected]650b2d52013-02-10 03:41:45234 base::FilePath match_filename(kTestString);
[email protected]c5899b252013-01-13 00:42:10235 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
236 match_filename));
[email protected]650b2d52013-02-10 03:41:45237 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10238 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
239 fail_filename));
240 GURL fail_url("http://example.com/fail");
241 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
242 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
[email protected]f1d784d62013-07-28 18:36:09243 std::vector<base::FilePath::StringType> query_terms;
244 query_terms.push_back(kTestString);
245 AddFilter(DownloadQuery::FILTER_QUERY, query_terms);
[email protected]c5899b252013-01-13 00:42:10246 ExpectStandardFilterResults();
247}
248
249TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilenameRegex) {
250 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45251 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10252 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
253 match_filename));
[email protected]650b2d52013-02-10 03:41:45254 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10255 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
256 fail_filename));
257 AddFilter(DownloadQuery::FILTER_FILENAME_REGEX, "y");
258 ExpectStandardFilterResults();
259}
260
261TEST_F(DownloadQueryTest, DownloadQueryTest_SortFilename) {
262 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45263 base::FilePath b_filename(FILE_PATH_LITERAL("b"));
[email protected]c5899b252013-01-13 00:42:10264 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
265 b_filename));
[email protected]650b2d52013-02-10 03:41:45266 base::FilePath a_filename(FILE_PATH_LITERAL("a"));
[email protected]c5899b252013-01-13 00:42:10267 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
268 a_filename));
269 query()->AddSorter(DownloadQuery::SORT_FILENAME, DownloadQuery::ASCENDING);
270 ExpectSortInverted();
271}
272
273TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilename) {
274 CreateMocks(2);
[email protected]650b2d52013-02-10 03:41:45275 base::FilePath match_filename(FILE_PATH_LITERAL("query"));
[email protected]c5899b252013-01-13 00:42:10276 EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef(
277 match_filename));
[email protected]650b2d52013-02-10 03:41:45278 base::FilePath fail_filename(FILE_PATH_LITERAL("fail"));
[email protected]c5899b252013-01-13 00:42:10279 EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef(
280 fail_filename));
281 AddFilter(DownloadQuery::FILTER_FILENAME, match_filename.value().c_str());
282 ExpectStandardFilterResults();
283}
284
285TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrlRegex) {
286 CreateMocks(2);
287 GURL match_url("http://query.com/query");
288 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
289 GURL fail_url("http://example.com/fail");
290 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
291 AddFilter(DownloadQuery::FILTER_URL_REGEX, "query");
292 ExpectStandardFilterResults();
293}
294
295TEST_F(DownloadQueryTest, DownloadQueryTest_SortUrl) {
296 CreateMocks(2);
297 GURL b_url("http://example.com/b");
298 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(b_url));
299 GURL a_url("http://example.com/a");
300 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(a_url));
301 query()->AddSorter(DownloadQuery::SORT_URL, DownloadQuery::ASCENDING);
302 ExpectSortInverted();
303}
304
305TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrl) {
306 CreateMocks(2);
307 GURL match_url("http://query.com/query");
308 EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url));
309 GURL fail_url("http://example.com/fail");
310 EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url));
311 AddFilter(DownloadQuery::FILTER_URL, match_url.spec().c_str());
312 ExpectStandardFilterResults();
313}
314
315TEST_F(DownloadQueryTest, DownloadQueryTest_FilterCallback) {
316 CreateMocks(2);
317 CHECK(query()->AddFilter(base::Bind(&IdNotEqual, 1)));
318 ExpectStandardFilterResults();
319}
320
321TEST_F(DownloadQueryTest, DownloadQueryTest_FilterBytesReceived) {
[email protected]be052212011-12-14 18:40:40322 CreateMocks(2);
323 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
324 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1));
[email protected]c5899b252013-01-13 00:42:10325 AddFilter(DownloadQuery::FILTER_BYTES_RECEIVED, 0);
326 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40327}
328
[email protected]c5899b252013-01-13 00:42:10329TEST_F(DownloadQueryTest, DownloadQueryTest_SortBytesReceived) {
[email protected]be052212011-12-14 18:40:40330 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10331 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
332 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1));
333 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
334 DownloadQuery::DESCENDING);
335 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40336}
337
[email protected]c5899b252013-01-13 00:42:10338TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDangerAccepted) {
[email protected]be052212011-12-14 18:40:40339 CreateMocks(2);
[email protected]cda79062013-01-17 01:50:52340 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
341 content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
342 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
343 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
[email protected]c5899b252013-01-13 00:42:10344 AddFilter(DownloadQuery::FILTER_DANGER_ACCEPTED, true);
345 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40346}
347
[email protected]c5899b252013-01-13 00:42:10348TEST_F(DownloadQueryTest, DownloadQueryTest_SortDangerAccepted) {
[email protected]be052212011-12-14 18:40:40349 CreateMocks(2);
[email protected]cda79062013-01-17 01:50:52350 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
351 content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED));
352 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
353 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
[email protected]c5899b252013-01-13 00:42:10354 query()->AddSorter(DownloadQuery::SORT_DANGER_ACCEPTED,
355 DownloadQuery::ASCENDING);
356 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40357}
358
[email protected]3291a4982013-01-14 00:58:52359TEST_F(DownloadQueryTest, DownloadQueryTest_FilterExists) {
360 CreateMocks(2);
361 EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return(
362 false));
363 EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return(
364 true));
365 AddFilter(DownloadQuery::FILTER_EXISTS, true);
366 ExpectStandardFilterResults();
367}
368
369TEST_F(DownloadQueryTest, DownloadQueryTest_SortExists) {
370 CreateMocks(2);
371 EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return(
372 false));
373 EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return(
374 true));
375 query()->AddSorter(DownloadQuery::SORT_EXISTS,
376 DownloadQuery::ASCENDING);
377 ExpectSortInverted();
378}
379
[email protected]c5899b252013-01-13 00:42:10380TEST_F(DownloadQueryTest, DownloadQueryTest_FilterMime) {
[email protected]be052212011-12-14 18:40:40381 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10382 EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("text"));
383 EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("image"));
384 AddFilter(DownloadQuery::FILTER_MIME, "text");
385 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40386}
387
[email protected]c5899b252013-01-13 00:42:10388TEST_F(DownloadQueryTest, DownloadQueryTest_SortMime) {
389 CreateMocks(2);
390 EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("b"));
391 EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("a"));
392 query()->AddSorter(DownloadQuery::SORT_MIME, DownloadQuery::ASCENDING);
393 ExpectSortInverted();
394}
395
396TEST_F(DownloadQueryTest, DownloadQueryTest_FilterPaused) {
[email protected]be052212011-12-14 18:40:40397 CreateMocks(2);
398 EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true));
399 EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false));
[email protected]c5899b252013-01-13 00:42:10400 AddFilter(DownloadQuery::FILTER_PAUSED, true);
401 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40402}
403
[email protected]c5899b252013-01-13 00:42:10404TEST_F(DownloadQueryTest, DownloadQueryTest_SortPaused) {
[email protected]d5e60e192013-01-04 00:09:50405 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10406 EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true));
407 EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false));
408 query()->AddSorter(DownloadQuery::SORT_PAUSED, DownloadQuery::ASCENDING);
409 ExpectSortInverted();
[email protected]d5e60e192013-01-04 00:09:50410}
411
[email protected]c5899b252013-01-13 00:42:10412TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedAfter) {
[email protected]be052212011-12-14 18:40:40413 CreateMocks(2);
414 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10415 base::Time::FromTimeT(kSomeKnownTime + 2)));
[email protected]be052212011-12-14 18:40:40416 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10417 base::Time::FromTimeT(kSomeKnownTime + 1)));
418 AddFilter(DownloadQuery::FILTER_STARTED_AFTER,
419 std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix));
420 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40421}
422
[email protected]c5899b252013-01-13 00:42:10423TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedBefore) {
424 CreateMocks(2);
425 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
426 base::Time::FromTimeT(kSomeKnownTime + 2)));
427 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
428 base::Time::FromTimeT(kSomeKnownTime + 4)));
429 AddFilter(DownloadQuery::FILTER_STARTED_BEFORE,
430 std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix));
431 ExpectStandardFilterResults();
432}
433
434TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartTime) {
435 CreateMocks(2);
436 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
437 base::Time::FromTimeT(kSomeKnownTime + 2)));
438 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
439 base::Time::FromTimeT(kSomeKnownTime + 4)));
440 AddFilter(DownloadQuery::FILTER_START_TIME,
441 std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix));
442 ExpectStandardFilterResults();
443}
444
445TEST_F(DownloadQueryTest, DownloadQueryTest_SortStartTime) {
446 CreateMocks(2);
447 EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return(
448 base::Time::FromTimeT(kSomeKnownTime + 2)));
449 EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return(
450 base::Time::FromTimeT(kSomeKnownTime + 4)));
451 query()->AddSorter(DownloadQuery::SORT_START_TIME, DownloadQuery::DESCENDING);
452 ExpectSortInverted();
453}
454
455TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedAfter) {
456 CreateMocks(2);
457 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
458 base::Time::FromTimeT(kSomeKnownTime + 2)));
459 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
460 base::Time::FromTimeT(kSomeKnownTime + 1)));
461 AddFilter(DownloadQuery::FILTER_ENDED_AFTER,
462 std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix));
463 ExpectStandardFilterResults();
464}
465
466TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedBefore) {
467 CreateMocks(2);
468 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
469 base::Time::FromTimeT(kSomeKnownTime + 2)));
470 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
471 base::Time::FromTimeT(kSomeKnownTime + 4)));
472 AddFilter(DownloadQuery::FILTER_ENDED_BEFORE,
473 std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix));
474 ExpectStandardFilterResults();
475}
476
477TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndTime) {
478 CreateMocks(2);
479 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
480 base::Time::FromTimeT(kSomeKnownTime + 2)));
481 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
482 base::Time::FromTimeT(kSomeKnownTime + 4)));
483 AddFilter(DownloadQuery::FILTER_END_TIME,
484 std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix));
485 ExpectStandardFilterResults();
486}
487
488TEST_F(DownloadQueryTest, DownloadQueryTest_SortEndTime) {
489 CreateMocks(2);
490 EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return(
491 base::Time::FromTimeT(kSomeKnownTime + 2)));
492 EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return(
493 base::Time::FromTimeT(kSomeKnownTime + 4)));
494 query()->AddSorter(DownloadQuery::SORT_END_TIME, DownloadQuery::DESCENDING);
495 ExpectSortInverted();
496}
497
498TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater) {
499 CreateMocks(2);
500 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
501 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(1));
502 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, 1);
503 ExpectStandardFilterResults();
504}
505
506TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess) {
507 CreateMocks(2);
508 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
509 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
510 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, 4);
511 ExpectStandardFilterResults();
512}
513
514TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes) {
515 CreateMocks(2);
516 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
517 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
518 AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, 2);
519 ExpectStandardFilterResults();
520}
521
522TEST_F(DownloadQueryTest, DownloadQueryTest_SortTotalBytes) {
523 CreateMocks(2);
524 EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2));
525 EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4));
526 query()->AddSorter(DownloadQuery::SORT_TOTAL_BYTES,
527 DownloadQuery::DESCENDING);
528 ExpectSortInverted();
529}
530
531TEST_F(DownloadQueryTest, DownloadQueryTest_FilterState) {
[email protected]be052212011-12-14 18:40:40532 CreateMocks(2);
533 EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10534 DownloadItem::IN_PROGRESS));
[email protected]be052212011-12-14 18:40:40535 EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return(
[email protected]c5899b252013-01-13 00:42:10536 DownloadItem::CANCELLED));
537 query()->AddFilter(DownloadItem::IN_PROGRESS);
538 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40539}
540
[email protected]c5899b252013-01-13 00:42:10541TEST_F(DownloadQueryTest, DownloadQueryTest_SortState) {
[email protected]be052212011-12-14 18:40:40542 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10543 EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return(
544 DownloadItem::IN_PROGRESS));
545 EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return(
546 DownloadItem::CANCELLED));
547 query()->AddSorter(DownloadQuery::SORT_STATE, DownloadQuery::DESCENDING);
548 ExpectSortInverted();
[email protected]be052212011-12-14 18:40:40549}
550
[email protected]c5899b252013-01-13 00:42:10551TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDanger) {
[email protected]be052212011-12-14 18:40:40552 CreateMocks(2);
[email protected]c5899b252013-01-13 00:42:10553 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
554 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
555 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
556 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
557 query()->AddFilter(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS);
558 ExpectStandardFilterResults();
[email protected]be052212011-12-14 18:40:40559}
560
[email protected]c5899b252013-01-13 00:42:10561TEST_F(DownloadQueryTest, DownloadQueryTest_SortDanger) {
562 CreateMocks(2);
563 EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return(
564 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
565 EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return(
566 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE));
567 query()->AddSorter(DownloadQuery::SORT_DANGER, DownloadQuery::DESCENDING);
568 ExpectSortInverted();
569}
570
571TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById1) {
[email protected]be052212011-12-14 18:40:40572 CreateMocks(2);
573 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
574 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0));
[email protected]c5899b252013-01-13 00:42:10575 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
576 DownloadQuery::ASCENDING);
[email protected]be052212011-12-14 18:40:40577 Search();
[email protected]c5899b252013-01-13 00:42:10578 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25579 EXPECT_EQ(0U, results()->at(0)->GetId());
580 EXPECT_EQ(1U, results()->at(1)->GetId());
[email protected]c5899b252013-01-13 00:42:10581}
582
583TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById2) {
584 CreateMocks(2);
585 EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0));
586 EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0));
587 query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED,
588 DownloadQuery::DESCENDING);
589 Search();
590 ASSERT_EQ(2U, results()->size());
[email protected]530047e2013-07-12 17:02:25591 EXPECT_EQ(0U, results()->at(0)->GetId());
592 EXPECT_EQ(1U, results()->at(1)->GetId());
[email protected]be052212011-12-14 18:40:40593}
594
595TEST_F(DownloadQueryTest, DownloadQueryFilterPerformance) {
[email protected]c5899b252013-01-13 00:42:10596 static const int kNumItems = 100;
597 static const int kNumFilters = 100;
[email protected]be052212011-12-14 18:40:40598 CreateMocks(kNumItems);
599 for (size_t i = 0; i < (kNumFilters - 1); ++i) {
600 query()->AddFilter(base::Bind(&AlwaysReturn, true));
601 }
602 query()->AddFilter(base::Bind(&AlwaysReturn, false));
603 base::Time start = base::Time::Now();
604 Search();
605 base::Time end = base::Time::Now();
606 double nanos = (end - start).InMillisecondsF() * 1000.0 * 1000.0;
607 double nanos_per_item = nanos / static_cast<double>(kNumItems);
608 double nanos_per_item_per_filter = nanos_per_item
609 / static_cast<double>(kNumFilters);
610 std::cout << "Search took " << nanos_per_item_per_filter
611 << " nanoseconds per item per filter.\n";
612}