| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_snippets/ntp_snippets_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 12 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 14 #include "components/ntp_snippets/ntp_snippet.h" | 15 #include "components/ntp_snippets/ntp_snippet.h" |
| 15 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 16 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 16 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 17 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 17 #include "components/prefs/testing_pref_service.h" | 18 #include "components/prefs/testing_pref_service.h" |
| 18 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 " { \"contentInfo\": { \"url\" : \"http://second\" }}" | 250 " { \"contentInfo\": { \"url\" : \"http://second\" }}" |
| 250 "]}"); | 251 "]}"); |
| 251 LoadFromJSONString(json_str2); | 252 LoadFromJSONString(json_str2); |
| 252 ASSERT_EQ(service()->size(), 2u); | 253 ASSERT_EQ(service()->size(), 2u); |
| 253 | 254 |
| 254 // The snippet loaded last should be at the first position in the list now. | 255 // The snippet loaded last should be at the first position in the list now. |
| 255 const NTPSnippet& first_snippet = *service()->begin(); | 256 const NTPSnippet& first_snippet = *service()->begin(); |
| 256 EXPECT_EQ(first_snippet.url(), GURL("http://second")); | 257 EXPECT_EQ(first_snippet.url(), GURL("http://second")); |
| 257 } | 258 } |
| 258 | 259 |
| 260 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { |
| 261 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); |
| 262 int snippets_per_load = max_snippet_count / 2 + 1; |
| 263 |
| 264 const char snippet_format[] = |
| 265 "{ \"contentInfo\": { \"url\" : \"http://localhost/%i\" }}"; |
| 266 std::vector<std::string> snippets1; |
| 267 std::vector<std::string> snippets2; |
| 268 for (int i = 0; i < snippets_per_load; i++) { |
| 269 snippets1.push_back(base::StringPrintf(snippet_format, i)); |
| 270 snippets2.push_back(base::StringPrintf(snippet_format, |
| 271 snippets_per_load + i)); |
| 272 } |
| 273 |
| 274 LoadFromJSONString( |
| 275 "{ \"recos\": [ " + base::JoinString(snippets1, ", ") + "]}"); |
| 276 ASSERT_EQ(snippets1.size(), service()->size()); |
| 277 |
| 278 LoadFromJSONString( |
| 279 "{ \"recos\": [ " + base::JoinString(snippets2, ", ") + "]}"); |
| 280 EXPECT_EQ(max_snippet_count, (int)service()->size()); |
| 281 } |
| 282 |
| 259 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { | 283 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { |
| 260 SetExpectJsonParseSuccess(false); | 284 SetExpectJsonParseSuccess(false); |
| 261 LoadFromJSONString(GetInvalidJson()); | 285 LoadFromJSONString(GetInvalidJson()); |
| 262 EXPECT_EQ(service()->size(), 0u); | 286 EXPECT_EQ(service()->size(), 0u); |
| 263 } | 287 } |
| 264 | 288 |
| 265 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { | 289 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { |
| 266 LoadFromJSONString(GetTestJson()); | 290 LoadFromJSONString(GetTestJson()); |
| 267 ASSERT_EQ(service()->size(), 1u); | 291 ASSERT_EQ(service()->size(), 1u); |
| 268 | 292 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 } | 377 } |
| 354 | 378 |
| 355 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { | 379 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { |
| 356 std::string json_str(GetTestExpiredJson()); | 380 std::string json_str(GetTestExpiredJson()); |
| 357 | 381 |
| 358 LoadFromJSONString(json_str); | 382 LoadFromJSONString(json_str); |
| 359 EXPECT_EQ(service()->size(), 0u); | 383 EXPECT_EQ(service()->size(), 0u); |
| 360 } | 384 } |
| 361 | 385 |
| 362 } // namespace ntp_snippets | 386 } // namespace ntp_snippets |
| OLD | NEW |