OLD | NEW |
(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: |
| 53 // 5 -- 2 -- 4 |
| 54 // 0 -- 6 |
| 55 // \ 1 |
| 56 // \ 3 |
| 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[2].parent_name = "5"; |
| 65 nodes[4].parent_name = "2"; |
| 66 nodes[6].parent_name = "0"; |
| 67 nodes[1].parent_name = "0"; |
| 68 nodes[3].parent_name = "0"; |
| 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 // 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 } |
OLD | NEW |