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 { cleanup_all_tasks_called_ = true; } |
| 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 bool cleanup_all_tasks_called() const { return cleanup_all_tasks_called_; } |
| 42 |
| 43 private: |
| 44 std::string expected_id_; |
| 45 bool cleanup_all_tasks_called_ = false; |
| 46 }; |
| 47 |
| 48 } // namespace |
| 49 |
| 50 class BackgroundFetchClientTest : public ::testing::Test { |
| 51 public: |
| 52 BackgroundFetchClientTest() {} |
| 53 ~BackgroundFetchClientTest() override {} |
| 54 |
| 55 protected: |
| 56 content::TestBrowserThreadBundle thread_bundle_; |
| 57 |
| 58 TestingProfile profile_; |
| 59 }; |
| 60 |
| 61 TEST_F(BackgroundFetchClientTest, IncognitoCleanupTest) { |
| 62 FakeBackgroundFetchDelegate delegate(kRegistrationId); |
| 63 { |
| 64 TestingProfile profile; |
| 65 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
| 66 profile.GetOffTheRecordProfile()->GetBackgroundFetchClient()); |
| 67 client->SetDelegate(&delegate); |
| 68 // |profile| leaves scope so Shutdown() should be called for the |
| 69 // BackgroundFetchClientImpl created for the incognito profile. |
| 70 } |
| 71 |
| 72 ASSERT_TRUE(delegate.cleanup_all_tasks_called()); |
| 73 } |
| 74 |
| 75 TEST_F(BackgroundFetchClientTest, NonIncognitoCleanupTest) { |
| 76 FakeBackgroundFetchDelegate delegate(kRegistrationId); |
| 77 { |
| 78 TestingProfile profile; |
| 79 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
| 80 profile.GetBackgroundFetchClient()); |
| 81 client->SetDelegate(&delegate); |
| 82 // |profile| leaves scope, so Shutdown() should be called on |client|. |
| 83 } |
| 84 |
| 85 ASSERT_FALSE(delegate.cleanup_all_tasks_called()); |
| 86 } |
| 87 |
| 88 TEST_F(BackgroundFetchClientTest, CancelDownloadTest) { |
| 89 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
| 90 profile_.GetBackgroundFetchClient()); |
| 91 FakeBackgroundFetchDelegate delegate(kRegistrationId); |
| 92 client->SetDelegate(&delegate); |
| 93 |
| 94 offline_items_collection::ContentId id(kNamespace, kRegistrationId); |
| 95 ASSERT_NO_FATAL_FAILURE(client->CancelDownload(id)); |
| 96 |
| 97 client->SetDelegate(nullptr); |
| 98 ASSERT_NO_FATAL_FAILURE(client->CancelDownload(id)); |
| 99 } |
| 100 |
| 101 TEST_F(BackgroundFetchClientTest, PauseDownloadTest) { |
| 102 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
| 103 profile_.GetBackgroundFetchClient()); |
| 104 FakeBackgroundFetchDelegate delegate(kRegistrationId); |
| 105 client->SetDelegate(&delegate); |
| 106 |
| 107 offline_items_collection::ContentId id(kNamespace, kRegistrationId); |
| 108 ASSERT_NO_FATAL_FAILURE(client->PauseDownload(id)); |
| 109 |
| 110 client->SetDelegate(nullptr); |
| 111 ASSERT_NO_FATAL_FAILURE(client->PauseDownload(id)); |
| 112 } |
| 113 |
| 114 TEST_F(BackgroundFetchClientTest, ResumeDownloadTest) { |
| 115 BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
| 116 profile_.GetBackgroundFetchClient()); |
| 117 FakeBackgroundFetchDelegate delegate(kRegistrationId); |
| 118 client->SetDelegate(&delegate); |
| 119 |
| 120 offline_items_collection::ContentId id(kNamespace, kRegistrationId); |
| 121 ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id)); |
| 122 |
| 123 client->SetDelegate(nullptr); |
| 124 ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id)); |
| 125 } |
OLD | NEW |