[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 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame^] | 5 | #include <stdint.h> |
| 6 | |
| 7 | #include <limits> |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 12 | #include "base/logging.h" |
| 13 | #include "base/memory/scoped_ptr.h" |
| 14 | #include "base/stl_util.h" |
[email protected] | 340f55d7c | 2013-06-10 20:49:11 | [diff] [blame] | 15 | #include "base/strings/string16.h" |
[email protected] | 41a17c5 | 2013-06-28 00:27:53 | [diff] [blame] | 16 | #include "base/time/time.h" |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 17 | #include "base/values.h" |
[email protected] | 0a2bafc | 2012-02-01 22:35:00 | [diff] [blame] | 18 | #include "chrome/browser/download/download_query.h" |
[email protected] | b7d000b | 2012-06-02 22:18:21 | [diff] [blame] | 19 | #include "content/public/test/mock_download_item.h" |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 20 | #include "testing/gmock/include/gmock/gmock.h" |
| 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
| 23 | using ::testing::Return; |
| 24 | using ::testing::ReturnRef; |
| 25 | using ::testing::_; |
| 26 | using base::Time; |
| 27 | using base::Value; |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 28 | using content::DownloadItem; |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 29 | typedef DownloadQuery::DownloadVector DownloadVector; |
| 30 | |
| 31 | namespace { |
| 32 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 33 | static const int kSomeKnownTime = 1355864160; |
| 34 | static const char kSomeKnownTime8601[] = "2012-12-18T20:56:0"; |
| 35 | static const char k8601Suffix[] = ".000Z"; |
| 36 | |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame^] | 37 | bool IdNotEqual(uint32_t not_id, const DownloadItem& item) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 38 | return item.GetId() != not_id; |
| 39 | } |
| 40 | |
| 41 | bool AlwaysReturn(bool result, const DownloadItem& item) { |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | } // anonymous namespace |
| 46 | |
| 47 | class DownloadQueryTest : public testing::Test { |
| 48 | public: |
| 49 | DownloadQueryTest() {} |
| 50 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 51 | ~DownloadQueryTest() override {} |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 52 | |
dcheng | e1bc798 | 2014-10-30 00:32:40 | [diff] [blame] | 53 | void TearDown() override { STLDeleteElements(&mocks_); } |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 54 | |
| 55 | void CreateMocks(int count) { |
| 56 | for (int i = 0; i < count; ++i) { |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 57 | mocks_.push_back(new content::MockDownloadItem()); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 58 | EXPECT_CALL(mock(mocks_.size() - 1), GetId()).WillRepeatedly(Return( |
| 59 | mocks_.size() - 1)); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 63 | content::MockDownloadItem& mock(int index) { return *mocks_[index]; } |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 64 | |
| 65 | DownloadQuery* query() { return &query_; } |
| 66 | |
| 67 | template<typename ValueType> void AddFilter( |
| 68 | DownloadQuery::FilterType name, ValueType value); |
| 69 | |
| 70 | void Search() { |
| 71 | query_.Search(mocks_.begin(), mocks_.end(), &results_); |
| 72 | } |
| 73 | |
| 74 | DownloadVector* results() { return &results_; } |
| 75 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 76 | // Filter tests generally contain 2 items. mock(0) matches the filter, mock(1) |
| 77 | // does not. |
| 78 | void ExpectStandardFilterResults() { |
| 79 | Search(); |
| 80 | ASSERT_EQ(1U, results()->size()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 81 | ASSERT_EQ(0U, results()->at(0)->GetId()); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // If no sorters distinguish between two items, then DownloadQuery sorts by ID |
| 85 | // ascending. In order to test that a sorter distinguishes between two items, |
| 86 | // the sorter must sort them by ID descending. |
| 87 | void ExpectSortInverted() { |
| 88 | Search(); |
| 89 | ASSERT_EQ(2U, results()->size()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 90 | ASSERT_EQ(1U, results()->at(0)->GetId()); |
| 91 | ASSERT_EQ(0U, results()->at(1)->GetId()); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 92 | } |
| 93 | |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 94 | private: |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 95 | std::vector<content::MockDownloadItem*> mocks_; |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 96 | DownloadQuery query_; |
| 97 | DownloadVector results_; |
| 98 | |
| 99 | DISALLOW_COPY_AND_ASSIGN(DownloadQueryTest); |
| 100 | }; |
| 101 | |
| 102 | template<> void DownloadQueryTest::AddFilter( |
| 103 | DownloadQuery::FilterType name, bool cpp_value) { |
[email protected] | 012d131 | 2014-07-17 06:37:40 | [diff] [blame] | 104 | scoped_ptr<base::Value> value(new base::FundamentalValue(cpp_value)); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 105 | CHECK(query_.AddFilter(name, *value.get())); |
| 106 | } |
| 107 | |
| 108 | template<> void DownloadQueryTest::AddFilter( |
| 109 | DownloadQuery::FilterType name, int cpp_value) { |
[email protected] | a7965a4 | 2014-07-22 02:35:56 | [diff] [blame] | 110 | scoped_ptr<base::Value> value(new base::FundamentalValue(cpp_value)); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 111 | CHECK(query_.AddFilter(name, *value.get())); |
| 112 | } |
| 113 | |
| 114 | template<> void DownloadQueryTest::AddFilter( |
| 115 | DownloadQuery::FilterType name, const char* cpp_value) { |
[email protected] | df287e1 | 2014-03-24 18:33:39 | [diff] [blame] | 116 | CHECK(query_.AddFilter(name, base::StringValue(cpp_value))); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | template<> void DownloadQueryTest::AddFilter( |
[email protected] | d5e60e19 | 2013-01-04 00:09:50 | [diff] [blame] | 120 | DownloadQuery::FilterType name, std::string cpp_value) { |
[email protected] | df287e1 | 2014-03-24 18:33:39 | [diff] [blame] | 121 | CHECK(query_.AddFilter(name, base::StringValue(cpp_value))); |
[email protected] | d5e60e19 | 2013-01-04 00:09:50 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | template<> void DownloadQueryTest::AddFilter( |
[email protected] | b6775d78 | 2013-12-25 20:04:53 | [diff] [blame] | 125 | DownloadQuery::FilterType name, const base::char16* cpp_value) { |
[email protected] | df287e1 | 2014-03-24 18:33:39 | [diff] [blame] | 126 | CHECK(query_.AddFilter(name, base::StringValue(cpp_value))); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 129 | template<> void DownloadQueryTest::AddFilter( |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 130 | DownloadQuery::FilterType name, std::vector<base::string16> cpp_value) { |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 131 | scoped_ptr<base::ListValue> list(new base::ListValue()); |
[email protected] | 9692015 | 2013-12-04 21:00:16 | [diff] [blame] | 132 | for (std::vector<base::string16>::const_iterator it = cpp_value.begin(); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 133 | it != cpp_value.end(); ++it) { |
[email protected] | df287e1 | 2014-03-24 18:33:39 | [diff] [blame] | 134 | list->Append(new base::StringValue(*it)); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 135 | } |
| 136 | CHECK(query_.AddFilter(name, *list.get())); |
| 137 | } |
| 138 | |
| 139 | template<> void DownloadQueryTest::AddFilter( |
| 140 | DownloadQuery::FilterType name, std::vector<std::string> cpp_value) { |
| 141 | scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 142 | for (std::vector<std::string>::const_iterator it = cpp_value.begin(); |
| 143 | it != cpp_value.end(); ++it) { |
[email protected] | df287e1 | 2014-03-24 18:33:39 | [diff] [blame] | 144 | list->Append(new base::StringValue(*it)); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 145 | } |
| 146 | CHECK(query_.AddFilter(name, *list.get())); |
| 147 | } |
| 148 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 149 | #if defined(OS_WIN) |
| 150 | template<> void DownloadQueryTest::AddFilter( |
| 151 | DownloadQuery::FilterType name, std::wstring cpp_value) { |
[email protected] | df287e1 | 2014-03-24 18:33:39 | [diff] [blame] | 152 | CHECK(query_.AddFilter(name, base::StringValue(cpp_value))); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 153 | } |
| 154 | #endif |
| 155 | |
| 156 | TEST_F(DownloadQueryTest, DownloadQueryTest_ZeroItems) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 157 | Search(); |
| 158 | EXPECT_EQ(0U, results()->size()); |
| 159 | } |
| 160 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 161 | TEST_F(DownloadQueryTest, DownloadQueryTest_InvalidFilter) { |
[email protected] | a7965a4 | 2014-07-22 02:35:56 | [diff] [blame] | 162 | scoped_ptr<base::Value> value(new base::FundamentalValue(0)); |
avi | d0181f3 | 2015-12-10 19:41:47 | [diff] [blame^] | 163 | EXPECT_FALSE(query()->AddFilter(static_cast<DownloadQuery::FilterType>( |
| 164 | std::numeric_limits<int32_t>::max()), |
| 165 | *value.get())); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 168 | TEST_F(DownloadQueryTest, DownloadQueryTest_EmptyQuery) { |
| 169 | CreateMocks(2); |
| 170 | Search(); |
| 171 | ASSERT_EQ(2U, results()->size()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 172 | ASSERT_EQ(0U, results()->at(0)->GetId()); |
| 173 | ASSERT_EQ(1U, results()->at(1)->GetId()); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | TEST_F(DownloadQueryTest, DownloadQueryTest_Limit) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 177 | CreateMocks(2); |
| 178 | query()->Limit(1); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 179 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 180 | } |
| 181 | |
dbeam | 901ce73 | 2015-10-28 03:26:58 | [diff] [blame] | 182 | TEST_F(DownloadQueryTest, DownloadQueryTest_Skip) { |
| 183 | CreateMocks(2); |
| 184 | query()->Skip(1); |
| 185 | Search(); |
| 186 | EXPECT_EQ(1U, results()->size()); |
| 187 | } |
| 188 | |
| 189 | TEST_F(DownloadQueryTest, DownloadQueryTest_SkipMoreThanExist) { |
| 190 | query()->Skip(20); |
| 191 | Search(); |
| 192 | EXPECT_EQ(0U, results()->size()); |
| 193 | } |
| 194 | |
| 195 | TEST_F(DownloadQueryTest, DownloadQueryTest_LimitSkipAndSort) { |
| 196 | const size_t kNumMocks = 10U; |
| 197 | CreateMocks(kNumMocks); |
| 198 | for (size_t i = 0; i < kNumMocks; ++i) { |
| 199 | EXPECT_CALL(mock(i), GetStartTime()).WillRepeatedly(Return( |
| 200 | base::Time::FromTimeT(kSomeKnownTime + 1000 * i))); |
| 201 | } |
| 202 | query()->AddSorter(DownloadQuery::SORT_START_TIME, DownloadQuery::ASCENDING); |
| 203 | query()->Limit(2); |
| 204 | query()->Skip(5); |
| 205 | Search(); |
| 206 | EXPECT_EQ(2U, results()->size()); |
| 207 | EXPECT_EQ(5U, results()->at(0)->GetId()); |
| 208 | EXPECT_EQ(6U, results()->at(1)->GetId()); |
| 209 | } |
| 210 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 211 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilename) { |
| 212 | CreateMocks(2); |
| 213 | EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return( |
| 214 | static_cast<content::BrowserContext*>(NULL))); |
| 215 | EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return( |
| 216 | static_cast<content::BrowserContext*>(NULL))); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 217 | base::FilePath match_filename(FILE_PATH_LITERAL("query")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 218 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 219 | match_filename)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 220 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 221 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 222 | fail_filename)); |
[email protected] | 9dccd806 | 2012-09-17 17:19:12 | [diff] [blame] | 223 | GURL fail_url("http://example.com/fail"); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 224 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
| 225 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 226 | std::vector<std::string> query_terms; |
| 227 | query_terms.push_back("query"); |
| 228 | AddFilter(DownloadQuery::FILTER_QUERY, query_terms); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 229 | ExpectStandardFilterResults(); |
| 230 | } |
| 231 | |
| 232 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryUrl) { |
| 233 | CreateMocks(2); |
| 234 | EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return( |
| 235 | static_cast<content::BrowserContext*>(NULL))); |
| 236 | EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return( |
| 237 | static_cast<content::BrowserContext*>(NULL))); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 238 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 239 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 240 | fail_filename)); |
| 241 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 242 | fail_filename)); |
[email protected] | 9dccd806 | 2012-09-17 17:19:12 | [diff] [blame] | 243 | GURL match_url("http://query.com/query"); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 244 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url)); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 245 | GURL fail_url("http://example.com/fail"); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 246 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 247 | std::vector<std::string> query_terms; |
| 248 | query_terms.push_back("query"); |
| 249 | AddFilter(DownloadQuery::FILTER_QUERY, query_terms); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 250 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 251 | } |
| 252 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 253 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterGenericQueryFilenameI18N) { |
| 254 | CreateMocks(2); |
| 255 | EXPECT_CALL(mock(0), GetBrowserContext()).WillRepeatedly(Return( |
| 256 | static_cast<content::BrowserContext*>(NULL))); |
| 257 | EXPECT_CALL(mock(1), GetBrowserContext()).WillRepeatedly(Return( |
| 258 | static_cast<content::BrowserContext*>(NULL))); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 259 | const base::FilePath::StringType kTestString( |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 260 | #if defined(OS_POSIX) |
| 261 | "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd" |
| 262 | #elif defined(OS_WIN) |
| 263 | L"/\x4f60\x597d\x4f60\x597d" |
| 264 | #endif |
| 265 | ); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 266 | base::FilePath match_filename(kTestString); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 267 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 268 | match_filename)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 269 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 270 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 271 | fail_filename)); |
| 272 | GURL fail_url("http://example.com/fail"); |
| 273 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
| 274 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
[email protected] | f1d784d6 | 2013-07-28 18:36:09 | [diff] [blame] | 275 | std::vector<base::FilePath::StringType> query_terms; |
| 276 | query_terms.push_back(kTestString); |
| 277 | AddFilter(DownloadQuery::FILTER_QUERY, query_terms); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 278 | ExpectStandardFilterResults(); |
| 279 | } |
| 280 | |
| 281 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilenameRegex) { |
| 282 | CreateMocks(2); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 283 | base::FilePath match_filename(FILE_PATH_LITERAL("query")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 284 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 285 | match_filename)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 286 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 287 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 288 | fail_filename)); |
| 289 | AddFilter(DownloadQuery::FILTER_FILENAME_REGEX, "y"); |
| 290 | ExpectStandardFilterResults(); |
| 291 | } |
| 292 | |
| 293 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortFilename) { |
| 294 | CreateMocks(2); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 295 | base::FilePath b_filename(FILE_PATH_LITERAL("b")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 296 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 297 | b_filename)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 298 | base::FilePath a_filename(FILE_PATH_LITERAL("a")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 299 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 300 | a_filename)); |
| 301 | query()->AddSorter(DownloadQuery::SORT_FILENAME, DownloadQuery::ASCENDING); |
| 302 | ExpectSortInverted(); |
| 303 | } |
| 304 | |
| 305 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterFilename) { |
| 306 | CreateMocks(2); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 307 | base::FilePath match_filename(FILE_PATH_LITERAL("query")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 308 | EXPECT_CALL(mock(0), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 309 | match_filename)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 310 | base::FilePath fail_filename(FILE_PATH_LITERAL("fail")); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 311 | EXPECT_CALL(mock(1), GetTargetFilePath()).WillRepeatedly(ReturnRef( |
| 312 | fail_filename)); |
| 313 | AddFilter(DownloadQuery::FILTER_FILENAME, match_filename.value().c_str()); |
| 314 | ExpectStandardFilterResults(); |
| 315 | } |
| 316 | |
| 317 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrlRegex) { |
| 318 | CreateMocks(2); |
| 319 | GURL match_url("http://query.com/query"); |
| 320 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url)); |
| 321 | GURL fail_url("http://example.com/fail"); |
| 322 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
| 323 | AddFilter(DownloadQuery::FILTER_URL_REGEX, "query"); |
| 324 | ExpectStandardFilterResults(); |
| 325 | } |
| 326 | |
| 327 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortUrl) { |
| 328 | CreateMocks(2); |
| 329 | GURL b_url("http://example.com/b"); |
| 330 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(b_url)); |
| 331 | GURL a_url("http://example.com/a"); |
| 332 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(a_url)); |
| 333 | query()->AddSorter(DownloadQuery::SORT_URL, DownloadQuery::ASCENDING); |
| 334 | ExpectSortInverted(); |
| 335 | } |
| 336 | |
| 337 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterUrl) { |
| 338 | CreateMocks(2); |
| 339 | GURL match_url("http://query.com/query"); |
| 340 | EXPECT_CALL(mock(0), GetOriginalUrl()).WillRepeatedly(ReturnRef(match_url)); |
| 341 | GURL fail_url("http://example.com/fail"); |
| 342 | EXPECT_CALL(mock(1), GetOriginalUrl()).WillRepeatedly(ReturnRef(fail_url)); |
| 343 | AddFilter(DownloadQuery::FILTER_URL, match_url.spec().c_str()); |
| 344 | ExpectStandardFilterResults(); |
| 345 | } |
| 346 | |
| 347 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterCallback) { |
| 348 | CreateMocks(2); |
| 349 | CHECK(query()->AddFilter(base::Bind(&IdNotEqual, 1))); |
| 350 | ExpectStandardFilterResults(); |
| 351 | } |
| 352 | |
| 353 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterBytesReceived) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 354 | CreateMocks(2); |
| 355 | EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0)); |
| 356 | EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 357 | AddFilter(DownloadQuery::FILTER_BYTES_RECEIVED, 0); |
| 358 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 359 | } |
| 360 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 361 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortBytesReceived) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 362 | CreateMocks(2); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 363 | EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0)); |
| 364 | EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(1)); |
| 365 | query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED, |
| 366 | DownloadQuery::DESCENDING); |
| 367 | ExpectSortInverted(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 370 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDangerAccepted) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 371 | CreateMocks(2); |
[email protected] | cda7906 | 2013-01-17 01:50:52 | [diff] [blame] | 372 | EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return( |
| 373 | content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED)); |
| 374 | EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return( |
| 375 | content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 376 | AddFilter(DownloadQuery::FILTER_DANGER_ACCEPTED, true); |
| 377 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 378 | } |
| 379 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 380 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortDangerAccepted) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 381 | CreateMocks(2); |
[email protected] | cda7906 | 2013-01-17 01:50:52 | [diff] [blame] | 382 | EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return( |
| 383 | content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED)); |
| 384 | EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return( |
| 385 | content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 386 | query()->AddSorter(DownloadQuery::SORT_DANGER_ACCEPTED, |
| 387 | DownloadQuery::ASCENDING); |
| 388 | ExpectSortInverted(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 389 | } |
| 390 | |
[email protected] | 3291a498 | 2013-01-14 00:58:52 | [diff] [blame] | 391 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterExists) { |
| 392 | CreateMocks(2); |
| 393 | EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return( |
| 394 | false)); |
| 395 | EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return( |
| 396 | true)); |
| 397 | AddFilter(DownloadQuery::FILTER_EXISTS, true); |
| 398 | ExpectStandardFilterResults(); |
| 399 | } |
| 400 | |
| 401 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortExists) { |
| 402 | CreateMocks(2); |
| 403 | EXPECT_CALL(mock(0), GetFileExternallyRemoved()).WillRepeatedly(Return( |
| 404 | false)); |
| 405 | EXPECT_CALL(mock(1), GetFileExternallyRemoved()).WillRepeatedly(Return( |
| 406 | true)); |
| 407 | query()->AddSorter(DownloadQuery::SORT_EXISTS, |
| 408 | DownloadQuery::ASCENDING); |
| 409 | ExpectSortInverted(); |
| 410 | } |
| 411 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 412 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterMime) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 413 | CreateMocks(2); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 414 | EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("text")); |
| 415 | EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("image")); |
| 416 | AddFilter(DownloadQuery::FILTER_MIME, "text"); |
| 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_SortMime) { |
| 421 | CreateMocks(2); |
| 422 | EXPECT_CALL(mock(0), GetMimeType()).WillRepeatedly(Return("b")); |
| 423 | EXPECT_CALL(mock(1), GetMimeType()).WillRepeatedly(Return("a")); |
| 424 | query()->AddSorter(DownloadQuery::SORT_MIME, DownloadQuery::ASCENDING); |
| 425 | ExpectSortInverted(); |
| 426 | } |
| 427 | |
| 428 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterPaused) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 429 | CreateMocks(2); |
| 430 | EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true)); |
| 431 | EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 432 | AddFilter(DownloadQuery::FILTER_PAUSED, true); |
| 433 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 434 | } |
| 435 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 436 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortPaused) { |
[email protected] | d5e60e19 | 2013-01-04 00:09:50 | [diff] [blame] | 437 | CreateMocks(2); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 438 | EXPECT_CALL(mock(0), IsPaused()).WillRepeatedly(Return(true)); |
| 439 | EXPECT_CALL(mock(1), IsPaused()).WillRepeatedly(Return(false)); |
| 440 | query()->AddSorter(DownloadQuery::SORT_PAUSED, DownloadQuery::ASCENDING); |
| 441 | ExpectSortInverted(); |
[email protected] | d5e60e19 | 2013-01-04 00:09:50 | [diff] [blame] | 442 | } |
| 443 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 444 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedAfter) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 445 | CreateMocks(2); |
| 446 | EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return( |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 447 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 448 | EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return( |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 449 | base::Time::FromTimeT(kSomeKnownTime + 1))); |
| 450 | AddFilter(DownloadQuery::FILTER_STARTED_AFTER, |
| 451 | std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix)); |
| 452 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 453 | } |
| 454 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 455 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartedBefore) { |
| 456 | CreateMocks(2); |
| 457 | EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return( |
| 458 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 459 | EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return( |
| 460 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 461 | AddFilter(DownloadQuery::FILTER_STARTED_BEFORE, |
| 462 | std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix)); |
| 463 | ExpectStandardFilterResults(); |
| 464 | } |
| 465 | |
| 466 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterStartTime) { |
| 467 | CreateMocks(2); |
| 468 | EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return( |
| 469 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 470 | EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return( |
| 471 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 472 | AddFilter(DownloadQuery::FILTER_START_TIME, |
| 473 | std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix)); |
| 474 | ExpectStandardFilterResults(); |
| 475 | } |
| 476 | |
| 477 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortStartTime) { |
| 478 | CreateMocks(2); |
| 479 | EXPECT_CALL(mock(0), GetStartTime()).WillRepeatedly(Return( |
| 480 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 481 | EXPECT_CALL(mock(1), GetStartTime()).WillRepeatedly(Return( |
| 482 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 483 | query()->AddSorter(DownloadQuery::SORT_START_TIME, DownloadQuery::DESCENDING); |
| 484 | ExpectSortInverted(); |
| 485 | } |
| 486 | |
| 487 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedAfter) { |
| 488 | CreateMocks(2); |
| 489 | EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return( |
| 490 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 491 | EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return( |
| 492 | base::Time::FromTimeT(kSomeKnownTime + 1))); |
| 493 | AddFilter(DownloadQuery::FILTER_ENDED_AFTER, |
| 494 | std::string(kSomeKnownTime8601) + "1" + std::string(k8601Suffix)); |
| 495 | ExpectStandardFilterResults(); |
| 496 | } |
| 497 | |
| 498 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndedBefore) { |
| 499 | CreateMocks(2); |
| 500 | EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return( |
| 501 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 502 | EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return( |
| 503 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 504 | AddFilter(DownloadQuery::FILTER_ENDED_BEFORE, |
| 505 | std::string(kSomeKnownTime8601) + "4" + std::string(k8601Suffix)); |
| 506 | ExpectStandardFilterResults(); |
| 507 | } |
| 508 | |
| 509 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterEndTime) { |
| 510 | CreateMocks(2); |
| 511 | EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return( |
| 512 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 513 | EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return( |
| 514 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 515 | AddFilter(DownloadQuery::FILTER_END_TIME, |
| 516 | std::string(kSomeKnownTime8601) + "2" + std::string(k8601Suffix)); |
| 517 | ExpectStandardFilterResults(); |
| 518 | } |
| 519 | |
| 520 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortEndTime) { |
| 521 | CreateMocks(2); |
| 522 | EXPECT_CALL(mock(0), GetEndTime()).WillRepeatedly(Return( |
| 523 | base::Time::FromTimeT(kSomeKnownTime + 2))); |
| 524 | EXPECT_CALL(mock(1), GetEndTime()).WillRepeatedly(Return( |
| 525 | base::Time::FromTimeT(kSomeKnownTime + 4))); |
| 526 | query()->AddSorter(DownloadQuery::SORT_END_TIME, DownloadQuery::DESCENDING); |
| 527 | ExpectSortInverted(); |
| 528 | } |
| 529 | |
| 530 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesGreater) { |
| 531 | CreateMocks(2); |
| 532 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2)); |
| 533 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(1)); |
| 534 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_GREATER, 1); |
| 535 | ExpectStandardFilterResults(); |
| 536 | } |
| 537 | |
| 538 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytesLess) { |
| 539 | CreateMocks(2); |
| 540 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2)); |
| 541 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4)); |
| 542 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES_LESS, 4); |
| 543 | ExpectStandardFilterResults(); |
| 544 | } |
| 545 | |
| 546 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterTotalBytes) { |
| 547 | CreateMocks(2); |
| 548 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2)); |
| 549 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4)); |
| 550 | AddFilter(DownloadQuery::FILTER_TOTAL_BYTES, 2); |
| 551 | ExpectStandardFilterResults(); |
| 552 | } |
| 553 | |
| 554 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortTotalBytes) { |
| 555 | CreateMocks(2); |
| 556 | EXPECT_CALL(mock(0), GetTotalBytes()).WillRepeatedly(Return(2)); |
| 557 | EXPECT_CALL(mock(1), GetTotalBytes()).WillRepeatedly(Return(4)); |
| 558 | query()->AddSorter(DownloadQuery::SORT_TOTAL_BYTES, |
| 559 | DownloadQuery::DESCENDING); |
| 560 | ExpectSortInverted(); |
| 561 | } |
| 562 | |
| 563 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterState) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 564 | CreateMocks(2); |
| 565 | EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return( |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 566 | DownloadItem::IN_PROGRESS)); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 567 | EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return( |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 568 | DownloadItem::CANCELLED)); |
| 569 | query()->AddFilter(DownloadItem::IN_PROGRESS); |
| 570 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 571 | } |
| 572 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 573 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortState) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 574 | CreateMocks(2); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 575 | EXPECT_CALL(mock(0), GetState()).WillRepeatedly(Return( |
| 576 | DownloadItem::IN_PROGRESS)); |
| 577 | EXPECT_CALL(mock(1), GetState()).WillRepeatedly(Return( |
| 578 | DownloadItem::CANCELLED)); |
| 579 | query()->AddSorter(DownloadQuery::SORT_STATE, DownloadQuery::DESCENDING); |
| 580 | ExpectSortInverted(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 581 | } |
| 582 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 583 | TEST_F(DownloadQueryTest, DownloadQueryTest_FilterDanger) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 584 | CreateMocks(2); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 585 | EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return( |
| 586 | content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); |
| 587 | EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return( |
| 588 | content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE)); |
| 589 | query()->AddFilter(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS); |
| 590 | ExpectStandardFilterResults(); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 591 | } |
| 592 | |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 593 | TEST_F(DownloadQueryTest, DownloadQueryTest_SortDanger) { |
| 594 | CreateMocks(2); |
| 595 | EXPECT_CALL(mock(0), GetDangerType()).WillRepeatedly(Return( |
| 596 | content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); |
| 597 | EXPECT_CALL(mock(1), GetDangerType()).WillRepeatedly(Return( |
| 598 | content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE)); |
| 599 | query()->AddSorter(DownloadQuery::SORT_DANGER, DownloadQuery::DESCENDING); |
| 600 | ExpectSortInverted(); |
| 601 | } |
| 602 | |
| 603 | TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById1) { |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 604 | CreateMocks(2); |
| 605 | EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0)); |
| 606 | EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0)); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 607 | query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED, |
| 608 | DownloadQuery::ASCENDING); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 609 | Search(); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 610 | ASSERT_EQ(2U, results()->size()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 611 | EXPECT_EQ(0U, results()->at(0)->GetId()); |
| 612 | EXPECT_EQ(1U, results()->at(1)->GetId()); |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | TEST_F(DownloadQueryTest, DownloadQueryTest_DefaultSortById2) { |
| 616 | CreateMocks(2); |
| 617 | EXPECT_CALL(mock(0), GetReceivedBytes()).WillRepeatedly(Return(0)); |
| 618 | EXPECT_CALL(mock(1), GetReceivedBytes()).WillRepeatedly(Return(0)); |
| 619 | query()->AddSorter(DownloadQuery::SORT_BYTES_RECEIVED, |
| 620 | DownloadQuery::DESCENDING); |
| 621 | Search(); |
| 622 | ASSERT_EQ(2U, results()->size()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 623 | EXPECT_EQ(0U, results()->at(0)->GetId()); |
| 624 | EXPECT_EQ(1U, results()->at(1)->GetId()); |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | TEST_F(DownloadQueryTest, DownloadQueryFilterPerformance) { |
[email protected] | c5899b25 | 2013-01-13 00:42:10 | [diff] [blame] | 628 | static const int kNumItems = 100; |
| 629 | static const int kNumFilters = 100; |
[email protected] | be05221 | 2011-12-14 18:40:40 | [diff] [blame] | 630 | CreateMocks(kNumItems); |
| 631 | for (size_t i = 0; i < (kNumFilters - 1); ++i) { |
| 632 | query()->AddFilter(base::Bind(&AlwaysReturn, true)); |
| 633 | } |
| 634 | query()->AddFilter(base::Bind(&AlwaysReturn, false)); |
| 635 | base::Time start = base::Time::Now(); |
| 636 | Search(); |
| 637 | base::Time end = base::Time::Now(); |
| 638 | double nanos = (end - start).InMillisecondsF() * 1000.0 * 1000.0; |
| 639 | double nanos_per_item = nanos / static_cast<double>(kNumItems); |
| 640 | double nanos_per_item_per_filter = nanos_per_item |
| 641 | / static_cast<double>(kNumFilters); |
| 642 | std::cout << "Search took " << nanos_per_item_per_filter |
| 643 | << " nanoseconds per item per filter.\n"; |
| 644 | } |