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

Side by Side Diff: mojo/services/catalog/entry_unittest.cc

Issue 1818373003: Move serialize/deserialize logic onto Entry. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 9 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
« no previous file with comments | « mojo/services/catalog/entry.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/services/catalog/builder.h" 5 #include "mojo/services/catalog/entry.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/json/json_file_value_serializer.h" 8 #include "base/json/json_file_value_serializer.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "mojo/shell/public/cpp/capabilities.h" 12 #include "mojo/shell/public/cpp/capabilities.h"
13 #include "mojo/shell/public/cpp/names.h" 13 #include "mojo/shell/public/cpp/names.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 namespace catalog { 16 namespace catalog {
17 17
18 class BuilderTest : public testing::Test { 18 class EntryTest : public testing::Test {
19 public: 19 public:
20 BuilderTest() {} 20 EntryTest() {}
21 ~BuilderTest() override {} 21 ~EntryTest() override {}
22 22
23 protected: 23 protected:
24 scoped_ptr<base::Value> ReadEntry(const std::string& manifest, Entry* entry) { 24 scoped_ptr<Entry> ReadEntry(const std::string& manifest,
25 DCHECK(entry); 25 scoped_ptr<base::Value>* out_value) {
26 scoped_ptr<base::Value> value = ReadManifest(manifest); 26 scoped_ptr<base::Value> value = ReadManifest(manifest);
27 base::DictionaryValue* dictionary = nullptr; 27 base::DictionaryValue* dictionary = nullptr;
28 CHECK(value->GetAsDictionary(&dictionary)); 28 CHECK(value->GetAsDictionary(&dictionary));
29 *entry = BuildEntry(*dictionary); 29 if (out_value)
30 return value; 30 *out_value = std::move(value);
31 return Entry::Deserialize(*dictionary);
31 } 32 }
32 33
33 scoped_ptr<base::Value> ReadManifest(const std::string& manifest) { 34 scoped_ptr<base::Value> ReadManifest(const std::string& manifest) {
34 base::FilePath manifest_path; 35 base::FilePath manifest_path;
35 PathService::Get(base::DIR_SOURCE_ROOT, &manifest_path); 36 PathService::Get(base::DIR_SOURCE_ROOT, &manifest_path);
36 manifest_path = manifest_path.AppendASCII( 37 manifest_path = manifest_path.AppendASCII(
37 "mojo/services/catalog/data/" + manifest); 38 "mojo/services/catalog/data/" + manifest);
38 39
39 JSONFileValueDeserializer deserializer(manifest_path); 40 JSONFileValueDeserializer deserializer(manifest_path);
40 int error = 0; 41 int error = 0;
41 std::string message; 42 std::string message;
42 // TODO(beng): probably want to do more detailed error checking. This should 43 // TODO(beng): probably want to do more detailed error checking. This should
43 // be done when figuring out if to unblock connection 44 // be done when figuring out if to unblock connection
44 // completion. 45 // completion.
45 return deserializer.Deserialize(&error, &message); 46 return deserializer.Deserialize(&error, &message);
46 } 47 }
47 48
48 private: 49 private:
49 void SetUp() override {} 50 void SetUp() override {}
50 void TearDown() override {} 51 void TearDown() override {}
51 52
52 DISALLOW_COPY_AND_ASSIGN(BuilderTest); 53 DISALLOW_COPY_AND_ASSIGN(EntryTest);
53 }; 54 };
54 55
55 TEST_F(BuilderTest, Simple) { 56 TEST_F(EntryTest, Simple) {
56 Entry entry; 57 scoped_ptr<Entry> entry = ReadEntry("simple", nullptr);
57 ReadEntry("simple", &entry); 58 EXPECT_EQ("mojo:foo", entry->name());
58 59 EXPECT_EQ(mojo::GetNamePath(entry->name()), entry->qualifier());
59 EXPECT_EQ("mojo:foo", entry.name); 60 EXPECT_EQ("Foo", entry->display_name());
60 EXPECT_EQ(mojo::GetNamePath(entry.name), entry.qualifier);
61 EXPECT_EQ("Foo", entry.display_name);
62 } 61 }
63 62
64 TEST_F(BuilderTest, Instance) { 63 TEST_F(EntryTest, Instance) {
65 Entry entry; 64 scoped_ptr<Entry> entry = ReadEntry("instance", nullptr);
66 ReadEntry("instance", &entry); 65 EXPECT_EQ("mojo:foo", entry->name());
67 66 EXPECT_EQ("bar", entry->qualifier());
68 EXPECT_EQ("mojo:foo", entry.name); 67 EXPECT_EQ("Foo", entry->display_name());
69 EXPECT_EQ("bar", entry.qualifier);
70 EXPECT_EQ("Foo", entry.display_name);
71 } 68 }
72 69
73 TEST_F(BuilderTest, Capabilities) { 70 TEST_F(EntryTest, Capabilities) {
74 Entry entry; 71 scoped_ptr<Entry> entry = ReadEntry("capabilities", nullptr);
75 ReadEntry("capabilities", &entry);
76 72
77 EXPECT_EQ("mojo:foo", entry.name); 73 EXPECT_EQ("mojo:foo", entry->name());
78 EXPECT_EQ("bar", entry.qualifier); 74 EXPECT_EQ("bar", entry->qualifier());
79 EXPECT_EQ("Foo", entry.display_name); 75 EXPECT_EQ("Foo", entry->display_name());
80 mojo::CapabilitySpec spec; 76 mojo::CapabilitySpec spec;
81 mojo::CapabilityRequest request; 77 mojo::CapabilityRequest request;
82 request.interfaces.insert("mojo::Bar"); 78 request.interfaces.insert("mojo::Bar");
83 spec.required["mojo:bar"] = request; 79 spec.required["mojo:bar"] = request;
84 EXPECT_EQ(spec, entry.capabilities); 80 EXPECT_EQ(spec, entry->capabilities());
85 } 81 }
86 82
87 TEST_F(BuilderTest, Serialization) { 83 TEST_F(EntryTest, Serialization) {
88 Entry entry; 84 scoped_ptr<base::Value> value;
89 scoped_ptr<base::Value> value = ReadEntry("serialization", &entry); 85 scoped_ptr<Entry> entry = ReadEntry("serialization", &value);
90 86
91 scoped_ptr<base::DictionaryValue> serialized(SerializeEntry(entry)); 87 scoped_ptr<base::DictionaryValue> serialized(entry->Serialize());
92 88
93 // We can't just compare values, since during deserialization some of the 89 // We can't just compare values, since during deserialization some of the
94 // lists get converted to std::sets, which are sorted, so Value::Equals will 90 // lists get converted to std::sets, which are sorted, so Value::Equals will
95 // fail. 91 // fail.
96 Entry reconstituted = BuildEntry(*serialized.get()); 92 scoped_ptr<Entry> reconstituted = Entry::Deserialize(*serialized.get());
97 EXPECT_EQ(entry, reconstituted); 93 EXPECT_EQ(*entry, *reconstituted);
98 } 94 }
99 95
100 TEST_F(BuilderTest, Malformed) { 96 TEST_F(EntryTest, Malformed) {
101 scoped_ptr<base::Value> value = ReadManifest("malformed"); 97 scoped_ptr<base::Value> value = ReadManifest("malformed");
102 EXPECT_FALSE(value.get()); 98 EXPECT_FALSE(value.get());
103 } 99 }
104 100
105 101
106 } // namespace catalog 102 } // namespace catalog
OLDNEW
« no previous file with comments | « mojo/services/catalog/entry.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698