forked from kishikawakatsumi/JavaScriptBridge
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJavaScriptBridgeTests.m
More file actions
160 lines (126 loc) · 4.31 KB
/
JavaScriptBridgeTests.m
File metadata and controls
160 lines (126 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// JavaScriptBridgeTests.m
// JavaScriptBridgeTests
//
// Created by kishikawa katsumi on 2014/01/02.
// Copyright (c) 2014 kishikawa katsumi. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "JavaScriptBridge.h"
@import ObjectiveC;
@interface JavaScriptBridgeTests : XCTestCase
@end
@implementation JavaScriptBridgeTests
- (void)setUp
{
[super setUp];
}
- (void)tearDown
{
[super tearDown];
}
- (void)testGlobalContext
{
JSContext *globalContext = [JSBScriptingSupport globalContext];
XCTAssertNotNil(globalContext);
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
- (void)testCustomContext
{
JSContext *globalContext = [JSBScriptingSupport globalContext];
XCTAssertNotNil(globalContext);
[globalContext evaluateScript:@"var string = 'string';"];
JSValue *value = nil;
value = globalContext[@"string"];
XCTAssertTrue(value && !value.isUndefined);
XCTAssertEqualObjects(value.toObject, @"string");
JSContext *context = [[JSContext alloc] init];
context.exceptionHandler = ^(JSContext *context, JSValue *value) {
NSLog(@"%@", value);
};
[context addScriptingSupport:@"Foundation"];
[context addScriptingSupport:@"UIKit"];
[context addScriptingSupport:@"QuartzCore"];
[context evaluateScript:
@"var window = UIWindow.alloc().initWithFrame(UIScreen.mainScreen().bounds);"
@"window.backgroundColor = UIColor.whiteColor();"
@""
@"var navigationController = UINavigationController.new();"
@""
@"var tableViewController = UITableViewController.new();"
@"tableViewController.navigationItem.title = 'JavaScriptBridge';"
@"navigationController.viewControllers = [tableViewController];"
@""
@"var MyObject = JSB.defineClass('MyObject : NSObject', {"
@" myMethod: function() {"
@" return 'myMethod';"
@" }"
@"});"
@""
@"var myObject = MyObject.new();"
@""
@"window.rootViewController = navigationController;"
];
XCTAssertNotNil(context);
value = context[@"string"];
XCTAssertTrue(value && value.isUndefined);
value = context[@"window"];
XCTAssertTrue(value && !value.isUndefined);
value = context[@"navigationController"];
XCTAssertTrue(value && !value.isUndefined);
value = context[@"tableViewController"];
XCTAssertTrue(value && !value.isUndefined);
value = context[@"myObject"];
XCTAssertTrue(value && !value.isUndefined);
id object = value.toObject;
NSString *string = [object performSelector:@selector(myMethod) withObject:nil];
XCTAssertEqualObjects(string, @"myMethod");
}
- (void)testRedefineClass
{
JSContext *context = [[JSContext alloc] init];
context.exceptionHandler = ^(JSContext *context, JSValue *value) {
NSLog(@"%@", value);
};
[context addScriptingSupport:@"Foundation"];
[context addScriptingSupport:@"UIKit"];
[context addScriptingSupport:@"QuartzCore"];
[context evaluateScript:
@"var MyObject = JSB.defineClass('MyObject : NSObject', {"
@" myMethod: function() {"
@" return 'myMethod';"
@" }"
@"});"
@""
@"var myObject = MyObject.new();"
];
XCTAssertNotNil(context);
JSValue *value = nil;
value = context[@"myObject"];
XCTAssertTrue(value && !value.isUndefined);
id object1 = value.toObject;
NSString *string1 = [object1 performSelector:@selector(myMethod) withObject:nil];
XCTAssertEqualObjects(string1, @"myMethod");
[context evaluateScript:
@"var MyObject = JSB.defineClass('MyObject : NSObject', {"
@" myMethod: function() {"
@" return 'myMethod2';"
@" },"
@" anotherMethod: function() {"
@" return 'anotherMethod';"
@" }"
@"});"
@""
@"var myObject = MyObject.new();"
];
value = context[@"myObject"];
XCTAssertTrue(value && !value.isUndefined);
id object2 = value.toObject;
NSString *string2 = [object2 performSelector:@selector(myMethod) withObject:nil];
XCTAssertEqualObjects(string2, @"myMethod2");
NSString *string3 = [object2 performSelector:@selector(anotherMethod) withObject:nil];
XCTAssertEqualObjects(string3, @"anotherMethod");
}
#pragma clang diagnostic pop
@end