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
|
// Copyright 2025 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/metrics/dwa/dwa_entry_builder.h"
#include <string>
#include <unordered_set>
#include "base/metrics/metrics_hashes.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace dwa {
// Tests that calling `SetMetric` repeatedly on an `DwaEntryBuilder` updates the
// value stored for the metric.
TEST(DwaEntryBuilderTest, BuilderAllowsUpdatingMetrics) {
DwaEntryBuilder builder("Kangaroo.Jumped");
builder.SetMetric("Length", 4);
EXPECT_THAT(
(*builder.GetEntryForTesting())->metrics,
testing::ElementsAre(testing::Pair(base::HashMetricName("Length"), 4)));
builder.SetMetric("Length", 5);
EXPECT_THAT(
(*builder.GetEntryForTesting())->metrics,
testing::ElementsAre(testing::Pair(base::HashMetricName("Length"), 5)));
}
// Tests that calling `AddStudy` repeatedly on an `DwaEntryBuilder` updates the
// value stored for the study.
TEST(DwaEntryBuilderTest, BuilderAllowsAddingStudiesOfInterest) {
DwaEntryBuilder builder("Kangaroo.Jumped");
builder.AddToStudiesOfInterest("Study1");
EXPECT_THAT((*builder.GetEntryForTesting())->studies_of_interest,
testing::ElementsAre(testing::Pair("Study1", true)));
builder.AddToStudiesOfInterest("Study2");
EXPECT_THAT((*builder.GetEntryForTesting())->studies_of_interest,
testing::ElementsAre(testing::Pair("Study1", true),
testing::Pair("Study2", true)));
builder.AddToStudiesOfInterest("Study1");
EXPECT_THAT((*builder.GetEntryForTesting())->studies_of_interest,
testing::ElementsAre(testing::Pair("Study1", true),
testing::Pair("Study2", true)));
}
TEST(DwaEntryBuilderTest, BuilderAllowsSettingContent) {
DwaEntryBuilder builder("Kangaroo.Jumped");
builder.SetContent("https://google.com/content");
EXPECT_THAT((*builder.GetEntryForTesting())->content_hash,
testing::Eq(base::HashMetricName("google.com")));
// Invalid content are expected to be hashed as empty strings.
builder.SetContent("content_");
EXPECT_THAT((*builder.GetEntryForTesting())->content_hash,
testing::Eq(base::HashMetricName("")));
}
TEST(DwaEntryBuilderTest, SanitizeContent) {
struct {
std::string input;
std::string expected_output;
} test_cases[] = {
{"https://google.com", "google.com"},
{"https://google.com/", "google.com"},
{"https://google.com/mail", "google.com"},
{"https://google.com/file.html", "google.com"},
{"https://www.google.com/mail", "google.com"},
{"https://..google.com/file.html", "google.com"},
{"https://www.google.co.uk/mail", "google.co.uk"},
{"http://www.google.co.uk/mail", "google.co.uk"},
{"http://www.google.co.uk/mail/", "google.co.uk"},
{"http://www.google.co.uk/xyz/index.html", "google.co.uk"},
{"mail://www.google.co.uk/mail", "google.co.uk"},
{"https://google.com./file.html", "google.com."},
{"https://a.b.co.uk/file.html", "b.co.uk"},
{"http://foo.bar/file.html", "foo.bar"},
{"google.co.uk/", ""},
{"google.co.uk", ""},
{"google.co.uk/mail", ""},
{"www.google.co.uk/mail/", ""},
{"file:///C:/bar.html", ""},
{"http://foo.com../file.html", ""},
{"http://192.168.0.1/file.html", ""},
{"http://bar/file.html", ""},
{"http://co.uk/file.html", ""},
};
for (const auto& test_case : test_cases) {
EXPECT_THAT(DwaEntryBuilder::SanitizeContent(test_case.input),
testing::Eq(test_case.expected_output));
}
}
} // namespace dwa
|