chromium/chrome/browser/reduce_accept_language/reduce_accept_language_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 <optional>
#include <string_view>

#include "base/containers/fixed_flat_set.h"
#include "base/feature_list.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/statistics_recorder.h"
#include "base/no_destructor.h"
#include "base/ranges/algorithm.h"
#include "base/strings/strcat.h"
#include "base/strings/string_split.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/embedder_support/switches.h"
#include "components/language/core/browser/language_prefs.h"
#include "components/language/core/browser/pref_names.h"
#include "components/metrics/content/subprocess_metrics_provider.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/origin_trials_controller_delegate.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "services/network/public/cpp/features.h"
#include "third_party/blink/public/common/features.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/origin.h"

namespace {

URLLoaderInterceptor;
EmbeddedTestServer;

enum class FeatureEnableType {};

struct ReduceAcceptLanguageTestOptions {};

struct ServerPortAndValidOriginToken {};

const char kLargeLanguages[] =;

static constexpr const char kFirstPartyOriginUrl[] =;
static constexpr char kThirdPartyOriginUrl[] =;

// Notes: Only use to test origin trial feature with URLLoaderInterceptor.
// generate_token.py https://127.0.0.1:44444 DisableReduceAcceptLanguage
// --expire-timestamp=2000000000
static constexpr const char kValidFirstPartyToken[] =;

// Notes: Only use to test origin trial feature with URLLoaderInterceptor.
// generate_token.py https://my-site.com:44444 DisableReduceAcceptLanguage
// --is-third-party --expire-timestamp=2000000000
static constexpr const char kValidThirdPartyToken[] =;

// Notes: Only use to test origin trial feature with URLLoaderInterceptor.
// generate_token.py https://my-site.com:44444 DisableReduceAcceptLanguage
// --expire-timestamp=2000000000
static constexpr const char kValidMySiteFirstPartyToken[] =;

static constexpr const char kInvalidOriginToken[] =;

static constexpr const char kDeprecationTrialName[] =;

}  // namespace

class ReduceAcceptLanguageBrowserTest : public InProcessBrowserTest {};

// Browser tests that consider ReduceAcceptLanguage feature disabled.
class DisableFeatureReduceAcceptLanguageBrowserTest
    : public ReduceAcceptLanguageBrowserTest {};

IN_PROC_BROWSER_TEST_F(DisableFeatureReduceAcceptLanguageBrowserTest,
                       NoAcceptLanguageHeader) {}

IN_PROC_BROWSER_TEST_F(DisableFeatureReduceAcceptLanguageBrowserTest,
                       IframeNoAcceptLanguageHeader) {}

// Tests same origin requests with the ReduceAcceptLanguage feature enabled.
class SameOriginReduceAcceptLanguageBrowserTest
    : public ReduceAcceptLanguageBrowserTest {};

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       LargeLanguageListAndScriptDisable) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       EmptyUserAcceptLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       NoAvailLanguageHeader) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       NoContentLanguageHeader) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       EmptyAvailLanguageAcceptLanguages) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       AvailLanguageAcceptLanguagesWhiteSpace) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       SiteLanguageMatchNonPrimaryLanguage) {}

// Verify no endless resend requests for the service worker navigation preload
// requests.
IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       ServiceWorkerNavigationPreload) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       SiteLanguageMatchPrimaryLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       SubresourceRequestNoRestart) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       SiteLanguageMatchMultipleLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       SiteLanguageDontMatchAnyPreferredLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       PersistedAcceptLanguageNotAvailable) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       IframeReduceAcceptLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       ImgSubresourceReduceAcceptLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       IframeNoContentLanguageInChild) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       IframeNoAvailLanguageAcceptLanguageInChild) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       IframeSameContentLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageBrowserTest,
                       IframeDifferentContentLanguage) {}

class ThirdPartyReduceAcceptLanguageBrowserTest
    : public ReduceAcceptLanguageBrowserTest {};

IN_PROC_BROWSER_TEST_F(ThirdPartyReduceAcceptLanguageBrowserTest,
                       IframeDifferentContentLanguage) {}

IN_PROC_BROWSER_TEST_F(ThirdPartyReduceAcceptLanguageBrowserTest,
                       ThirdPartyIframeWithSubresourceRequests) {}

IN_PROC_BROWSER_TEST_F(ThirdPartyReduceAcceptLanguageBrowserTest,
                       ThirdPartyIframeWithSubresourceRedirectRequests) {}

class FencedFrameReduceAcceptLanguageBrowserTest
    : public ReduceAcceptLanguageBrowserTest {};

IN_PROC_BROWSER_TEST_F(FencedFrameReduceAcceptLanguageBrowserTest,
                       CrossOriginFencedFrame) {}

IN_PROC_BROWSER_TEST_F(FencedFrameReduceAcceptLanguageBrowserTest,
                       SameOriginFencedFrame) {}

