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