OLD | NEW |
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 #import "ios/web/webui/mojo_facade.h" | 5 #import "ios/web/webui/mojo_facade.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #import "base/mac/scoped_nsobject.h" | |
10 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
12 #import "base/test/ios/wait_util.h" | 11 #import "base/test/ios/wait_util.h" |
13 #include "ios/web/public/test/web_test.h" | 12 #include "ios/web/public/test/web_test.h" |
14 #import "ios/web/public/web_state/js/crw_js_injection_evaluator.h" | 13 #import "ios/web/public/web_state/js/crw_js_injection_evaluator.h" |
15 #include "ios/web/public/web_state/web_state_interface_provider.h" | 14 #include "ios/web/public/web_state/web_state_interface_provider.h" |
16 #include "ios/web/test/mojo_test.mojom.h" | 15 #include "ios/web/test/mojo_test.mojom.h" |
17 #include "ios/web/web_state/web_state_impl.h" | 16 #include "ios/web/web_state/web_state_impl.h" |
18 #include "mojo/public/cpp/bindings/binding_set.h" | 17 #include "mojo/public/cpp/bindings/binding_set.h" |
19 #include "services/service_manager/public/cpp/bind_source_info.h" | 18 #include "services/service_manager/public/cpp/bind_source_info.h" |
20 #import "testing/gtest_mac.h" | 19 #import "testing/gtest_mac.h" |
21 #import "third_party/ocmock/OCMock/OCMock.h" | 20 #import "third_party/ocmock/OCMock/OCMock.h" |
22 | 21 |
| 22 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 23 #error "This file requires ARC support." |
| 24 #endif |
| 25 |
23 namespace web { | 26 namespace web { |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 // Serializes the given |object| to JSON string. | 30 // Serializes the given |object| to JSON string. |
28 std::string GetJson(id object) { | 31 std::string GetJson(id object) { |
29 NSData* json_as_data = | 32 NSData* json_as_data = |
30 [NSJSONSerialization dataWithJSONObject:object options:0 error:nil]; | 33 [NSJSONSerialization dataWithJSONObject:object options:0 error:nil]; |
31 base::scoped_nsobject<NSString> json_as_string([[NSString alloc] | 34 NSString* json_as_string = |
32 initWithData:json_as_data | 35 [[NSString alloc] initWithData:json_as_data |
33 encoding:NSUTF8StringEncoding]); | 36 encoding:NSUTF8StringEncoding]; |
34 return base::SysNSStringToUTF8(json_as_string); | 37 return base::SysNSStringToUTF8(json_as_string); |
35 } | 38 } |
36 | 39 |
37 // Deserializes the given |json| to an object. | 40 // Deserializes the given |json| to an object. |
38 id GetObject(const std::string& json) { | 41 id GetObject(const std::string& json) { |
39 NSData* json_as_data = | 42 NSData* json_as_data = |
40 [base::SysUTF8ToNSString(json) dataUsingEncoding:NSUTF8StringEncoding]; | 43 [base::SysUTF8ToNSString(json) dataUsingEncoding:NSUTF8StringEncoding]; |
41 return [NSJSONSerialization JSONObjectWithData:json_as_data | 44 return [NSJSONSerialization JSONObjectWithData:json_as_data |
42 options:0 | 45 options:0 |
43 error:nil]; | 46 error:nil]; |
44 } | 47 } |
45 | 48 |
46 } // namespace | 49 } // namespace |
47 | 50 |
48 // A test fixture to test MojoFacade class. | 51 // A test fixture to test MojoFacade class. |
49 class MojoFacadeTest : public WebTest { | 52 class MojoFacadeTest : public WebTest { |
50 protected: | 53 protected: |
51 MojoFacadeTest() { | 54 MojoFacadeTest() { |
52 interface_provider_ = base::MakeUnique<WebStateInterfaceProvider>(); | 55 interface_provider_ = base::MakeUnique<WebStateInterfaceProvider>(); |
53 interface_provider_->registry()->AddInterface(base::Bind( | 56 interface_provider_->registry()->AddInterface(base::Bind( |
54 &MojoFacadeTest::BindTestUIHandlerMojoRequest, base::Unretained(this))); | 57 &MojoFacadeTest::BindTestUIHandlerMojoRequest, base::Unretained(this))); |
55 evaluator_.reset([[OCMockObject | 58 evaluator_ = |
56 mockForProtocol:@protocol(CRWJSInjectionEvaluator)] retain]); | 59 [OCMockObject mockForProtocol:@protocol(CRWJSInjectionEvaluator)]; |
57 facade_.reset(new MojoFacade( | 60 facade_ = base::MakeUnique<MojoFacade>( |
58 interface_provider_.get(), | 61 interface_provider_.get(), |
59 static_cast<id<CRWJSInjectionEvaluator>>(evaluator_.get()))); | 62 static_cast<id<CRWJSInjectionEvaluator>>(evaluator_)); |
60 } | 63 } |
61 | 64 |
62 OCMockObject* evaluator() { return evaluator_.get(); } | 65 OCMockObject* evaluator() { return evaluator_; } |
63 MojoFacade* facade() { return facade_.get(); } | 66 MojoFacade* facade() { return facade_.get(); } |
64 | 67 |
65 private: | 68 private: |
66 void BindTestUIHandlerMojoRequest( | 69 void BindTestUIHandlerMojoRequest( |
67 const service_manager::BindSourceInfo& source_info, | 70 const service_manager::BindSourceInfo& source_info, |
68 TestUIHandlerMojoRequest request) {} | 71 TestUIHandlerMojoRequest request) {} |
69 | 72 |
70 std::unique_ptr<WebStateInterfaceProvider> interface_provider_; | 73 std::unique_ptr<WebStateInterfaceProvider> interface_provider_; |
71 base::scoped_nsobject<OCMockObject> evaluator_; | 74 OCMockObject* evaluator_; |
72 std::unique_ptr<MojoFacade> facade_; | 75 std::unique_ptr<MojoFacade> facade_; |
73 }; | 76 }; |
74 | 77 |
75 // Tests connecting to existing interface and closing the handle. | 78 // Tests connecting to existing interface and closing the handle. |
76 TEST_F(MojoFacadeTest, GetInterfaceAndCloseHandle) { | 79 TEST_F(MojoFacadeTest, GetInterfaceAndCloseHandle) { |
77 // Bind to the interface. | 80 // Bind to the interface. |
78 NSDictionary* connect = @{ | 81 NSDictionary* connect = @{ |
79 @"name" : @"interface_provider.getInterface", | 82 @"name" : @"interface_provider.getInterface", |
80 @"args" : @{ | 83 @"args" : @{ |
81 @"interfaceName" : @"::TestUIHandlerMojo", | 84 @"interfaceName" : @"::TestUIHandlerMojo", |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 NSDictionary* message = GetObject(facade()->HandleMojoMessage(GetJson(read))); | 277 NSDictionary* message = GetObject(facade()->HandleMojoMessage(GetJson(read))); |
275 EXPECT_TRUE([message isKindOfClass:[NSDictionary class]]); | 278 EXPECT_TRUE([message isKindOfClass:[NSDictionary class]]); |
276 EXPECT_TRUE(message); | 279 EXPECT_TRUE(message); |
277 NSArray* expected_message = @[ @9, @2, @216 ]; // 2008 does not fit 8-bit. | 280 NSArray* expected_message = @[ @9, @2, @216 ]; // 2008 does not fit 8-bit. |
278 EXPECT_NSEQ(expected_message, message[@"buffer"]); | 281 EXPECT_NSEQ(expected_message, message[@"buffer"]); |
279 EXPECT_FALSE([message[@"handles"] count]); | 282 EXPECT_FALSE([message[@"handles"] count]); |
280 EXPECT_EQ(MOJO_RESULT_OK, [message[@"result"] unsignedIntValue]); | 283 EXPECT_EQ(MOJO_RESULT_OK, [message[@"result"] unsignedIntValue]); |
281 } | 284 } |
282 | 285 |
283 } // namespace web | 286 } // namespace web |
OLD | NEW |