blob: 16bd1da97a5babca6e2af27155f86c5e05e323e6 [file] [log] [blame]
Lingqi Chidb949732022-11-08 05:55:211// Copyright 2022 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/browser/preloading/prerender/prerender_metrics.h"
6
7#include "base/test/metrics/histogram_tester.h"
Kouhei Ueno3f37992b2023-11-09 23:29:028#include "content/public/browser/preloading_trigger_type.h"
Lingqi Chidb949732022-11-08 05:55:219#include "net/http/http_request_headers.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace content {
13namespace {
14
Lingqi Chidb949732022-11-08 05:55:2115// Tests to ensure the ReportHeaderMismatch implementation is aligned with the
16// enum generator.
17TEST(PrerenderMetricsTest, NavigationHeaderMismatchMetric) {
18 const char kMetricName[] =
Lingqi Chi746334662024-06-04 01:11:2219 "Prerender.Experimental.ActivationHeadersMismatch.ForTesting";
Lingqi Chidb949732022-11-08 05:55:2120 {
21 base::HistogramTester histogram_tester;
Doug Turnerb204399e2025-07-30 16:51:1622 PrerenderCancellationReason reason = PrerenderCancellationReason::
23 CreateCandidateReasonForActivationParameterMismatch();
24 net::HttpRequestHeaders prerender_headers;
25 prerender_headers.SetHeader("Content-Type", "ab");
26 prerender_headers.SetHeader("If-Match", "xy");
27 net::HttpRequestHeaders potential_headers;
28 potential_headers.SetHeader("Content-Type", "cd");
29 potential_headers.SetHeader("If-Match", "xy");
30 ASSERT_FALSE(PrerenderHost::IsActivationHeaderMatch(
31 potential_headers, prerender_headers, reason));
32 reason.ReportMetrics(".ForTesting");
Lingqi Chidb949732022-11-08 05:55:2133 // label="content-type: value mismatch"
34 histogram_tester.ExpectUniqueSample(kMetricName, 808179719, 1);
35 }
36 {
37 base::HistogramTester histogram_tester;
Doug Turnerb204399e2025-07-30 16:51:1638 PrerenderCancellationReason reason = PrerenderCancellationReason::
39 CreateCandidateReasonForActivationParameterMismatch();
40 net::HttpRequestHeaders prerender_headers;
41 prerender_headers.SetHeader("Content-Type", "ab");
42 net::HttpRequestHeaders potential_headers;
43 potential_headers.SetHeader("Content-Type", "ab");
44 potential_headers.SetHeader("If-Match", "xys");
45 ASSERT_FALSE(PrerenderHost::IsActivationHeaderMatch(
46 potential_headers, prerender_headers, reason));
47 reason.ReportMetrics(".ForTesting");
Lingqi Chidb949732022-11-08 05:55:2148 // label="if-match: missing in prerendering request's headers"
49 histogram_tester.ExpectUniqueSample(kMetricName, 667272509, 1);
50 }
51 {
52 base::HistogramTester histogram_tester;
Doug Turnerb204399e2025-07-30 16:51:1653 PrerenderCancellationReason reason = PrerenderCancellationReason::
54 CreateCandidateReasonForActivationParameterMismatch();
55 net::HttpRequestHeaders prerender_headers;
56 prerender_headers.SetHeader("service-worker", "xyz");
57 net::HttpRequestHeaders potential_headers;
58 ASSERT_FALSE(PrerenderHost::IsActivationHeaderMatch(
59 potential_headers, prerender_headers, reason));
60 reason.ReportMetrics(".ForTesting");
Lingqi Chidb949732022-11-08 05:55:2161 // label="service-worker: missing in activation request's headers"
62 histogram_tester.ExpectUniqueSample(kMetricName, -578377770, 1);
63 }
Lingqi Chidb949732022-11-08 05:55:2164}
65
66} // namespace
67} // namespace content