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

content / browser / background_sync / background_sync_base_browsertest.h [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.

#ifndef CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_BASE_BROWSERTEST_H_
#define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_BASE_BROWSERTEST_H_

#include <memory>
#include <string>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "content/browser/background_sync/background_sync_context_impl.h"
#include "content/browser/background_sync/background_sync_status.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/public/browser/background_sync_parameters.h"
#include "content/public/browser/background_sync_registration.h"
#include "content/public/test/content_browser_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/blink/public/common/service_worker/service_worker_status_code.h"

namespace {

const char kDefaultTestURL[] = "/background_sync/test.html";
const char kEmptyURL[] = "/background_sync/empty.html";
const char kRegisterSyncFromIFrameURL[] =
    "/background_sync/register_sync_from_iframe.html";
const char kRegisterPeriodicSyncFromIFrameURL[] =
    "/background_sync/register_periodicsync_from_iframe.html";
const char kRegisterSyncFromSWURL[] =
    "/background_sync/register_sync_from_sw.html";
const char kSuccessfulOperationPrefix[] = "ok - ";

}  // namespace

namespace content {

class Shell;
class StoragePartitionImpl;
class WebContents;
struct EvalJsResult;

class BackgroundSyncBaseBrowserTest : public ContentBrowserTest {
 public:
  BackgroundSyncBaseBrowserTest();
  ~BackgroundSyncBaseBrowserTest() override;

  void SetUp() override;
  void SetUpOnMainThread() override;
  void TearDownOnMainThread() override;

 protected:
  std::string BuildScriptString(const std::string& function,
                                const std::string& argument);
  std::string BuildExpectedResult(const std::string& tag,
                                  const std::string& action);

  // Returns true if the registration with tag |tag| is currently pending. Fails
  // (assertion failure) if the tag isn't registered.
  bool RegistrationPending(const std::string& tag);

  void CompleteDelayedSyncEvent();

  void SetTestClock(base::SimpleTestClock* clock);

  void ClearStoragePartitionData();

  EvalJsResult PopConsoleString();
  void RegisterServiceWorker();
  void SetIncognitoMode(bool incognito);
  WebContents* web_contents();
  void LoadTestPage(const std::string& path);
  net::EmbeddedTestServer* https_server() { return https_server_.get(); }

 private:
  void RegistrationPendingCallback(
      base::OnceClosure quit,
      const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
      bool* result_out,
      bool result);
  void RegistrationPendingDidGetSyncRegistration(
      const std::string& tag,
      base::OnceCallback<void(bool)> callback,
      BackgroundSyncStatus error_type,
      std::vector<std::unique_ptr<BackgroundSyncRegistration>> registrations);
  void RegistrationPendingDidGetSWRegistration(
      const scoped_refptr<BackgroundSyncContextImpl> sync_context,
      const std::string& tag,
      base::OnceCallback<void(bool)> callback,
      blink::ServiceWorkerStatusCode status,
      scoped_refptr<ServiceWorkerRegistration> registration);
  void RegistrationPendingOnCoreThread(
      const scoped_refptr<BackgroundSyncContextImpl> sync_context,
      const scoped_refptr<ServiceWorkerContextWrapper> sw_context,
      const std::string& tag,
      const GURL& url,
      base::OnceCallback<void(bool)> callback);
  StoragePartitionImpl* GetStorage();

  raw_ptr<Shell, DanglingUntriaged> shell_ = nullptr;
  std::unique_ptr<net::EmbeddedTestServer> https_server_;
  base::test::ScopedFeatureList scoped_feature_list_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_BASE_BROWSERTEST_H_