Index: chrome/browser/devtools/serialize_dictionary_forest_unittest.cc |
diff --git a/chrome/browser/devtools/serialize_dictionary_forest_unittest.cc b/chrome/browser/devtools/serialize_dictionary_forest_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6a4e7bc2e6bab84b19e4a8e8ba18af04218b543b |
--- /dev/null |
+++ b/chrome/browser/devtools/serialize_dictionary_forest_unittest.cc |
@@ -0,0 +1,109 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/devtools/serialize_dictionary_forest.h" |
+ |
+#include <utility> |
+#include <vector> |
+ |
+#include "base/values.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+DictionaryForestNode GetDummyNode() { |
+ return {nullptr, base::DictionaryValue()}; |
+} |
+ |
+DictionaryForestNode GetNodeWithLabel(int id) { |
+ base::DictionaryValue dict; |
+ dict.SetInteger("label", id); |
+ return {nullptr, std::move(dict)}; |
+} |
+ |
+int GetLabel(const base::DictionaryValue* dict) { |
+ int result = 0; |
+ EXPECT_TRUE(dict->GetInteger("label", &result)); |
+ return result; |
+} |
+ |
+} // namespace |
+ |
+TEST(SerializeDictionaryForestTest, Empty) { |
+ base::ListValue result = |
+ SerializeDictionaryForest(std::vector<DictionaryForestNode>(), "123"); |
+ EXPECT_TRUE(result.empty()); |
+} |
+ |
+// Test serializing a forest of stubs (no edges). |
+TEST(SerializeDictionaryForestTest, Stubs) { |
+ base::ListValue result = SerializeDictionaryForest( |
+ {GetDummyNode(), GetDummyNode(), GetDummyNode()}, "123"); |
+ EXPECT_EQ(3u, result.GetSize()); |
+ for (const base::Value& value : result) { |
+ const base::DictionaryValue* dict = nullptr; |
+ ASSERT_TRUE(value.GetAsDictionary(&dict)); |
+ EXPECT_FALSE(dict->HasKey("123")); |
+ } |
+} |
+ |
+// Test serializing a small forest, of this structure: |
+// 0 -- 1 -- 2 |
+// 3 -- 4 |
+// \ 5 |
+// \ 6 |
+TEST(SerializeDictionaryForestTest, Forest) { |
+ std::vector<DictionaryForestNode> nodes(7); |
+ for (size_t i = 0; i < 7; ++i) |
+ nodes[i] = GetNodeWithLabel(i); |
+ nodes[1].parent = &nodes[0]; |
+ nodes[2].parent = &nodes[1]; |
+ nodes[4].parent = &nodes[3]; |
+ nodes[5].parent = &nodes[3]; |
+ nodes[6].parent = &nodes[3]; |
+ |
+ base::ListValue result = |
+ SerializeDictionaryForest(std::move(nodes), "children"); |
+ |
+ EXPECT_EQ(2u, result.GetSize()); |
+ const base::Value* value = nullptr; |
+ const base::DictionaryValue* dict = nullptr; |
+ const base::ListValue* list = nullptr; |
+ |
+ // Node 0 |
+ ASSERT_TRUE(result.Get(0, &value)); |
+ ASSERT_TRUE(value->GetAsDictionary(&dict)); |
+ EXPECT_EQ(0, GetLabel(dict)); |
+ ASSERT_TRUE(dict->GetList("children", &list)); |
+ EXPECT_EQ(1u, list->GetSize()); |
+ |
+ // Node 1 |
+ ASSERT_TRUE(list->Get(0, &value)); |
+ ASSERT_TRUE(value->GetAsDictionary(&dict)); |
+ EXPECT_EQ(1, GetLabel(dict)); |
+ ASSERT_TRUE(dict->GetList("children", &list)); |
+ EXPECT_EQ(1u, list->GetSize()); |
+ |
+ // Node 2 |
+ ASSERT_TRUE(list->Get(0, &value)); |
+ ASSERT_TRUE(value->GetAsDictionary(&dict)); |
+ EXPECT_EQ(2, GetLabel(dict)); |
+ EXPECT_FALSE(dict->HasKey("children")); |
+ |
+ // Node 3 |
+ ASSERT_TRUE(result.Get(1, &value)); |
+ ASSERT_TRUE(value->GetAsDictionary(&dict)); |
+ EXPECT_EQ(3, GetLabel(dict)); |
+ ASSERT_TRUE(dict->GetList("children", &list)); |
+ EXPECT_EQ(3u, list->GetSize()); |
+ |
+ // Nodes 4-6 |
+ for (int i = 0; i < 3; ++i) { |
+ ASSERT_TRUE(list->Get(i, &value)); |
+ ASSERT_TRUE(value->GetAsDictionary(&dict)); |
+ EXPECT_EQ(i + 4, GetLabel(dict)); |
+ EXPECT_FALSE(dict->HasKey("children")); |
+ } |
+} |