• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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 "components/metrics/dwa/dwa_entry_builder.h"
6 
7 #include <string>
8 #include <unordered_set>
9 
10 #include "base/metrics/metrics_hashes.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 namespace dwa {
15 
16 // Tests that calling `SetMetric` repeatedly on an `DwaEntryBuilder` updates the
17 // value stored for the metric.
TEST(DwaEntryBuilderTest,BuilderAllowsUpdatingMetrics)18 TEST(DwaEntryBuilderTest, BuilderAllowsUpdatingMetrics) {
19   DwaEntryBuilder builder("Kangaroo.Jumped");
20   builder.SetMetric("Length", 4);
21   EXPECT_THAT(
22       (*builder.GetEntryForTesting())->metrics,
23       testing::ElementsAre(testing::Pair(base::HashMetricName("Length"), 4)));
24 
25   builder.SetMetric("Length", 5);
26   EXPECT_THAT(
27       (*builder.GetEntryForTesting())->metrics,
28       testing::ElementsAre(testing::Pair(base::HashMetricName("Length"), 5)));
29 }
30 
31 // Tests that calling `AddStudy` repeatedly on an `DwaEntryBuilder` updates the
32 // value stored for the study.
TEST(DwaEntryBuilderTest,BuilderAllowsAddingStudiesOfInterest)33 TEST(DwaEntryBuilderTest, BuilderAllowsAddingStudiesOfInterest) {
34   DwaEntryBuilder builder("Kangaroo.Jumped");
35   builder.AddToStudiesOfInterest("Study1");
36   EXPECT_THAT((*builder.GetEntryForTesting())->studies_of_interest,
37               testing::ElementsAre(testing::Pair("Study1", true)));
38 
39   builder.AddToStudiesOfInterest("Study2");
40   EXPECT_THAT((*builder.GetEntryForTesting())->studies_of_interest,
41               testing::ElementsAre(testing::Pair("Study1", true),
42                                    testing::Pair("Study2", true)));
43 
44   builder.AddToStudiesOfInterest("Study1");
45   EXPECT_THAT((*builder.GetEntryForTesting())->studies_of_interest,
46               testing::ElementsAre(testing::Pair("Study1", true),
47                                    testing::Pair("Study2", true)));
48 }
49 
TEST(DwaEntryBuilderTest,BuilderAllowsSettingContent)50 TEST(DwaEntryBuilderTest, BuilderAllowsSettingContent) {
51   DwaEntryBuilder builder("Kangaroo.Jumped");
52   builder.SetContent("content_");
53   EXPECT_THAT((*builder.GetEntryForTesting())->content_hash,
54               testing::Eq(base::HashMetricName("content_")));
55 
56   builder.SetContent("content_");
57   EXPECT_THAT((*builder.GetEntryForTesting())->content_hash,
58               testing::Eq(base::HashMetricName("content_")));
59 }
60 
61 }  // namespace dwa
62