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

content / browser / background_sync / periodic_background_sync_service_impl_unittest.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/periodic_background_sync_service_impl.h"

#include "base/memory/raw_ptr.h"
#include "content/browser/background_sync/background_sync_service_impl_test_harness.h"
#include "content/public/test/mock_render_process_host.h"

namespace content {

class PeriodicBackgroundSyncServiceImplTest
    : public BackgroundSyncServiceImplTestHarness {
 public:
  PeriodicBackgroundSyncServiceImplTest() = default;

  void SetUp() override {
    BackgroundSyncServiceImplTestHarness::SetUp();
    CreatePeriodicBackgroundSyncServiceImpl();
  }
  void TearDown() override {
    periodic_sync_service_impl_ = nullptr;
    BackgroundSyncServiceImplTestHarness::TearDown();
  }

 protected:
  void CreatePeriodicBackgroundSyncServiceImpl() {
    render_process_host_ =
        std::make_unique<MockRenderProcessHost>(browser_context());

    // Create a dummy mojo channel so that the PeriodicBackgroundSyncServiceImpl
    // can be instantiated.
    mojo::PendingReceiver<blink::mojom::PeriodicBackgroundSyncService>
        receiver = periodic_sync_service_remote_.BindNewPipeAndPassReceiver();
    // Create a new PeriodicBackgroundSyncServiceImpl bound to the dummy
    // channel.
    background_sync_context_->CreatePeriodicSyncService(
        url::Origin::Create(GURL(kServiceWorkerOrigin)),
        render_process_host_.get(), std::move(receiver));
    base::RunLoop().RunUntilIdle();

    // Since |background_sync_context_| is deleted after
    // PeriodicBackgroundSyncServiceImplTest is, this is safe.
    periodic_sync_service_impl_ =
        background_sync_context_->periodic_sync_services_.begin()->get();
    ASSERT_TRUE(periodic_sync_service_impl_);
  }

  // Helpers for testing *BackgroundSyncServiceImpl methods
  void RegisterPeriodicSync(
      blink::mojom::SyncRegistrationOptionsPtr sync,
      blink::mojom::PeriodicBackgroundSyncService::RegisterCallback callback) {
    periodic_sync_service_impl_->Register(std::move(sync), sw_registration_id_,
                                          std::move(callback));
    base::RunLoop().RunUntilIdle();
  }

  void UnregisterPeriodicSync(
      const std::string& tag,
      blink::mojom::PeriodicBackgroundSyncService::UnregisterCallback
          callback) {
    periodic_sync_service_impl_->Unregister(sw_registration_id_, tag,
                                            std::move(callback));
    base::RunLoop().RunUntilIdle();
  }

  void GetPeriodicSyncRegistrations(
      blink::mojom::PeriodicBackgroundSyncService::GetRegistrationsCallback
          callback) {
    periodic_sync_service_impl_->GetRegistrations(sw_registration_id_,
                                                  std::move(callback));
    base::RunLoop().RunUntilIdle();
  }

  std::unique_ptr<MockRenderProcessHost> render_process_host_;
  mojo::Remote<blink::mojom::PeriodicBackgroundSyncService>
      periodic_sync_service_remote_;

  // Owned by |background_sync_context_|
  raw_ptr<PeriodicBackgroundSyncServiceImpl> periodic_sync_service_impl_;
};

// Tests

TEST_F(PeriodicBackgroundSyncServiceImplTest, RegisterPeriodicSync) {
  bool called = false;
  blink::mojom::BackgroundSyncError error;
  blink::mojom::SyncRegistrationOptionsPtr reg;
  auto to_register = default_sync_registration_.Clone();
  to_register->min_interval = 3600;
  RegisterPeriodicSync(
      std::move(to_register),
      base::BindOnce(&ErrorAndRegistrationCallback, &called, &error, ®));
  ASSERT_TRUE(called);
  EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error);
  EXPECT_EQ("", reg->tag);
  EXPECT_EQ(3600, reg->min_interval);
}

