Commit ca7bda88 authored by Istiaque Ahmed's avatar Istiaque Ahmed Committed by Commit Bot

Cleanup: Construct TestRegistrationObserver with BrowserContext.

This CL changes the constructor to take BrowserContext instead of
ServiceWorkerContext. Typically, BrowserContext (Profile) is
readily available in tests, so it makes the test code using the
observer a bit straightforward to read.

Bug: None
Change-Id: Ife88eaf1d82708c9675f6d917b59a51d8777bdd3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2432806Reviewed-by: default avatarDavid Bertoni <dbertoni@chromium.org>
Commit-Queue: Istiaque Ahmed <lazyboy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810862}
parent 72d19b28
......@@ -1650,13 +1650,9 @@ class TestWorkerObserver : public content::ServiceWorkerContextObserver {
IN_PROC_BROWSER_TEST_F(ServiceWorkerBasedBackgroundTest,
EventsToStoppedWorker) {
content::StoragePartition* storage_partition =
content::BrowserContext::GetDefaultStoragePartition(browser()->profile());
content::ServiceWorkerContext* context =
storage_partition->GetServiceWorkerContext();
// Set up an observer to wait for the registration to be stored.
service_worker_test_utils::TestRegistrationObserver observer(context);
service_worker_test_utils::TestRegistrationObserver observer(
browser()->profile());
ExtensionTestMessageListener event_listener_added("ready", false);
event_listener_added.set_failure_message("ERROR");
......@@ -1674,6 +1670,10 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerBasedBackgroundTest,
// Stop the service worker.
{
base::RunLoop run_loop;
content::ServiceWorkerContext* context =
content::BrowserContext::GetDefaultStoragePartition(
browser()->profile())
->GetServiceWorkerContext();
// The service worker is registered at the root scope.
content::StopServiceWorkerForScope(context, extension->url(),
run_loop.QuitClosure());
......@@ -1913,15 +1913,11 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerBasedBackgroundTest,
// Tests the restriction on registering service worker scripts at root scope.
IN_PROC_BROWSER_TEST_F(ServiceWorkerBasedBackgroundTest,
ServiceWorkerScriptRootScope) {
content::StoragePartition* storage_partition =
content::BrowserContext::GetDefaultStoragePartition(browser()->profile());
content::ServiceWorkerContext* context =
storage_partition->GetServiceWorkerContext();
// Set up an observer to track all SW registrations. We expect only
// one for the extension's root scope. This test attempts to register
// an additional service worker, which will fail.
service_worker_test_utils::TestRegistrationObserver observer(context);
service_worker_test_utils::TestRegistrationObserver observer(
browser()->profile());
ExtensionTestMessageListener registration_listener("REGISTRATION_FAILED",
false);
registration_listener.set_failure_message("WORKER_STARTED");
......@@ -2207,13 +2203,9 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerBasedBackgroundTest, TabsOnCreated) {
#endif
IN_PROC_BROWSER_TEST_F(ServiceWorkerBasedBackgroundTest,
MAYBE_PRE_FilteredEventsAfterRestart) {
content::StoragePartition* storage_partition =
content::BrowserContext::GetDefaultStoragePartition(browser()->profile());
content::ServiceWorkerContext* context =
storage_partition->GetServiceWorkerContext();
// Set up an observer to wait for the registration to be stored.
service_worker_test_utils::TestRegistrationObserver observer(context);
service_worker_test_utils::TestRegistrationObserver observer(
browser()->profile());
ExtensionTestMessageListener listener_added("ready", false);
base::FilePath test_dir =
......
......@@ -8,7 +8,6 @@
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/service_worker_test_helpers.h"
#include "extensions/browser/service_worker/service_worker_test_utils.h"
......@@ -340,11 +339,8 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerMessagingTest,
// stopping the service worker doesn't cause message port in
// |message_port_extension| to crash.
ExtensionTestMessageListener worker_running_listener("worker_running", false);
content::ServiceWorkerContext* service_worker_context =
content::BrowserContext::GetDefaultStoragePartition(browser()->profile())
->GetServiceWorkerContext();
service_worker_test_utils::TestRegistrationObserver registration_observer(
service_worker_context);
browser()->profile());
TestExtensionDir worker_extension_dir;
const Extension* service_worker_extension =
......
......@@ -4,6 +4,7 @@
#include "extensions/browser/service_worker/service_worker_test_utils.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/service_worker_context.h"
#include "extensions/common/constants.h"
......@@ -13,8 +14,10 @@ namespace service_worker_test_utils {
// TestRegistrationObserver ----------------------------------------------------
TestRegistrationObserver::TestRegistrationObserver(
content::ServiceWorkerContext* context)
: context_(context) {
content::BrowserContext* browser_context)
: context_(
content::BrowserContext::GetDefaultStoragePartition(browser_context)
->GetServiceWorkerContext()) {
context_->AddObserver(this);
}
......
......@@ -8,6 +8,7 @@
#include "base/run_loop.h"
#include "base/scoped_observer.h"
#include "content/public/browser/service_worker_context_observer.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/process_manager_observer.h"
#include "extensions/common/extension_id.h"
......@@ -15,6 +16,7 @@
namespace content {
class ServiceWorkerContext;
class BrowserContext;
}
namespace extensions {
......@@ -25,7 +27,7 @@ class TestRegistrationObserver : public content::ServiceWorkerContextObserver {
public:
using RegistrationsMap = std::map<GURL, int>;
explicit TestRegistrationObserver(content::ServiceWorkerContext* context);
explicit TestRegistrationObserver(content::BrowserContext* browser_context);
~TestRegistrationObserver() override;
TestRegistrationObserver(const TestRegistrationObserver&) = delete;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment