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