| Index: content/browser/background_fetch/background_fetch_data_manager_unittest.cc
|
| diff --git a/content/browser/background_fetch/background_fetch_data_manager_unittest.cc b/content/browser/background_fetch/background_fetch_data_manager_unittest.cc
|
| index 02212d597001e0c7c4ef31910deeb262a22cfa11..05dcc3447acb6a820574539e715a1f9f11056406 100644
|
| --- a/content/browser/background_fetch/background_fetch_data_manager_unittest.cc
|
| +++ b/content/browser/background_fetch/background_fetch_data_manager_unittest.cc
|
| @@ -14,12 +14,11 @@
|
| #include "content/browser/background_fetch/background_fetch_job_info.h"
|
| #include "content/browser/background_fetch/background_fetch_job_response_data.h"
|
| #include "content/browser/background_fetch/background_fetch_request_info.h"
|
| +#include "content/browser/background_fetch/background_fetch_test_base.h"
|
| #include "content/public/browser/browser_thread.h"
|
| #include "content/public/browser/download_interrupt_reasons.h"
|
| #include "content/public/browser/download_item.h"
|
| #include "content/public/test/test_browser_context.h"
|
| -#include "content/public/test/test_browser_thread_bundle.h"
|
| -#include "testing/gtest/include/gtest/gtest.h"
|
|
|
| namespace content {
|
| namespace {
|
| @@ -31,7 +30,7 @@ const int64_t kServiceWorkerRegistrationId = 9001;
|
|
|
| } // namespace
|
|
|
| -class BackgroundFetchDataManagerTest : public ::testing::Test {
|
| +class BackgroundFetchDataManagerTest : public BackgroundFetchTestBase {
|
| public:
|
| BackgroundFetchDataManagerTest()
|
| : background_fetch_data_manager_(
|
| @@ -39,8 +38,39 @@ class BackgroundFetchDataManagerTest : public ::testing::Test {
|
| ~BackgroundFetchDataManagerTest() override = default;
|
|
|
| protected:
|
| + // Synchronous version of BackgroundFetchDataManager::CreateRegistration().
|
| + void CreateRegistration(
|
| + const BackgroundFetchRegistrationId& registration_id,
|
| + const std::vector<ServiceWorkerFetchRequest>& requests,
|
| + const BackgroundFetchOptions& options,
|
| + blink::mojom::BackgroundFetchError* out_error) {
|
| + DCHECK(out_error);
|
| +
|
| + base::RunLoop run_loop;
|
| + background_fetch_data_manager_->CreateRegistration(
|
| + registration_id, requests, options,
|
| + base::BindOnce(&BackgroundFetchDataManagerTest::DidCreateRegistration,
|
| + base::Unretained(this), run_loop.QuitClosure(),
|
| + out_error));
|
| + run_loop.Run();
|
| + }
|
| +
|
| + // Synchronous version of BackgroundFetchDataManager::DeleteRegistration().
|
| + void DeleteRegistration(const BackgroundFetchRegistrationId& registration_id,
|
| + blink::mojom::BackgroundFetchError* out_error) {
|
| + DCHECK(out_error);
|
| +
|
| + base::RunLoop run_loop;
|
| + background_fetch_data_manager_->DeleteRegistration(
|
| + registration_id,
|
| + base::BindOnce(&BackgroundFetchDataManagerTest::DidDeleteRegistration,
|
| + base::Unretained(this), run_loop.QuitClosure(),
|
| + out_error));
|
| + run_loop.Run();
|
| + }
|
| +
|
| void CreateRequests(int num_requests) {
|
| - DCHECK(num_requests > 0);
|
| + DCHECK_GT(num_requests, 0);
|
| // Create |num_requests| BackgroundFetchRequestInfo's.
|
| BackgroundFetchRequestInfos request_infos;
|
| for (int i = 0; i < num_requests; i++) {
|
| @@ -50,9 +80,12 @@ class BackgroundFetchDataManagerTest : public ::testing::Test {
|
| std::unique_ptr<BackgroundFetchJobInfo> job_info =
|
| base::MakeUnique<BackgroundFetchJobInfo>(
|
| "tag", url::Origin(GURL(kJobOrigin)), kServiceWorkerRegistrationId);
|
| + job_info->set_num_requests(num_requests);
|
| +
|
| job_guid_ = job_info->guid();
|
| - background_fetch_data_manager_->CreateRequest(std::move(job_info),
|
| - std::move(request_infos));
|
| +
|
| + background_fetch_data_manager_->WriteJobToStorage(std::move(job_info),
|
| + std::move(request_infos));
|
| }
|
|
|
| const std::string& job_guid() const { return job_guid_; }
|
| @@ -61,12 +94,63 @@ class BackgroundFetchDataManagerTest : public ::testing::Test {
|
| }
|
|
|
| private:
|
| + void DidCreateRegistration(base::Closure quit_closure,
|
| + blink::mojom::BackgroundFetchError* out_error,
|
| + blink::mojom::BackgroundFetchError error) {
|
| + *out_error = error;
|
| +
|
| + quit_closure.Run();
|
| + }
|
| +
|
| + void DidDeleteRegistration(base::Closure quit_closure,
|
| + blink::mojom::BackgroundFetchError* out_error,
|
| + blink::mojom::BackgroundFetchError error) {
|
| + *out_error = error;
|
| +
|
| + quit_closure.Run();
|
| + }
|
| +
|
| std::string job_guid_;
|
| TestBrowserContext browser_context_;
|
| std::unique_ptr<BackgroundFetchDataManager> background_fetch_data_manager_;
|
| - TestBrowserThreadBundle thread_bundle_;
|
| };
|
|
|
| +TEST_F(BackgroundFetchDataManagerTest, NoDuplicateRegistrations) {
|
| + // Tests that the BackgroundFetchDataManager correctly rejects creating a
|
| + // registration that's already known to the system.
|
| +
|
| + BackgroundFetchRegistrationId registration_id;
|
| + ASSERT_TRUE(CreateRegistrationId(kTag, ®istration_id));
|
| +
|
| + std::vector<ServiceWorkerFetchRequest> requests;
|
| + BackgroundFetchOptions options;
|
| +
|
| + blink::mojom::BackgroundFetchError error;
|
| +
|
| + // Deleting the not-yet-created registration should fail.
|
| + ASSERT_NO_FATAL_FAILURE(DeleteRegistration(registration_id, &error));
|
| + EXPECT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_TAG);
|
| +
|
| + // Creating the initial registration should succeed.
|
| + ASSERT_NO_FATAL_FAILURE(
|
| + CreateRegistration(registration_id, requests, options, &error));
|
| + EXPECT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
|
| +
|
| + // Attempting to create it again should yield an error.
|
| + ASSERT_NO_FATAL_FAILURE(
|
| + CreateRegistration(registration_id, requests, options, &error));
|
| + EXPECT_EQ(error, blink::mojom::BackgroundFetchError::DUPLICATED_TAG);
|
| +
|
| + // Deleting the registration should succeed.
|
| + ASSERT_NO_FATAL_FAILURE(DeleteRegistration(registration_id, &error));
|
| + EXPECT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
|
| +
|
| + // And then recreating the registration again should work fine.
|
| + ASSERT_NO_FATAL_FAILURE(
|
| + CreateRegistration(registration_id, requests, options, &error));
|
| + EXPECT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
|
| +}
|
| +
|
| TEST_F(BackgroundFetchDataManagerTest, OutOfOrderCompletion) {
|
| CreateRequests(10);
|
| BackgroundFetchDataManager* data_manager = background_fetch_data_manager();
|
|
|