Index: chrome/browser/background_fetch/background_fetch_client_impl_unittest.cc |
diff --git a/chrome/browser/background_fetch/background_fetch_client_impl_unittest.cc b/chrome/browser/background_fetch/background_fetch_client_impl_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a563c535b6eb3853b392bada24983bd0f1bc2c4b |
--- /dev/null |
+++ b/chrome/browser/background_fetch/background_fetch_client_impl_unittest.cc |
@@ -0,0 +1,125 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/background_fetch/background_fetch_client_impl.h" |
+ |
+#include "base/macros.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+#include "chrome/browser/background_fetch/background_fetch_client_factory.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include "components/offline_items_collection/core/offline_item.h" |
+#include "content/public/browser/background_fetch_client.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+const char kRegistrationId[] = "1234:www.example.com:game_data"; |
+const char kNamespace[] = "BackgroundFetchNamespace"; |
+ |
+class FakeBackgroundFetchDelegate |
+ : public content::BackgroundFetchClient::Delegate { |
+ public: |
+ explicit FakeBackgroundFetchDelegate(const std::string& id) |
+ : expected_id_(id) {} |
+ |
+ void CleanupAllTasks() override { cleanup_all_tasks_called_ = true; } |
+ |
+ void CancelDownload(const std::string& registration_id) override { |
+ ASSERT_EQ(registration_id, expected_id_); |
+ } |
+ |
+ void PauseDownload(const std::string& registration_id) override { |
+ ASSERT_EQ(registration_id, expected_id_); |
+ } |
+ |
+ void ResumeDownload(const std::string& registration_id) override { |
+ ASSERT_EQ(registration_id, expected_id_); |
+ } |
+ |
+ bool cleanup_all_tasks_called() const { return cleanup_all_tasks_called_; } |
+ |
+ private: |
+ std::string expected_id_; |
+ bool cleanup_all_tasks_called_ = false; |
+}; |
+ |
+} // namespace |
+ |
+class BackgroundFetchClientTest : public ::testing::Test { |
+ public: |
+ BackgroundFetchClientTest() {} |
+ ~BackgroundFetchClientTest() override {} |
+ |
+ protected: |
+ content::TestBrowserThreadBundle thread_bundle_; |
+ |
+ TestingProfile profile_; |
+}; |
+ |
+TEST_F(BackgroundFetchClientTest, IncognitoCleanupTest) { |
+ FakeBackgroundFetchDelegate delegate(kRegistrationId); |
+ { |
+ TestingProfile profile; |
+ BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
+ profile.GetOffTheRecordProfile()->GetBackgroundFetchClient()); |
+ client->SetDelegate(&delegate); |
+ // |profile| leaves scope so Shutdown() should be called for the |
+ // BackgroundFetchClientImpl created for the incognito profile. |
+ } |
+ |
+ ASSERT_TRUE(delegate.cleanup_all_tasks_called()); |
+} |
+ |
+TEST_F(BackgroundFetchClientTest, NonIncognitoCleanupTest) { |
+ FakeBackgroundFetchDelegate delegate(kRegistrationId); |
+ { |
+ TestingProfile profile; |
+ BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
+ profile.GetBackgroundFetchClient()); |
+ client->SetDelegate(&delegate); |
+ // |profile| leaves scope, so Shutdown() should be called on |client|. |
+ } |
+ |
+ ASSERT_FALSE(delegate.cleanup_all_tasks_called()); |
+} |
+ |
+TEST_F(BackgroundFetchClientTest, CancelDownloadTest) { |
+ BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
+ profile_.GetBackgroundFetchClient()); |
+ FakeBackgroundFetchDelegate delegate(kRegistrationId); |
+ client->SetDelegate(&delegate); |
+ |
+ offline_items_collection::ContentId id(kNamespace, kRegistrationId); |
+ ASSERT_NO_FATAL_FAILURE(client->CancelDownload(id)); |
+ |
+ client->SetDelegate(nullptr); |
+ ASSERT_NO_FATAL_FAILURE(client->CancelDownload(id)); |
+} |
+ |
+TEST_F(BackgroundFetchClientTest, PauseDownloadTest) { |
+ BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
+ profile_.GetBackgroundFetchClient()); |
+ FakeBackgroundFetchDelegate delegate(kRegistrationId); |
+ client->SetDelegate(&delegate); |
+ |
+ offline_items_collection::ContentId id(kNamespace, kRegistrationId); |
+ ASSERT_NO_FATAL_FAILURE(client->PauseDownload(id)); |
+ |
+ client->SetDelegate(nullptr); |
+ ASSERT_NO_FATAL_FAILURE(client->PauseDownload(id)); |
+} |
+ |
+TEST_F(BackgroundFetchClientTest, ResumeDownloadTest) { |
+ BackgroundFetchClientImpl* client = static_cast<BackgroundFetchClientImpl*>( |
+ profile_.GetBackgroundFetchClient()); |
+ FakeBackgroundFetchDelegate delegate(kRegistrationId); |
+ client->SetDelegate(&delegate); |
+ |
+ offline_items_collection::ContentId id(kNamespace, kRegistrationId); |
+ ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id)); |
+ |
+ client->SetDelegate(nullptr); |
+ ASSERT_NO_FATAL_FAILURE(client->ResumeDownload(id)); |
+} |