| Index: ios/web/webui/mojo_facade_unittest.mm
|
| diff --git a/ios/web/webui/mojo_facade_unittest.mm b/ios/web/webui/mojo_facade_unittest.mm
|
| index a8da2855d667ebe6bb8d45c6418952ba04338748..4aee73d9f1cd427214573f5d708aa67479db797e 100644
|
| --- a/ios/web/webui/mojo_facade_unittest.mm
|
| +++ b/ios/web/webui/mojo_facade_unittest.mm
|
| @@ -65,6 +65,34 @@ class MojoFacadeTest : public WebTest {
|
| OCMockObject* evaluator() { return evaluator_; }
|
| MojoFacade* facade() { return facade_.get(); }
|
|
|
| + void CreateMessagePipe(uint32_t* handle0, uint32_t* handle1) {
|
| + NSDictionary* create = @{
|
| + @"name" : @"Mojo.createMessagePipe",
|
| + @"args" : @{},
|
| + };
|
| + std::string response_as_string =
|
| + facade()->HandleMojoMessage(GetJson(create));
|
| +
|
| + // Verify handles.
|
| + ASSERT_FALSE(response_as_string.empty());
|
| + NSDictionary* response_as_dict = GetObject(response_as_string);
|
| + ASSERT_TRUE([response_as_dict isKindOfClass:[NSDictionary class]]);
|
| + ASSERT_EQ(MOJO_RESULT_OK, [response_as_dict[@"result"] unsignedIntValue]);
|
| + *handle0 = [response_as_dict[@"handle0"] unsignedIntValue];
|
| + *handle1 = [response_as_dict[@"handle1"] unsignedIntValue];
|
| + }
|
| +
|
| + void CloseHandle(uint32_t handle) {
|
| + NSDictionary* close = @{
|
| + @"name" : @"MojoHandle.close",
|
| + @"args" : @{
|
| + @"handle" : @(handle),
|
| + },
|
| + };
|
| + std::string result = facade()->HandleMojoMessage(GetJson(close));
|
| + EXPECT_TRUE(result.empty());
|
| + }
|
| +
|
| private:
|
| void BindTestUIHandlerMojoRequest(
|
| const service_manager::BindSourceInfo& source_info,
|
| @@ -75,108 +103,47 @@ class MojoFacadeTest : public WebTest {
|
| std::unique_ptr<MojoFacade> facade_;
|
| };
|
|
|
| -// Tests connecting to existing interface and closing the handle.
|
| -TEST_F(MojoFacadeTest, GetInterfaceAndCloseHandle) {
|
| - // Bind to the interface.
|
| +// Tests binding an interface.
|
| +TEST_F(MojoFacadeTest, BindInterface) {
|
| + uint32_t handle0 = 0;
|
| + uint32_t handle1 = 0;
|
| + CreateMessagePipe(&handle0, &handle1);
|
| +
|
| + // Pass handle0 as interface request.
|
| NSDictionary* connect = @{
|
| - @"name" : @"interface_provider.getInterface",
|
| + @"name" : @"Mojo.bindInterface",
|
| @"args" : @{
|
| @"interfaceName" : @"::TestUIHandlerMojo",
|
| + @"requestHandle" : @(handle0),
|
| },
|
| };
|
|
|
| std::string handle_as_string = facade()->HandleMojoMessage(GetJson(connect));
|
| - EXPECT_FALSE(handle_as_string.empty());
|
| - int handle = 0;
|
| - EXPECT_TRUE(base::StringToInt(handle_as_string, &handle));
|
| + EXPECT_TRUE(handle_as_string.empty());
|
|
|
| - // Close the handle.
|
| - NSDictionary* close = @{
|
| - @"name" : @"core.close",
|
| - @"args" : @{
|
| - @"handle" : @(handle),
|
| - },
|
| - };
|
| - std::string result_as_string = facade()->HandleMojoMessage(GetJson(close));
|
| - EXPECT_FALSE(result_as_string.empty());
|
| - int result = 0;
|
| - EXPECT_TRUE(base::StringToInt(result_as_string, &result));
|
| - EXPECT_EQ(MOJO_RESULT_OK, static_cast<MojoResult>(result));
|
| + CloseHandle(handle1);
|
| }
|
|
|
| -// Tests creating a message pipe without options.
|
| -TEST_F(MojoFacadeTest, CreateMessagePipeWithoutOptions) {
|
| - // Create a message pipe.
|
| - NSDictionary* create = @{
|
| - @"name" : @"core.createMessagePipe",
|
| - @"args" : @{
|
| - @"optionsDict" : [NSNull null],
|
| - },
|
| - };
|
| - std::string response_as_string = facade()->HandleMojoMessage(GetJson(create));
|
| -
|
| - // Verify handles.
|
| - EXPECT_FALSE(response_as_string.empty());
|
| - NSDictionary* response_as_dict = GetObject(response_as_string);
|
| - EXPECT_TRUE([response_as_dict isKindOfClass:[NSDictionary class]]);
|
| - id handle0 = response_as_dict[@"handle0"];
|
| - EXPECT_TRUE(handle0);
|
| - id handle1 = response_as_dict[@"handle1"];
|
| - EXPECT_TRUE(handle1);
|
| -
|
| - // Close handle0.
|
| - NSDictionary* close0 = @{
|
| - @"name" : @"core.close",
|
| - @"args" : @{
|
| - @"handle" : handle0,
|
| - },
|
| - };
|
| - std::string result0_as_string = facade()->HandleMojoMessage(GetJson(close0));
|
| - EXPECT_FALSE(result0_as_string.empty());
|
| - int result0 = 0;
|
| - EXPECT_TRUE(base::StringToInt(result0_as_string, &result0));
|
| - EXPECT_EQ(MOJO_RESULT_OK, static_cast<MojoResult>(result0));
|
| -
|
| - // Close handle1.
|
| - NSDictionary* close1 = @{
|
| - @"name" : @"core.close",
|
| - @"args" : @{
|
| - @"handle" : handle1,
|
| - },
|
| - };
|
| - std::string result1_as_string = facade()->HandleMojoMessage(GetJson(close1));
|
| - EXPECT_FALSE(result1_as_string.empty());
|
| - int result1 = 0;
|
| - EXPECT_TRUE(base::StringToInt(result1_as_string, &result1));
|
| - EXPECT_EQ(MOJO_RESULT_OK, static_cast<MojoResult>(result1));
|
| +// Tests creating a message pipe.
|
| +TEST_F(MojoFacadeTest, CreateMessagePipe) {
|
| + uint32_t handle0, handle1;
|
| + CreateMessagePipe(&handle0, &handle1);
|
| +
|
| + CloseHandle(handle0);
|
| + CloseHandle(handle1);
|
| }
|
|
|
| // Tests watching the pipe.
|
| TEST_F(MojoFacadeTest, Watch) {
|
| - // Create a message pipe.
|
| - NSDictionary* create = @{
|
| - @"name" : @"core.createMessagePipe",
|
| - @"args" : @{
|
| - @"optionsDict" : [NSNull null],
|
| - },
|
| - };
|
| - std::string response_as_string = facade()->HandleMojoMessage(GetJson(create));
|
| -
|
| - // Verify handles.
|
| - EXPECT_FALSE(response_as_string.empty());
|
| - NSDictionary* response_as_dict = GetObject(response_as_string);
|
| - EXPECT_TRUE([response_as_dict isKindOfClass:[NSDictionary class]]);
|
| - id handle0 = response_as_dict[@"handle0"];
|
| - EXPECT_TRUE(handle0);
|
| - id handle1 = response_as_dict[@"handle1"];
|
| - EXPECT_TRUE(handle1);
|
| + uint32_t handle0, handle1;
|
| + CreateMessagePipe(&handle0, &handle1);
|
|
|
| // Start watching one end of the pipe.
|
| int callback_id = 99;
|
| NSDictionary* watch = @{
|
| - @"name" : @"support.watch",
|
| + @"name" : @"MojoHandle.watch",
|
| @"args" : @{
|
| - @"handle" : handle0,
|
| + @"handle" : @(handle0),
|
| @"signals" : @(MOJO_HANDLE_SIGNAL_READABLE),
|
| @"callbackId" : @(callback_id),
|
| },
|
| @@ -189,15 +156,16 @@ TEST_F(MojoFacadeTest, Watch) {
|
| // Start waiting for the watch callback.
|
| __block bool callback_received = false;
|
| NSString* expected_script =
|
| - [NSString stringWithFormat:@"__crWeb.mojo.signalWatch(%d, %d)",
|
| - callback_id, MOJO_RESULT_OK];
|
| + [NSString stringWithFormat:
|
| + @"Mojo.internal.watchCallbacksHolder.callCallback(%d, %d)",
|
| + callback_id, MOJO_RESULT_OK];
|
| [[[evaluator() expect] andDo:^(NSInvocation*) {
|
| callback_received = true;
|
|
|
| // Cancel the watch immediately to ensure there are no additional
|
| // notifications.
|
| NSDictionary* cancel_watch = @{
|
| - @"name" : @"support.cancelWatch",
|
| + @"name" : @"MojoWatcher.cancel",
|
| @"args" : @{
|
| @"watchId" : @(watch_id),
|
| },
|
| @@ -209,13 +177,9 @@ TEST_F(MojoFacadeTest, Watch) {
|
|
|
| // Write to the other end of the pipe.
|
| NSDictionary* write = @{
|
| - @"name" : @"core.writeMessage",
|
| - @"args" : @{
|
| - @"handle" : handle1,
|
| - @"handles" : @[],
|
| - @"flags" : @(MOJO_WRITE_MESSAGE_FLAG_NONE),
|
| - @"buffer" : @{@"0" : @0}
|
| - },
|
| + @"name" : @"MojoHandle.writeMessage",
|
| + @"args" :
|
| + @{@"handle" : @(handle1), @"handles" : @[], @"buffer" : @{@"0" : @0}},
|
| };
|
| std::string result_as_string = facade()->HandleMojoMessage(GetJson(write));
|
| EXPECT_FALSE(result_as_string.empty());
|
| @@ -228,35 +192,22 @@ TEST_F(MojoFacadeTest, Watch) {
|
| return callback_received;
|
| },
|
| true, base::TimeDelta());
|
| +
|
| + CloseHandle(handle0);
|
| + CloseHandle(handle1);
|
| }
|
|
|
| // Tests reading the message from the pipe.
|
| TEST_F(MojoFacadeTest, ReadWrite) {
|
| - // Create a message pipe.
|
| - NSDictionary* create = @{
|
| - @"name" : @"core.createMessagePipe",
|
| - @"args" : @{
|
| - @"optionsDict" : [NSNull null],
|
| - },
|
| - };
|
| - std::string response_as_string = facade()->HandleMojoMessage(GetJson(create));
|
| -
|
| - // Verify handles.
|
| - EXPECT_FALSE(response_as_string.empty());
|
| - NSDictionary* response_as_dict = GetObject(response_as_string);
|
| - EXPECT_TRUE([response_as_dict isKindOfClass:[NSDictionary class]]);
|
| - id handle0 = response_as_dict[@"handle0"];
|
| - EXPECT_TRUE(handle0);
|
| - id handle1 = response_as_dict[@"handle1"];
|
| - EXPECT_TRUE(handle1);
|
| + uint32_t handle0, handle1;
|
| + CreateMessagePipe(&handle0, &handle1);
|
|
|
| // Write to the other end of the pipe.
|
| NSDictionary* write = @{
|
| - @"name" : @"core.writeMessage",
|
| + @"name" : @"MojoHandle.writeMessage",
|
| @"args" : @{
|
| - @"handle" : handle1,
|
| + @"handle" : @(handle1),
|
| @"handles" : @[],
|
| - @"flags" : @(MOJO_WRITE_MESSAGE_FLAG_NONE),
|
| @"buffer" : @{@"0" : @9, @"1" : @2, @"2" : @2008}
|
| },
|
| };
|
| @@ -268,10 +219,9 @@ TEST_F(MojoFacadeTest, ReadWrite) {
|
|
|
| // Read the message from the pipe.
|
| NSDictionary* read = @{
|
| - @"name" : @"core.readMessage",
|
| + @"name" : @"MojoHandle.readMessage",
|
| @"args" : @{
|
| - @"handle" : handle0,
|
| - @"flags" : @(MOJO_READ_MESSAGE_FLAG_NONE),
|
| + @"handle" : @(handle0),
|
| },
|
| };
|
| NSDictionary* message = GetObject(facade()->HandleMojoMessage(GetJson(read)));
|
| @@ -281,6 +231,9 @@ TEST_F(MojoFacadeTest, ReadWrite) {
|
| EXPECT_NSEQ(expected_message, message[@"buffer"]);
|
| EXPECT_FALSE([message[@"handles"] count]);
|
| EXPECT_EQ(MOJO_RESULT_OK, [message[@"result"] unsignedIntValue]);
|
| +
|
| + CloseHandle(handle0);
|
| + CloseHandle(handle1);
|
| }
|
|
|
| } // namespace web
|
|
|