chromium/chrome/browser/data_saver/data_saver_browsertest.cc

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>

#include "base/barrier_closure.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/prerender_test_util.h"
#include "data_saver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "services/network/public/cpp/network_quality_tracker.h"
#include "third_party/blink/public/common/features.h"

namespace {

std::unique_ptr<net::test_server::HttpResponse> CaptureHeaderHandlerWithContent(
    const std::string& path,
    net::test_server::HttpRequest::HeaderMap* header_map,
    const std::string& mime_type,
    const std::string& content,
    base::OnceClosure done_callback,
    const net::test_server::HttpRequest& request) {}

// Test version of the observer. Used to wait for the event when the network
// quality tracker sends the network quality change notification.
class TestEffectiveConnectionTypeObserver
    : public network::NetworkQualityTracker::EffectiveConnectionTypeObserver {};

// Test version of the observer. Used to wait for the event when the network
// quality tracker sends the network quality change notification.
class TestRTTAndThroughputEstimatesObserver
    : public network::NetworkQualityTracker::RTTAndThroughputEstimatesObserver {};

}  // namespace

class DataSaverBrowserTest : public InProcessBrowserTest {};

IN_PROC_BROWSER_TEST_F(DataSaverBrowserTest, DataSaverEnabled) {}

IN_PROC_BROWSER_TEST_F(DataSaverBrowserTest, DataSaverDisabled) {}

IN_PROC_BROWSER_TEST_F(DataSaverBrowserTest, DataSaverDisabledInIncognito) {}

IN_PROC_BROWSER_TEST_F(DataSaverBrowserTest,
                       DataSaverEnabledDisablesPrerendering) {}

class DataSaverWithServerBrowserTest : public InProcessBrowserTest {};

// TODO(crbug.com/40250644): Fix and enable test.
IN_PROC_BROWSER_TEST_F(DataSaverWithServerBrowserTest, DISABLED_ReloadPage) {}

class DataSaverForWorkerBrowserTest : public InProcessBrowserTest,
                                      public testing::WithParamInterface<bool> {};

INSTANTIATE_TEST_SUITE_P();

// Checks that the Save-Data header is sent in a request for dedicated worker
// script when the data saver is enabled.
// TODO(crbug.com/40250644): Fix and enable test.
IN_PROC_BROWSER_TEST_P(DataSaverForWorkerBrowserTest,
                       DISABLED_DedicatedWorker) {}

// Checks that the Save-Data header is sent in a request for shared worker
// script when the data saver is enabled. Disabled on Android since a shared
// worker is not available on Android.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_SharedWorker
#else
#define MAYBE_SharedWorker
#endif
IN_PROC_BROWSER_TEST_P(DataSaverForWorkerBrowserTest, MAYBE_SharedWorker) {}

// Checks that the Save-Data header is not sent in a request for a service
// worker script when it's disabled.
IN_PROC_BROWSER_TEST_P(DataSaverForWorkerBrowserTest, ServiceWorker_Register) {}

// Checks that the Save-Data header is not sent in a request for a service
// worker script when it's disabled.
IN_PROC_BROWSER_TEST_P(DataSaverForWorkerBrowserTest, ServiceWorker_Update) {}

// Checks that Save-Data header is appropriately set to requests from fetch() in
// a dedicated worker.
// TODO(crbug.com/40250644): Fix and enable test.
IN_PROC_BROWSER_TEST_P(DataSaverForWorkerBrowserTest,
                       DISABLED_FetchFromWorker) {}

// Checks that Save-Data header is appropriately set to requests from fetch() in
// a shared worker.
// TODO(crbug.com/40250644): Fix and enable test.
IN_PROC_BROWSER_TEST_P(DataSaverForWorkerBrowserTest,
                       DISABLED_FetchFromSharedWorker) {}

// Checks that Save-Data header is appropriately set to requests from fetch() in
// a service worker.
// TODO(crbug.com/40250644): Fix and enable test.
IN_PROC_BROWSER_TEST_P(DataSaverForWorkerBrowserTest,
                       DISABLED_FetchFromServiceWorker) {}

// Checks that Save-Data header is appropriately set to requests from fetch() in
// a page controlled by a service worker without fetch handler.
// TODO(crbug.com/40250644): Fix and enable test.
IN_PROC_BROWSER_TEST_P(
    DataSaverForWorkerBrowserTest,
    DISABLED_FetchFromServiceWorkerControlledPage_NoFetchHandler) {}

// Checks that Save-Data header is appropriately set to requests from fetch() in
// a page controlled by a service worker with fetch handler but no respondWith.
// TODO(crbug.com/40250644): Fix and enable test.
IN_PROC_BROWSER_TEST_P(
    DataSaverForWorkerBrowserTest,
    DISABLED_FetchFromServiceWorkerControlledPage_PassThrough) {}

// Checks that Save-Data header is appropriately set to requests from fetch() in
// a page controlled by a service worker with fetch handler and responds with
// fetch().
// TODO(crbug.com/40250644): Fix and enable test.
IN_PROC_BROWSER_TEST_P(
    DataSaverForWorkerBrowserTest,
    DISABLED_FetchFromServiceWorkerControlledPage_RespondWithFetch) {}

class DataSaverWithImageServerBrowserTest : public InProcessBrowserTest {};

IN_PROC_BROWSER_TEST_F(DataSaverWithImageServerBrowserTest,
                       ImgSrcset_DataSaverEnabled) {}

IN_PROC_BROWSER_TEST_F(DataSaverWithImageServerBrowserTest,
                       ImgSrcset_DataSaverDisabled) {}