forked from infinitered/rmq
/
app.rb
215 lines (177 loc) · 7.06 KB
/
app.rb
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
describe 'app' do
before do
@app = RubyMotionQuery::App
end
it 'should return App with rmq#app' do
rmq = RubyMotionQuery::RMQ.new
rmq.app.should == @app
end
it 'should return App with RMQ.app' do
RubyMotionQuery::RMQ.app.should == @app
end
it 'should return app window' do
@app.window.should == UIApplication.sharedApplication.keyWindow
end
it 'should return app windows' do
@app.windows.should == UIApplication.sharedApplication.windows
end
it 'should return app delegate' do
@app.delegate.should == UIApplication.sharedApplication.delegate
end
it 'should return app version' do
@app.version.should == NSBundle.mainBundle.infoDictionary['CFBundleVersion']
end
it 'should return app name' do
@app.name.should == NSBundle.mainBundle.objectForInfoDictionaryKey('CFBundleDisplayName')
end
it 'should return app identifier' do
@app.identifier.should == NSBundle.mainBundle.bundleIdentifier
end
it 'should return app resource_path' do
@app.resource_path.should == NSBundle.mainBundle.resourcePath
end
it 'should return app document_path' do
@app.document_path.should == NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0]
end
it 'should delay 0.01 second then fire callback' do
before_time = Time.now
@app.after 0.01 do
after_time = Time.now
(after_time - before_time).should.be >= 0.01
(after_time - before_time).should.be.close 0.01, 0.005
resume
end
wait {}
end
it 'should fire twice 0.01 seconds apart' do
before_time = Time.now
count = 0
timer = @app.every 0.01 do
after_time = Time.now
(after_time - before_time).should.be >= 0.01
(after_time - before_time).should.be.close 0.01, 0.005
count += 1
before_time = after_time # reset
resume if count >= 2
end
wait { timer.invalidate }
end
describe 'environment' do
it 'should return environment as symbol, :test in this case' do
@app.environment.should == :test
end
it 'should return true if environment is test when test? is called' do
@app.test?.should == true
end
it 'should return false if environment is not development when development? is called' do
@app.development?.should == false
end
it 'should return false if environment is not release when release? or production? are called' do
@app.release?.should == false
@app.production?.should == false
end
end
describe 'app - current_view_controller' do
before do
UIView.setAnimationsEnabled false
end
after do
UIView.setAnimationsEnabled true
end
it 'should return controller that is passed in to current_view_controller if it is justa UIViewController' do
new_controller = UIViewController.new
rmq.app.current_view_controller(new_controller).should == new_controller
end
it 'should return current_view_controller from current window' do
rmq.app.current_view_controller.should == rmq.app.window.rootViewController.visibleViewController
end
it 'should return current_view_controller when presenting a new controller' do
controller = UIViewController.alloc.init
rmq.app.window.rootViewController.presentViewController(controller, animated: false, completion: nil)
rmq.app.current_view_controller.should == controller
controller.dismissViewControllerAnimated(false, completion: nil)
end
# Disabling, this works, but isn't working in tests, TODO, fix
# it 'should return current_view_controller when root controller is UINavigationController with multiple controllers' do
# cur = rmq.app.current_view_controller
# cur.class.should == MainController
# end
it 'should return current_view_controller when root controller is UITabController with multiple controllers' do
tabbar = UITabBarController.alloc.init
new_controller = UIViewController.new
tabbar.viewControllers = [new_controller]
old_root = rmq.app.window.rootViewController
rmq.app.window.rootViewController = tabbar
rmq.app.current_view_controller.should == new_controller
rmq.app.window.rootViewController = old_root
end
it 'should return current_view_controller when root controller is container controller with a topViewController method' do
controller = MyTopViewController.alloc.init
new_controller = UIViewController.new
controller.my_controller = new_controller
controller.my_controller.should == new_controller
old_root = rmq.app.window.rootViewController
rmq.app.window.rootViewController = controller
rmq.app.current_view_controller.should == new_controller
rmq.app.window.rootViewController = old_root
end
it 'should return current_view_controller when root controller is container controller with a visibleViewController method' do
controller = MyVisibleViewController.alloc.init
new_controller = UIViewController.new
controller.my_controller = new_controller
controller.my_controller.should == new_controller
old_root = rmq.app.window.rootViewController
rmq.app.window.rootViewController = controller
rmq.app.current_view_controller.should == new_controller
rmq.app.window.rootViewController = old_root
end
it 'should return current_view_controller when root controller is container controller with a frontViewController method' do
controller = MyFrontViewControllerController.alloc.init
new_controller = UIViewController.new
controller.my_controller = new_controller
controller.my_controller.should == new_controller
old_root = rmq.app.window.rootViewController
rmq.app.window.rootViewController = controller
rmq.app.current_view_controller.should == new_controller
rmq.app.window.rootViewController = old_root
end
it 'should return current_view_controller when root controller is container controller with a center method' do
controller = MyCenterViewControllerController.alloc.init
new_controller = UIViewController.new
controller.my_controller = new_controller
controller.my_controller.should == new_controller
old_root = rmq.app.window.rootViewController
rmq.app.window.rootViewController = controller
rmq.app.current_view_controller.should == new_controller
rmq.app.window.rootViewController = old_root
end
it 'should return current_view_controller when root controller is container controller with more than one child controllers' do
# TODO
1.should == 1
end
end
end
class MyTopViewController < UIViewController
attr_accessor :my_controller
def topViewController
@my_controller
end
end
class MyVisibleViewController < UIViewController
attr_accessor :my_controller
def visibleViewController
@my_controller
end
end
class MyFrontViewControllerController < UIViewController
attr_accessor :my_controller
def frontViewController
@my_controller
end
end
class MyCenterViewControllerController < UIViewController
attr_accessor :my_controller
def center
@my_controller
end
end