Commit f55562d6 authored by Kenichi Ishibashi's avatar Kenichi Ishibashi Committed by Commit Bot

service worker: Remove direct use of old resource reader

This removes direct use of ServiceWorkerResponseReader from tests.
Tests use the mojo version instead.

Bug: 1055677
Change-Id: Id1d955a88d76b515c5e2be9ed0b8bd12dbade814
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2302978
Commit-Queue: Kenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#789846}
parent 87fe6786
...@@ -367,7 +367,7 @@ TEST_F(ServiceWorkerInstalledScriptsSenderTest, FailedToSendMetaData) { ...@@ -367,7 +367,7 @@ TEST_F(ServiceWorkerInstalledScriptsSenderTest, FailedToSendMetaData) {
TEST_F(ServiceWorkerInstalledScriptsSenderTest, Histograms) { TEST_F(ServiceWorkerInstalledScriptsSenderTest, Histograms) {
const GURL kMainScriptURL = version()->script_url(); const GURL kMainScriptURL = version()->script_url();
// Use script bodies small enough to be read by one // Use script bodies small enough to be read by one
// ServiceWorkerResponseReader::ReadData(). The number of // ServiceWorkerResourceReader::ReadData(). The number of
// ServiceWorker.DiskCache.ReadResponseResult will be two per script (one is // ServiceWorker.DiskCache.ReadResponseResult will be two per script (one is
// reading the body and the other is saying EOD). // reading the body and the other is saying EOD).
std::map<GURL, ExpectedScriptInfo> kExpectedScriptInfoMap = { std::map<GURL, ExpectedScriptInfo> kExpectedScriptInfoMap = {
......
...@@ -239,7 +239,7 @@ class ServiceWorkerNewScriptLoaderTest : public testing::Test { ...@@ -239,7 +239,7 @@ class ServiceWorkerNewScriptLoaderTest : public testing::Test {
// Returns false if the entry for |url| doesn't exist in the storage. // Returns false if the entry for |url| doesn't exist in the storage.
bool VerifyStoredResponse(const GURL& url) { bool VerifyStoredResponse(const GURL& url) {
return ServiceWorkerUpdateCheckTestUtils::VerifyStoredResponse( return ServiceWorkerUpdateCheckTestUtils::VerifyStoredResponse(
LookupResourceId(url), context()->storage(), LookupResourceId(url), context()->GetStorageControl(),
mock_server_.Get(url).body); mock_server_.Get(url).body);
} }
......
...@@ -142,7 +142,7 @@ class ServiceWorkerScriptLoaderFactoryCopyResumeTest ...@@ -142,7 +142,7 @@ class ServiceWorkerScriptLoaderFactoryCopyResumeTest
// The response should also be stored in the storage. // The response should also be stored in the storage.
EXPECT_TRUE(ServiceWorkerUpdateCheckTestUtils::VerifyStoredResponse( EXPECT_TRUE(ServiceWorkerUpdateCheckTestUtils::VerifyStoredResponse(
version_->script_cache_map()->LookupResourceId(script_url_), version_->script_cache_map()->LookupResourceId(script_url_),
helper_->context()->storage(), expected_body)); helper_->context()->GetStorageControl(), expected_body));
EXPECT_TRUE(client_.has_received_response()); EXPECT_TRUE(client_.has_received_response());
EXPECT_TRUE(client_.response_body().is_valid()); EXPECT_TRUE(client_.response_body().is_valid());
......
...@@ -100,37 +100,6 @@ ReadResponseHeadResult ReadResponseHead( ...@@ -100,37 +100,6 @@ ReadResponseHeadResult ReadResponseHead(
return result; return result;
} }
class MockServiceWorkerDataPipeStateNotifier
: public storage::mojom::ServiceWorkerDataPipeStateNotifier {
public:
MockServiceWorkerDataPipeStateNotifier() = default;
mojo::PendingRemote<storage::mojom::ServiceWorkerDataPipeStateNotifier>
BindNewPipeAndPassRemote() {
return receiver_.BindNewPipeAndPassRemote();
}
int32_t WaitUntilComplete() {
if (!complete_status_.has_value()) {
loop_.Run();
DCHECK(complete_status_.has_value());
}
return *complete_status_;
}
private:
void OnComplete(int32_t status) override {
complete_status_ = status;
if (loop_.running())
loop_.Quit();
}
base::Optional<int32_t> complete_status_;
base::RunLoop loop_;
mojo::Receiver<storage::mojom::ServiceWorkerDataPipeStateNotifier> receiver_{
this};
};
ReadDataResult ReadResponseData( ReadDataResult ReadResponseData(
storage::mojom::ServiceWorkerResourceReader* reader, storage::mojom::ServiceWorkerResourceReader* reader,
int data_size) { int data_size) {
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include <vector> #include <vector>
#include "base/barrier_closure.h" #include "base/barrier_closure.h"
#include "base/run_loop.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "content/browser/frame_host/frame_tree_node.h" #include "content/browser/frame_host/frame_tree_node.h"
...@@ -707,6 +706,33 @@ void MockServiceWorkerResourceWriter::CompletePendingWrite() { ...@@ -707,6 +706,33 @@ void MockServiceWorkerResourceWriter::CompletePendingWrite() {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
MockServiceWorkerDataPipeStateNotifier::
MockServiceWorkerDataPipeStateNotifier() = default;
MockServiceWorkerDataPipeStateNotifier::
~MockServiceWorkerDataPipeStateNotifier() = default;
mojo::PendingRemote<storage::mojom::ServiceWorkerDataPipeStateNotifier>
MockServiceWorkerDataPipeStateNotifier::BindNewPipeAndPassRemote() {
return receiver_.BindNewPipeAndPassRemote();
}
int32_t MockServiceWorkerDataPipeStateNotifier::WaitUntilComplete() {
if (!complete_status_.has_value()) {
base::RunLoop loop;
on_complete_callback_ = loop.QuitClosure();
loop.Run();
DCHECK(complete_status_.has_value());
}
return *complete_status_;
}
void MockServiceWorkerDataPipeStateNotifier::OnComplete(int32_t status) {
complete_status_ = status;
if (on_complete_callback_)
std::move(on_complete_callback_).Run();
}
ServiceWorkerUpdateCheckTestUtils::ServiceWorkerUpdateCheckTestUtils() = ServiceWorkerUpdateCheckTestUtils::ServiceWorkerUpdateCheckTestUtils() =
default; default;
ServiceWorkerUpdateCheckTestUtils::~ServiceWorkerUpdateCheckTestUtils() = ServiceWorkerUpdateCheckTestUtils::~ServiceWorkerUpdateCheckTestUtils() =
...@@ -848,43 +874,57 @@ void ServiceWorkerUpdateCheckTestUtils:: ...@@ -848,43 +874,57 @@ void ServiceWorkerUpdateCheckTestUtils::
bool ServiceWorkerUpdateCheckTestUtils::VerifyStoredResponse( bool ServiceWorkerUpdateCheckTestUtils::VerifyStoredResponse(
int64_t resource_id, int64_t resource_id,
ServiceWorkerStorage* storage, mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
const std::string& expected_body) { const std::string& expected_body) {
DCHECK(storage); DCHECK(storage);
if (resource_id == blink::mojom::kInvalidServiceWorkerResourceId) if (resource_id == blink::mojom::kInvalidServiceWorkerResourceId)
return false; return false;
mojo::Remote<storage::mojom::ServiceWorkerResourceReader> reader;
storage->CreateResourceReader(resource_id,
reader.BindNewPipeAndPassReceiver());
// Verify the response status. // Verify the response status.
size_t response_data_size = 0; size_t response_data_size = 0;
{ {
std::unique_ptr<ServiceWorkerResponseReader> reader = int rv;
storage->CreateResponseReader(resource_id); std::string status_text;
auto info_buffer = base::MakeRefCounted<HttpResponseInfoIOBuffer>(); base::RunLoop loop;
net::TestCompletionCallback cb; reader->ReadResponseHead(base::BindLambdaForTesting(
reader->ReadInfo(info_buffer.get(), cb.callback()); [&](int status, network::mojom::URLResponseHeadPtr response_head,
int rv = cb.WaitForResult(); base::Optional<mojo_base::BigBuffer> metadata) {
rv = status;
status_text = response_head->headers->GetStatusText();
response_data_size = response_head->content_length;
loop.Quit();
}));
loop.Run();
if (rv < 0) if (rv < 0)
return false; return false;
EXPECT_LT(0, rv); EXPECT_LT(0, rv);
EXPECT_EQ("OK", info_buffer->http_info->headers->GetStatusText()); EXPECT_EQ("OK", status_text);
response_data_size = info_buffer->response_data_size;
} }
// Verify the response body. // Verify the response body.
{ {
std::unique_ptr<ServiceWorkerResponseReader> reader = MockServiceWorkerDataPipeStateNotifier notifier;
storage->CreateResponseReader(resource_id); mojo::ScopedDataPipeConsumerHandle data_consumer;
auto buffer = base::RunLoop loop;
base::MakeRefCounted<net::IOBufferWithSize>(response_data_size); reader->ReadData(response_data_size, notifier.BindNewPipeAndPassRemote(),
net::TestCompletionCallback cb; base::BindLambdaForTesting(
reader->ReadData(buffer.get(), buffer->size(), cb.callback()); [&](mojo::ScopedDataPipeConsumerHandle pipe) {
int rv = cb.WaitForResult(); data_consumer = std::move(pipe);
loop.Quit();
}));
loop.Run();
std::string body = ReadDataPipe(std::move(data_consumer));
int rv = notifier.WaitUntilComplete();
if (rv < 0) if (rv < 0)
return false; return false;
EXPECT_EQ(static_cast<int>(expected_body.size()), rv); EXPECT_EQ(static_cast<int>(expected_body.size()), rv);
EXPECT_EQ(expected_body, body);
std::string received_body(buffer->data(), rv);
EXPECT_EQ(expected_body, received_body);
} }
return true; return true;
} }
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "components/services/storage/public/mojom/service_worker_storage_control.mojom.h" #include "components/services/storage/public/mojom/service_worker_storage_control.mojom.h"
#include "content/browser/service_worker/service_worker_cache_writer.h" #include "content/browser/service_worker/service_worker_cache_writer.h"
...@@ -372,6 +373,28 @@ class MockServiceWorkerResourceWriter ...@@ -372,6 +373,28 @@ class MockServiceWorkerResourceWriter
DISALLOW_COPY_AND_ASSIGN(MockServiceWorkerResourceWriter); DISALLOW_COPY_AND_ASSIGN(MockServiceWorkerResourceWriter);
}; };
// A test implementation of ServiceWorkerDataPipeStateNotifier.
class MockServiceWorkerDataPipeStateNotifier
: public storage::mojom::ServiceWorkerDataPipeStateNotifier {
public:
MockServiceWorkerDataPipeStateNotifier();
~MockServiceWorkerDataPipeStateNotifier() override;
mojo::PendingRemote<storage::mojom::ServiceWorkerDataPipeStateNotifier>
BindNewPipeAndPassRemote();
int32_t WaitUntilComplete();
private:
// storage::mojom::ServiceWorkerDataPipeStateNotifier implementations:
void OnComplete(int32_t status) override;
base::Optional<int32_t> complete_status_;
base::OnceClosure on_complete_callback_;
mojo::Receiver<storage::mojom::ServiceWorkerDataPipeStateNotifier> receiver_{
this};
};
class ServiceWorkerUpdateCheckTestUtils { class ServiceWorkerUpdateCheckTestUtils {
public: public:
ServiceWorkerUpdateCheckTestUtils(); ServiceWorkerUpdateCheckTestUtils();
...@@ -428,9 +451,10 @@ class ServiceWorkerUpdateCheckTestUtils { ...@@ -428,9 +451,10 @@ class ServiceWorkerUpdateCheckTestUtils {
// Returns false if the entry for |resource_id| doesn't exist in the storage. // Returns false if the entry for |resource_id| doesn't exist in the storage.
// Returns true when response status is "OK" and response body is same as // Returns true when response status is "OK" and response body is same as
// expected if body exists. // expected if body exists.
static bool VerifyStoredResponse(int64_t resource_id, static bool VerifyStoredResponse(
ServiceWorkerStorage* storage, int64_t resource_id,
const std::string& expected_body); mojo::Remote<storage::mojom::ServiceWorkerStorageControl>& storage,
const std::string& expected_body);
}; };
// Reads all data from the given |handle| and returns data as a string. // Reads all data from the given |handle| and returns data as a string.
......
...@@ -167,7 +167,8 @@ class ServiceWorkerUpdatedScriptLoaderTest : public testing::Test { ...@@ -167,7 +167,8 @@ class ServiceWorkerUpdatedScriptLoaderTest : public testing::Test {
// The response should also be stored in the storage. // The response should also be stored in the storage.
EXPECT_TRUE(ServiceWorkerUpdateCheckTestUtils::VerifyStoredResponse( EXPECT_TRUE(ServiceWorkerUpdateCheckTestUtils::VerifyStoredResponse(
LookupResourceId(kScriptURL), context()->storage(), expected_body)); LookupResourceId(kScriptURL), context()->GetStorageControl(),
expected_body));
std::string response; std::string response;
EXPECT_TRUE(mojo::BlockingCopyToString(client_->response_body_release(), EXPECT_TRUE(mojo::BlockingCopyToString(client_->response_body_release(),
......
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