Commit 639ad08e authored by Jenny Blessing's avatar Jenny Blessing Committed by Commit Bot

Add browser test util file to reduce code duplication

This specifically reduces duplication between
ClearSiteDataHandlerBrowserTest and SameSiteDataRemoverBrowserTest
related to creating and retrieving service workers.

Bug: 987177
Change-Id: Iaa5f772e61e0ca73cceaff56fdaf9c370390dfeb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1751202
Commit-Queue: Jenny Blessing <jblessing@google.com>
Reviewed-by: default avatarChristian Dullweber <dullweber@chromium.org>
Reviewed-by: default avatarMartin Šrámek <msramek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#690775}
parent 74b13158
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/browsing_data/browsing_data_browsertest_utils.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/run_loop.h"
#include "base/task/post_task.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/browser/browsing_data/browsing_data_test_utils.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/storage_usage_info.h"
#include "content/public/browser/system_connector.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/network_service_util.h"
#include "content/public/common/service_names.mojom.h"
#include "content/public/test/content_browser_test_utils.h"
#include "net/base/url_util.h"
#include "net/test/embedded_test_server/http_response.h"
#include "services/network/public/mojom/network_service_test.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace browsing_data_browsertest_utils {
namespace {
void AddServiceWorkerCallback(bool success) {
ASSERT_TRUE(success);
}
void GetServiceWorkersCallback(
base::OnceClosure callback,
std::vector<StorageUsageInfo>* out_service_workers,
const std::vector<StorageUsageInfo>& service_workers) {
*out_service_workers = service_workers;
std::move(callback).Run();
}
} // namespace
void ServiceWorkerActivationObserver::SignalActivation(
ServiceWorkerContextWrapper* context,
const base::Closure& callback) {
new ServiceWorkerActivationObserver(context, callback);
}
ServiceWorkerActivationObserver::ServiceWorkerActivationObserver(
ServiceWorkerContextWrapper* context,
const base::Closure& callback)
: context_(context), scoped_observer_(this), callback_(callback) {
scoped_observer_.Add(context);
}
ServiceWorkerActivationObserver::~ServiceWorkerActivationObserver() {}
void ServiceWorkerActivationObserver::OnVersionStateChanged(
int64_t version_id,
const GURL& scope,
ServiceWorkerVersion::Status) {
if (context_->GetLiveVersion(version_id)->status() ==
ServiceWorkerVersion::ACTIVATED) {
callback_.Run();
delete this;
}
}
void SetIgnoreCertificateErrors(base::CommandLine* command_line) {
if (IsOutOfProcessNetworkService()) {
// |MockCertVerifier| only seems to work when Network Service was enabled.
command_line->AppendSwitch(switches::kUseMockCertVerifierForTesting);
} else {
// We're redirecting all hosts to localhost even on HTTPS, so we'll get
// certificate errors.
command_line->AppendSwitch(switches::kIgnoreCertificateErrors);
}
}
void AddServiceWorker(const std::string& origin,
StoragePartition* storage_partition,
net::EmbeddedTestServer* https_server) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
ServiceWorkerContextWrapper* service_worker_context =
static_cast<ServiceWorkerContextWrapper*>(
storage_partition->GetServiceWorkerContext());
GURL scope_url = https_server->GetURL(origin, "/");
GURL js_url = https_server->GetURL(origin, "/?file=worker.js");
// Register the worker.
blink::mojom::ServiceWorkerRegistrationOptions options(
scope_url, blink::mojom::ScriptType::kClassic,
blink::mojom::ServiceWorkerUpdateViaCache::kImports);
base::PostTask(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&ServiceWorkerContextWrapper::RegisterServiceWorker,
base::Unretained(service_worker_context), js_url, options,
base::Bind(&AddServiceWorkerCallback)));
// Wait for its activation.
base::RunLoop run_loop;
base::PostTask(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&ServiceWorkerActivationObserver::SignalActivation,
base::Unretained(service_worker_context),
run_loop.QuitClosure()));
run_loop.Run();
}
std::vector<StorageUsageInfo> GetServiceWorkers(
StoragePartition* storage_partition) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
ServiceWorkerContextWrapper* service_worker_context =
static_cast<ServiceWorkerContextWrapper*>(
storage_partition->GetServiceWorkerContext());
std::vector<StorageUsageInfo> service_workers;
base::RunLoop run_loop;
base::PostTask(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(
&ServiceWorkerContextWrapper::GetAllOriginsInfo,
base::Unretained(service_worker_context),
base::Bind(&GetServiceWorkersCallback, run_loop.QuitClosure(),
base::Unretained(&service_workers))));
run_loop.Run();
return service_workers;
}
void SetResponseContent(const GURL& url,
std::string* value,
net::test_server::BasicHttpResponse* response) {
if (net::GetValueForKeyInQuery(url, "file", value)) {
base::FilePath path(GetTestFilePath("browsing_data", value->c_str()));
base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
EXPECT_TRUE(file.IsValid());
int64_t length = file.GetLength();
EXPECT_GE(length, 0);
std::unique_ptr<char[]> buffer(new char[length + 1]);
file.Read(0, buffer.get(), length);
buffer[length] = '\0';
if (path.Extension() == FILE_PATH_LITERAL(".js"))
response->set_content_type("application/javascript");
else if (path.Extension() == FILE_PATH_LITERAL(".html"))
response->set_content_type("text/html");
else
NOTREACHED();
response->set_content(buffer.get());
}
}
void SetUpMockCertVerifier(int32_t default_result) {
network::mojom::NetworkServiceTestPtr network_service_test;
GetSystemConnector()->BindInterface(mojom::kNetworkServiceName,
&network_service_test);
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
network_service_test->MockCertVerifierSetDefaultResult(
default_result, run_loop.QuitClosure());
run_loop.Run();
}
} // namespace browsing_data_browsertest_utils
} // namespace content
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_BROWSING_DATA_BROWSING_DATA_BROWSERTEST_UTILS_H_
#define CONTENT_BROWSER_BROWSING_DATA_BROWSING_DATA_BROWSERTEST_UTILS_H_
#include <string>
#include <vector>
#include "base/scoped_observer.h"
#include "content/browser/service_worker/service_worker_context_core_observer.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_response.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"
namespace content {
class StoragePartition;
namespace browsing_data_browsertest_utils {
// TODO(msramek): A class like this already exists in ServiceWorkerBrowserTest.
// Consider extracting it to a different test utils file.
class ServiceWorkerActivationObserver
: public ServiceWorkerContextCoreObserver {
public:
// |callback| is called when |context| is activated.
static void SignalActivation(ServiceWorkerContextWrapper* context,
const base::Closure& callback);
private:
ServiceWorkerActivationObserver(ServiceWorkerContextWrapper* context,
const base::Closure& callback);
~ServiceWorkerActivationObserver() override;
// ServiceWorkerContextCoreObserver overrides.
void OnVersionStateChanged(int64_t version_id,
const GURL& scope,
ServiceWorkerVersion::Status) override;
ServiceWorkerContextWrapper* context_;
ScopedObserver<ServiceWorkerContextWrapper, ServiceWorkerContextCoreObserver>
scoped_observer_;
base::Closure callback_;
};
// Appends a switch to the |command_line| based on whether the network service
// is enabled. The browser will ignore certificate errors if the network service
// is not enabled.
void SetIgnoreCertificateErrors(base::CommandLine* command_line);
// Adds a service worker for the given |origin|. The EmbeddedTestServer
// |https_server| is required to retrieve a URL to the server based on the
// |origin|.
void AddServiceWorker(const std::string& origin,
StoragePartition* storage_partition,
net::EmbeddedTestServer* https_server);
// Retrieves the list of all service workers.
std::vector<StorageUsageInfo> GetServiceWorkers(
StoragePartition* storage_partition);
// Populates the content and content type fields of a given HTTP |response|
// based on the file extension of the |url| as follows:
//
// For .js:
// Example: "https://localhost/?file=file.js"
// will set the response header as
// Content-Type: application/javascript
//
// For .html:
// Example: "https://localhost/?file=file.html"
// will set the response header as
// Content-Type: text/html
//
// Response content type is only set for .js and .html files.
void SetResponseContent(const GURL& url,
std::string* value,
net::test_server::BasicHttpResponse* response);
// Sets up a MockCertVerifier with default return value |default_result|.
void SetUpMockCertVerifier(int32_t default_result);
} // namespace browsing_data_browsertest_utils
} // namespace content
#endif // CONTENT_BROWSER_BROWSING_DATA_BROWSING_DATA_BROWSERTEST_UTILS_H_
...@@ -593,6 +593,8 @@ jumbo_static_library("browsertest_support") { ...@@ -593,6 +593,8 @@ jumbo_static_library("browsertest_support") {
} }
sources = [ sources = [
"../browser/browsing_data/browsing_data_browsertest_utils.cc",
"../browser/browsing_data/browsing_data_browsertest_utils.h",
"../public/test/content_browser_test.cc", "../public/test/content_browser_test.cc",
"../public/test/content_browser_test.h", "../public/test/content_browser_test.h",
"../public/test/content_browser_test_utils.cc", "../public/test/content_browser_test_utils.cc",
...@@ -611,6 +613,7 @@ jumbo_static_library("browsertest_support") { ...@@ -611,6 +613,7 @@ jumbo_static_library("browsertest_support") {
"//base:i18n", "//base:i18n",
"//base/test:test_config", "//base/test:test_config",
"//base/test:test_support", "//base/test:test_support",
"//components/network_session_configurator/common:common",
"//components/viz/service", "//components/viz/service",
"//content/app:both_for_content_tests", "//content/app:both_for_content_tests",
"//content/browser:for_content_tests", "//content/browser:for_content_tests",
......
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