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_dictionary_forest.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 DictionaryForestNode GetDummyNode() { |
| 17 return {nullptr, base::DictionaryValue()}; |
| 18 } |
| 19 |
| 20 DictionaryForestNode GetNodeWithLabel(int id) { |
| 21 base::DictionaryValue dict; |
| 22 dict.SetInteger("label", id); |
| 23 return {nullptr, 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 SerializeDictionaryForest(std::vector<DictionaryForestNode>(), "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 = SerializeDictionaryForest( |
| 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 // 0 -- 1 -- 2 |
| 54 // 3 -- 4 |
| 55 // \ 5 |
| 56 // \ 6 |
| 57 TEST(SerializeDictionaryForestTest, Forest) { |
| 58 std::vector<DictionaryForestNode> nodes(7); |
| 59 for (size_t i = 0; i < 7; ++i) |
| 60 nodes[i] = GetNodeWithLabel(i); |
| 61 nodes[1].parent = &nodes[0]; |
| 62 nodes[2].parent = &nodes[1]; |
| 63 nodes[4].parent = &nodes[3]; |
| 64 nodes[5].parent = &nodes[3]; |
| 65 nodes[6].parent = &nodes[3]; |
| 66 |
| 67 base::ListValue result = |
| 68 SerializeDictionaryForest(std::move(nodes), "children"); |
| 69 |
| 70 EXPECT_EQ(2u, result.GetSize()); |
| 71 const base::Value* value = nullptr; |
| 72 const base::DictionaryValue* dict = nullptr; |
| 73 const base::ListValue* list = nullptr; |
| 74 |
| 75 // Node 0 |
| 76 ASSERT_TRUE(result.Get(0, &value)); |
| 77 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| 78 EXPECT_EQ(0, GetLabel(dict)); |
| 79 ASSERT_TRUE(dict->GetList("children", &list)); |
| 80 EXPECT_EQ(1u, list->GetSize()); |
| 81 |
| 82 // Node 1 |
| 83 ASSERT_TRUE(list->Get(0, &value)); |
| 84 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| 85 EXPECT_EQ(1, GetLabel(dict)); |
| 86 ASSERT_TRUE(dict->GetList("children", &list)); |
| 87 EXPECT_EQ(1u, list->GetSize()); |
| 88 |
| 89 // Node 2 |
| 90 ASSERT_TRUE(list->Get(0, &value)); |
| 91 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| 92 EXPECT_EQ(2, GetLabel(dict)); |
| 93 EXPECT_FALSE(dict->HasKey("children")); |
| 94 |
| 95 // Node 3 |
| 96 ASSERT_TRUE(result.Get(1, &value)); |
| 97 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| 98 EXPECT_EQ(3, GetLabel(dict)); |
| 99 ASSERT_TRUE(dict->GetList("children", &list)); |
| 100 EXPECT_EQ(3u, list->GetSize()); |
| 101 |
| 102 // Nodes 4-6 |
| 103 for (int i = 0; i < 3; ++i) { |
| 104 ASSERT_TRUE(list->Get(i, &value)); |
| 105 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| 106 EXPECT_EQ(i + 4, GetLabel(dict)); |
| 107 EXPECT_FALSE(dict->HasKey("children")); |
| 108 } |
| 109 } |
OLD | NEW |