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..580067989013d6d05a5d74a3d5ff200e44a8f7a5 |
--- /dev/null |
+++ b/chrome/browser/background_fetch/background_fetch_client_impl_unittest.cc |
@@ -0,0 +1,103 @@ |
+// 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/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 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_); |
+ } |
+ |
+ private: |
+ std::string expected_id_; |
+}; |
+ |
+class BackgroundFetchTestingProfile : public TestingProfile { |
+ public: |
+ BackgroundFetchTestingProfile() {} |
+ ~BackgroundFetchTestingProfile() override {} |
+ |
+ BackgroundFetchClientImpl* GetBackgroundFetchClient() override { |
+ return BackgroundFetchClientFactory::GetForProfile(this); |
Peter Beverloo
2017/03/31 11:54:06
What do you think about just calling the factory i
harkness
2017/04/03 12:47:22
Done.
|
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(BackgroundFetchTestingProfile); |
+}; |
+ |
+} // namespace |
+ |
+class BackgroundFetchClientTest : public ::testing::Test { |
+ public: |
+ BackgroundFetchClientTest() {} |
+ ~BackgroundFetchClientTest() override {} |
+ |
+ private: |
+ content::TestBrowserThreadBundle thread_bundle_; |
+ |
+ protected: |
+ BackgroundFetchTestingProfile profile_; |
Peter Beverloo
2017/03/31 11:54:06
style: Protected before private. Just make both pr
harkness
2017/04/03 12:47:22
Done.
|
+}; |
+ |
+TEST_F(BackgroundFetchClientTest, CancelDownloadTest) { |
+ BackgroundFetchClientImpl* client = 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 = 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 = 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)); |
+} |