chromium/chrome/browser/extensions/api/extension_action/extension_action_apitest.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 <map>
#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
#include "chrome/browser/extensions/api/extension_action/test_extension_action_api_observer.h"
#include "chrome/browser/extensions/api/extension_action/test_icon_image_observer.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/extensions/extension_action_test_helper.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/sessions/content/session_tab_helper.h"
#include "components/version_info/channel.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/background_script_executor.h"
#include "extensions/browser/extension_action.h"
#include "extensions/browser/extension_action_manager.h"
#include "extensions/browser/extension_icon_image.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/script_executor.h"
#include "extensions/browser/service_worker/service_worker_test_utils.h"
#include "extensions/browser/state_store.h"
#include "extensions/common/api/extension_action/action_info.h"
#include "extensions/common/api/extension_action/action_info_test_util.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_extension_dir.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/base/window_open_disposition.h"
#include "ui/gfx/color_utils.h"

namespace extensions {
namespace {

// A background script that allows for setting the icon dynamically.
constexpr char kSetIconBackgroundJsTemplate[] =;

constexpr char kPageHtmlTemplate[] =;

// Runs |script| in the given |web_contents| and waits for it to send a
// test-passed result. This will fail if the test in |script| fails. Note:
// |web_contents| is expected to be an extension contents with access to
// extension APIs.
void RunTestAndWaitForSuccess(content::WebContents* web_contents,
                              const std::string& script) {}

// A helper class to track StateStore changes.
class TestStateStoreObserver : public StateStore::TestObserver {};

// A helper class to handle setting or getting the values for an action from JS.
class ActionTestHelper {};

// Forces a flush of the StateStore, where action state is persisted.
void FlushStateStore(Profile* profile) {}

}  // namespace

// A class that allows for cross-origin navigations with embedded test server.
class ExtensionActionAPITest : public ExtensionApiTest {};

// Alias these for readability, when a test only exercises one type of action.
BrowserActionAPITest;
PageActionAPITest;

// A class that runs tests exercising each type of possible toolbar action.
class MultiActionAPITest
    : public ExtensionActionAPITest,
      public testing::WithParamInterface<ActionInfo::Type> {};

// Canvas tests rely on the harness producing pixel output in order to read back
// pixels from a canvas element. So we have to override the setup function.
class MultiActionAPICanvasTest : public MultiActionAPITest {};

// Check that updating the browser action badge for a specific tab id does not
// cause a disk write (since we only persist the defaults).
// Only browser actions persist settings.
IN_PROC_BROWSER_TEST_F(BrowserActionAPITest, TestNoUnnecessaryIO) {}

// Verify that tab-specific values are cleared on navigation and on tab
// removal. Regression test for https://crbug.com/834033.
IN_PROC_BROWSER_TEST_P(MultiActionAPITest,
                       ValuesAreClearedOnNavigationAndTabRemoval) {}

// Tests that tooltips of an extension action icon can be specified using UTF8.
// See http://crbug.com/25349.
IN_PROC_BROWSER_TEST_P(MultiActionAPITest, TitleLocalization) {}

// Tests dispatching the onClicked event to listeners when the extension action
// in the toolbar is pressed.
IN_PROC_BROWSER_TEST_P(MultiActionAPITest, OnClickedDispatching) {}

// Tests the creation of a popup when one is specified in the manifest.
IN_PROC_BROWSER_TEST_P(MultiActionAPITest, PopupCreation) {}

// Tests that sessionStorage does not persist between closing and opening of a
// popup.
// TODO(crbug.com/40795982): Flaky on Linux.
#if BUILDFLAG(IS_LINUX)
#define MAYBE_SessionStorageDoesNotPersistBetweenOpenings
#else
#define MAYBE_SessionStorageDoesNotPersistBetweenOpenings
#endif
IN_PROC_BROWSER_TEST_P(MultiActionAPITest,
                       MAYBE_SessionStorageDoesNotPersistBetweenOpenings) {}

ActionAndBrowserActionAPITest;

// Tests whether action values persist across sessions.
// Note: Since pageActions are only applicable on a specific tab, this test
// doesn't apply to them.
IN_PROC_BROWSER_TEST_P(ActionAndBrowserActionAPITest, PRE_ValuesArePersisted) {}

IN_PROC_BROWSER_TEST_P(ActionAndBrowserActionAPITest, ValuesArePersisted) {}

// Tests setting the icon dynamically from the background page.
// TODO(crbug.com/40230315): flaky.
IN_PROC_BROWSER_TEST_P(MultiActionAPICanvasTest, DISABLED_DynamicSetIcon) {}

// Tests calling setIcon() from JS with hooks that might cause issues with our
// custom bindings.
// Regression test for https://crbug.com/1087948.
IN_PROC_BROWSER_TEST_P(MultiActionAPITest, SetIconWithJavascriptHooks) {}

// Tests calling setIcon() from JS with `self` defined at the top-level.
// Regression test for https://crbug.com/1087948.
IN_PROC_BROWSER_TEST_P(MultiActionAPITest, SetIconWithSelfDefined) {}

// Tests calling setIcon() for a tab with an invalid icon path specified.
IN_PROC_BROWSER_TEST_P(MultiActionAPITest, SetIconInTabWithInvalidPath) {}

// Tests calling setIcon() in the service worker with an invalid icon paths
// specified. Regression test for https://crbug.com/1262029. Regression test for
// https://crbug.com/1372518.
IN_PROC_BROWSER_TEST_F(ExtensionActionAPITest, SetIconInWorkerWithInvalidPath) {}

// Tests multiple cases of setting an invalid popup that violate same-origin
// checks.
IN_PROC_BROWSER_TEST_P(MultiActionAPITest, SetPopupWithInvalidPath) {}

// Tests various getter and setter methods.
IN_PROC_BROWSER_TEST_P(MultiActionAPITest, GettersAndSetters) {}

// Tests the functions to enable and disable extension actions.
IN_PROC_BROWSER_TEST_P(MultiActionAPITest, EnableAndDisable) {}

// Tests that the check for enabled and disabled status are correctly reported.
IN_PROC_BROWSER_TEST_F(ExtensionActionAPITest, IsEnabled) {}

// Tests that isEnabled correctly ignores declarativeContent rules for enable.
IN_PROC_BROWSER_TEST_F(ExtensionActionAPITest, IsEnabledIgnoreDeclarative) {}

ActionAPITest;

IN_PROC_BROWSER_TEST_F(ActionAPITest, TestGetUserSettings) {}

// Tests dispatching the onUserSettingsChanged event to listeners when the user
// pins or unpins the extension action.
IN_PROC_BROWSER_TEST_F(ActionAPITest, OnUserSettingsChanged) {}

// Tests that invalid badge text colors return an API error to the caller.
IN_PROC_BROWSER_TEST_F(ActionAPITest, TestBadgeTextColorErrors) {}

// Tests the setting and unsetting of badge text works for both global and tab
// specific cases.
IN_PROC_BROWSER_TEST_P(ActionAndBrowserActionAPITest,
                       TestSetBadgeTextGlobalAndTab) {}

class ExtensionActionWithOpenPopupFeatureDisabledTest
    : public ExtensionActionAPITest {};

// Tests that the action.openPopup() API is available to policy-installed
// extensions on even if the feature flag is disabled. Since this is controlled
// through our features files (which are tested separately), this is more of a
// smoke test than an end-to-end test.
// TODO(crbug.com/40057101): Remove this test when the API is available
// for all extensions on stable without a feature flag.
IN_PROC_BROWSER_TEST_F(ExtensionActionWithOpenPopupFeatureDisabledTest,
                       OpenPopupAvailabilityOnStableChannel) {}

INSTANTIATE_TEST_SUITE_P();

INSTANTIATE_TEST_SUITE_P();

INSTANTIATE_TEST_SUITE_P();

}  // namespace extensions