OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/doodle/doodle_service.h" | 5 #include "components/doodle/doodle_service.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 } | 48 } |
49 | 49 |
50 private: | 50 private: |
51 std::vector<FinishedCallback> callbacks_; | 51 std::vector<FinishedCallback> callbacks_; |
52 }; | 52 }; |
53 | 53 |
54 class MockDoodleObserver : public DoodleService::Observer { | 54 class MockDoodleObserver : public DoodleService::Observer { |
55 public: | 55 public: |
56 MOCK_METHOD1(OnDoodleConfigUpdated, | 56 MOCK_METHOD1(OnDoodleConfigUpdated, |
57 void(const base::Optional<DoodleConfig>&)); | 57 void(const base::Optional<DoodleConfig>&)); |
| 58 MOCK_METHOD1(OnDoodleConfigRevalidated, void(bool)); |
58 }; | 59 }; |
59 | 60 |
60 DoodleConfig CreateConfig(DoodleType type) { | 61 DoodleConfig CreateConfig(DoodleType type) { |
61 return DoodleConfig(type, DoodleImage(GURL("https://doodle.com/image.jpg"))); | 62 return DoodleConfig(type, DoodleImage(GURL("https://doodle.com/image.jpg"))); |
62 } | 63 } |
63 | 64 |
64 } // namespace | 65 } // namespace |
65 | 66 |
66 class DoodleServiceTest : public testing::Test { | 67 class DoodleServiceTest : public testing::Test { |
67 public: | 68 public: |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 DoodleConfig other_config = CreateConfig(DoodleType::SLIDESHOW); | 303 DoodleConfig other_config = CreateConfig(DoodleType::SLIDESHOW); |
303 DCHECK(config != other_config); | 304 DCHECK(config != other_config); |
304 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(other_config))); | 305 EXPECT_CALL(observer, OnDoodleConfigUpdated(Eq(other_config))); |
305 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, | 306 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, |
306 base::TimeDelta::FromHours(1), other_config); | 307 base::TimeDelta::FromHours(1), other_config); |
307 | 308 |
308 // Remove the observer before the service gets destroyed. | 309 // Remove the observer before the service gets destroyed. |
309 service()->RemoveObserver(&observer); | 310 service()->RemoveObserver(&observer); |
310 } | 311 } |
311 | 312 |
312 TEST_F(DoodleServiceTest, DoesNotCallObserverIfConfigEquivalent) { | 313 TEST_F(DoodleServiceTest, CallsObserverIfConfigRevalidatedByNetworkRequest) { |
313 // Load some doodle config. | 314 // Load some doodle config. |
314 service()->Refresh(); | 315 service()->Refresh(); |
315 DoodleConfig config = CreateConfig(DoodleType::SIMPLE); | 316 DoodleConfig config = CreateConfig(DoodleType::SIMPLE); |
316 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, | 317 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, |
317 base::TimeDelta::FromHours(1), config); | 318 base::TimeDelta::FromHours(1), config); |
318 ASSERT_THAT(service()->config(), Eq(config)); | 319 ASSERT_THAT(service()->config(), Eq(config)); |
319 | 320 |
320 // Register an observer and request a refresh. | 321 // Let some time pass (more than the refresh interval). |
| 322 task_runner()->FastForwardBy(base::TimeDelta::FromMinutes(16)); |
| 323 |
| 324 // Register an observer and request a refresh after refresh intervall passed. |
321 StrictMock<MockDoodleObserver> observer; | 325 StrictMock<MockDoodleObserver> observer; |
| 326 EXPECT_CALL(observer, OnDoodleConfigRevalidated(Eq(/*from_cache=*/false))); |
322 service()->AddObserver(&observer); | 327 service()->AddObserver(&observer); |
323 service()->Refresh(); | 328 service()->Refresh(); |
324 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); | 329 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(1u)); |
325 | 330 |
326 // Serve the request with an equivalent doodle config. The observer should | 331 // Serve the request with an equivalent doodle config. The observer should |
327 // *not* get notified. | 332 // get notified about a (non-cached) revalidation. |
328 DoodleConfig equivalent_config = CreateConfig(DoodleType::SIMPLE); | 333 DoodleConfig equivalent_config = CreateConfig(DoodleType::SIMPLE); |
329 DCHECK(config == equivalent_config); | 334 DCHECK(config == equivalent_config); |
330 fetcher()->ServeAllCallbacks( | 335 fetcher()->ServeAllCallbacks( |
331 DoodleState::AVAILABLE, base::TimeDelta::FromHours(1), equivalent_config); | 336 DoodleState::AVAILABLE, base::TimeDelta::FromHours(1), equivalent_config); |
332 | 337 |
333 // Remove the observer before the service gets destroyed. | 338 // Remove the observer before the service gets destroyed. |
334 service()->RemoveObserver(&observer); | 339 service()->RemoveObserver(&observer); |
335 } | 340 } |
336 | 341 |
| 342 TEST_F(DoodleServiceTest, CallsObserverIfConfigRevalidatedByCache) { |
| 343 // Create a service with the default refresh interval. |
| 344 RecreateService(/*min_refresh_interval=*/base::nullopt); |
| 345 |
| 346 // Load some doodle config. |
| 347 service()->Refresh(); |
| 348 DoodleConfig config = CreateConfig(DoodleType::SIMPLE); |
| 349 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, |
| 350 base::TimeDelta::FromHours(1), config); |
| 351 ASSERT_THAT(service()->config(), Eq(config)); |
| 352 |
| 353 // Register an observer and request a refresh within refresh intervall. |
| 354 StrictMock<MockDoodleObserver> observer; |
| 355 EXPECT_CALL(observer, OnDoodleConfigRevalidated(Eq(/*from_cache=*/true))); |
| 356 service()->AddObserver(&observer); |
| 357 service()->Refresh(); |
| 358 ASSERT_THAT(fetcher()->num_pending_callbacks(), Eq(0u)); |
| 359 |
| 360 // Remove the observer before the service gets destroyed. |
| 361 service()->RemoveObserver(&observer); |
| 362 } |
| 363 |
337 TEST_F(DoodleServiceTest, CallsObserverWhenConfigExpires) { | 364 TEST_F(DoodleServiceTest, CallsObserverWhenConfigExpires) { |
338 // Load some doodle config. | 365 // Load some doodle config. |
339 service()->Refresh(); | 366 service()->Refresh(); |
340 DoodleConfig config = CreateConfig(DoodleType::SIMPLE); | 367 DoodleConfig config = CreateConfig(DoodleType::SIMPLE); |
341 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, | 368 fetcher()->ServeAllCallbacks(DoodleState::AVAILABLE, |
342 base::TimeDelta::FromHours(1), config); | 369 base::TimeDelta::FromHours(1), config); |
343 ASSERT_THAT(service()->config(), Eq(config)); | 370 ASSERT_THAT(service()->config(), Eq(config)); |
344 | 371 |
345 // Make sure the task arrived at the timer's task runner. | 372 // Make sure the task arrived at the timer's task runner. |
346 EXPECT_THAT(task_runner()->GetPendingTaskCount(), Eq(1u)); | 373 EXPECT_THAT(task_runner()->GetPendingTaskCount(), Eq(1u)); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 // Fast-forward time so that the config expires. | 556 // Fast-forward time so that the config expires. |
530 task_runner()->FastForwardBy(base::TimeDelta::FromHours(1)); | 557 task_runner()->FastForwardBy(base::TimeDelta::FromHours(1)); |
531 ASSERT_THAT(service()->config(), Eq(base::nullopt)); | 558 ASSERT_THAT(service()->config(), Eq(base::nullopt)); |
532 | 559 |
533 // This should not have resulted in any metrics being emitted. | 560 // This should not have resulted in any metrics being emitted. |
534 histograms.ExpectTotalCount("Doodle.ConfigDownloadOutcome", 0); | 561 histograms.ExpectTotalCount("Doodle.ConfigDownloadOutcome", 0); |
535 histograms.ExpectTotalCount("Doodle.ConfigDownloadTime", 0); | 562 histograms.ExpectTotalCount("Doodle.ConfigDownloadTime", 0); |
536 } | 563 } |
537 | 564 |
538 } // namespace doodle | 565 } // namespace doodle |
OLD | NEW |