1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210

content / browser / background_sync / background_sync_service_impl_test_harness.cc [blame]

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/background_sync/background_sync_service_impl_test_harness.h"

#include <stdint.h>
#include <utility>

#include "base/check_deref.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "content/browser/background_sync/background_sync_manager.h"
#include "content/browser/background_sync/background_sync_network_observer.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/background_sync_test_util.h"
#include "content/public/test/mock_permission_manager.h"
#include "content/public/test/test_browser_context.h"
#include "content/test/storage_partition_test_helpers.h"
#include "content/test/test_background_sync_context.h"
#include "mojo/public/cpp/system/functions.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"

namespace content {

using ::testing::_;

const char kServiceWorkerScope[] = "https://example.com/a/";
const char kServiceWorkerScript[] = "https://example.com/a/script.js";

void BackgroundSyncServiceImplTestHarness::RegisterServiceWorkerCallback(
    bool* called,
    int64_t* out_registration_id,
    blink::ServiceWorkerStatusCode status,
    const std::string& status_message,
    int64_t registration_id) {
  EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk, status)
      << blink::ServiceWorkerStatusToString(status);
  *called = true;
  *out_registration_id = registration_id;
}

void BackgroundSyncServiceImplTestHarness::
    FindServiceWorkerRegistrationCallback(
        scoped_refptr<ServiceWorkerRegistration>* out_registration,
        blink::ServiceWorkerStatusCode status,
        scoped_refptr<ServiceWorkerRegistration> registration) {
  EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk, status)
      << blink::ServiceWorkerStatusToString(status);
  *out_registration = std::move(registration);
}

void BackgroundSyncServiceImplTestHarness::ErrorAndRegistrationCallback(
    bool* called,
    blink::mojom::BackgroundSyncError* out_error,
    blink::mojom::SyncRegistrationOptionsPtr* out_registration,
    blink::mojom::BackgroundSyncError error,
    blink::mojom::SyncRegistrationOptionsPtr registration) {
  *called = true;
  *out_error = error;
  *out_registration = registration.Clone();
}

void BackgroundSyncServiceImplTestHarness::ErrorAndRegistrationListCallback(
    bool* called,
    blink::mojom::BackgroundSyncError* out_error,
    unsigned long* out_array_size,
    blink::mojom::BackgroundSyncError error,
    std::vector<blink::mojom::SyncRegistrationOptionsPtr> registrations) {
  *called = true;
  *out_error = error;
  if (error == blink::mojom::BackgroundSyncError::NONE)
    *out_array_size = registrations.size();
}

void BackgroundSyncServiceImplTestHarness::ErrorCallback(
    bool* called,
    blink::mojom::BackgroundSyncError* out_error,
    blink::mojom::BackgroundSyncError error) {
  *called = true;
  *out_error = error;
}

BackgroundSyncServiceImplTestHarness::BackgroundSyncServiceImplTestHarness()
    : task_environment_(BrowserTaskEnvironment::IO_MAINLOOP) {
  default_sync_registration_ = blink::mojom::SyncRegistrationOptions::New();
}

BackgroundSyncServiceImplTestHarness::~BackgroundSyncServiceImplTestHarness() =
    default;

void BackgroundSyncServiceImplTestHarness::SetUp() {
  // Don't let the tests be confused by the real-world device connectivity
  background_sync_test_util::SetIgnoreNetworkChanges(true);

  mojo::SetDefaultProcessErrorHandler(base::BindRepeating(
      &BackgroundSyncServiceImplTestHarness::CollectMojoError,
      base::Unretained(this)));

  CreateTestHelper();
  CreateStoragePartition();
  CreateBackgroundSyncContext();
  ASSERT_NO_FATAL_FAILURE(CreateServiceWorkerRegistration());
}

void BackgroundSyncServiceImplTestHarness::CollectMojoError(
    const std::string& message) {
  mojo_bad_messages_.push_back(message);
}

void BackgroundSyncServiceImplTestHarness::TearDown() {
  // This must be explicitly destroyed here to ensure that destruction
  // of both the BackgroundSyncContextImpl and the BackgroundSyncManager
  // occurs on the correct thread.
  background_sync_context_->Shutdown();
  base::RunLoop().RunUntilIdle();
  background_sync_context_ = nullptr;

  // Restore the network observer functionality for subsequent tests
  background_sync_test_util::SetIgnoreNetworkChanges(false);

  mojo::SetDefaultProcessErrorHandler(base::NullCallback());

  storage_partition_impl_->OnBrowserContextWillBeDestroyed();
}

