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 "base/test/metrics/action_suffix_reader.h"
6
7 #include <optional>
8 #include <string>
9
10 #include "base/containers/contains.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace base {
14
15 constexpr char kTestActionXml[] = R"(
16 <actions>
17
18 <action name="Action1">
19 <owner>notarealuser@google.com</owner>
20 <description>This is an action</description>
21 </action>
22
23 <action-suffix separator="." ordering="suffix">
24 <suffix name="Suffix1" label="The first suffix"/>
25 <affected-action name="Action.WithSuffix1"/>
26 <suffix name="Suffix2" label="The second suffix"/>
27 <affected-action name="Action.WithSuffix2"/>
28 </action-suffix>
29
30 <action-suffix separator="." ordering="suffix">
31 <suffix name="Suffix3" label="The third suffix"/>
32 <affected-action name="Action.WithSuffix3"/>
33 </action-suffix>
34
35 <action-suffix separator="." ordering="suffix">
36 <affected-action name="Action.WithSuffix1"/>
37 <suffix name="Suffix4" label="The fourth suffix"/>
38 </action-suffix>
39
40 </actions>
41 )";
42
43 // Forward declare the private entry point for testing. This prevents having to
44 // import XmlReader, which is visible from base::test_support, but not
45 // base_unittests.
46 extern std::vector<ActionSuffixEntryMap> ReadActionSuffixesForActionForTesting(
47 const std::string& xml_string,
48 const std::string& affected_action);
49
TEST(ActionSuffixReaderTest,NoSuffixesFound)50 TEST(ActionSuffixReaderTest, NoSuffixesFound) {
51 const auto results = ReadActionSuffixesForActionForTesting(
52 kTestActionXml, "Action.DoesNotExist");
53 EXPECT_TRUE(results.empty());
54 }
55
TEST(ActionSuffixReaderTest,OneResult)56 TEST(ActionSuffixReaderTest, OneResult) {
57 const auto results = ReadActionSuffixesForActionForTesting(
58 kTestActionXml, "Action.WithSuffix3");
59 EXPECT_EQ(1U, results.size());
60 EXPECT_EQ(1U, results[0].size());
61 EXPECT_TRUE(Contains(results[0], "Suffix3"));
62 }
63
TEST(ActionSuffixReaderTest,OneResultFromBlockWithOtherActions)64 TEST(ActionSuffixReaderTest, OneResultFromBlockWithOtherActions) {
65 const auto results = ReadActionSuffixesForActionForTesting(
66 kTestActionXml, "Action.WithSuffix2");
67 EXPECT_EQ(1U, results.size());
68 EXPECT_EQ(2U, results[0].size());
69 EXPECT_TRUE(Contains(results[0], "Suffix1"));
70 EXPECT_TRUE(Contains(results[0], "Suffix2"));
71 }
72
TEST(ActionSuffixReaderTest,MultipleResults)73 TEST(ActionSuffixReaderTest, MultipleResults) {
74 const auto results = ReadActionSuffixesForActionForTesting(
75 kTestActionXml, "Action.WithSuffix1");
76 EXPECT_EQ(2U, results.size());
77 EXPECT_EQ(2U, results[0].size());
78 EXPECT_TRUE(Contains(results[0], "Suffix1"));
79 EXPECT_TRUE(Contains(results[0], "Suffix2"));
80 EXPECT_EQ(1U, results[1].size());
81 EXPECT_TRUE(Contains(results[1], "Suffix4"));
82 }
83
TEST(ActionSuffixReaderTest,CallActualMethod)84 TEST(ActionSuffixReaderTest, CallActualMethod) {
85 const auto results = ReadActionSuffixesForAction("Action.DoesNotExist");
86 EXPECT_TRUE(results.empty());
87 }
88
89 } // namespace base
90