[email protected] | 33d2210 | 2012-01-25 17:46:53 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 5 | #include "chrome/browser/download/download_query.h" |
| 6 | |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 7 | #include <stddef.h> |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <limits> |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 11 | #include <memory> |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 12 | #include <string> |
| 13 | |
| 14 | #include "base/bind.h" |
Hans Wennborg | 1790e6b | 2020-04-24 19:10:33 | [diff] [blame] | 15 | #include "base/check.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
[email protected] | 41a17c5 | 2013-06-28 00:27:53 | [diff] [blame] | 17 | #include "base/time/time.h" |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 18 | #include "base/values.h" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 19 | #include "build/build_config.h" |
Min Qin | addcd16 | 2018-03-28 23:25:47 | [diff] [blame] | 20 | #include "components/download/public/common/mock_download_item.h" |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 21 | #include "content/public/browser/download_item_utils.h" |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 22 | #include "testing/gmock/include/gmock/gmock.h" |
| 23 | #include "testing/gtest/include/gtest/gtest.h" |
| 24 | |
| 25 | using ::testing::Return; |
| 26 | using ::testing::ReturnRef; |
| 27 | using ::testing::_; |
| 28 | using base::Time; |
| 29 | using base::Value; |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 30 | using download::DownloadItem; |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 31 | typedef DownloadQuery::DownloadVector DownloadVector; |
| 32 | |
| 33 | namespace { |
| 34 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 35 | static const int kSomeKnownTime = 1355864160; |
| 36 | static const char kSomeKnownTime8601[] = "2012-12-18T20:56:0"; |
| 37 | static const char k8601Suffix[] = ".000Z"; |
| 38 | |
lazyboy | a61f420b | 2016-06-27 23:48:18 | [diff] [blame] | 39 | static const int64_t kEightGB = 1LL << 33; |
| 40 | static const int64_t kSixteenGB = 1LL << 34; |
| 41 | static const double kEightGBDouble = 8.0 * (1LL << 30); |
| 42 | static const double kNineGBDouble = 9.0 * (1LL << 30); |
| 43 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 44 | bool IdNotEqual(uint32_t not_id, const DownloadItem& item) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 45 | return item.GetId() != not_id; |
| 46 | } |
| 47 | |
| 48 | bool AlwaysReturn(bool result, const DownloadItem& item) { |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | } // anonymous namespace |
| 53 | |
| 54 | class DownloadQueryTest : public testing::Test { |
| 55 | public: |
| 56 | DownloadQueryTest() {} |
| 57 | |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 58 | DownloadQueryTest(const DownloadQueryTest&) = delete; |
| 59 | DownloadQueryTest& operator=(const DownloadQueryTest&) = delete; |
| 60 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 61 | ~DownloadQueryTest() override {} |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 62 | |
avi | aa346b0d | 2016-10-28 14:52:51 | [diff] [blame] | 63 | void TearDown() override {} |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 64 | |
| 65 | void CreateMocks(int count) { |
| 66 | for (int i = 0; i < count; ++i) { |
Min Qin | addcd16 | 2018-03-28 23:25:47 | [diff] [blame] | 67 | owned_mocks_.push_back(std::make_unique<download::MockDownloadItem>()); |
avi | aa346b0d | 2016-10-28 14:52:51 | [diff] [blame] | 68 | mocks_.push_back(owned_mocks_.back().get()); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 69 | EXPECT_CALL(mock(mocks_.size() - 1), GetId()).WillRepeatedly(Return( |
| 70 | mocks_.size() - 1)); |
Ian Vollick | 8f08232 | 2021-12-11 14:27:56 | [diff] [blame] | 71 | content::DownloadItemUtils::AttachInfoForTesting(mocks_.back(), nullptr, |
| 72 | nullptr); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Min Qin | addcd16 | 2018-03-28 23:25:47 | [diff] [blame] | 76 | download::MockDownloadItem& mock(int index) { return *mocks_[index]; } |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 77 | |
| 78 | DownloadQuery* query() { return &query_; } |
| 79 | |
| 80 | template<typename ValueType> void AddFilter( |
| 81 | DownloadQuery::FilterType name, ValueType value); |
| 82 | |
| 83 | void Search() { |
| 84 | query_.Search(mocks_.begin(), mocks_.end(), &results_); |
| 85 | } |
| 86 | |
| 87 | DownloadVector* results() { return &results_; } |
| 88 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 89 | // Filter tests generally contain 2 items. mock(0) matches the filter, mock(1) |
| 90 | // does not. |
| 91 | void ExpectStandardFilterResults() { |
| 92 | Search(); |
| 93 | ASSERT_EQ(1U, results()->size()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 94 | ASSERT_EQ(0U, results()->at(0)->GetId()); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // If no sorters distinguish between two items, then DownloadQuery sorts by ID |
| 98 | // ascending. In order to test that a sorter distinguishes between two items, |
| 99 | // the sorter must sort them by ID descending. |
| 100 | void ExpectSortInverted() { |
| 101 | Search(); |
| 102 | ASSERT_EQ(2U, results()->size()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 103 | ASSERT_EQ(1U, results()->at(0)->GetId()); |
| 104 | ASSERT_EQ(0U, results()->at(1)->GetId()); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 107 | private: |
avi | aa346b0d | 2016-10-28 14:52:51 | [diff] [blame] | 108 | // These two vectors hold the MockDownloadItems. |mocks_| contains just the |
| 109 | // pointers, but is necessary because DownloadQuery processes vectors of |
| 110 | // unowned pointers. |owned_mocks_| holds the ownership of the mock objects. |
Min Qin | addcd16 | 2018-03-28 23:25:47 | [diff] [blame] | 111 | std::vector<download::MockDownloadItem*> mocks_; |
| 112 | std::vector<std::unique_ptr<download::MockDownloadItem>> owned_mocks_; |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 113 | DownloadQuery query_; |
| 114 | DownloadVector results_; |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | template<> void DownloadQueryTest::AddFilter( |
| 118 | DownloadQuery::FilterType name, bool cpp_value) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 119 | std::unique_ptr<base::Value> value(new base::Value(cpp_value)); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 120 | CHECK(query_.AddFilter(name, *value.get())); |
| 121 | } |
| 122 | |
lazyboy | a61f420b | 2016-06-27 23:48:18 | [diff] [blame] | 123 | template <> |
| 124 | void DownloadQueryTest::AddFilter(DownloadQuery::FilterType name, |
| 125 | double cpp_value) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 126 | std::unique_ptr<base::Value> value(new base::Value(cpp_value)); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 127 | CHECK(query_.AddFilter(name, *value.get())); |
| 128 | } |
| 129 | |
| 130 | template<> void DownloadQueryTest::AddFilter( |
| 131 | DownloadQuery::FilterType name, const char* cpp_value) { |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 132 | CHECK(query_.AddFilter(name, base::Value(cpp_value))); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | template<> void DownloadQueryTest::AddFilter( |
[email protected] | d5e60e19 | 2013-01-04 00:09:50 | [diff] [blame] | 136 | DownloadQuery::FilterType name, std::string cpp_value) { |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 137 | CHECK(query_.AddFilter(name, base::Value(cpp_value))); |
[email protected] | d5e60e19 | 2013-01-04 00:09:50 | [diff] [blame] | 138 | } |
| 139 | |
Jan Wilken Dörrie | 362098d | 2021-03-09 08:47:09 | [diff] [blame] | 140 | template <> |
| 141 | void DownloadQueryTest::AddFilter(DownloadQuery::FilterType name, |
| 142 | const char16_t* cpp_value) { |
jdoerrie | 122c4da | 2017-03-06 11:12:04 | [diff] [blame] | 143 | CHECK(query_.AddFilter(name, base::Value(cpp_value))); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 144 | } |
| 145 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 146 | template <> |
| 147 | void DownloadQueryTest::AddFilter(DownloadQuery::FilterType name, |
| 148 | std::vector<std::u16string> cpp_value) { |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 149 | std::unique_ptr<base::ListValue> list(new base::ListValue()); |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 150 | for (std::vector<std::u16string>::const_iterator it = cpp_value.begin(); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 151 | it != cpp_value.end(); ++it) { |
Clark DuVall | e5ee6df | 2021-08-31 19:39:41 | [diff] [blame] | 152 | list->Append(*it); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 153 | } |
| 154 | CHECK(query_.AddFilter(name, *list.get())); |
| 155 | } |
| 156 | |
| 157 | template<> void DownloadQueryTest::AddFilter( |
| 158 | DownloadQuery::FilterType name, std::vector<std::string> cpp_value) { |
dcheng | abbf4465 | 2016-04-07 22:23:39 | [diff] [blame] | 159 | std::unique_ptr<base::ListValue> list(new base::ListValue()); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 160 | for (std::vector<std::string>::const_iterator it = cpp_value.begin(); |
| 161 | it != cpp_value.end(); ++it) { |
Clark DuVall | e5ee6df | 2021-08-31 19:39:41 | [diff] [blame] | 162 | list->Append(*it); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 163 | } |
| 164 | CHECK(query_.AddFilter(name, *list.get())); |
| 165 | } |
| 166 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 167 | TEST_F(DownloadQueryTest, DownloadQueryTest_ZeroItems) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 168 | Search(); |
| 169 | EXPECT_EQ(0U, results()->size()); |
| 170 | } |
| 171 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 172 | TEST_F(DownloadQueryTest, DownloadQueryTest_InvalidFilter) { |
jdoerrie | 23972357 | 2017-03-02 12:09:19 | [diff] [blame] | 173 | std::unique_ptr<base::Value> value(new base::Value(0)); |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame] | 174 | EXPECT_FALSE(query()->AddFilter(static_cast<DownloadQuery::FilterType>( |
| 175 | std::numeric_limits<int32_t>::max()), |
| 176 | *value.get())); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 179 | TEST_F(DownloadQueryTest, DownloadQueryTest_EmptyQuery) { |
| 180 | CreateMocks(2); |
| 181 | Search(); |
| 182 | ASSERT_EQ(2U, results()->size()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 183 | ASSERT_EQ(0U, results()->at(0)->GetId()); |
| 184 | ASSERT_EQ(1U, results()->at(1)->GetId()); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TEST_F(DownloadQueryTest, DownloadQueryTest_Limit) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 188 | CreateMocks(2); |
| 189 | query()->Limit(1); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 190 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 191 | } |
| 192 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 193 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilename) { |
| 194 | CreateMocks(2); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 195 | base::FilePath match_filename(FILE_PATH_LITERAL("query")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 196 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 197 | match_filename)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 198 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 199 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 200 | fail_filename)); |
[email protected] | 9dccd806 | 2012-09-17 17:19:12 | [diff] [blame] | 201 | GURL fail_url("http://example.com/fail"); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 202 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
| 203 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 204 | EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
| 205 | EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
| 206 | std::vector<std::string> query_terms; |
| 207 | query_terms.push_back("query"); |
| 208 | AddFilter(DownloadQuery::FILTER_QUERY, query_terms); |
| 209 | ExpectStandardFilterResults(); |
| 210 | } |
| 211 | |
| 212 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryOriginalUrl) { |
| 213 | CreateMocks(2); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 214 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
| 215 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 216 | fail_filename)); |
| 217 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 218 | fail_filename)); |
| 219 | GURL match_url("http://query.com/query"); |
| 220 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url)); |
| 221 | GURL fail_url("http://example.com/fail"); |
| 222 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
| 223 | EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
| 224 | EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
| 225 | std::vector<std::string> query_terms; |
| 226 | query_terms.push_back("query"); |
| 227 | AddFilter(DownloadQuery::FILTER_QUERY, query_terms); |
| 228 | ExpectStandardFilterResults(); |
| 229 | } |
| 230 | |
| 231 | TEST_F(DownloadQueryTest, |
| 232 | DownloadQueryTest_FilterGenericQueryOriginalUrlUnescaping) { |
| 233 | CreateMocks(2); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 234 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
| 235 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 236 | fail_filename)); |
| 237 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 238 | fail_filename)); |
| 239 | GURL match_url("http://q%75%65%72y.c%6Fm/%71uer%79"); |
| 240 | GURL fail_url("http://%65xampl%65.com/%66ai%6C"); |
| 241 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url)); |
| 242 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
| 243 | EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
| 244 | EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 245 | std::vector<std::string> query_terms; |
| 246 | query_terms.push_back("query"); |
| 247 | AddFilter(DownloadQuery::FILTER_QUERY, query_terms); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 248 | ExpectStandardFilterResults(); |
| 249 | } |
| 250 | |
| 251 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryUrl) { |
| 252 | CreateMocks(2); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 253 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 254 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 255 | fail_filename)); |
| 256 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 257 | fail_filename)); |
[email protected] | 9dccd806 | 2012-09-17 17:19:12 | [diff] [blame] | 258 | GURL match_url("http://query.com/query"); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 259 | GURL fail_url("http://example.com/fail"); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 260 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 261 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 262 | EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url)); |
| 263 | EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
| 264 | std::vector<std::string> query_terms; |
| 265 | query_terms.push_back("query"); |
| 266 | AddFilter(DownloadQuery::FILTER_QUERY, query_terms); |
| 267 | ExpectStandardFilterResults(); |
| 268 | } |
| 269 | |
| 270 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryUrlUnescaping) { |
| 271 | CreateMocks(2); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 272 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
| 273 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 274 | fail_filename)); |
| 275 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 276 | fail_filename)); |
| 277 | GURL match_url("http://%71uer%79.com/qu%65ry"); |
| 278 | GURL fail_url("http://e%78am%70le.com/f%61il"); |
| 279 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
| 280 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
| 281 | EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url)); |
| 282 | EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 283 | std::vector<std::string> query_terms; |
| 284 | query_terms.push_back("query"); |
| 285 | AddFilter(DownloadQuery::FILTER_QUERY, query_terms); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 286 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 287 | } |
| 288 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 289 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilenameI18N) { |
| 290 | CreateMocks(2); |
Peter Kasting | 690f8c8 | 2021-02-13 18:09:54 | [diff] [blame] | 291 | const std::string kTestString( |
| 292 | "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd"); |
| 293 | base::FilePath match_filename = base::FilePath::FromUTF8Unsafe(kTestString); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 294 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 295 | match_filename)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 296 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 297 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 298 | fail_filename)); |
| 299 | GURL fail_url("http://example.com/fail"); |
| 300 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
| 301 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 302 | EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
| 303 | EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
Peter Kasting | 690f8c8 | 2021-02-13 18:09:54 | [diff] [blame] | 304 | std::vector<std::string> query_terms; |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 305 | query_terms.push_back(kTestString); |
| 306 | AddFilter(DownloadQuery::FILTER_QUERY, query_terms); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 307 | ExpectStandardFilterResults(); |
| 308 | } |
| 309 | |
| 310 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilenameRegex) { |
| 311 | CreateMocks(2); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 312 | base::FilePath match_filename(FILE_PATH_LITERAL("query")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 313 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 314 | match_filename)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 315 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 316 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 317 | fail_filename)); |
| 318 | AddFilter(DownloadQuery::FILTER_FILENAME_REGEX, "y"); |
| 319 | ExpectStandardFilterResults(); |
| 320 | } |
| 321 | |
| 322 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortFilename) { |
| 323 | CreateMocks(2); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 324 | base::FilePath b_filename(FILE_PATH_LITERAL("b")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 325 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 326 | b_filename)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 327 | base::FilePath a_filename(FILE_PATH_LITERAL("a")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 328 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 329 | a_filename)); |
| 330 | query()->AddSorter(DownloadQuery::SORT_FILENAME, DownloadQuery::ASCENDING); |
| 331 | ExpectSortInverted(); |
| 332 | } |
| 333 | |
| 334 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilename) { |
| 335 | CreateMocks(2); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 336 | base::FilePath match_filename(FILE_PATH_LITERAL("query")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 337 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 338 | match_filename)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 339 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 340 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 341 | fail_filename)); |
Peter Kasting | 690f8c8 | 2021-02-13 18:09:54 | [diff] [blame] | 342 | AddFilter(DownloadQuery::FILTER_FILENAME, |
| 343 | match_filename.AsUTF8Unsafe().c_str()); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 344 | ExpectStandardFilterResults(); |
| 345 | } |
| 346 | |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 347 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterOriginalUrlRegex) { |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 348 | CreateMocks(2); |
| 349 | GURL match_url("http://query.com/query"); |
| 350 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url)); |
| 351 | GURL fail_url("http://example.com/fail"); |
| 352 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 353 | AddFilter(DownloadQuery::FILTER_ORIGINAL_URL_REGEX, "query"); |
| 354 | ExpectStandardFilterResults(); |
| 355 | } |
| 356 | |
| 357 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortOriginalUrl) { |
| 358 | CreateMocks(2); |
| 359 | GURL b_url("http://example.com/b"); |
| 360 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(b_url)); |
| 361 | GURL a_url("http://example.com/a"); |
| 362 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(a_url)); |
| 363 | query()->AddSorter( |
| 364 | DownloadQuery::SORT_ORIGINAL_URL, DownloadQuery::ASCENDING); |
| 365 | ExpectSortInverted(); |
| 366 | } |
| 367 | |
| 368 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterOriginalUrl) { |
| 369 | CreateMocks(2); |
| 370 | GURL match_url("http://query.com/query"); |
| 371 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url)); |
| 372 | GURL fail_url("http://example.com/fail"); |
| 373 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
| 374 | AddFilter(DownloadQuery::FILTER_ORIGINAL_URL, match_url.spec().c_str()); |
| 375 | ExpectStandardFilterResults(); |
| 376 | } |
| 377 | |
| 378 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrlRegex) { |
| 379 | CreateMocks(2); |
| 380 | GURL match_url("http://query.com/query"); |
| 381 | EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url)); |
| 382 | GURL fail_url("http://example.com/fail"); |
| 383 | EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 384 | AddFilter(DownloadQuery::FILTER_URL_REGEX, "query"); |
| 385 | ExpectStandardFilterResults(); |
| 386 | } |
| 387 | |
| 388 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortUrl) { |
| 389 | CreateMocks(2); |
| 390 | GURL b_url("http://example.com/b"); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 391 | EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(b_url)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 392 | GURL a_url("http://example.com/a"); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 393 | EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(a_url)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 394 | query()->AddSorter(DownloadQuery::SORT_URL, DownloadQuery::ASCENDING); |
| 395 | ExpectSortInverted(); |
| 396 | } |
| 397 | |
| 398 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrl) { |
| 399 | CreateMocks(2); |
| 400 | GURL match_url("http://query.com/query"); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 401 | EXPECT_CALL(mock(0), GetURL()).WillRepeatedly(ReturnRef(match_url)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 402 | GURL fail_url("http://example.com/fail"); |
mharanczyk | 235b8cbc | 2016-07-11 09:05:03 | [diff] [blame] | 403 | EXPECT_CALL(mock(1), GetURL()).WillRepeatedly(ReturnRef(fail_url)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 404 | AddFilter(DownloadQuery::FILTER_URL, match_url.spec().c_str()); |
| 405 | ExpectStandardFilterResults(); |
| 406 | } |
| 407 | |
| 408 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterCallback) { |
| 409 | CreateMocks(2); |
Evan Stade | be3a158 | 2020-12-11 23:08:51 | [diff] [blame] | 410 | CHECK(query()->AddFilter(base::BindRepeating(&IdNotEqual, 1))); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 411 | ExpectStandardFilterResults(); |
| 412 | } |
| 413 | |
| 414 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterBytesReceived) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 415 | CreateMocks(2); |
| 416 | EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0)); |
| 417 | EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1)); |
lazyboy | a61f420b | 2016-06-27 23:48:18 | [diff] [blame] | 418 | AddFilter(DownloadQuery::FILTER_BYTES_RECEIVED, 0.0); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 419 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 420 | } |
| 421 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 422 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortBytesReceived) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 423 | CreateMocks(2); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 424 | EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0)); |
| 425 | EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1)); |
| 426 | query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED, |
| 427 | DownloadQuery::DESCENDING); |
| 428 | ExpectSortInverted(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 429 | } |
| 430 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 431 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDangerAccepted) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 432 | CreateMocks(2); |
Min Qin | 0ca8e1ee | 2018-01-31 00:49:35 | [diff] [blame] | 433 | EXPECT_CALL(mock(0), GetDangerType()) |
| 434 | .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_USER_VALIDATED)); |
| 435 | EXPECT_CALL(mock(1), GetDangerType()) |
| 436 | .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 437 | AddFilter(DownloadQuery::FILTER_DANGER_ACCEPTED, true); |
| 438 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 441 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortDangerAccepted) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 442 | CreateMocks(2); |
Min Qin | 0ca8e1ee | 2018-01-31 00:49:35 | [diff] [blame] | 443 | EXPECT_CALL(mock(0), GetDangerType()) |
| 444 | .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_USER_VALIDATED)); |
| 445 | EXPECT_CALL(mock(1), GetDangerType()) |
| 446 | .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 447 | query()->AddSorter(DownloadQuery::SORT_DANGER_ACCEPTED, |
| 448 | DownloadQuery::ASCENDING); |
| 449 | ExpectSortInverted(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 450 | } |
| 451 | |
[email protected] | 3291a498 | 2013-01-14 00:58:52 | [diff] [blame] | 452 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterExists) { |
| 453 | CreateMocks(2); |
| 454 | EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return( |
| 455 | false)); |
| 456 | EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return( |
| 457 | true)); |
| 458 | AddFilter(DownloadQuery::FILTER_EXISTS, true); |
| 459 | ExpectStandardFilterResults(); |
| 460 | } |
| 461 | |
| 462 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortExists) { |
| 463 | CreateMocks(2); |
| 464 | EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return( |
| 465 | false)); |
| 466 | EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return( |
| 467 | true)); |
| 468 | query()->AddSorter(DownloadQuery::SORT_EXISTS, |
| 469 | DownloadQuery::ASCENDING); |
| 470 | ExpectSortInverted(); |
| 471 | } |
| 472 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 473 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterMime) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 474 | CreateMocks(2); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 475 | EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("text")); |
| 476 | EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("image")); |
| 477 | AddFilter(DownloadQuery::FILTER_MIME, "text"); |
| 478 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 479 | } |
| 480 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 481 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortMime) { |
| 482 | CreateMocks(2); |
| 483 | EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("b")); |
| 484 | EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("a")); |
| 485 | query()->AddSorter(DownloadQuery::SORT_MIME, DownloadQuery::ASCENDING); |
| 486 | ExpectSortInverted(); |
| 487 | } |
| 488 | |
| 489 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterPaused) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 490 | CreateMocks(2); |
| 491 | EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true)); |
| 492 | EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 493 | AddFilter(DownloadQuery::FILTER_PAUSED, true); |
| 494 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 495 | } |
| 496 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 497 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortPaused) { |
[email protected] | d5e60e19 | 2013-01-04 00:09:50 | [diff] [blame] | 498 | CreateMocks(2); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 499 | EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true)); |
| 500 | EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false)); |
| 501 | query()->AddSorter(DownloadQuery::SORT_PAUSED, DownloadQuery::ASCENDING); |
| 502 | ExpectSortInverted(); |
[email protected] | d5e60e19 | 2013-01-04 00:09:50 | [diff] [blame] | 503 | } |
| 504 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 505 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedAfter) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 506 | CreateMocks(2); |
| 507 | EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return( |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 508 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 509 | EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return( |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 510 | base::Time::FromTimeT(kSomeKnownTime + 1))); |
| 511 | AddFilter(DownloadQuery::FILTER_STARTED_AFTER, |
| 512 | std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix)); |
| 513 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 514 | } |
| 515 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 516 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedBefore) { |
| 517 | CreateMocks(2); |
| 518 | EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return( |
| 519 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 520 | EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return( |
| 521 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 522 | AddFilter(DownloadQuery::FILTER_STARTED_BEFORE, |
| 523 | std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix)); |
| 524 | ExpectStandardFilterResults(); |
| 525 | } |
| 526 | |
| 527 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartTime) { |
| 528 | CreateMocks(2); |
| 529 | EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return( |
| 530 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 531 | EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return( |
| 532 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 533 | AddFilter(DownloadQuery::FILTER_START_TIME, |
| 534 | std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix)); |
| 535 | ExpectStandardFilterResults(); |
| 536 | } |
| 537 | |
| 538 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortStartTime) { |
| 539 | CreateMocks(2); |
| 540 | EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return( |
| 541 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 542 | EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return( |
| 543 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 544 | query()->AddSorter(DownloadQuery::SORT_START_TIME, DownloadQuery::DESCENDING); |
| 545 | ExpectSortInverted(); |
| 546 | } |
| 547 | |
| 548 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedAfter) { |
| 549 | CreateMocks(2); |
| 550 | EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return( |
| 551 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 552 | EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return( |
| 553 | base::Time::FromTimeT(kSomeKnownTime + 1))); |
| 554 | AddFilter(DownloadQuery::FILTER_ENDED_AFTER, |
| 555 | std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix)); |
| 556 | ExpectStandardFilterResults(); |
| 557 | } |
| 558 | |
| 559 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedBefore) { |
| 560 | CreateMocks(2); |
| 561 | EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return( |
| 562 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 563 | EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return( |
| 564 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 565 | AddFilter(DownloadQuery::FILTER_ENDED_BEFORE, |
| 566 | std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix)); |
| 567 | ExpectStandardFilterResults(); |
| 568 | } |
| 569 | |
| 570 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndTime) { |
| 571 | CreateMocks(2); |
| 572 | EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return( |
| 573 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 574 | EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return( |
| 575 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 576 | AddFilter(DownloadQuery::FILTER_END_TIME, |
| 577 | std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix)); |
| 578 | ExpectStandardFilterResults(); |
| 579 | } |
| 580 | |
| 581 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortEndTime) { |
| 582 | CreateMocks(2); |
| 583 | EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return( |
| 584 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 585 | EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return( |
| 586 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 587 | query()->AddSorter(DownloadQuery::SORT_END_TIME, DownloadQuery::DESCENDING); |
| 588 | ExpectSortInverted(); |
| 589 | } |
| 590 | |
lazyboy | a61f420b | 2016-06-27 23:48:18 | [diff] [blame] | 591 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater1) { |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 592 | CreateMocks(2); |
| 593 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2)); |
| 594 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(1)); |
lazyboy | a61f420b | 2016-06-27 23:48:18 | [diff] [blame] | 595 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, 1.0); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 596 | ExpectStandardFilterResults(); |
| 597 | } |
| 598 | |
lazyboy | a61f420b | 2016-06-27 23:48:18 | [diff] [blame] | 599 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater2) { |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 600 | CreateMocks(2); |
| 601 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2)); |
lazyboy | a61f420b | 2016-06-27 23:48:18 | [diff] [blame] | 602 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(1)); |
| 603 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, 1.2); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 604 | ExpectStandardFilterResults(); |
| 605 | } |
| 606 | |
lazyboy | a61f420b | 2016-06-27 23:48:18 | [diff] [blame] | 607 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater3) { |
| 608 | CreateMocks(2); |
| 609 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB)); |
| 610 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kEightGB)); |
| 611 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, kNineGBDouble); |
| 612 | ExpectStandardFilterResults(); |
| 613 | } |
| 614 | |
| 615 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater4) { |
| 616 | CreateMocks(2); |
| 617 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB)); |
| 618 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kEightGB)); |
| 619 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, kEightGBDouble + 1.0); |
| 620 | ExpectStandardFilterResults(); |
| 621 | } |
| 622 | |
| 623 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess1) { |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 624 | CreateMocks(2); |
| 625 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2)); |
| 626 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4)); |
lazyboy | a61f420b | 2016-06-27 23:48:18 | [diff] [blame] | 627 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, 4.0); |
| 628 | ExpectStandardFilterResults(); |
| 629 | } |
| 630 | |
| 631 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess2) { |
| 632 | CreateMocks(2); |
| 633 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(1)); |
| 634 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(2)); |
| 635 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, 1.2); |
| 636 | ExpectStandardFilterResults(); |
| 637 | } |
| 638 | |
| 639 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess3) { |
| 640 | CreateMocks(2); |
| 641 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kEightGB)); |
| 642 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB)); |
| 643 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, kEightGBDouble + 1.0); |
| 644 | ExpectStandardFilterResults(); |
| 645 | } |
| 646 | |
| 647 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess4) { |
| 648 | CreateMocks(2); |
| 649 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kEightGB)); |
| 650 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB)); |
| 651 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, kNineGBDouble); |
| 652 | ExpectStandardFilterResults(); |
| 653 | } |
| 654 | |
| 655 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes1) { |
| 656 | CreateMocks(2); |
| 657 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2)); |
| 658 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4)); |
| 659 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, 2.0); |
| 660 | ExpectStandardFilterResults(); |
| 661 | } |
| 662 | |
| 663 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes2) { |
| 664 | CreateMocks(2); |
| 665 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(1)); |
| 666 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(2)); |
| 667 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, 1.0); |
| 668 | ExpectStandardFilterResults(); |
| 669 | } |
| 670 | |
| 671 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes3) { |
| 672 | CreateMocks(2); |
| 673 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(kEightGB)); |
| 674 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(kSixteenGB)); |
| 675 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, kEightGBDouble); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 676 | ExpectStandardFilterResults(); |
| 677 | } |
| 678 | |
| 679 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortTotalBytes) { |
| 680 | CreateMocks(2); |
| 681 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2)); |
| 682 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4)); |
| 683 | query()->AddSorter(DownloadQuery::SORT_TOTAL_BYTES, |
| 684 | DownloadQuery::DESCENDING); |
| 685 | ExpectSortInverted(); |
| 686 | } |
| 687 | |
| 688 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterState) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 689 | CreateMocks(2); |
| 690 | EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return( |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 691 | DownloadItem::IN_PROGRESS)); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 692 | EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return( |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 693 | DownloadItem::CANCELLED)); |
| 694 | query()->AddFilter(DownloadItem::IN_PROGRESS); |
| 695 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 696 | } |
| 697 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 698 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortState) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 699 | CreateMocks(2); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 700 | EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return( |
| 701 | DownloadItem::IN_PROGRESS)); |
| 702 | EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return( |
| 703 | DownloadItem::CANCELLED)); |
| 704 | query()->AddSorter(DownloadQuery::SORT_STATE, DownloadQuery::DESCENDING); |
| 705 | ExpectSortInverted(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 706 | } |
| 707 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 708 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDanger) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 709 | CreateMocks(2); |
Min Qin | 0ca8e1ee | 2018-01-31 00:49:35 | [diff] [blame] | 710 | EXPECT_CALL(mock(0), GetDangerType()) |
| 711 | .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); |
| 712 | EXPECT_CALL(mock(1), GetDangerType()) |
| 713 | .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE)); |
| 714 | query()->AddFilter(download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 715 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 716 | } |
| 717 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 718 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortDanger) { |
| 719 | CreateMocks(2); |
Min Qin | 0ca8e1ee | 2018-01-31 00:49:35 | [diff] [blame] | 720 | EXPECT_CALL(mock(0), GetDangerType()) |
| 721 | .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); |
| 722 | EXPECT_CALL(mock(1), GetDangerType()) |
| 723 | .WillRepeatedly(Return(download::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 724 | query()->AddSorter(DownloadQuery::SORT_DANGER, DownloadQuery::DESCENDING); |
| 725 | ExpectSortInverted(); |
| 726 | } |
| 727 | |
| 728 | TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById1) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 729 | CreateMocks(2); |
| 730 | EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0)); |
| 731 | EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 732 | query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED, |
| 733 | DownloadQuery::ASCENDING); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 734 | Search(); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 735 | ASSERT_EQ(2U, results()->size()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 736 | EXPECT_EQ(0U, results()->at(0)->GetId()); |
| 737 | EXPECT_EQ(1U, results()->at(1)->GetId()); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById2) { |
| 741 | CreateMocks(2); |
| 742 | EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0)); |
| 743 | EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0)); |
| 744 | query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED, |
| 745 | DownloadQuery::DESCENDING); |
| 746 | Search(); |
| 747 | ASSERT_EQ(2U, results()->size()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 748 | EXPECT_EQ(0U, results()->at(0)->GetId()); |
| 749 | EXPECT_EQ(1U, results()->at(1)->GetId()); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | TEST_F(DownloadQueryTest, DownloadQueryFilterPerformance) { |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 753 | static const int kNumItems = 100; |
| 754 | static const int kNumFilters = 100; |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 755 | CreateMocks(kNumItems); |
| 756 | for (size_t i = 0; i < (kNumFilters - 1); ++i) { |
Evan Stade | be3a158 | 2020-12-11 23:08:51 | [diff] [blame] | 757 | query()->AddFilter(base::BindRepeating(&AlwaysReturn, true)); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 758 | } |
Evan Stade | be3a158 | 2020-12-11 23:08:51 | [diff] [blame] | 759 | query()->AddFilter(base::BindRepeating(&AlwaysReturn, false)); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 760 | base::Time start = base::Time::Now(); |
| 761 | Search(); |
| 762 | base::Time end = base::Time::Now(); |
| 763 | double nanos = (end - start).InMillisecondsF() * 1000.0 * 1000.0; |
| 764 | double nanos_per_item = nanos / static_cast<double>(kNumItems); |
| 765 | double nanos_per_item_per_filter = nanos_per_item |
| 766 | / static_cast<double>(kNumFilters); |
| 767 | std::cout << "Search took " << nanos_per_item_per_filter |
| 768 | << " nanoseconds per item per filter.\n"; |
| 769 | } |