// SetUp helper methods
void BackgroundSyncServiceImplTestHarness::CreateTestHelper() {
  embedded_worker_helper_ =
      std::make_unique<EmbeddedWorkerTestHelper>((base::FilePath()));
  std::unique_ptr<MockPermissionManager> mock_permission_manager =
      std::make_unique<testing::NiceMock<MockPermissionManager>>();
  ON_CALL(*mock_permission_manager,
          GetPermissionStatus(blink::PermissionType::BACKGROUND_SYNC, _, _))
      .WillByDefault(testing::Return(blink::mojom::PermissionStatus::GRANTED));
  TestBrowserContext::FromBrowserContext(
      embedded_worker_helper_->browser_context())
      ->SetPermissionControllerDelegate(std::move(mock_permission_manager));
}

void BackgroundSyncServiceImplTestHarness::CreateStoragePartition() {
  // Creates a StoragePartition so that the BackgroundSyncManager can
  // use it to access the BrowserContext.
  storage_partition_impl_ = StoragePartitionImpl::Create(
      embedded_worker_helper_->browser_context(),
      CreateStoragePartitionConfigForTesting(/*in_memory=*/true),
      base::FilePath() /* relative_partition_path */);
  storage_partition_impl_->Initialize();
  embedded_worker_helper_->context_wrapper()->set_storage_partition(
      storage_partition_impl_.get());
}

void BackgroundSyncServiceImplTestHarness::CreateBackgroundSyncContext() {
  // Registering for background sync includes a check for having a same-origin
  // main frame. Use a test context that allows control over that check.
  background_sync_context_ = base::MakeRefCounted<TestBackgroundSyncContext>();
  background_sync_context_->Init(
      embedded_worker_helper_->context_wrapper(),
      CHECK_DEREF(static_cast<DevToolsBackgroundServicesContextImpl*>(
          storage_partition_impl_->GetDevToolsBackgroundServicesContext())));

  // Tests do not expect the sync event to fire immediately after
  // register (and cleanup up the sync registrations).  Prevent the sync
  // event from firing by setting the network state to have no connection.
  // NOTE: The setup of the network connection must happen after the
  //       BackgroundSyncManager has been setup, including any asynchronous
  //       initialization.
  base::RunLoop().RunUntilIdle();
  BackgroundSyncNetworkObserver* network_observer =
      background_sync_context_->background_sync_manager()
          ->GetNetworkObserverForTesting();
  network_observer->NotifyManagerIfConnectionChangedForTesting(
      network::mojom::ConnectionType::CONNECTION_NONE);
  base::RunLoop().RunUntilIdle();
}

void BackgroundSyncServiceImplTestHarness::CreateServiceWorkerRegistration() {
  bool called = false;
  blink::mojom::ServiceWorkerRegistrationOptions options;
  options.scope = GURL(kServiceWorkerScope);
  const blink::StorageKey key =
      blink::StorageKey::CreateFromStringForTesting(kServiceWorkerScope);
  embedded_worker_helper_->context()->RegisterServiceWorker(
      GURL(kServiceWorkerScript), key, options,
      blink::mojom::FetchClientSettingsObject::New(),
      base::BindOnce(&RegisterServiceWorkerCallback, &called,
                     &sw_registration_id_),
      /*requesting_frame_id=*/GlobalRenderFrameHostId(),
      PolicyContainerPolicies());
  base::RunLoop().RunUntilIdle();
  ASSERT_TRUE(called);

  embedded_worker_helper_->context_wrapper()->FindReadyRegistrationForId(
      sw_registration_id_,
      blink::StorageKey::CreateFromStringForTesting(kServiceWorkerScope),
      base::BindOnce(FindServiceWorkerRegistrationCallback, &sw_registration_));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(sw_registration_);
}

BrowserContext* BackgroundSyncServiceImplTestHarness::browser_context() {
  return embedded_worker_helper_->browser_context();
}

}  // namespace content