chromium/content/browser/service_worker/service_worker_main_resource_loader_unittest.cc

// Copyright 2017 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/service_worker/service_worker_main_resource_loader.h"

#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "content/browser/loader/navigation_loader_interceptor.h"
#include "content/browser/loader/response_head_update_params.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "content/browser/service_worker/fake_embedded_worker_instance_client.h"
#include "content/browser/service_worker/fake_service_worker.h"
#include "content/browser/service_worker/service_worker_client.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_fetch_dispatcher.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_test_utils.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/mock_render_process_host.h"
#include "mojo/public/cpp/system/data_pipe_utils.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_data_directory.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/single_request_url_loader_factory.h"
#include "services/network/public/mojom/fetch_api.mojom.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "services/network/test/test_url_loader_client.h"
#include "storage/browser/blob/blob_data_builder.h"
#include "storage/browser/blob/blob_data_handle.h"
#include "storage/browser/blob/blob_impl.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_event_status.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"
#include "url/origin.h"

namespace content {
namespace service_worker_main_resource_loader_unittest {

class ScopedOverrideToDisableHighPriorityFetchResponseCallback {};

void ReceiveRequestHandler(
    network::SingleRequestURLLoaderFactory::RequestHandler* out_handler,
    network::SingleRequestURLLoaderFactory::RequestHandler handler) {}

blink::mojom::FetchAPIResponsePtr OkResponse(
    blink::mojom::SerializedBlobPtr blob_body,
    network::mojom::FetchResponseSource response_source,
    base::Time response_time,
    std::string cache_storage_cache_name) {}

blink::mojom::FetchAPIResponsePtr ErrorResponse() {}

blink::mojom::FetchAPIResponsePtr RedirectResponse(
    const std::string& redirect_location_header) {}

blink::mojom::FetchAPIResponsePtr HeadersResponse(
    const base::flat_map<std::string, std::string>& headers) {}

// ServiceWorkerMainResourceLoader::RecordTimingMetrics() records the metrics
// only when the consistent high-resolution timer is used among processes.
bool LoaderRecordsTimingMetrics() {}

// Simulates a service worker handling fetch events. The response can be
// customized via RespondWith* functions.
class FetchEventServiceWorker : public FakeServiceWorker {};

// Returns typical response info for a resource load that went through a service
// worker.
network::mojom::URLResponseHeadPtr CreateResponseInfoFromServiceWorker() {}

const char kHistogramMainResourceFetchEvent[] =;

// ServiceWorkerMainResourceLoaderTest is for testing the handling of requests
// by a service worker via ServiceWorkerMainResourceLoader.
//
// Of course, no actual service worker runs in the unit test, it is simulated
// via EmbeddedWorkerTestHelper receiving IPC messages from the browser and
// responding as if a service worker is running in the renderer.
class ServiceWorkerMainResourceLoaderTest : public testing::Test {};

TEST_F(ServiceWorkerMainResourceLoaderTest, Basic) {}

TEST_F(ServiceWorkerMainResourceLoaderTest, NoActiveWorker) {}

// Test that the request body is passed to the fetch event.
TEST_F(ServiceWorkerMainResourceLoaderTest, RequestBody) {}

TEST_F(ServiceWorkerMainResourceLoaderTest, BlobResponse) {}

// Tell the helper to respond with a non-existent Blob.
TEST_F(ServiceWorkerMainResourceLoaderTest, BrokenBlobResponse) {}

TEST_F(ServiceWorkerMainResourceLoaderTest, StreamResponse) {}

// Test when a stream response body is aborted.
TEST_F(ServiceWorkerMainResourceLoaderTest, StreamResponse_Abort) {}

// Test when the loader is cancelled while a stream response is being written.
TEST_F(ServiceWorkerMainResourceLoaderTest, StreamResponseAndCancel) {}

// Test when the service worker responds with network fallback.
// i.e., does not call respondWith().
TEST_F(ServiceWorkerMainResourceLoaderTest, FallbackResponse) {}

// Test when the service worker rejects the FetchEvent.
TEST_F(ServiceWorkerMainResourceLoaderTest, ErrorResponse) {}

// Test when dispatching the fetch event to the service worker failed.
TEST_F(ServiceWorkerMainResourceLoaderTest, FailFetchDispatch) {}

// Test when the respondWith() promise resolves before the waitUntil() promise
// resolves. The response should be received before the event finishes.
TEST_F(ServiceWorkerMainResourceLoaderTest, EarlyResponse) {}

// Test responding to the fetch event with a redirect response.
TEST_F(ServiceWorkerMainResourceLoaderTest, Redirect) {}

TEST_F(ServiceWorkerMainResourceLoaderTest, Lifetime) {}

TEST_F(ServiceWorkerMainResourceLoaderTest, ConnectionErrorDuringFetchEvent) {}

TEST_F(ServiceWorkerMainResourceLoaderTest, CancelNavigationDuringFetchEvent) {}

TEST_F(ServiceWorkerMainResourceLoaderTest, TimingInfo) {}

TEST_F(ServiceWorkerMainResourceLoaderTest, FencedFrameNavigationPreload) {}

}  // namespace service_worker_main_resource_loader_unittest
}  // namespace content