chromium/chrome/browser/extensions/api/management/management_api_unittest.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 "extensions/browser/api/management/management_api.h"

#include <memory>
#include <optional>
#include <utility>

#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/metrics/user_action_tester.h"
#include "base/types/optional_ref.h"
#include "base/values.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/background/background_contents.h"
#include "chrome/browser/extensions/extension_install_prompt_show_params.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_service_test_base.h"
#include "chrome/browser/extensions/extension_service_test_with_install.h"
#include "chrome/browser/extensions/extension_uninstall_dialog.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/supervised_user/supervised_user_extensions_delegate_impl.h"
#include "chrome/browser/supervised_user/supervised_user_extensions_metrics_recorder.h"
#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
#include "chrome/browser/supervised_user/supervised_user_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/extensions/extensions_dialogs.h"
#include "chrome/test/base/test_browser_window.h"
#include "components/supervised_user/core/browser/supervised_user_service.h"
#include "components/supervised_user/core/common/features.h"
#include "components/supervised_user/core/common/pref_names.h"
#include "components/supervised_user/core/common/supervised_user_constants.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/gpu_data_manager.h"
#include "content/public/test/web_contents_tester.h"
#include "extensions/browser/api/management/management_api_constants.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/disable_reason.h"
#include "extensions/browser/event_router_factory.h"
#include "extensions/browser/extension_dialog_auto_confirm.h"
#include "extensions/browser/extension_function_dispatcher.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/management_policy.h"
#include "extensions/browser/supervised_user_extensions_delegate.h"
#include "extensions/browser/test_management_policy.h"
#include "extensions/browser/uninstall_reason.h"
#include "extensions/common/api/management.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/extension_urls.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/common/permissions/permission_set.h"

ManifestLocation;