TEST_F(PeriodicBackgroundSyncServiceImplTest, RegisterWithInvalidMinInterval) {
  bool called = false;
  blink::mojom::BackgroundSyncError error;
  blink::mojom::SyncRegistrationOptionsPtr reg;
  auto to_register = default_sync_registration_.Clone();
  to_register->min_interval = -1;

  mojo::FakeMessageDispatchContext fake_dispatch_context;
  RegisterPeriodicSync(
      std::move(to_register),
      base::BindOnce(&ErrorAndRegistrationCallback, &called, &error, ®));
  ASSERT_TRUE(called);
  EXPECT_EQ(mojo_bad_messages_.size(), 1u);
}

TEST_F(PeriodicBackgroundSyncServiceImplTest,
       GetPeriodicSyncRegistrationsNoSyncRegistered) {
  bool called = false;
  blink::mojom::BackgroundSyncError error;
  unsigned long array_size = 0UL;
  GetPeriodicSyncRegistrations(base::BindOnce(&ErrorAndRegistrationListCallback,
                                              &called, &error, &array_size));
  ASSERT_TRUE(called);
  EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error);
  EXPECT_EQ(0UL, array_size);
}

TEST_F(PeriodicBackgroundSyncServiceImplTest,
       GetPeriodicSyncRegistrationsWithRegisteredSync) {
  {
    bool called = false;
    blink::mojom::BackgroundSyncError error;
    blink::mojom::SyncRegistrationOptionsPtr registered_reg;

    auto to_register = default_sync_registration_.Clone();
    to_register->min_interval = 3600;

    RegisterPeriodicSync(std::move(to_register),
                         base::BindOnce(&ErrorAndRegistrationCallback, &called,
                                        &error, ®istered_reg));
    ASSERT_TRUE(called);
    EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error);
  }

  {
    bool called = false;
    blink::mojom::BackgroundSyncError error;
    unsigned long array_size = 0UL;
    GetPeriodicSyncRegistrations(base::BindOnce(
        &ErrorAndRegistrationListCallback, &called, &error, &array_size));
    ASSERT_TRUE(called);
    EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error);
    EXPECT_EQ(1UL, array_size);
  }
}

TEST_F(PeriodicBackgroundSyncServiceImplTest, Unregister) {
  {
    bool called = false;
    blink::mojom::BackgroundSyncError error;

    UnregisterPeriodicSync("non_existent",
                           base::BindOnce(&ErrorCallback, &called, &error));
    ASSERT_TRUE(called);
    EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error);
  }

  {
    bool called = false;
    blink::mojom::BackgroundSyncError error;
    blink::mojom::SyncRegistrationOptionsPtr reg;
    auto to_register = default_sync_registration_.Clone();
    to_register->tag = "shared_tag";
    to_register->min_interval = 3600;
    RegisterPeriodicSync(
        std::move(to_register),
        base::BindOnce(&ErrorAndRegistrationCallback, &called, &error, ®));
    ASSERT_TRUE(called);
    EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error);
    EXPECT_EQ("shared_tag", reg->tag);
    EXPECT_EQ(3600, reg->min_interval);
  }
  {
    bool called = false;
    blink::mojom::BackgroundSyncError error;

    UnregisterPeriodicSync("shared_tag",
                           base::BindOnce(&ErrorCallback, &called, &error));
    ASSERT_TRUE(called);
    EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error);
  }
  {
    bool called = false;
    blink::mojom::BackgroundSyncError error;
    unsigned long array_size = 0UL;
    GetPeriodicSyncRegistrations(base::BindOnce(
        &ErrorAndRegistrationListCallback, &called, &error, &array_size));
    ASSERT_TRUE(called);
    EXPECT_EQ(blink::mojom::BackgroundSyncError::NONE, error);
    EXPECT_EQ(0UL, array_size);
  }
}

}  // namespace content