xref: /aosp_15_r20/external/cronet/components/metrics/structured/mojom/mojom_traits_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 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 <string>
6 
7 #include "base/values.h"
8 #include "components/metrics/structured/event.h"
9 #include "components/metrics/structured/mojom/event.mojom.h"
10 #include "components/metrics/structured/mojom/event_mojom_traits.h"
11 #include "mojo/public/cpp/test_support/test_utils.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 using testing::AllOf;
16 using testing::ContainerEq;
17 using testing::Eq;
18 using testing::Property;
19 
20 namespace metrics::structured {
21 
22 namespace {
ExpectEventsEqual(const Event & expected,const Event & actual)23 void ExpectEventsEqual(const Event& expected, const Event& actual) {
24   EXPECT_THAT(expected,
25               AllOf(Property(&Event::project_name, Eq(actual.project_name())),
26                     Property(&Event::event_name, Eq(actual.event_name())),
27                     Property(&Event::IsEventSequenceType,
28                              Eq(actual.IsEventSequenceType()))));
29 
30   for (const auto& expected_pair : expected.metric_values()) {
31     auto actual_pair = actual.metric_values().find(expected_pair.first);
32     ASSERT_FALSE(actual_pair == actual.metric_values().end());
33     EXPECT_EQ(expected_pair.second, actual_pair->second);
34   }
35 
36   // Check uptimes only if event is part of a sequence.
37   if (expected.IsEventSequenceType() && actual.IsEventSequenceType()) {
38     EXPECT_EQ(expected.recorded_time_since_boot(),
39               actual.recorded_time_since_boot());
40   }
41 }
42 
43 }  // namespace
44 
TEST(EventStructTraitsTest,ValidEvent)45 TEST(EventStructTraitsTest, ValidEvent) {
46   const std::string kProjectName = "project_name";
47   const std::string kEventName = "event_name";
48 
49   Event test_event(kProjectName, kEventName);
50 
51   ASSERT_TRUE(test_event.AddMetric("hmac", Event::MetricType::kHmac,
52                                    base::Value("1234")));
53   ASSERT_TRUE(test_event.AddMetric("long", Event::MetricType::kLong,
54                                    base::Value("123456789")));
55   ASSERT_TRUE(
56       test_event.AddMetric("int", Event::MetricType::kInt, base::Value(123)));
57   ASSERT_TRUE(test_event.AddMetric("double", Event::MetricType::kDouble,
58                                    base::Value(123.4)));
59   ASSERT_TRUE(test_event.AddMetric("string", Event::MetricType::kRawString,
60                                    base::Value("string")));
61   ASSERT_TRUE(test_event.AddMetric("boolean", Event::MetricType::kBoolean,
62                                    base::Value(false)));
63 
64   // Doesn't matter what the values of the string are.
65   Event output("", "");
66   ASSERT_TRUE(
67       mojo::test::SerializeAndDeserialize<mojom::Event>(test_event, output));
68   ExpectEventsEqual(test_event, output);
69 }
70 
TEST(EventStructTraitsTest,EventWithUptime)71 TEST(EventStructTraitsTest, EventWithUptime) {
72   Event sequence_event("project_name", "event_name", true);
73   sequence_event.SetRecordedTimeSinceBoot(base::Microseconds(500));
74 
75   ASSERT_TRUE(sequence_event.AddMetric("double", Event::MetricType::kDouble,
76                                        base::Value(1.0)));
77 
78   // Doesn't matter what the values of the string are.
79   Event test_output("", "");
80   ASSERT_TRUE(mojo::test::SerializeAndDeserialize<mojom::Event>(sequence_event,
81                                                                 test_output));
82   ExpectEventsEqual(sequence_event, test_output);
83 }
84 
85 }  // namespace metrics::structured
86