Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: chrome/browser/devtools/serialize_host_descriptions_unittest.cc

Issue 2835823002: SerializeHostDescriptions ignores hosts with duplicated names (Closed)
Patch Set: emplace Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/devtools/serialize_host_descriptions.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/devtools/serialize_host_descriptions.h" 5 #include "chrome/browser/devtools/serialize_host_descriptions.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "testing/gmock/include/gmock/gmock.h" 11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 using ::testing::UnorderedElementsAre;
15
14 namespace { 16 namespace {
15 17
16 HostDescriptionNode GetDummyNode() { 18 HostDescriptionNode GetNodeWithLabel(const char* name, int label) {
17 return {std::string(), std::string(), base::DictionaryValue()}; 19 HostDescriptionNode node = {name, std::string(), base::DictionaryValue()};
20 node.representation.SetInteger("label", label);
21 return node;
18 } 22 }
19 23
20 HostDescriptionNode GetNodeWithLabel(int id) { 24 // Returns the list of children of |arg|.
21 base::DictionaryValue dict; 25 const base::Value* GetChildren(const base::Value& arg) {
22 dict.SetInteger("label", id); 26 const base::DictionaryValue* dict = nullptr;
23 return {std::string(), std::string(), std::move(dict)}; 27 EXPECT_TRUE(arg.GetAsDictionary(&dict));
28
29 const base::Value* children = nullptr;
30 if (!dict->Get("children", &children))
31 return nullptr;
32 EXPECT_EQ(base::Value::Type::LIST, children->type());
33 return children;
24 } 34 }
25 35
26 int GetLabel(const base::DictionaryValue* dict) { 36 // Checks that |arg| is a description of a node with label |l|.
37 bool CheckLabel(const base::Value& arg, int l) {
38 const base::DictionaryValue* dict = nullptr;
39 EXPECT_TRUE(arg.GetAsDictionary(&dict));
27 int result = 0; 40 int result = 0;
28 EXPECT_TRUE(dict->GetInteger("label", &result)); 41 if (!dict->GetInteger("label", &result))
29 return result; 42 return false;
43 return l == result;
44 }
45
46 // Matches every |arg| with label |label| and checks that it has no children.
47 MATCHER_P(EmptyNode, label, "") {
48 if (!CheckLabel(arg, label))
49 return false;
50 EXPECT_FALSE(GetChildren(arg));
51 return true;
30 } 52 }
31 53
32 } // namespace 54 } // namespace
33 55
34 TEST(SerializeDictionaryForestTest, Empty) { 56 TEST(SerializeHostDescriptionTest, Empty) {
35 base::ListValue result = 57 base::ListValue result =
36 SerializeHostDescriptions(std::vector<HostDescriptionNode>(), "123"); 58 SerializeHostDescriptions(std::vector<HostDescriptionNode>(), "123");
37 EXPECT_TRUE(result.empty()); 59 EXPECT_THAT(result.base::Value::GetList(), ::testing::IsEmpty());
38 } 60 }
39 61
40 // Test serializing a forest of stubs (no edges). 62 // Test serializing a forest of stubs (no edges).
41 TEST(SerializeDictionaryForestTest, Stubs) { 63 TEST(SerializeHostDescriptionTest, Stubs) {
42 base::ListValue result = SerializeHostDescriptions( 64 base::ListValue result = SerializeHostDescriptions(
43 {GetDummyNode(), GetDummyNode(), GetDummyNode()}, "123"); 65 {GetNodeWithLabel("1", 1), GetNodeWithLabel("2", 2),
44 EXPECT_EQ(3u, result.GetSize()); 66 GetNodeWithLabel("3", 3)},
45 for (const base::Value& value : result) { 67 "children");
46 const base::DictionaryValue* dict = nullptr; 68 EXPECT_THAT(result.base::Value::GetList(),
47 ASSERT_TRUE(value.GetAsDictionary(&dict)); 69 UnorderedElementsAre(EmptyNode(1), EmptyNode(2), EmptyNode(3)));
48 EXPECT_FALSE(dict->HasKey("123")); 70 }
49 } 71
72 // Test handling multiple nodes sharing the same name.
73 TEST(SerializeHostDescriptionTest, SameNames) {
74 std::vector<HostDescriptionNode> nodes = {
75 GetNodeWithLabel("A", 1), GetNodeWithLabel("A", 2),
76 GetNodeWithLabel("A", 3), GetNodeWithLabel("B", 4),
77 GetNodeWithLabel("C", 5)};
78
79 base::ListValue result =
80 SerializeHostDescriptions(std::move(nodes), "children");
81
82 // Only the first node called "A", and both nodes "B" and "C" should be
83 // returned.
84 EXPECT_THAT(result.base::Value::GetList(),
85 UnorderedElementsAre(EmptyNode(1), EmptyNode(4), EmptyNode(5)));
50 } 86 }
51 87
52 // Test serializing a small forest, of this structure: 88 // Test serializing a small forest, of this structure:
53 // 5 -- 2 -- 4 89 // 5 -- 2 -- 4
54 // 0 -- 6 90 // 0 -- 6
55 // \ 1 91 // \ 1
56 // \ 3 92 // \ 3
57 TEST(SerializeDictionaryForestTest, Forest) { 93
94 namespace {
95
96 // Matchers for non-empty nodes specifically in this test:
97 MATCHER(Node2, "") {
98 if (!CheckLabel(arg, 2))
99 return false;
100 EXPECT_THAT(GetChildren(arg)->GetList(), UnorderedElementsAre(EmptyNode(4)));
101 return true;
102 }
103
104 MATCHER(Node5, "") {
105 if (!CheckLabel(arg, 5))
106 return false;
107 EXPECT_THAT(GetChildren(arg)->GetList(), UnorderedElementsAre(Node2()));
108 return true;
109 }
110
111 MATCHER(Node0, "") {
112 if (!CheckLabel(arg, 0))
113 return false;
114 EXPECT_THAT(GetChildren(arg)->GetList(),
115 UnorderedElementsAre(EmptyNode(1), EmptyNode(3), EmptyNode(6)));
116 return true;
117 }
118
119 } // namespace
120
121 TEST(SerializeHostDescriptionTest, Forest) {
58 std::vector<HostDescriptionNode> nodes(7); 122 std::vector<HostDescriptionNode> nodes(7);
59 const char* kNames[] = {"0", "1", "2", "3", "4", "5", "6"}; 123 const char* kNames[] = {"0", "1", "2", "3", "4", "5", "6"};
60 for (size_t i = 0; i < 7; ++i) { 124 for (size_t i = 0; i < 7; ++i) {
61 nodes[i] = GetNodeWithLabel(i); 125 nodes[i] = GetNodeWithLabel(kNames[i], i);
62 nodes[i].name = kNames[i];
63 } 126 }
64 nodes[2].parent_name = "5"; 127 nodes[2].parent_name = "5";
65 nodes[4].parent_name = "2"; 128 nodes[4].parent_name = "2";
66 nodes[6].parent_name = "0"; 129 nodes[6].parent_name = "0";
67 nodes[1].parent_name = "0"; 130 nodes[1].parent_name = "0";
68 nodes[3].parent_name = "0"; 131 nodes[3].parent_name = "0";
69 132
70 base::ListValue result = 133 base::ListValue result =
71 SerializeHostDescriptions(std::move(nodes), "children"); 134 SerializeHostDescriptions(std::move(nodes), "children");
72 135
73 EXPECT_EQ(2u, result.GetSize()); 136 EXPECT_THAT(result.base::Value::GetList(),
74 const base::Value* value = nullptr; 137 UnorderedElementsAre(Node0(), Node5()));
75 const base::DictionaryValue* dict = nullptr;
76 const base::ListValue* list = nullptr;
77
78 // Check the result. Note that sibling nodes are in the same order in which
79 // they appear in |nodes|.
80
81 // Node 0
82 ASSERT_TRUE(result.Get(0, &value));
83 ASSERT_TRUE(value->GetAsDictionary(&dict));
84 EXPECT_EQ(0, GetLabel(dict));
85 ASSERT_TRUE(dict->GetList("children", &list));
86 EXPECT_EQ(3u, list->GetSize());
87
88 // Nodes 1, 3, 6
89 constexpr int kLabels[] = {1, 3, 6};
90 for (int i = 0; i < 3; ++i) {
91 ASSERT_TRUE(list->Get(i, &value));
92 ASSERT_TRUE(value->GetAsDictionary(&dict));
93 EXPECT_EQ(kLabels[i], GetLabel(dict));
94 EXPECT_FALSE(dict->HasKey("children"));
95 }
96
97 // Node 5
98 ASSERT_TRUE(result.Get(1, &value));
99 ASSERT_TRUE(value->GetAsDictionary(&dict));
100 EXPECT_EQ(5, GetLabel(dict));
101 ASSERT_TRUE(dict->GetList("children", &list));
102 EXPECT_EQ(1u, list->GetSize());
103
104 // Node 2
105 ASSERT_TRUE(list->Get(0, &value));
106 ASSERT_TRUE(value->GetAsDictionary(&dict));
107 EXPECT_EQ(2, GetLabel(dict));
108 ASSERT_TRUE(dict->GetList("children", &list));
109 EXPECT_EQ(1u, list->GetSize());
110
111 // Node 4
112 ASSERT_TRUE(list->Get(0, &value));
113 ASSERT_TRUE(value->GetAsDictionary(&dict));
114 EXPECT_EQ(4, GetLabel(dict));
115 EXPECT_FALSE(dict->HasKey("children"));
116 } 138 }
OLDNEW
« no previous file with comments | « chrome/browser/devtools/serialize_host_descriptions.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698