chromium/chrome/browser/extensions/api/declarative_content/declarative_content_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/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/extensions/chrome_extension_test_notification_observer.h"
#include "chrome/browser/extensions/chrome_test_extension_loader.h"
#include "chrome/browser/extensions/extension_action_test_util.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/common/bookmark_metrics.h"
#include "components/sessions/content/session_tab_helper.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/api/declarative/rules_registry.h"
#include "extensions/browser/api/declarative/rules_registry_service.h"
#include "extensions/browser/api/declarative_webrequest/webrequest_constants.h"
#include "extensions/browser/browsertest_util.h"
#include "extensions/browser/extension_action.h"
#include "extensions/browser/extension_action_manager.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/common/api/extension_action/action_info.h"
#include "extensions/common/api/extension_action/action_info_test_util.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/test_extension_dir.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace extensions {
namespace {

static constexpr char kDeclarativeContentManifest[] =;

constexpr char kIncognitoSpecificBackground[] =;

constexpr char kBackgroundHelpers[] =;

ContextType;

class DeclarativeContentApiTest : public ExtensionApiTest {};

std::string DeclarativeContentApiTest::FormatManifest(IncognitoMode mode) {}

void DeclarativeContentApiTest::CheckIncognito(IncognitoMode mode,
                                               bool is_enabled_in_incognito) {}

void DeclarativeContentApiTest::CheckBookmarkEvents(bool match_is_bookmarked) {}

class DeclarativeContentApiTestWithContextType
    : public DeclarativeContentApiTest,
      public testing::WithParamInterface<ContextType> {};

INSTANTIATE_TEST_SUITE_P();
// These tests use page_action, which is unavailable in MV3.
INSTANTIATE_TEST_SUITE_P();

// TODO(crbug.com/40260777): Convert this to run in both modes.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest, Overview) {}

// Test that adds two rules pointing to single action instance.
// Regression test for http://crbug.com/574149.
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       ReusedActionInstance) {}

// Tests that the rules are evaluated at the time they are added or removed.
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       RulesEvaluatedOnAddRemove) {}

struct ShowActionParams {};

class ParameterizedShowActionDeclarativeContentApiTest
    : public DeclarativeContentApiTest,
      public testing::WithParamInterface<ShowActionParams> {};

void ParameterizedShowActionDeclarativeContentApiTest::TestShowAction(
    std::optional<ActionInfo::Type> action_type) {}

IN_PROC_BROWSER_TEST_P(ParameterizedShowActionDeclarativeContentApiTest,
                       NoActionInManifest) {}

IN_PROC_BROWSER_TEST_P(ParameterizedShowActionDeclarativeContentApiTest,
                       PageActionInManifest) {}

IN_PROC_BROWSER_TEST_P(ParameterizedShowActionDeclarativeContentApiTest,
                       BrowserActionInManifest) {}

IN_PROC_BROWSER_TEST_P(ParameterizedShowActionDeclarativeContentApiTest,
                       ActionInManifest) {}

// Tests that rules from manifest are added and evaluated properly.
IN_PROC_BROWSER_TEST_P(ParameterizedShowActionDeclarativeContentApiTest,
                       RulesAddedFromManifest) {}

INSTANTIATE_TEST_SUITE_P();

INSTANTIATE_TEST_SUITE_P();

// Tests that rules are not evaluated in incognito browser windows when the
// extension specifies spanning incognito mode but is not enabled for incognito.
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       DisabledForSpanningIncognito) {}

// Tests that rules are evaluated in incognito browser windows when the
// extension specifies spanning incognito mode and is enabled for incognito.
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       EnabledForSpanningIncognito) {}

// Tests that rules are not evaluated in incognito browser windows when the
// extension specifies split incognito mode but is not enabled for incognito.
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       DisabledForSplitIncognito) {}

// Tests that rules are evaluated in incognito browser windows when the
// extension specifies split incognito mode and is enabled for incognito.
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       EnabledForSplitIncognito) {}

// Tests that rules are evaluated for an incognito tab that exists at the time
// the rules are added.
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       RulesEvaluatedForExistingIncognitoTab) {}

constexpr char kRulesExtensionName[] =;

// TODO(crbug.com/41189874): Flaky on Windows release builds and on LACROS.
#if (BUILDFLAG(IS_WIN) && defined(NDEBUG)) || BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_PRE_RulesPersistence
#else
#define MAYBE_PRE_RulesPersistence
#endif
// Sets up rules matching http://test1/ in a normal and incognito browser.
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       MAYBE_PRE_RulesPersistence) {}

// TODO(crbug.com/41189874): Flaky on Windows release builds and on LACROS.
#if (BUILDFLAG(IS_WIN) && defined(NDEBUG)) || BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_RulesPersistence
#else
#define MAYBE_RulesPersistence
#endif
// Reloads the extension from PRE_RulesPersistence and checks that the rules
// continue to work as expected after being persisted and reloaded.
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       MAYBE_RulesPersistence) {}

// http://crbug.com/304373
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       UninstallWhileActivePageAction) {}

// This tests against a renderer crash that was present during development.
// TODO(crbug.com/40260777): Convert this to run in both modes.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
                       AddExtensionMatchingExistingTabWithDeadFrames) {}

// TODO(crbug.com/40260777): Convert this to run in both modes.
IN_PROC_BROWSER_TEST_F(DeclarativeContentApiTest,
                       CanonicalizesPageStateMatcherCss) {}

// Tests that the rules with isBookmarked: true are evaluated when handling
// bookmarking events.
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       IsBookmarkedRulesEvaluatedOnBookmarkEvents) {}

// Tests that the rules with isBookmarked: false are evaluated when handling
// bookmarking events.
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       NotBookmarkedRulesEvaluatedOnBookmarkEvents) {}

// https://crbug.com/497586
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       WebContentsWithoutTabAddedNotificationAtOnLoaded) {}

// https://crbug.com/501225
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       PendingWebContentsClearedOnRemoveRules) {}

// https://crbug.com/517492
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       RemoveAllRulesAfterExtensionUninstall) {}

// Test that an extension with a RequestContentScript rule in its manifest can
// be loaded.
// Regression for crbug.com/1211316, which could cause this test to flake if
// RulesRegistry::OnExtensionLoaded() was called before
// UserScriptManager::OnExtensionLoaded().
IN_PROC_BROWSER_TEST_P(DeclarativeContentApiTestWithContextType,
                       RequestContentScriptRule) {}

// TODO(wittman): Once ChromeContentRulesRegistry operates on condition and
// action interfaces, add a test that checks that a navigation always evaluates
// consistent URL state for all conditions. i.e.: if condition1 evaluates to
// false on url0 and true on url1, and condition2 evaluates to true on url0 and
// false on url1, navigate from url0 to url1 and validate that no action is
// triggered. Do the same when navigating back to url0. This kind of test is
// unfortunately not feasible with the current implementation and the existing
// supported conditions and actions.

}  // namespace
}  // namespace extensions