chromium/chrome/browser/extensions/events_apitest.cc

// Copyright 2010 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/files/file_path.h"
#include "base/files/file_util.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/api/permissions/permissions_api.h"
#include "chrome/browser/extensions/chrome_extension_test_notification_observer.h"
#include "chrome/browser/extensions/chrome_extensions_browser_client.h"
#include "chrome/browser/extensions/chrome_test_extension_loader.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/extensions/api/tabs.h"
#include "chrome/common/extensions/api/web_navigation.h"
#include "chrome/test/base/profile_destruction_waiter.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/result_codes.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/service_worker_test_helpers.h"
#include "extensions/browser/background_script_executor.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_event_histogram_value.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/process_manager_observer.h"
#include "extensions/test/extension_background_page_waiter.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 "testing/gtest/include/gtest/gtest.h"

namespace extensions {

namespace {

ContextType;

IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Events) {}

// Tests that events are unregistered when an extension page shuts down.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, EventsAreUnregistered) {}

// Test that listeners for webview-related events are not stored (even for lazy
// contexts). See crbug.com/736381.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WebViewEventRegistration) {}

// Tests that registering a listener for an event that requires a permission and
// then removing that permission using the permissions API does not lead to a
// crash. Regression test for crbug.com/1402642.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, EventAfterPermissionRemoved) {}

// Tests that events broadcast right after a profile has started to be destroyed
// do not cause a crash. Regression test for crbug.com/1335837.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DispatchEventDuringShutdown) {}

class EventsApiTest : public ExtensionApiTest {};

// Tests that updating an extension sends runtime.onInstalled event to the
// updated extension.
IN_PROC_BROWSER_TEST_F(EventsApiTest, ExtensionUpdateSendsOnInstalledEvent) {}

// Tests that if updating an extension makes the extension disabled (due to
// permissions increase), then enabling the extension fires runtime.onInstalled
// correctly to the updated extension.
IN_PROC_BROWSER_TEST_F(EventsApiTest,
                       UpdateDispatchesOnInstalledAfterEnablement) {}

// This test is OK on Windows, but times out on other platforms.
// https://crbug.com/833854
#if BUILDFLAG(IS_WIN)
#define MAYBE_NewlyIntroducedListener
#else
#define MAYBE_NewlyIntroducedListener
#endif
// Tests that if an extension's updated version has a new lazy listener, it
// fires properly after the update.
IN_PROC_BROWSER_TEST_F(EventsApiTest, MAYBE_NewlyIntroducedListener) {}

// Tests that, if an extension registers multiple listeners for a filtered
// event where the listeners overlap, but are not identical, each listener is
// only triggered once for a given event.
// TODO(crbug.com/40365717): This test is currently (intentionally)
// testing improper behavior and will be fixed as part of the linked bug.
IN_PROC_BROWSER_TEST_F(
    EventsApiTest,
    MultipleFilteredListenersWithOverlappingFiltersShouldOnlyTriggerOnce) {}

class ChromeUpdatesEventsApiTest : public EventsApiTest,
                                   public ProcessManagerObserver {};

IN_PROC_BROWSER_TEST_F(ChromeUpdatesEventsApiTest, PRE_ChromeUpdates) {}

// Test that we only dispatch the onInstalled event triggered by a chrome update
// to extensions that have a registered onInstalled listener.
IN_PROC_BROWSER_TEST_F(ChromeUpdatesEventsApiTest, ChromeUpdates) {}

// TODO(crbug.com/41493334): Also test extensions with service workers and their
// equivalent to ExtensionHost (EventAckData) for these scenarios:
//   1. Dispatch to background context acks (duplicate of
//     DispatchToBackgroundPage_Acks)
//   2. Dispatch to content script doesn't ack (duplicate of
//     DispatchToContentScript_DoesNotRecordMessageForAcking)
//   2. Dispatch guest view event (EventRouter::DispatchEventToSender()) acks
//     (similar to DispatchToPage_Acks)

class EventDispatchingApiTest
    : public ExtensionApiTest,
      public testing::WithParamInterface<ContextType> {};

// Tests that background pages will receive an event message (routed through the
// EventRouter::DispatchToProcess() flow) and properly track and remove the
// unacked event message in ExtensionHost. Rather than send generate webRequest
// events this uses storage events to use this flow.
IN_PROC_BROWSER_TEST_P(EventDispatchingApiTest, DispatchToBackgroundPage_Acks) {}

INSTANTIATE_TEST_SUITE_P();
INSTANTIATE_TEST_SUITE_P();

// This allows tests to perform web navigations that trigger webRequest API
// events to be sent.
class NavigatingEventDispatchingApiTest : public EventDispatchingApiTest {};

PersistentBackgroundPageDispatchEventToSenderEventApiTest;

// Tests that persistent background pages will receive an event message (routed
// through the EventRouter::DispatchEventToSender() flow) and properly track and
// remove the unacked event message in ExtensionHost. Only persistent background
// pages can use the webRequest API so event pages are not tested.
IN_PROC_BROWSER_TEST_F(
    PersistentBackgroundPageDispatchEventToSenderEventApiTest,
    DispatchToPage_Acks) {}

// Tests that an event targeted to a content script listener is not recorded in
// unacked event messages in ExtensionHost (for event pages and persistent
// background pages). EventRouter::DispatchEventToSender() flow is not tested
// since content scripts cannot listen to webRequest events.
IN_PROC_BROWSER_TEST_P(NavigatingEventDispatchingApiTest,
                       DispatchToContentScript_DoesNotRecordMessageForAcking) {}

INSTANTIATE_TEST_SUITE_P();
INSTANTIATE_TEST_SUITE_P();

ServiceWorkerEventAckBrowserTest;

// Tests that when a renderer process is no longer available that we clear any
// unacked events from EventAckData for that render process. Otherwise we would
// leak these unacked events and never remove them.
IN_PROC_BROWSER_TEST_F(ServiceWorkerEventAckBrowserTest,
                       RendererProcessGoesAway_ClearsUnackedEventData) {}

}  // namespace
}  // namespace extensions