chromium/extensions/browser/extension_registrar_unittest.cc

// Copyright 2017 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/extension_registrar.h"

#include <memory>
#include <optional>

#include "base/location.h"
#include "base/task/sequenced_task_runner.h"
#include "build/chromeos_buildflags.h"
#include "content/public/browser/browser_context.h"
#include "extensions/browser/blocklist_extension_prefs.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extensions_test.h"
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/browser/test_extensions_browser_client.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "base/test/scoped_command_line.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ash/crosapi/browser_util.h"
#include "chrome/common/pref_names.h"  // nogncheck
#include "chromeos/ash/components/standalone_browser/feature_refs.h"
#include "chromeos/ash/components/standalone_browser/lacros_availability.h"
#include "components/account_id/account_id.h"  // nogncheck
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

namespace extensions {

namespace {

Return;
_;

LoadErrorBehavior;

class TestExtensionSystem : public MockExtensionSystem {};

class TestExtensionRegistrarDelegate : public ExtensionRegistrar::Delegate {};

}  // namespace

class ExtensionRegistrarTest : public ExtensionsTest {};

TEST_F(ExtensionRegistrarTest, Basic) {}

TEST_F(ExtensionRegistrarTest, AlreadyEnabled) {}

TEST_F(ExtensionRegistrarTest, Disable) {}

TEST_F(ExtensionRegistrarTest, DisableAndEnable) {}

TEST_F(ExtensionRegistrarTest, AddDisabled) {}

TEST_F(ExtensionRegistrarTest, AddForceEnabled) {}

TEST_F(ExtensionRegistrarTest, AddForceDisabled) {}

TEST_F(ExtensionRegistrarTest, AddBlocklisted) {}

TEST_F(ExtensionRegistrarTest, AddBlocked) {}

TEST_F(ExtensionRegistrarTest, TerminateExtension) {}

TEST_F(ExtensionRegistrarTest, DisableTerminatedExtension) {}

TEST_F(ExtensionRegistrarTest, EnableTerminatedExtension) {}

TEST_F(ExtensionRegistrarTest, ReloadExtension) {}

TEST_F(ExtensionRegistrarTest, RemoveReloadedExtension) {}

TEST_F(ExtensionRegistrarTest, ReloadTerminatedExtension) {}

// Test that an extension which is not controlled (e.g. by policy) and which is
// not on the ash keep-list can be disabled.
TEST_F(ExtensionRegistrarTest, DisableNotAshKeeplistedExtension) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)
// Test that a controlled extension that is not on the ash keep-list can be
// disabled if ash is disabled.
TEST_F(ExtensionRegistrarTest,
       DisableNotAshKeeplistedForceInstalledExtensionIfAshDisabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitWithFeatures(ash::standalone_browser::GetFeatureRefs(), {});
  base::test::ScopedCommandLine scoped_command_line;
  scoped_command_line.GetProcessCommandLine()->AppendSwitch(
      ash::switches::kEnableLacrosForTesting);
  auto fake_user_manager = std::make_unique<user_manager::FakeUserManager>();
  auto* primary_user =
      fake_user_manager->AddUser(AccountId::FromUserEmail("test@test"));
  fake_user_manager->UserLoggedIn(primary_user->GetAccountId(),
                                  primary_user->username_hash(),
                                  /*browser_restart=*/false,
                                  /*is_child=*/false);
  auto scoped_user_manager = std::make_unique<user_manager::ScopedUserManager>(
      std::move(fake_user_manager));

  static_cast<TestingPrefServiceSimple*>(pref_service())
      ->registry()
      ->RegisterIntegerPref(
          prefs::kLacrosLaunchSwitch,
          static_cast<int>(
              ash::standalone_browser::LacrosAvailability::kLacrosOnly));
  EXPECT_FALSE(crosapi::browser_util::IsAshWebBrowserEnabled());

  // Prevent the extension from being disabled (by the user).
  ON_CALL(*delegate(), CanDisableExtension(extension().get()))
      .WillByDefault(Return(false));
  AddEnabledExtension();

  TryDisablingNotAshKeeplistedExtension(/* expect_extension_disabled= */ true);
}

// Test that a controlled extension that is not on the ash keep-list cannot be
// disabled if ash is still enabled.
TEST_F(ExtensionRegistrarTest,
       NotDisableNotAshKeeplistedForceInstalledExtensionIfAshEnabled) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitWithFeatures({}, ash::standalone_browser::GetFeatureRefs());
  static_cast<TestingPrefServiceSimple*>(pref_service())
      ->registry()
      ->RegisterIntegerPref(
          prefs::kLacrosLaunchSwitch,
          static_cast<int>(
              ash::standalone_browser::LacrosAvailability::kLacrosOnly));
  EXPECT_TRUE(crosapi::browser_util::IsAshWebBrowserEnabled());

  // Prevent the extension from being disabled (by the user).
  ON_CALL(*delegate(), CanDisableExtension(extension().get()))
      .WillByDefault(Return(false));
  AddEnabledExtension();

  TryDisablingNotAshKeeplistedExtension(/* expect_extension_disabled= */ false);
}
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

}  // namespace extensions