OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/background_fetch/background_fetch_client_impl.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "chrome/browser/background_fetch/background_fetch_client_factory.h" |
| 10 #include "chrome/test/base/testing_profile.h" |
| 11 #include "components/offline_items_collection/core/offline_item.h" |
| 12 #include "content/public/browser/background_fetch_client.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 const char kRegistrationId[] = "1234:www.example.com:game_data"; |
| 19 const char kNamespace[] = "BackgroundFetchNamespace"; |
| 20 |
| 21 class FakeBackgroundFetchDelegate |
| 22 : public content::BackgroundFetchClient::Delegate { |
| 23 public: |
| 24 explicit FakeBackgroundFetchDelegate(const std::string& id) |
| 25 : expected_id_(id) {} |
| 26 |
| 27 void CleanupAllTasks() override {} |
| 28 |
| 29 void CancelDownload(const std::string& registration_id) override { |
| 30 ASSERT_EQ(registration_id, expected_id_); |
| 31 } |
| 32 |
| 33 void PauseDownload(const std::string& registration_id) override { |
| 34 ASSERT_EQ(registration_id, expected_id_); |
| 35 } |
| 36 |
| 37 void ResumeDownload(const std::string& registration_id) override { |
| 38 ASSERT_EQ(registration_id, expected_id_); |
| 39 } |
| 40 |
| 41 private: |
| 42 std::string expected_id_; |
| 43 }; |
| 44 |
| 45 } // namespace |
| 46 |
| 47 class BackgroundFetchClientTest : public ::testing::Test { |
| 48 public: |
| 49 BackgroundFetchClientTest() {} |
| 50 ~BackgroundFetchClientTest() override {} |
| 51 |
| 52 protected: |
| 53 content::TestBrowserThreadBundle thread_bundle_; |
| 54 |
| 55 TestingProfile profile_; |
| 56 }; |
| 57 |
| 58 TEST_F(BackgroundFetchClientTest, CancelDownloadTest) { |
| 59 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
| 60 profile_.GetBackgroundFetchClient()); |
| 61 FakeBackgroundFetchDelegate delegate(kRegistrationId); |
| 62 client->SetDelegate(&delegate); |
| 63 |
| 64 offline_items_collection::ContentId id(kNamespace, kRegistrationId); |
| 65 ASSERT_NO_FATAL_FAILURE(client->CancelDownload(id)); |
| 66 |
| 67 client->SetDelegate(nullptr); |
| 68 ASSERT_NO_FATAL_FAILURE(client->CancelDownload(id)); |
| 69 } |
| 70 |
| 71 TEST_F(BackgroundFetchClientTest, PauseDownloadTest) { |
| 72 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
| 73 profile_.GetBackgroundFetchClient()); |
| 74 FakeBackgroundFetchDelegate delegate(kRegistrationId); |
| 75 client->SetDelegate(&delegate); |
| 76 |
| 77 offline_items_collection::ContentId id(kNamespace, kRegistrationId); |
| 78 ASSERT_NO_FATAL_FAILURE(client->PauseDownload(id)); |
| 79 |
| 80 client->SetDelegate(nullptr); |
| 81 ASSERT_NO_FATAL_FAILURE(client->PauseDownload(id)); |
| 82 } |
| 83 |
| 84 TEST_F(BackgroundFetchClientTest, ResumeDownloadTest) { |
| 85 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
| 86 profile_.GetBackgroundFetchClient()); |
| 87 FakeBackgroundFetchDelegate delegate(kRegistrationId); |
| 88 client->SetDelegate(&delegate); |
| 89 |
| 90 offline_items_collection::ContentId id(kNamespace, kRegistrationId); |
| 91 ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id)); |
| 92 |
| 93 client->SetDelegate(nullptr); |
| 94 ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id)); |
| 95 } |
OLD | NEW |