chromium/extensions/browser/renderer_startup_helper_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/renderer_startup_helper.h"

#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "components/crx_file/id_util.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_factory.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/extensions_test.h"
#include "extensions/browser/test_extensions_browser_client.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/mojom/host_id.mojom.h"
#include "extensions/common/mojom/renderer.mojom.h"
#include "extensions/common/permissions/permissions_data.h"
#include "mojo/public/cpp/bindings/associated_receiver_set.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/constants/chromeos_features.h"
#include "chromeos/constants/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#endif

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chromeos/crosapi/mojom/crosapi.mojom.h"
#include "chromeos/startup/browser_init_params.h"
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS)

namespace extensions {

// Class that implements the binding of a new Renderer mojom interface and
// can receive callbacks on it for testing validation.
class RendererStartupHelperInterceptor : public RendererStartupHelper,
                                         public mojom::Renderer {};

class RendererStartupHelperTest : public ExtensionsTest {};

// Tests extension loading, unloading and activation and render process creation
// and termination.
TEST_F(RendererStartupHelperTest, NormalExtensionLifecycle) {}

// Tests that activating an extension in an uninitialized render process works
// fine.
TEST_F(RendererStartupHelperTest, EnabledBeforeProcessInitialized) {}

// Tests that themes aren't loaded in a renderer process.
TEST_F(RendererStartupHelperTest, LoadTheme) {}

// Tests that only incognito-enabled extensions are loaded in an incognito
// context.
TEST_F(RendererStartupHelperTest, ExtensionInIncognitoRenderer) {}

// Tests that platform apps are always loaded in an incognito renderer.
TEST_F(RendererStartupHelperTest, PlatformAppInIncognitoRenderer) {}

#if BUILDFLAG(IS_CHROMEOS)
class RendererStartupHelperTestCaptivePortalPopupWindow
    : public RendererStartupHelperTest {
 public:
  RendererStartupHelperTestCaptivePortalPopupWindow() = default;
  ~RendererStartupHelperTestCaptivePortalPopupWindow() override = default;
  void SetUp() override {
#if BUILDFLAG(IS_CHROMEOS_ASH)
    feature_list_.InitAndEnableFeature(
        chromeos::features::kCaptivePortalPopupWindow);
#elif BUILDFLAG(IS_CHROMEOS_LACROS)
    crosapi::mojom::BrowserInitParamsPtr init_params =
        chromeos::BrowserInitParams::GetForTests()->Clone();
    init_params->is_captive_portal_popup_window_enabled = true;
    chromeos::BrowserInitParams::SetInitParamsForTests(std::move(init_params));
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

    RendererStartupHelperTest::SetUp();
    static_cast<TestingPrefServiceSimple*>(pref_service())
        ->registry()
        ->RegisterBooleanPref(chromeos::prefs::kCaptivePortalSignin, false);
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

// Tests that only incognito-enabled extensions are loaded in an incognito
// context.
TEST_F(RendererStartupHelperTestCaptivePortalPopupWindow,
       ExtensionInCaptivePortalSigninRenderer) {
  // Set prefs::kCaptivePortalSignin to true in the shared PerfService instance.
  ASSERT_TRUE(pref_service());
  pref_service()->SetBoolean(chromeos::prefs::kCaptivePortalSignin, true);
  extensions_browser_client()->set_pref_service_for_context(incognito_context(),
                                                            pref_service());

  // Initialize the incognito renderer.
  EXPECT_FALSE(IsProcessInitialized(incognito_render_process_host_.get()));
  SimulateRenderProcessCreated(incognito_render_process_host_.get());
  EXPECT_TRUE(IsProcessInitialized(incognito_render_process_host_.get()));

  // Enable the extension. With the pref set it *should* be loaded in the
  // initialized incognito renderer.
  helper_->clear_extensions();
  AddExtensionToRegistry(extension_);
  helper_->OnExtensionLoaded(*extension_);
  EXPECT_TRUE(util::IsIncognitoEnabled(extension_->id(), incognito_context()));
  EXPECT_TRUE(IsExtensionLoaded(*extension_));
}
#endif

}  // namespace extensions