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