chromium/chrome/browser/extensions/event_metrics_browsertest.cc

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/service_worker_context.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/service_worker/service_worker_task_queue.h"
#include "extensions/browser/service_worker/service_worker_test_utils.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/extension_id.h"
#include "extensions/test/extension_background_page_waiter.h"
#include "extensions/test/extension_test_message_listener.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 {
// TODO(crbug.com/40909770): Create test cases where we test "failures" like
// events not acking.

ContextType;
EventMetricsBrowserTest;
TestServiceWorkerTaskQueueObserver;

TestServiceWorkerContextObserver;

// Tests that the only the dispatch time histogram provided to the test is
// emitted with a sane value, and that other provided metrics are not emitted.
// TODO(crbug.com/40282331): Disabled on ASAN due to leak caused by renderer gin
// objects which are intended to be leaked.
#if defined(ADDRESS_SANITIZER)
#define MAYBE_DispatchMetricTest
#else
#define MAYBE_DispatchMetricTest
#endif
IN_PROC_BROWSER_TEST_F(EventMetricsBrowserTest, MAYBE_DispatchMetricTest) {}

// Tests that only the dispatch time histogram for a persistent background page
// extension is emitted with a sane value, and that the same metric for other
// background context types are not emitted.
IN_PROC_BROWSER_TEST_F(EventMetricsBrowserTest,
                       PersistentBackgroundDispatchMetricTest) {}

// Tests that only the dispatch time histogram for a persistent background page
// extension is emitted with a sane value, and that the same metric for other
// background context types are not emitted.
IN_PROC_BROWSER_TEST_F(EventMetricsBrowserTest,
                       PersistentBackgroundStaleEventsMetricTest) {}

// Tests that for every event received there is a corresponding emit of starting
// and finishing status of the service worker external request.
IN_PROC_BROWSER_TEST_F(EventMetricsBrowserTest, ExternalRequestMetrics) {}

// Tests that an active event page will emit the proper dispatch time metric.
IN_PROC_BROWSER_TEST_F(EventMetricsBrowserTest,
                       EventPageDispatchToAckTimeActive) {}

// Tests that an inactive event page will emit the proper dispatch time metric.
IN_PROC_BROWSER_TEST_F(EventMetricsBrowserTest,
                       EventPageDispatchToAckTimeInactive) {}

// Tests that an active service worker will emit the proper dispatch time
// metric.
IN_PROC_BROWSER_TEST_F(EventMetricsBrowserTest,
                       ServiceWorkerDispatchToAckTimeActive) {}

// Tests that an inactive service worker will emit the proper dispatch time
// metric.
IN_PROC_BROWSER_TEST_F(EventMetricsBrowserTest,
                       ServiceWorkerDispatchToAckTimeInactive) {}

// Tests that when an event is "late" in being acked (not acked within a certain
// time) that we emit failure metrics for it.
// TODO(crbug.com/338378835): test is flaky across platforms.
IN_PROC_BROWSER_TEST_F(EventMetricsBrowserTest,
                       DISABLED_ServiceWorkerLateEventAckMetricTest) {}

// Tests that a running service worker will be unnecessarily started when it
// receives an event while it is already started if there are no pending events
// (the worker worker isn't in the process of starting).
IN_PROC_BROWSER_TEST_F(EventMetricsBrowserTest,
                       ServiceWorkerRedundantStartCountTest) {}

class EventMetricsDispatchToSenderBrowserTest
    : public ExtensionBrowserTest,
      public testing::WithParamInterface<ContextType> {};

// Tests that the we do not emit event dispatch time metrics for webRequest
// events with active listeners.
IN_PROC_BROWSER_TEST_P(EventMetricsDispatchToSenderBrowserTest,
                       DispatchToSenderMetricTest) {}

INSTANTIATE_TEST_SUITE_P();
INSTANTIATE_TEST_SUITE_P();

class LazyBackgroundEventMetricsApiTest : public ExtensionApiTest {};

// Tests that if there is a listener in the extension renderer process, but that
// listener is not in the lazy background page script, then do not emit
// background context event dispatching histograms.
IN_PROC_BROWSER_TEST_F(
    LazyBackgroundEventMetricsApiTest,
    ContextsOutsideLazyBackgroundDoNotEmitBackgroundContextMetrics) {}

}  // namespace

}  // namespace extensions