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

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

Issue 2825533002: Introduce SerializeDictionaryForest for devtools_target_ui (Closed)
Patch Set: Let the helper compute the tree 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/devtools/serialize_host_descriptions.h"
6
7 #include <utility>
8 #include <vector>
9
10 #include "base/values.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 HostDescriptionNode GetDummyNode() {
17 return {std::string(), std::string(), base::DictionaryValue()};
18 }
19
20 HostDescriptionNode GetNodeWithLabel(int id) {
21 base::DictionaryValue dict;
22 dict.SetInteger("label", id);
23 return {std::string(), std::string(), std::move(dict)};
24 }
25
26 int GetLabel(const base::DictionaryValue* dict) {
27 int result = 0;
28 EXPECT_TRUE(dict->GetInteger("label", &result));
29 return result;
30 }
31
32 } // namespace
33
34 TEST(SerializeDictionaryForestTest, Empty) {
35 base::ListValue result =
36 SerializeHostDescriptions(std::vector<HostDescriptionNode>(), "123");
37 EXPECT_TRUE(result.empty());
38 }
39
40 // Test serializing a forest of stubs (no edges).
41 TEST(SerializeDictionaryForestTest, Stubs) {
42 base::ListValue result = SerializeHostDescriptions(
43 {GetDummyNode(), GetDummyNode(), GetDummyNode()}, "123");
44 EXPECT_EQ(3u, result.GetSize());
45 for (const base::Value& value : result) {
46 const base::DictionaryValue* dict = nullptr;
47 ASSERT_TRUE(value.GetAsDictionary(&dict));
48 EXPECT_FALSE(dict->HasKey("123"));
49 }
50 }
51
52 // Test serializing a small forest, of this structure:
dgozman 2017/04/19 22:05:14 I'd suggest less linear test to check the topologi
vabr (Chromium) 2017/04/20 07:29:39 Done.
53 // 0 -- 1 -- 2
54 // 3 -- 4
55 // \ 5
56 // \ 6
57 TEST(SerializeDictionaryForestTest, Forest) {
58 std::vector<HostDescriptionNode> nodes(7);
59 const char* kNames[] = {"0", "1", "2", "3", "4", "5", "6"};
60 for (size_t i = 0; i < 7; ++i) {
61 nodes[i] = GetNodeWithLabel(i);
62 nodes[i].name = kNames[i];
63 }
64 nodes[1].parent_name = "0";
65 nodes[2].parent_name = "1";
66 nodes[4].parent_name = "3";
67 nodes[5].parent_name = "3";
68 nodes[6].parent_name = "3";
69
70 base::ListValue result =
71 SerializeHostDescriptions(std::move(nodes), "children");
72
73 EXPECT_EQ(2u, result.GetSize());
74 const base::Value* value = nullptr;
75 const base::DictionaryValue* dict = nullptr;
76 const base::ListValue* list = nullptr;
77
78 // Node 0
79 ASSERT_TRUE(result.Get(0, &value));
80 ASSERT_TRUE(value->GetAsDictionary(&dict));
81 EXPECT_EQ(0, GetLabel(dict));
82 ASSERT_TRUE(dict->GetList("children", &list));
83 EXPECT_EQ(1u, list->GetSize());
84
85 // Node 1
86 ASSERT_TRUE(list->Get(0, &value));
87 ASSERT_TRUE(value->GetAsDictionary(&dict));
88 EXPECT_EQ(1, GetLabel(dict));
89 ASSERT_TRUE(dict->GetList("children", &list));
90 EXPECT_EQ(1u, list->GetSize());
91
92 // Node 2
93 ASSERT_TRUE(list->Get(0, &value));
94 ASSERT_TRUE(value->GetAsDictionary(&dict));
95 EXPECT_EQ(2, GetLabel(dict));
96 EXPECT_FALSE(dict->HasKey("children"));
97
98 // Node 3
99 ASSERT_TRUE(result.Get(1, &value));
100 ASSERT_TRUE(value->GetAsDictionary(&dict));
101 EXPECT_EQ(3, GetLabel(dict));
102 ASSERT_TRUE(dict->GetList("children", &list));
103 EXPECT_EQ(3u, list->GetSize());
104
105 // Nodes 4-6
106 for (int i = 0; i < 3; ++i) {
107 ASSERT_TRUE(list->Get(i, &value));
108 ASSERT_TRUE(value->GetAsDictionary(&dict));
109 EXPECT_EQ(i + 4, GetLabel(dict));
110 EXPECT_FALSE(dict->HasKey("children"));
111 }
112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698