1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
// 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 "components/page_load_metrics/browser/page_load_metrics_util.h"
#include <optional>
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "components/page_load_metrics/browser/fake_page_load_metrics_observer_delegate.h"
#include "components/page_load_metrics/browser/features.h"
#include "components/page_load_metrics/common/page_load_metrics.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace page_load_metrics {
class PageLoadMetricsUtilTest : public testing::Test {};
TEST_F(PageLoadMetricsUtilTest, QueryContainsComponent) {
struct {
bool expected_result;
const char* query;
const char* component;
} test_cases[] = {
{true, "a=b", "a=b"},
{true, "a=b&c=d", "a=b"},
{true, "a=b&c=d", "c=d"},
{true, "a=b&c=d&e=f", "c=d"},
{true, "za=b&a=b", "a=b"},
{true, "a=bz&a=b", "a=b"},
{true, "a=ba=b&a=b", "a=b"},
{true, "a=a=a&a=a", "a=a"},
{true, "source=web", "source=web"},
{true, "a=b&source=web", "source=web"},
{true, "a=b&source=web&c=d", "source=web"},
{false, "a=a=a", "a=a"},
{false, "", ""},
{false, "a=b", ""},
{false, "", "a=b"},
{false, "za=b", "a=b"},
{false, "za=bz", "a=b"},
{false, "a=bz", "a=b"},
{false, "za=b&c=d", "a=b"},
{false, "a=b&c=dz", "c=d"},
{false, "a=b&zc=d&e=f", "c=d"},
{false, "a=b&c=dz&e=f", "c=d"},
{false, "a=b&zc=dz&e=f", "c=d"},
{false, "a=b&foosource=web&c=d", "source=web"},
{false, "a=b&source=webbar&c=d", "source=web"},
{false, "a=b&foosource=webbar&c=d", "source=web"},
// Correctly handle cases where there is a leading "?" or "#" character.
{true, "?a=b&source=web", "a=b"},
{false, "a=b&?source=web", "source=web"},
};
for (const auto& test : test_cases) {
EXPECT_EQ(test.expected_result, page_load_metrics::QueryContainsComponent(
test.query, test.component))
<< "For query: " << test.query << " with component: " << test.component;
}
}
TEST_F(PageLoadMetricsUtilTest, QueryContainsComponentPrefix) {
struct {
bool expected_result;
const char* query;
const char* component;
} test_cases[] = {
{true, "a=b", "a="},
{true, "a=b&c=d", "a="},
{true, "a=b&c=d", "c="},
{true, "a=b&c=d&e=f", "c="},
{true, "za=b&a=b", "a="},
{true, "ba=a=b&a=b", "a="},
{true, "q=test", "q="},
{true, "a=b&q=test", "q="},
{true, "q=test&c=d", "q="},
{true, "a=b&q=test&c=d", "q="},
{false, "", ""},
{false, "za=b", "a="},
{false, "za=b&c=d", "a="},
{false, "a=b&zc=d", "c="},
{false, "a=b&zc=d&e=f", "c="},
{false, "a=b&zq=test&c=d", "q="},
{false, "ba=a=b", "a="},
};
for (const auto& test : test_cases) {
EXPECT_EQ(test.expected_result,
page_load_metrics::QueryContainsComponentPrefix(test.query,
test.component))
<< "For query: " << test.query << " with component: " << test.component;
}
}
TEST_F(PageLoadMetricsUtilTest, UmaMaxCumulativeShiftScoreHistogram) {
constexpr char kTestMaxCumulativeShiftScoreSessionWindow[] = "Test";
const page_load_metrics::NormalizedCLSData normalized_cls_data{0.5, false};
base::HistogramTester histogram_tester;
page_load_metrics::UmaMaxCumulativeShiftScoreHistogram10000x(
kTestMaxCumulativeShiftScoreSessionWindow, normalized_cls_data);
histogram_tester.ExpectTotalCount(kTestMaxCumulativeShiftScoreSessionWindow,
1);
histogram_tester.ExpectBucketCount(kTestMaxCumulativeShiftScoreSessionWindow,
5000, 1);
}
TEST_F(PageLoadMetricsUtilTest, GetNonPrerenderingBackgroundStartTiming) {
struct {
PrerenderingState prerendering_state;
std::optional<base::TimeDelta> activation_start;
PageVisibility visibility_at_start_or_activation_;
std::optional<base::TimeDelta> time_to_first_background;
std::optional<base::TimeDelta> expected_result;
} test_cases[] = {
{PrerenderingState::kNoPrerendering, std::nullopt,
PageVisibility::kForeground, std::nullopt, std::nullopt},
{PrerenderingState::kNoPrerendering, std::nullopt,
PageVisibility::kForeground, base::Seconds(2), base::Seconds(2)},
{PrerenderingState::kNoPrerendering, std::nullopt,
PageVisibility::kBackground, std::nullopt, base::Seconds(0)},
{PrerenderingState::kNoPrerendering, std::nullopt,
PageVisibility::kBackground, base::Seconds(2), base::Seconds(0)},
{PrerenderingState::kInPrerendering, std::nullopt,
PageVisibility::kForeground, std::nullopt, std::nullopt},
{PrerenderingState::kInPrerendering, std::nullopt,
PageVisibility::kForeground, base::Seconds(10), std::nullopt},
{PrerenderingState::kActivatedNoActivationStart, std::nullopt,
PageVisibility::kForeground, base::Seconds(12), std::nullopt},
{PrerenderingState::kActivated, base::Seconds(10),
PageVisibility::kForeground, std::nullopt, std::nullopt},
{PrerenderingState::kActivated, base::Seconds(10),
PageVisibility::kForeground, base::Seconds(12), base::Seconds(12)},
// Invalid time_to_first_background. Not checked and may return invalid
// value.
{PrerenderingState::kActivated, base::Seconds(10),
PageVisibility::kForeground, base::Seconds(2), base::Seconds(2)},
{PrerenderingState::kActivated, base::Seconds(10),
PageVisibility::kBackground, std::nullopt, base::Seconds(10)},
{PrerenderingState::kActivated, base::Seconds(10),
PageVisibility::kBackground, base::Seconds(12), base::Seconds(10)},
// Invalid time_to_first_background. Not checked and may return invalid
// value.
{PrerenderingState::kActivated, base::Seconds(10),
PageVisibility::kBackground, base::Seconds(2), base::Seconds(10)},
};
for (const auto& test_case : test_cases) {
page_load_metrics::FakePageLoadMetricsObserverDelegate delegate;
delegate.prerendering_state_ = test_case.prerendering_state;
delegate.activation_start_ = test_case.activation_start;
if (test_case.time_to_first_background.has_value()) {
delegate.first_background_time_ =
delegate.navigation_start_ +
test_case.time_to_first_background.value();
} else {
delegate.first_background_time_ = std::nullopt;
}
switch (test_case.prerendering_state) {
case PrerenderingState::kNoPrerendering:
case PrerenderingState::kInPreview:
DCHECK_NE(test_case.visibility_at_start_or_activation_,
PageVisibility::kNotInitialized);
delegate.started_in_foreground_ =
(test_case.visibility_at_start_or_activation_ ==
PageVisibility::kForeground);
delegate.visibility_at_activation_ = PageVisibility::kNotInitialized;
break;
case PrerenderingState::kInPrerendering:
delegate.started_in_foreground_ = false;
delegate.visibility_at_activation_ = PageVisibility::kNotInitialized;
break;
case PrerenderingState::kActivatedNoActivationStart:
delegate.started_in_foreground_ = false;
delegate.visibility_at_activation_ =
test_case.visibility_at_start_or_activation_;
break;
case PrerenderingState::kActivated:
delegate.started_in_foreground_ = false;
delegate.visibility_at_activation_ =
test_case.visibility_at_start_or_activation_;
break;
}
std::optional<base::TimeDelta> got =
GetNonPrerenderingBackgroundStartTiming(delegate);
EXPECT_EQ(test_case.expected_result, got);
}
}
TEST_F(PageLoadMetricsUtilTest, CorrectEventAsNavigationOrActivationOrigined) {
struct {
PrerenderingState prerendering_state;
std::optional<base::TimeDelta> activation_start;
base::TimeDelta event;
std::optional<base::TimeDelta> expected_result;
} test_cases[] = {
// Not modified
{PrerenderingState::kNoPrerendering, std::nullopt, base::Seconds(2),
base::Seconds(2)},
// max(0, 2 - x), where x is time of activation start that may come in the
// future and should be greater than an already occurred event.
{PrerenderingState::kInPrerendering, std::nullopt, base::Seconds(2),
base::Seconds(0)},
{PrerenderingState::kActivatedNoActivationStart, std::nullopt,
base::Seconds(2), base::Seconds(0)},
// crash due to incorrect data
{PrerenderingState::kActivated, base::Seconds(10), base::Seconds(2),
base::Seconds(-1)},
// max(0, 12 - 10)
{PrerenderingState::kActivated, base::Seconds(10), base::Seconds(12),
base::Seconds(2)},
};
page_load_metrics::mojom::PageLoadTiming timing;
page_load_metrics::InitPageLoadTimingForTest(&timing);
for (const auto& test_case : test_cases) {
page_load_metrics::FakePageLoadMetricsObserverDelegate delegate;
delegate.prerendering_state_ = test_case.prerendering_state;
delegate.activation_start_ = test_case.activation_start;
auto test_expectation_runner =
[&](base::TimeDelta event,
std::optional<base::TimeDelta> expected_result) {
if (expected_result->is_negative()) {
EXPECT_DEATH_IF_SUPPORTED(
CorrectEventAsNavigationOrActivationOrigined(delegate, timing,
event),
"");
} else {
base::TimeDelta got = CorrectEventAsNavigationOrActivationOrigined(
delegate, timing, event);
EXPECT_EQ(expected_result, got);
}
};
test_expectation_runner(test_case.event, test_case.expected_result);
// Currently, multiple implementations of PageLoadMetricsObserver is
// ongoing. We'll left the old version for a while.
// TODO(crbug.com/40222513): Delete below.
timing.navigation_start = base::Time::FromSecondsSinceUnixEpoch(1);
timing.activation_start = test_case.activation_start;
test_expectation_runner(test_case.event, test_case.expected_result);
// In some path, this function is called with old PageLoadTiming, which can
// lack activation_start. The result is the same for such case.
timing.activation_start = std::nullopt;
test_expectation_runner(test_case.event, test_case.expected_result);
}
}
// A type to support parameterized testing for the category of the request.
struct UrlCategoryTestCase {
std::string test_case;
std::string url_string;
std::optional<uint32_t> expected;
};
class GetCategoryIdFromUrlTest
: public testing::Test,
public testing::WithParamInterface<UrlCategoryTestCase> {
protected:
using FeaturesType = std::vector<base::test::FeatureRefAndParams>;
GetCategoryIdFromUrlTest() {
static const FeaturesType enabled_features = {
{features::kBeaconLeakageLogging,
{{"category_prefix", "test-prefix"}}}};
scoped_feature_list_.InitWithFeaturesAndParameters(enabled_features, {});
}
~GetCategoryIdFromUrlTest() override = default;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
INSTANTIATE_TEST_SUITE_P(
All,
GetCategoryIdFromUrlTest,
testing::ValuesIn<UrlCategoryTestCase>({
{"EmptyCategory", "", std::nullopt},
{"InvalidCategory", "https://a.com?category=invalid-category",
std::nullopt},
{"ValidCategory0", "https://a.com?category=test-prefix0",
std::make_optional(0u)},
{"ValidCategory1", "https://a.com?param1=true&category=test-prefix1",
std::make_optional(1u)},
{"ValidCategory200", "https://a.com?category=test-prefix200",
std::make_optional(200u)},
}),
[](const testing::TestParamInfo<UrlCategoryTestCase>& info) {
return info.param.test_case;
});
TEST_P(GetCategoryIdFromUrlTest, GetCategoryIdFromUrl) {
EXPECT_THAT(GetCategoryIdFromUrl(GURL(GetParam().url_string)),
testing::Eq(GetParam().expected));
}
} // namespace page_load_metrics
|