namespace extensions {

namespace {

std::unique_ptr<KeyedService> BuildManagementApi(
    content::BrowserContext* context) {}

std::unique_ptr<KeyedService> BuildEventRouter(
    content::BrowserContext* profile) {}

constants;

// TODO(devlin): Unittests are awesome. Test more with unittests and less with
// heavy api/browser tests.
class ManagementApiUnitTest : public ExtensionServiceTestWithInstall {};

bool ManagementApiUnitTest::RunFunction(
    const scoped_refptr<ExtensionFunction>& function,
    const base::Value::List& args) {}

bool ManagementApiUnitTest::RunSetEnabledFunction(
    content::WebContents* web_contents,
    const ExtensionId& extension_id,
    bool use_user_gesture,
    bool accept_dialog,
    std::string* error,
    bool enabled) {}

void ManagementApiUnitTest::SetUp() {}

void ManagementApiUnitTest::TearDown() {}

// Test the basic parts of management.setEnabled.
TEST_F(ManagementApiUnitTest, ManagementSetEnabled) {}

// Test that component extensions cannot be disabled, and that policy extensions
// can be disabled only by component/policy extensions.
TEST_F(ManagementApiUnitTest, ComponentPolicyDisabling) {}

// Test that policy extensions can be enabled only by component/policy
// extensions.
TEST_F(ManagementApiUnitTest, ComponentPolicyEnabling) {}

// Tests management.uninstall.
TEST_F(ManagementApiUnitTest, ManagementUninstall) {}

// Tests management.uninstall from the Web Store hosted app.
TEST_F(ManagementApiUnitTest, ManagementUninstallWebstoreHostedApp) {}

// Tests management.uninstall from the new Webstore domain.
TEST_F(ManagementApiUnitTest, ManagementUninstallNewWebstore) {}

// Tests management.uninstall from a normal extension, which will create a
// programmatic uninstall dialog that identifies the extension that called it.
TEST_F(ManagementApiUnitTest, ManagementUninstallProgramatic) {}
// Tests uninstalling a blocklisted extension via management.uninstall.
TEST_F(ManagementApiUnitTest, ManagementUninstallBlocklisted) {}

TEST_F(ManagementApiUnitTest, ManagementEnableOrDisableBlocklisted) {}

TEST_F(ManagementApiUnitTest, ExtensionInfo_MayEnable) {}

TEST_F(ManagementApiUnitTest, ExtensionInfo_MayDisable) {}

TEST_F(ManagementApiUnitTest, SetEnabled_UnsupportedRequirements) {}

// Tests enabling an extension via management API after it was disabled due to
// permission increase.
TEST_F(ManagementApiUnitTest, SetEnabled_IncreasedPermissions) {}

TEST_F(ManagementApiUnitTest,
       SetEnabled_UnsupportedRequirementsAndPermissionsIncrease) {}

// Test suite for cases where the user is in the "disable with re-enable"
// experiment phase.
class ManagementApiUnitTestMV2DisableWithReEnableUnitTest
    : public ManagementApiUnitTest {};

// Tests the extension is enabled when management.setEnabled is called for
// enabling an extension disabled due to the MV2 deprecation, and user accepted
// the dialog.
TEST_F(ManagementApiUnitTestMV2DisableWithReEnableUnitTest,
       SetEnabled_MV2Deprecation) {}

// Tests the extension is enabled when management.setEnabled is called for
// enabling an extension disabled due to the MV2 deprecation and with
// permissions increase, and user accepted both dialogs shown.
TEST_F(ManagementApiUnitTestMV2DisableWithReEnableUnitTest,
       SetEnabled_PermissionsIncreaseAndMV2Deprecation) {}

// A delegate that senses when extensions are enabled or disabled.
class TestManagementAPIDelegate : public ManagementAPIDelegate {};

// A delegate that allows a child to try to install an extension and tracks
// whether the parent permission dialog would have opened.
class TestSupervisedUserExtensionsDelegate
    : public SupervisedUserExtensionsDelegateImpl {};

// Whether the extension parental controls are managed by the Family Link
// "Extensions" switch ("Allow child to add extensions without asking for
// permission") or the "Permissions" switch ("Permissions for sites").
enum class ExtensionManagementSwitch : int {};

// Tests for supervised users (child accounts). Supervised users are not allowed
// to install apps or extensions unless their parent approves.
class ManagementApiSupervisedUserTest
    : public ManagementApiUnitTest,
      public ::testing::WithParamInterface<ExtensionManagementSwitch> {};

// Tests that locally approved extensions (on the feature release of
// `kEnableSupervisedUserSkipParentApprovalToInstallExtensions`) can be
// enabled by the supervised user. The enabling action does not grant parental
// approval. Prevents regressions to b/336759592.
TEST_P(ManagementApiSupervisedUserTest,
       SetEnabled_SetEnabledForLocallyApprovedExtension) {}

TEST_P(ManagementApiSupervisedUserTest, SetEnabled_BlockedByParent) {}

// Tests enabling an extension via management API after it was disabled due to
// permission increase for supervised users.
// Prevents a regression to crbug/1068660.
TEST_P(ManagementApiSupervisedUserTest, SetEnabled_AfterIncreasedPermissions) {}

// Tests that supervised users can't approve permission updates by themselves
// when the "Permissions for sites, apps and extensions" toggle is off and the
// Extension parental controls are managed by the "Permissions" Family Link
// switch.
TEST_P(ManagementApiSupervisedUserTest,
       SetEnabled_CantApprovePermissionUpdatesToggleOff) {}

// Tests that if an extension still requires parental consent, the supervised
// user approving it for permissions increase won't enable the extension and
// bypass parental consent.
// Prevents a regression to crbug/1070760.
TEST_P(ManagementApiSupervisedUserTest,
       SetEnabled_CustodianApprovalRequiredAndPermissionsIncrease) {}

// Tests that trying to enable an extension with parent approval for supervised
// users still fails, if there's unsupported requirements.
TEST_P(ManagementApiSupervisedUserTest, SetEnabled_UnsupportedRequirement) {}

// Tests UMA metrics related to supervised users enabling and disabling
// extensions.
TEST_P(ManagementApiSupervisedUserTest, SetEnabledDisabled_UmaMetrics) {}

INSTANTIATE_TEST_SUITE_P();

// Tests for supervised users (child accounts) with additional setup code.
class ManagementApiSupervisedUserTestWithSetup
    : public ManagementApiSupervisedUserTest {};

TEST_P(ManagementApiSupervisedUserTestWithSetup, SetEnabled_ParentApproves) {}

TEST_P(ManagementApiSupervisedUserTestWithSetup, SetEnabled_ParentDenies) {}

TEST_P(ManagementApiSupervisedUserTestWithSetup, SetEnabled_DialogFails) {}

TEST_P(ManagementApiSupervisedUserTestWithSetup, SetEnabled_PreviouslyAllowed) {}

// Tests launching the Parent Permission Dialog from a background page, where
// there isn't active web contents. The parent approves the request.
TEST_P(ManagementApiSupervisedUserTestWithSetup,
       SetEnabled_ParentPermissionApprovedFromBackgroundPage) {}

// Tests launching the Parent Permission Dialog from a background page, where
// there isn't active web contents. The parent cancels the request.
TEST_P(ManagementApiSupervisedUserTestWithSetup,
       SetEnabled_ParentPermissionCanceledFromBackgroundPage) {}

// Tests launching the Parent Permission Dialog from a background page, where
// there isn't active web contents. The request will fail due to some sort of
// error, such as a network error.
TEST_P(ManagementApiSupervisedUserTestWithSetup,
       SetEnabled_ParentPermissionFailedFromBackgroundPage) {}

// Tests launching the Extension Install Blocked By Parent Dialog from a
// background page, where there isn't active web contents. This dialog
// appears only then the "Permissions" Family Link switch managed the
// extension parental controls.
TEST_P(ManagementApiSupervisedUserTestWithSetup,
       SetEnabled_ExtensionInstallBlockedByParentFromBackgroundPage) {}

INSTANTIATE_TEST_SUITE_P();

}  // namespace
}  // namespace extensions