chromium/chrome/browser/extensions/api/tabs/execute_script_apitest.cc

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

#include "base/cfi_buildflags.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "extensions/common/utils/content_script_utils.h"
#include "net/base/filename_util.h"
#include "net/dns/mock_host_resolver.h"
#include "pdf/pdf_features.h"
#include "third_party/blink/public/common/features.h"

namespace extensions {

namespace {

struct BackForwardCacheDisabledDestructiveScriptTestPassToString {};

}  // namespace

ContextType;

class ExecuteScriptApiTestBase : public ExtensionApiTest {};

class ExecuteScriptApiTest : public ExecuteScriptApiTestBase,
                             public testing::WithParamInterface<ContextType> {};

INSTANTIATE_TEST_SUITE_P();
// These tests use chrome.tabs.executeScript, which is not available in MV3.
// See crbug.com/332328868.
INSTANTIATE_TEST_SUITE_P();

// If failing, mark disabled and update http://crbug.com/92105.
IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptBasic) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptBadEncoding) {}

// If failing, mark disabled and update http://crbug.com/92105.
IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptInFrame) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptByFrameId) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptPermissions) {}

// If failing, mark disabled and update http://crbug.com/84760.
IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptFileAfterClose) {}

// If crashing, mark disabled and update http://crbug.com/67774.
IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptFragmentNavigation) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, NavigationRaceExecuteScript) {}

// If failing, mark disabled and update http://crbug.com/92105.
IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptFrameAfterLoad) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, FrameWithHttp204) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptRunAt) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptCSSOrigin) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptCallback) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptRemoveCSS) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, UserGesture) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, InjectIntoSubframesOnLoad) {}

IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, RemovedFrames) {}

// Tests that tabs.executeScript called with files exceeding the max size limit
// will return an error and not execute.
IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, ExecuteScriptSizeLimit) {}

// Ensure that an extension can inject a script in a file frame provided it has
// access to file urls enabled and the necessary host permissions.
IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, InjectScriptInFileFrameAllowed) {}

// Ensure that an extension can't inject a script in a file frame if it doesn't
// have file access.
IN_PROC_BROWSER_TEST_P(ExecuteScriptApiTest, InjectScriptInFileFrameDenied) {}

// If tests time out because it takes too long to run them, then this value can
// be increased to split the DestructiveScriptTest tests in approximately equal
// parts. Each part takes approximately the same time to run.
const int kDestructiveScriptTestBucketCount =;

class DestructiveScriptTest : public ExecuteScriptApiTestBase {};

// For destructive script tests that don't involve PDFs and therefore don't need
// to run in OOPIF PDF viewer mode.
class DestructiveScriptTestWithoutOopifOverride
    : public DestructiveScriptTest,
      public testing::WithParamInterface<int> {};

// For destructive script tests that require BFCache to be disabled and involve
// PDFs.
class BackForwardCacheDisabledDestructiveScriptTest
    : public DestructiveScriptTest,
      public testing::WithParamInterface<std::tuple<int, bool>> {};

// Flaky on ASAN and -dbg, and Linux CFI bots. crbug.com/1293865
#if defined(ADDRESS_SANITIZER) || !defined(NDEBUG) || \
    (BUILDFLAG(CFI_ICALL_CHECK) && BUILDFLAG(IS_LINUX))
#define MAYBE_SynchronousRemoval
#else
#define MAYBE_SynchronousRemoval
#endif
// Removes the frame as soon as the content script is executed.
IN_PROC_BROWSER_TEST_P(BackForwardCacheDisabledDestructiveScriptTest,
                       MAYBE_SynchronousRemoval) {}

// Flaky on ASAN and -dbg and Linux CFI. crbug.com/1293865
#if defined(ADDRESS_SANITIZER) || !defined(NDEBUG) || \
    (BUILDFLAG(CFI_ICALL_CHECK) && BUILDFLAG(IS_LINUX))
#define MAYBE_MicrotaskRemoval
#else
#define MAYBE_MicrotaskRemoval
#endif
// Removes the frame at the frame's first scheduled microtask.
IN_PROC_BROWSER_TEST_P(BackForwardCacheDisabledDestructiveScriptTest,
                       MAYBE_MicrotaskRemoval) {}

// TODO(http://crbug.com/1028308): Flaky on multiple platforms
// Removes the frame at the frame's first scheduled macrotask.
IN_PROC_BROWSER_TEST_P(DestructiveScriptTestWithoutOopifOverride,
                       DISABLED_MacrotaskRemoval) {}

// Removes the frame at the first DOMNodeInserted event.
IN_PROC_BROWSER_TEST_P(DestructiveScriptTestWithoutOopifOverride,
                       DOMNodeInserted1) {}

// Removes the frame at the second DOMNodeInserted event.
IN_PROC_BROWSER_TEST_P(DestructiveScriptTestWithoutOopifOverride,
                       DOMNodeInserted2) {}

// Removes the frame at the third DOMNodeInserted event.
IN_PROC_BROWSER_TEST_P(DestructiveScriptTestWithoutOopifOverride,
                       DOMNodeInserted3) {}

// Removes the frame at the first DOMSubtreeModified event.
IN_PROC_BROWSER_TEST_P(DestructiveScriptTestWithoutOopifOverride,
                       DOMSubtreeModified1) {}

// Removes the frame at the second DOMSubtreeModified event.
IN_PROC_BROWSER_TEST_P(DestructiveScriptTestWithoutOopifOverride,
                       DOMSubtreeModified2) {}

// Removes the frame at the third DOMSubtreeModified event.
IN_PROC_BROWSER_TEST_P(DestructiveScriptTestWithoutOopifOverride,
                       DOMSubtreeModified3) {}

INSTANTIATE_TEST_SUITE_P();

// TODO(crbug.com/40268279): Stop testing GuestView PDF viewer once OOPIF PDF
// viewer launches.
INSTANTIATE_TEST_SUITE_P();

class ExecuteScriptApiFencedFrameTest : public ExecuteScriptApiTestBase {};

IN_PROC_BROWSER_TEST_F(ExecuteScriptApiFencedFrameTest, Load) {}

}  // namespace extensions