chromium/chrome/browser/site_isolation/site_details_browsertest.cc

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

#include "chrome/browser/site_isolation/site_details.h"

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <utility>
#include <vector>

#include "base/files/file_path.h"
#include "base/functional/callback_helpers.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/metrics/metrics_memory_details.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/metrics/metrics_service.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/prerender_test_util.h"
#include "content/public/test/test_utils.h"
#include "extensions/common/switches.h"
#include "extensions/test/test_extension_dir.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"

Bucket;
WebContents;
Extension;
TestExtensionDir;
ElementsAre;
PrintToString;

namespace {

class TestMemoryDetails : public MetricsMemoryDetails {};

// This matcher takes three other matchers as arguments, and applies one of them
// depending on the current site isolation mode. The first applies if no site
// isolation mode is active; the second applies under --isolate-extensions mode;
// and the third applies under --site-per-process mode.
MATCHER_P3(DependingOnPolicy,
           isolate_nothing,
           isolate_extensions,
           isolate_all_sites,
#if BUILDFLAG(IS_ANDROID)
           std::string("(with oopifs disabled) ") +
               PrintToString(isolate_nothing)
#else
           content::AreAllSitesIsolatedForTesting()
               ? std::string("(under --site-per-process) ") +
                     PrintToString(isolate_all_sites)
               : std::string("(under --isolate-extensions) ") +
                     PrintToString(isolate_extensions)
#endif
) {}

// Matcher for base::Bucket objects that allows bucket_min to be a matcher.
MATCHER_P2(Sample,
           bucket_min,
           count,
           std::string("is a Bucket whose count is ") + PrintToString(count) +
               std::string(" and whose value is ") +
               PrintToString(bucket_min)) {}

// Allow matchers to be pretty-printed when passed to PrintToString() for the
// cases we care about.
template <typename P1, typename P2, typename P3>
void PrintTo(const DependingOnPolicyMatcherP3<P1, P2, P3>& matcher,
             std::ostream* os) {}

template <typename P1, typename P2>
void PrintTo(const SampleMatcherP2<P1, P2>& matcher, std::ostream* os) {}

// Matches a container of histogram samples, for the common case where the
// histogram received just one sample.
#define HasOneSample(x)

}  // namespace

class SiteDetailsBrowserTest : public extensions::ExtensionBrowserTest {};

// Test the accuracy of SiteDetails process estimation, in the presence of
// multiple iframes, navigation, multiple BrowsingInstances, and multiple tabs
// in the same BrowsingInstance.
//
// Disabled since it's flaky: https://crbug.com/830318.
IN_PROC_BROWSER_TEST_F(SiteDetailsBrowserTest, DISABLED_ManyIframes) {}

// TODO(crbug.com/40496888): This test is flaky.
IN_PROC_BROWSER_TEST_F(SiteDetailsBrowserTest, DISABLED_IsolateExtensions) {}

// Exercises accounting in the case where an extension has two different-site
// web iframes.
IN_PROC_BROWSER_TEST_F(SiteDetailsBrowserTest, ExtensionWithTwoWebIframes) {}

// Verifies that --isolate-extensions doesn't isolate hosted apps.
//
// Disabled since it's flaky: https://crbug.com/830318.
IN_PROC_BROWSER_TEST_F(SiteDetailsBrowserTest,
                       DISABLED_IsolateExtensionsHostedApps) {}

// Verifies that the UMA counter for SiteInstances in a BrowsingInstance is
// correct when using tabs with web pages.
//
// Disabled since it's flaky. https://crbug.com/934900
IN_PROC_BROWSER_TEST_F(SiteDetailsBrowserTest,
                       DISABLED_VerifySiteInstanceCountInBrowsingInstance) {}

// Verifies that the UMA counter for SiteInstances in a BrowsingInstance is
// correct when extensions and web pages are mixed together.
//
// Disabled since it's flaky: https://crbug.com/830318.
IN_PROC_BROWSER_TEST_F(
    SiteDetailsBrowserTest,
    DISABLED_VerifySiteInstanceCountInBrowsingInstanceWithExtensions) {}

class PrerenderSiteDetailsBrowserTest : public InProcessBrowserTest {};

IN_PROC_BROWSER_TEST_F(PrerenderSiteDetailsBrowserTest,
                       MemoryDetailsForPrerender) {}

class FencedFrameSiteDetailsBrowserTest : public InProcessBrowserTest {};

IN_PROC_BROWSER_TEST_F(FencedFrameSiteDetailsBrowserTest,
                       MemoryDetailsForFencedFrame) {}

class BackForwardCacheSiteDetailsBrowserTest : public InProcessBrowserTest {};

IN_PROC_BROWSER_TEST_F(BackForwardCacheSiteDetailsBrowserTest,
                       MemoryDetailsForBackForwardCache) {}