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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/devtools/serialize_host_descriptions_unittest.cc
diff --git a/chrome/browser/devtools/serialize_host_descriptions_unittest.cc b/chrome/browser/devtools/serialize_host_descriptions_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3e3935ed3e2acf8da473a185407c0cf447e4702e
--- /dev/null
+++ b/chrome/browser/devtools/serialize_host_descriptions_unittest.cc
@@ -0,0 +1,112 @@
+// 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_host_descriptions.h"
+
+#include <utility>
+#include <vector>
+
+#include "base/values.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+HostDescriptionNode GetDummyNode() {
+ return {std::string(), std::string(), base::DictionaryValue()};
+}
+
+HostDescriptionNode GetNodeWithLabel(int id) {
+ base::DictionaryValue dict;
+ dict.SetInteger("label", id);
+ return {std::string(), std::string(), 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 =
+ SerializeHostDescriptions(std::vector<HostDescriptionNode>(), "123");
+ EXPECT_TRUE(result.empty());
+}
+
+// Test serializing a forest of stubs (no edges).
+TEST(SerializeDictionaryForestTest, Stubs) {
+ base::ListValue result = SerializeHostDescriptions(
+ {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:
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.
+// 0 -- 1 -- 2
+// 3 -- 4
+// \ 5
+// \ 6
+TEST(SerializeDictionaryForestTest, Forest) {
+ std::vector<HostDescriptionNode> nodes(7);
+ const char* kNames[] = {"0", "1", "2", "3", "4", "5", "6"};
+ for (size_t i = 0; i < 7; ++i) {
+ nodes[i] = GetNodeWithLabel(i);
+ nodes[i].name = kNames[i];
+ }
+ nodes[1].parent_name = "0";
+ nodes[2].parent_name = "1";
+ nodes[4].parent_name = "3";
+ nodes[5].parent_name = "3";
+ nodes[6].parent_name = "3";
+
+ base::ListValue result =
+ SerializeHostDescriptions(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"));
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698