// Browser tests verify redirect same origin with different cases.
class SameOriginRedirectReduceAcceptLanguageBrowserTest
    : public ReduceAcceptLanguageBrowserTest {};

IN_PROC_BROWSER_TEST_F(SameOriginRedirectReduceAcceptLanguageBrowserTest,
                       MatchFirstLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginRedirectReduceAcceptLanguageBrowserTest,
                       MatchSecondaryLanguage) {}

// Browser tests verify redirect cross origin A to B with different cases.
class CrossOriginRedirectReduceAcceptLanguageBrowserTest
    : public ReduceAcceptLanguageBrowserTest {};

IN_PROC_BROWSER_TEST_F(CrossOriginRedirectReduceAcceptLanguageBrowserTest,
                       RestartOnA) {}

IN_PROC_BROWSER_TEST_F(CrossOriginRedirectReduceAcceptLanguageBrowserTest,
                       RestartOnB) {}

IN_PROC_BROWSER_TEST_F(CrossOriginRedirectReduceAcceptLanguageBrowserTest,
                       RestartBothAB) {}

// Browser tests verify same origin redirect when DisableReduceAcceptLanguage
// deprecation origin trial enable.
// NOTES: As URLLoaderInterceptor doesn't support redirect, testing redirects
// with origin trial requires EmbeddedTestServer to start on specific ports, we
// can only add a single test in this test class in case the different tests run
// parallel to cause server can't starts on specific ports. It will cause tests
// flakiness. Also, we need to make sure it doesn't share port with any other
// browser_tests.
class SameOriginRedirectReduceAcceptLanguageOTBrowserTest
    : public SameOriginRedirectReduceAcceptLanguageBrowserTest {};

IN_PROC_BROWSER_TEST_F(SameOriginRedirectReduceAcceptLanguageOTBrowserTest,
                       MatchFirstLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginRedirectReduceAcceptLanguageOTBrowserTest,
                       MatchNonPrimaryLanguage) {}

// Browser tests verify cross origin redirect when DisableReduceAcceptLanguage
// origin trial enable.
// NOTES: As URLLoaderInterceptor doesn't support redirect, testing redirects
// with origin trial requires EmbeddedTestServer to start on specific ports, we
// can only add a single test in this test class in case the different tests run
// parallel to cause server can't starts on specific ports. It will cause tests
// flakiness. Also, we need to make sure it doesn't share port with any other
// browser_tests.
class CrossOriginRedirectReduceAcceptLanguageOTBrowserTest
    : public CrossOriginRedirectReduceAcceptLanguageBrowserTest {};

// Persistent origin trial doesn't works when a.com redirects to b.com and only
// a.com opt-in the deprecation origin trial, because persistent origin trial
// only parse and persist token for commit origin, in this redirect case b.com
// is always the commit origin.
IN_PROC_BROWSER_TEST_F(CrossOriginRedirectReduceAcceptLanguageOTBrowserTest,
                       RestartOnA) {}

IN_PROC_BROWSER_TEST_F(CrossOriginRedirectReduceAcceptLanguageOTBrowserTest,
                       RestartOnB) {}

IN_PROC_BROWSER_TEST_F(CrossOriginRedirectReduceAcceptLanguageOTBrowserTest,
                       RestartOnAB) {}

// Browser tests verify same origin deprecation origin trial.
class SameOriginReduceAcceptLanguageDeprecationOTBrowserTest
    : public ReduceAcceptLanguageBrowserTest {};

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       FirstRequestMatchPrimaryLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       FirstRequestMatchNonPrimaryLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       FirstRequestMatchNonPrimaryLanguageWithCriticalTrial) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       FirstRequestNoMatchLanguage) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       IframeRequestRestart) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       IframeRequestRestartWithCriticalTrial) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       IframeRequestNoRestart) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       IframeRequestNoRestartWithCriticalTrial) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       ImgSubresourceRestart) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       ImgSubresourceRestartWithCriticalTrial) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       ImgSubresourceNoRestart) {}

IN_PROC_BROWSER_TEST_F(SameOriginReduceAcceptLanguageDeprecationOTBrowserTest,
                       ImgSubresourceNoRestartWithCriticalTrial) {}

// Browser tests verify third party deprecation origin trial.
class ThirdPartyReduceAcceptLanguageDeprecationOTBrowserTest
    : public ThirdPartyReduceAcceptLanguageBrowserTest {};

// For third-party embedded as an iframe, the third-party can opt-in the
// deprecation trial as a first party deprecation origin trial.
IN_PROC_BROWSER_TEST_F(ThirdPartyReduceAcceptLanguageDeprecationOTBrowserTest,
                       IframeRequests) {}

IN_PROC_BROWSER_TEST_F(ThirdPartyReduceAcceptLanguageDeprecationOTBrowserTest,
                       JavaScriptRequest) {}