Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 that empty response is returned for an empty message. | |
| 86 TEST_F(MojoFacadeTest, EmptyMessage) { | |
| 87 EXPECT_TRUE(facade()->HandleMojoMessage("").empty()); | |
| 88 } | |
| 89 | |
| 90 // Tests that empty response is returned for non-dict message. | |
| 91 TEST_F(MojoFacadeTest, NonDictMessage) { | |
| 92 id message = @[]; | |
| 93 EXPECT_TRUE(facade()->HandleMojoMessage(GetJson(message)).empty()); | |
| 94 } | |
| 95 | |
| 96 // Tests that empty response is returned for message without "name" key. | |
| 97 TEST_F(MojoFacadeTest, MessageWithoutNameKey) { | |
| 98 id message = @{ | |
| 99 @"args" : @{}, | |
| 100 }; | |
| 101 EXPECT_TRUE(facade()->HandleMojoMessage(GetJson(message)).empty()); | |
| 102 } | |
| 103 | |
| 104 // Tests that empty response is returned for message without "args" key. | |
| 105 TEST_F(MojoFacadeTest, MessageWithoutArgsKey) { | |
| 106 id message = @{ | |
| 107 @"name" : @"", | |
| 108 }; | |
| 109 EXPECT_TRUE(facade()->HandleMojoMessage(GetJson(message)).empty()); | |
| 110 } | |
| 111 | |
| 112 // Tests service_provider.connectToService message without args. | |
| 113 TEST_F(MojoFacadeTest, ConnectToServiceWithoutArgs) { | |
| 114 NSDictionary* connect = @{ | |
| 115 @"name" : @"service_provider.connectToService", | |
| 116 @"args" : @{}, | |
| 117 }; | |
| 118 | |
| 119 std::string handle_as_string = facade()->HandleMojoMessage(GetJson(connect)); | |
| 120 EXPECT_TRUE(handle_as_string.empty()); | |
| 121 } | |
| 122 | |
| 123 // Tests connecting to existing service and closing the handle. | |
| 124 TEST_F(MojoFacadeTest, ConnectToServiceAndCloseHandle) { | |
| 125 // Connect to the service. | |
| 126 NSDictionary* connect = @{ | |
| 127 @"name" : @"service_provider.connectToService", | |
| 128 @"args" : @{ | |
| 129 @"serviceName" : @"::TestUIHandlerMojo", | |
| 130 }, | |
| 131 }; | |
| 132 | |
| 133 std::string handle_as_string = facade()->HandleMojoMessage(GetJson(connect)); | |
| 134 EXPECT_FALSE(handle_as_string.empty()); | |
| 135 int handle = 0; | |
| 136 EXPECT_TRUE(base::StringToInt(handle_as_string, &handle)); | |
| 137 | |
| 138 // Close the handle. | |
| 139 NSDictionary* close = @{ | |
| 140 @"name" : @"core.close", | |
| 141 @"args" : @{ | |
| 142 @"handle" : @(handle), | |
| 143 }, | |
| 144 }; | |
| 145 std::string result_as_string = facade()->HandleMojoMessage(GetJson(close)); | |
| 146 EXPECT_FALSE(result_as_string.empty()); | |
| 147 int result = 0; | |
| 148 EXPECT_TRUE(base::StringToInt(result_as_string, &result)); | |
| 149 EXPECT_EQ(MOJO_RESULT_OK, static_cast<MojoResult>(result)); | |
| 150 } | |
| 151 | |
| 152 // Tests closing handle without args. | |
| 153 TEST_F(MojoFacadeTest, CloseHandleWithoutArgs) { | |
| 154 NSDictionary* close = @{ | |
| 155 @"name" : @"core.close", | |
| 156 @"args" : @{}, | |
| 157 }; | |
| 158 std::string result_as_string = facade()->HandleMojoMessage(GetJson(close)); | |
| 159 EXPECT_TRUE(result_as_string.empty()); | |
| 160 } | |
| 161 | |
| 162 // Tests closing invalid handle. | |
| 163 TEST_F(MojoFacadeTest, CloseInvalidHandle) { | |
| 164 NSDictionary* close = @{ | |
| 165 @"name" : @"core.close", | |
| 166 @"args" : @{ | |
| 167 @"handle" : @(-1), | |
| 168 }, | |
| 169 }; | |
| 170 std::string result_as_string = facade()->HandleMojoMessage(GetJson(close)); | |
| 171 EXPECT_FALSE(result_as_string.empty()); | |
| 172 int result = 0; | |
| 173 EXPECT_TRUE(base::StringToInt(result_as_string, &result)); | |
| 174 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, static_cast<MojoResult>(result)); | |
| 175 } | |
| 176 | |
| 177 // Tests creating a message pipe without args. | |
| 178 TEST_F(MojoFacadeTest, CreateMessagePipeWithoutArgs) { | |
| 179 NSDictionary* create = @{ | |
| 180 @"name" : @"core.createMessagePipe", | |
| 181 @"args" : @{}, | |
| 182 }; | |
| 183 std::string result_as_string = facade()->HandleMojoMessage(GetJson(create)); | |
| 184 EXPECT_TRUE(result_as_string.empty()); | |
| 185 } | |
| 186 | |
| 187 // Tests creating a message pipe without options. | |
| 188 TEST_F(MojoFacadeTest, CreateMessagePipeWithoutOptions) { | |
| 189 // Create a message pipe. | |
| 190 NSDictionary* create = @{ | |
| 191 @"name" : @"core.createMessagePipe", | |
| 192 @"args" : @{ | |
| 193 @"optionsDict" : [NSNull null], | |
| 194 }, | |
| 195 }; | |
| 196 std::string response_as_string = facade()->HandleMojoMessage(GetJson(create)); | |
| 197 | |
| 198 // Verify handles. | |
| 199 EXPECT_FALSE(response_as_string.empty()); | |
| 200 NSDictionary* response_as_dict = GetObject(response_as_string); | |
| 201 EXPECT_TRUE([response_as_dict isKindOfClass:[NSDictionary class]]); | |
| 202 id handle0 = response_as_dict[@"handle0"]; | |
| 203 EXPECT_TRUE(handle0); | |
| 204 id handle1 = response_as_dict[@"handle1"]; | |
| 205 EXPECT_TRUE(handle1); | |
| 206 | |
| 207 // Close handle0. | |
| 208 NSDictionary* close0 = @{ | |
| 209 @"name" : @"core.close", | |
| 210 @"args" : @{ | |
| 211 @"handle" : handle0, | |
| 212 }, | |
| 213 }; | |
| 214 std::string result0_as_string = facade()->HandleMojoMessage(GetJson(close0)); | |
| 215 EXPECT_FALSE(result0_as_string.empty()); | |
| 216 int result0 = 0; | |
| 217 EXPECT_TRUE(base::StringToInt(result0_as_string, &result0)); | |
| 218 EXPECT_EQ(MOJO_RESULT_OK, static_cast<MojoResult>(result0)); | |
| 219 | |
| 220 // Close handle1. | |
| 221 NSDictionary* close1 = @{ | |
| 222 @"name" : @"core.close", | |
| 223 @"args" : @{ | |
| 224 @"handle" : handle1, | |
| 225 }, | |
| 226 }; | |
| 227 std::string result1_as_string = facade()->HandleMojoMessage(GetJson(close1)); | |
| 228 EXPECT_FALSE(result1_as_string.empty()); | |
| 229 int result1 = 0; | |
| 230 EXPECT_TRUE(base::StringToInt(result1_as_string, &result1)); | |
| 231 EXPECT_EQ(MOJO_RESULT_OK, static_cast<MojoResult>(result1)); | |
| 232 } | |
| 233 | |
| 234 // Tests writing a message without handle arg. | |
| 235 TEST_F(MojoFacadeTest, WriteMessageWithoutHandleArg) { | |
| 236 NSDictionary* write = @{ | |
| 237 @"name" : @"core.writeMessage", | |
| 238 @"args" : @{ | |
| 239 @"handles" : @[], | |
| 240 @"buffer" : @{}, | |
| 241 @"flags" : [NSNull null], | |
| 242 }, | |
| 243 }; | |
| 244 std::string result_as_string = facade()->HandleMojoMessage(GetJson(write)); | |
| 245 EXPECT_TRUE(result_as_string.empty()); | |
| 246 } | |
| 247 | |
| 248 // Tests writing a message without handles arg. | |
| 249 TEST_F(MojoFacadeTest, WriteMessageWithoutHandlesArg) { | |
| 250 NSDictionary* write = @{ | |
| 251 @"name" : @"core.writeMessage", | |
| 252 @"args" : @{ | |
| 253 @"handle" : @0, | |
| 254 @"buffer" : @{}, | |
| 255 @"flags" : [NSNull null], | |
| 256 }, | |
| 257 }; | |
| 258 std::string result_as_string = facade()->HandleMojoMessage(GetJson(write)); | |
| 259 EXPECT_TRUE(result_as_string.empty()); | |
| 260 } | |
| 261 | |
| 262 // Tests writing a message without buffer arg. | |
| 263 TEST_F(MojoFacadeTest, WriteMessageWithoutBufferArg) { | |
| 264 NSDictionary* write = @{ | |
| 265 @"name" : @"core.writeMessage", | |
| 266 @"args" : @{ | |
| 267 @"handle" : @0, | |
| 268 @"handles" : @[], | |
| 269 @"flags" : [NSNull null], | |
| 270 }, | |
| 271 }; | |
| 272 std::string result_as_string = facade()->HandleMojoMessage(GetJson(write)); | |
| 273 EXPECT_TRUE(result_as_string.empty()); | |
| 274 } | |
| 275 | |
| 276 // Tests writing a message without flags arg. | |
| 277 TEST_F(MojoFacadeTest, WriteMessageWithoutFlagsArg) { | |
| 278 NSDictionary* write = @{ | |
| 279 @"name" : @"core.writeMessage", | |
| 280 @"args" : @{ | |
| 281 @"handle" : @0, | |
| 282 @"handles" : @[], | |
| 283 @"buffer" : @{}, | |
| 284 }, | |
| 285 }; | |
| 286 std::string result_as_string = facade()->HandleMojoMessage(GetJson(write)); | |
| 287 EXPECT_TRUE(result_as_string.empty()); | |
| 288 } | |
| 289 | |
| 290 // Tests reading a message without handle arg. | |
| 291 TEST_F(MojoFacadeTest, ReadMessageWithoutHandleArg) { | |
| 292 NSDictionary* read = @{ | |
| 293 @"name" : @"core.readMessage", | |
| 294 @"args" : @{ | |
| 295 @"flags" : [NSNull null], | |
| 296 }, | |
| 297 }; | |
| 298 std::string result_as_string = facade()->HandleMojoMessage(GetJson(read)); | |
| 299 EXPECT_TRUE(result_as_string.empty()); | |
| 300 } | |
| 301 | |
| 302 // Tests reading a message without flags arg. | |
| 303 TEST_F(MojoFacadeTest, ReadMessageWithoutFlagsArg) { | |
| 304 NSDictionary* read = @{ | |
| 305 @"name" : @"core.readMessage", | |
| 306 @"args" : @{ | |
| 307 @"handle" : @0, | |
| 308 }, | |
| 309 }; | |
| 310 std::string result_as_string = facade()->HandleMojoMessage(GetJson(read)); | |
| 311 EXPECT_TRUE(result_as_string.empty()); | |
| 312 } | |
| 313 | |
| 314 // Tests watching without handle arg. | |
| 315 TEST_F(MojoFacadeTest, WatchWithoutHandleArg) { | |
| 316 NSDictionary* watch = @{ | |
| 317 @"name" : @"support.watch", | |
| 318 @"args" : @{ | |
| 319 @"signals" : @0, | |
| 320 @"callbackId" : @0, | |
| 321 }, | |
| 322 }; | |
| 323 std::string result_as_string = facade()->HandleMojoMessage(GetJson(watch)); | |
| 324 EXPECT_TRUE(result_as_string.empty()); | |
| 325 } | |
| 326 | |
| 327 // Tests watching without signals arg. | |
| 328 TEST_F(MojoFacadeTest, WatchWithoutSignalsArg) { | |
| 329 NSDictionary* watch = @{ | |
| 330 @"name" : @"support.watch", | |
| 331 @"args" : @{ | |
| 332 @"handle" : @0, | |
| 333 @"callbackId" : @0, | |
| 334 }, | |
| 335 }; | |
| 336 std::string result_as_string = facade()->HandleMojoMessage(GetJson(watch)); | |
| 337 EXPECT_TRUE(result_as_string.empty()); | |
| 338 } | |
| 339 | |
| 340 // Tests watching without callbackId arg. | |
| 341 TEST_F(MojoFacadeTest, WatchWithoutCallbackIdArg) { | |
| 342 NSDictionary* watch = @{ | |
| 343 @"name" : @"support.watch", | |
| 344 @"args" : @{ | |
| 345 @"handle" : @0, | |
| 346 @"signals" : @0, | |
| 347 }, | |
| 348 }; | |
| 349 std::string result_as_string = facade()->HandleMojoMessage(GetJson(watch)); | |
| 350 EXPECT_TRUE(result_as_string.empty()); | |
| 351 } | |
| 352 | |
|
Eugene But (OOO till 7-30)
2016/05/07 00:07:52
This is very basic unit test, but I do have an int
| |
| 353 } // namespace web | |
| OLD | NEW |