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

Side by Side Diff: ios/web/webui/mojo_facade_unittest.mm

Issue 1956113002: [ios Mojo] iOS facade class for Mojo API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated gyp files Created 4 years, 7 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
« ios/web/webui/mojo_facade.mm ('K') | « ios/web/webui/mojo_facade.mm ('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
(Empty)
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
3 // found in the LICENSE file.
4
5 #import "ios/web/webui/mojo_facade.h"
6
7 #include <memory>
8
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "ios/web/test/mojo_test.mojom.h"
12 #include "ios/web/test/web_test.h"
13 #include "ipc/ipc_channel.h"
14 #include "mojo/edk/embedder/embedder.h"
15 #include "mojo/public/cpp/bindings/binding_set.h"
16 #include "services/shell/public/cpp/interface_factory.h"
17 #include "services/shell/public/cpp/interface_registry.h"
18 #import "third_party/ocmock/OCMock/OCMockObject.h"
19
20 namespace web {
21
22 namespace {
23
24 // Serializes the given |object| to JSON string.
25 std::string GetJson(id object) {
26 NSData* json_as_data =
27 [NSJSONSerialization dataWithJSONObject:object options:0 error:nil];
28 base::scoped_nsobject<NSString> json_as_string([[NSString alloc]
29 initWithData:json_as_data
30 encoding:NSUTF8StringEncoding]);
31 return base::SysNSStringToUTF8(json_as_string);
32 }
33
34 // Deserializes the given |json| to an object.
35 id GetObject(const std::string& json) {
36 NSData* json_as_data =
37 [base::SysUTF8ToNSString(json) dataUsingEncoding:NSUTF8StringEncoding];
38 return [NSJSONSerialization JSONObjectWithData:json_as_data
39 options:0
40 error:nil];
41 }
42
43 // Test mojo handler factory.
44 class TestUIHandlerFactory : public shell::InterfaceFactory<TestUIHandlerMojo> {
45 public:
46 ~TestUIHandlerFactory() override {}
47
48 private:
49 // shell::InterfaceFactory overrides.
50 void Create(shell::Connection* connection,
51 mojo::InterfaceRequest<TestUIHandlerMojo> request) override {}
52 };
53
54 } // namespace
55
56 // A test fixture to test MojoFacade class.
57 class MojoFacadeTest : public WebTest {
58 protected:
59 MojoFacadeTest() {
60 // Initialize Mojo for testing.
61 static dispatch_once_t once_token;
62 dispatch_once(&once_token, ^{
63 mojo::edk::SetMaxMessageSize(IPC::Channel::kMaximumMessageSize);
64 mojo::edk::Init();
65 });
66
67 interface_registry_.reset(new shell::InterfaceRegistry(nullptr));
68 interface_registry_->AddInterface(&ui_handler_factory_);
69 evaluator_.reset([[OCMockObject
70 mockForProtocol:@protocol(CRWJSInjectionEvaluator)] retain]);
71 facade_.reset(new MojoFacade(
72 interface_registry_.get(),
73 static_cast<id<CRWJSInjectionEvaluator>>(evaluator_.get())));
74 }
75
76 MojoFacade* facade() { return facade_.get(); }
77
78 private:
79 TestUIHandlerFactory ui_handler_factory_;
80 std::unique_ptr<shell::InterfaceRegistry> interface_registry_;
81 base::scoped_nsobject<OCMockObject> evaluator_;
82 std::unique_ptr<MojoFacade> facade_;
83 };
84
85 // Tests connecting to existing service and closing the handle.
86 TEST_F(MojoFacadeTest, ConnectToServiceAndCloseHandle) {
87 // Connect to the service.
88 NSDictionary* connect = @{
89 @"name" : @"service_provider.connectToService",
90 @"args" : @{
91 @"serviceName" : @"::TestUIHandlerMojo",
92 },
93 };
94
95 std::string handle_as_string = facade()->HandleMojoMessage(GetJson(connect));
96 EXPECT_FALSE(handle_as_string.empty());
97 int handle = 0;
98 EXPECT_TRUE(base::StringToInt(handle_as_string, &handle));
99
100 // Close the handle.
101 NSDictionary* close = @{
102 @"name" : @"core.close",
103 @"args" : @{
104 @"handle" : @(handle),
105 },
106 };
107 std::string result_as_string = facade()->HandleMojoMessage(GetJson(close));
108 EXPECT_FALSE(result_as_string.empty());
109 int result = 0;
110 EXPECT_TRUE(base::StringToInt(result_as_string, &result));
111 EXPECT_EQ(MOJO_RESULT_OK, static_cast<MojoResult>(result));
112 }
113
114 // Tests closing invalid handle.
115 TEST_F(MojoFacadeTest, CloseInvalidHandle) {
116 NSDictionary* close = @{
117 @"name" : @"core.close",
118 @"args" : @{
119 @"handle" : @(-1),
120 },
121 };
122 std::string result_as_string = facade()->HandleMojoMessage(GetJson(close));
123 EXPECT_FALSE(result_as_string.empty());
124 int result = 0;
125 EXPECT_TRUE(base::StringToInt(result_as_string, &result));
126 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, static_cast<MojoResult>(result));
127 }
128
129 // Tests creating a message pipe without options.
130 TEST_F(MojoFacadeTest, CreateMessagePipeWithoutOptions) {
131 // Create a message pipe.
132 NSDictionary* create = @{
133 @"name" : @"core.createMessagePipe",
134 @"args" : @{
135 @"optionsDict" : [NSNull null],
136 },
137 };
138 std::string response_as_string = facade()->HandleMojoMessage(GetJson(create));
139
140 // Verify handles.
141 EXPECT_FALSE(response_as_string.empty());
142 NSDictionary* response_as_dict = GetObject(response_as_string);
143 EXPECT_TRUE([response_as_dict isKindOfClass:[NSDictionary class]]);
144 id handle0 = response_as_dict[@"handle0"];
145 EXPECT_TRUE(handle0);
146 id handle1 = response_as_dict[@"handle1"];
147 EXPECT_TRUE(handle1);
148
149 // Close handle0.
150 NSDictionary* close0 = @{
151 @"name" : @"core.close",
152 @"args" : @{
153 @"handle" : handle0,
154 },
155 };
156 std::string result0_as_string = facade()->HandleMojoMessage(GetJson(close0));
157 EXPECT_FALSE(result0_as_string.empty());
158 int result0 = 0;
159 EXPECT_TRUE(base::StringToInt(result0_as_string, &result0));
160 EXPECT_EQ(MOJO_RESULT_OK, static_cast<MojoResult>(result0));
161
162 // Close handle1.
163 NSDictionary* close1 = @{
164 @"name" : @"core.close",
165 @"args" : @{
166 @"handle" : handle1,
167 },
168 };
169 std::string result1_as_string = facade()->HandleMojoMessage(GetJson(close1));
170 EXPECT_FALSE(result1_as_string.empty());
171 int result1 = 0;
172 EXPECT_TRUE(base::StringToInt(result1_as_string, &result1));
173 EXPECT_EQ(MOJO_RESULT_OK, static_cast<MojoResult>(result1));
174 }
175
176 } // namespace web
OLDNEW
« ios/web/webui/mojo_facade.mm ('K') | « ios/web/webui/mojo_facade.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698