Lingqi Chi | db94973 | 2022-11-08 05:55:21 | [diff] [blame] | 1 | // 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 Ueno | 3f37992b | 2023-11-09 23:29:02 | [diff] [blame] | 8 | #include "content/public/browser/preloading_trigger_type.h" |
Lingqi Chi | db94973 | 2022-11-08 05:55:21 | [diff] [blame] | 9 | #include "net/http/http_request_headers.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace content { |
| 13 | namespace { |
| 14 | |
Lingqi Chi | db94973 | 2022-11-08 05:55:21 | [diff] [blame] | 15 | // Tests to ensure the ReportHeaderMismatch implementation is aligned with the |
| 16 | // enum generator. |
| 17 | TEST(PrerenderMetricsTest, NavigationHeaderMismatchMetric) { |
| 18 | const char kMetricName[] = |
Lingqi Chi | 74633466 | 2024-06-04 01:11:22 | [diff] [blame] | 19 | "Prerender.Experimental.ActivationHeadersMismatch.ForTesting"; |
Lingqi Chi | db94973 | 2022-11-08 05:55:21 | [diff] [blame] | 20 | { |
| 21 | base::HistogramTester histogram_tester; |
Doug Turner | b204399e | 2025-07-30 16:51:16 | [diff] [blame] | 22 | 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 Chi | db94973 | 2022-11-08 05:55:21 | [diff] [blame] | 33 | // label="content-type: value mismatch" |
| 34 | histogram_tester.ExpectUniqueSample(kMetricName, 808179719, 1); |
| 35 | } |
| 36 | { |
| 37 | base::HistogramTester histogram_tester; |
Doug Turner | b204399e | 2025-07-30 16:51:16 | [diff] [blame] | 38 | 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 Chi | db94973 | 2022-11-08 05:55:21 | [diff] [blame] | 48 | // label="if-match: missing in prerendering request's headers" |
| 49 | histogram_tester.ExpectUniqueSample(kMetricName, 667272509, 1); |
| 50 | } |
| 51 | { |
| 52 | base::HistogramTester histogram_tester; |
Doug Turner | b204399e | 2025-07-30 16:51:16 | [diff] [blame] | 53 | 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 Chi | db94973 | 2022-11-08 05:55:21 | [diff] [blame] | 61 | // label="service-worker: missing in activation request's headers" |
| 62 | histogram_tester.ExpectUniqueSample(kMetricName, -578377770, 1); |
| 63 | } |
Lingqi Chi | db94973 | 2022-11-08 05:55:21 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | } // namespace content |