win 0.3.24 → 0.3.25

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,269 +1,268 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
1
+ require 'spec_helper'
2
2
  require 'win/gui/input'
3
3
  require 'win/error'
4
4
 
5
- module WinGuiMessageTest
5
+ include WinTestApp
6
+ include Win::Gui::Message
7
+ include Win::Gui::Window
8
+ include Win::Gui::Input
9
+ include Win::Error
6
10
 
7
- include WinTestApp
8
- include Win::Gui::Message
9
- include Win::Gui::Window
10
- include Win::Gui::Input
11
- include Win::Error
12
-
13
- def buffer
14
- @buffer ||= FFI::MemoryPointer.new :char, 1024
15
- end
11
+ def buffer
12
+ @buffer ||= FFI::MemoryPointer.new :char, 1024
13
+ end
16
14
 
17
- def msg
18
- @msg ||= Win::Gui::Message::Msg.new
19
- end
15
+ def msg
16
+ @msg ||= Win::Gui::Message::Msg.new
17
+ end
20
18
 
21
- def msg_callback
22
- lambda {|handle, message, data, result| @handle = handle; @message = message; @data = data; @result = result }
23
- end
19
+ def msg_callback
20
+ lambda { |handle, message, data, result| @handle = handle; @message = message; @data = data; @result = result }
21
+ end
24
22
 
25
- def should_have msg, members
26
- members.each do |member, value|
27
- case member
28
- when :l_param
29
- msg[member].address.should == value
30
- when :time
31
- msg[member].should be > value
32
- else
33
- msg[member].should == value
34
- end
23
+ def should_have msg, members
24
+ members.each do |member, value|
25
+ case member
26
+ when :l_param
27
+ msg[member].address.should == value
28
+ when :time
29
+ msg[member].should be > value
30
+ else
31
+ msg[member].should == value
35
32
  end
36
33
  end
34
+ end
37
35
 
38
- def clear_thread_queue
39
- get_message while peek_message
40
- end
36
+ def clear_thread_queue
37
+ get_message while peek_message
38
+ end
41
39
 
42
- describe Win::Gui::Message, ' defines a set of API functions related to Window messaging' do
43
- before(:all){clear_thread_queue}
44
-
45
- describe '#post_message' do
46
- before(:each){clear_thread_queue}
47
- after(:all){close_test_app if @launched_test_app}
48
-
49
- spec{ use{ success = PostMessage(handle = 0, msg = 0, w_param = 0, l_param = nil) }}
50
- spec{ use{ success = post_message(handle = 0, msg = 0, w_param = 0, l_param = nil) }}
51
-
52
- context 'places (posts) a message in the message queue associated with the thread that created the specified window' do
53
- it 'with nil as last argument(lParam)' do
54
- app = launch_test_app
55
- post_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, nil).should == true
56
- sleep SLEEP_DELAY
57
- window?(app.handle).should == false
58
- end
59
-
60
- it 'with pointer as last argument(lParam)' do
61
- app = launch_test_app
62
- post_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, FFI::MemoryPointer.new(:long)).should == true
63
- sleep SLEEP_DELAY
64
- window?(app.handle).should == false
65
- end
66
-
67
- it 'with Fixnum as last argument(lParam)' do
68
- app = launch_test_app
69
- post_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, 0).should == true
70
- sleep SLEEP_DELAY
71
- window?(app.handle).should == false
72
- end
73
- end
40
+ describe Win::Gui::Message, ' defines a set of API functions related to Window messaging' do
41
+ before(:all) { clear_thread_queue }
74
42
 
75
- it 'places (posts) a message into current thread`s queue if first arg is 0' do
76
- post_message(0, WM_USER, 33, nil).should == true
77
- msg = get_message()
78
- should_have msg, hwnd: 0, message: WM_USER, w_param: 33, l_param: 0
43
+ describe '#post_message' do
44
+ before(:each) { clear_thread_queue }
45
+ after(:all) { close_test_app if @launched_test_app }
46
+
47
+ spec { use { success = PostMessage(handle = 0, msg = 0, w_param = 0, l_param = nil) } }
48
+ spec { use { success = post_message(handle = 0, msg = 0, w_param = 0, l_param = nil) } }
49
+
50
+ context 'places (posts) a message in the message queue associated with the thread that created the specified window' do
51
+ it 'with nil as last argument(lParam)' do
52
+ app = launch_test_app
53
+ post_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, nil).should == true
54
+ sleep SLEEP_DELAY
55
+ window?(app.handle).should == false
79
56
  end
80
57
 
81
- it 'returns without waiting for the thread to process the message'
82
-
83
- end # describe '#post_message'
84
-
85
- describe '#send_message' do
86
- after(:all){close_test_app if @launched_test_app}
87
-
88
- spec{ use{ success = SendMessage(handle = 0, msg = 0, w_param = 0, l_param = nil) }}
89
- spec{ use{ success = send_message(handle = 0, msg = 0, w_param = 0, l_param = nil) }}
90
-
91
- context 'sends a message to the specified window' do
92
- it 'with nil as last argument(lParam)' do
93
- app = launch_test_app
94
- send_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, nil)
95
- sleep SLEEP_DELAY
96
- window?(app.handle).should == false
97
- end
98
-
99
- it 'with pointer as last argument(lParam)' do
100
- app = launch_test_app
101
- send_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, FFI::MemoryPointer.new(:long))
102
- sleep SLEEP_DELAY
103
- window?(app.handle).should == false
104
- end
105
-
106
- it 'with Fixnum as last argument(lParam)' do
107
- app = launch_test_app
108
- send_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, 0)
109
- sleep SLEEP_DELAY
110
- window?(app.handle).should == false
111
- end
58
+ it 'with pointer as last argument(lParam)' do
59
+ app = launch_test_app
60
+ post_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, FFI::MemoryPointer.new(:long)).should == true
61
+ sleep SLEEP_DELAY
62
+ window?(app.handle).should == false
112
63
  end
113
- end # describe '#send_message'
114
-
115
- describe "#send_message_callback" do
116
- before(:all){@app=launch_test_app}
117
- after(:all){close_test_app if @launched_test_app}
118
-
119
- spec{ use{ success = SendMessageCallback(h_wnd=0, msg=0, w_param=0, l_param=nil, msg_callback, data=0) }}
120
- spec{ use{ success = send_message_callback(h_wnd=0, msg=0, w_param=0, l_param=nil, data=0, &msg_callback) }}
121
-
122
- it "sends message to window and returns, specifying callback to be called by system after message is processed" do
123
- sent = SendMessageCallback(@app.handle, WM_USER, 0, nil, msg_callback, data=13)
124
- sent.should == 1
125
- @handle.should == nil # Callback did not fire just yet
126
- @message.should == nil
127
- @data.should == nil
128
- @result.should == nil
129
-
130
- sleep SLEEP_DELAY # Small delay to allow message delivery
131
- peek_message # Dispatching sent message (even though there is nothing in queue)
132
-
133
- @handle.should == @app.handle
134
- @message.should == WM_USER
135
- @data.should == 13
136
- @result.should == 0
64
+
65
+ it 'with Fixnum as last argument(lParam)' do
66
+ app = launch_test_app
67
+ post_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, 0).should == true
68
+ sleep SLEEP_DELAY
69
+ window?(app.handle).should == false
137
70
  end
71
+ end
72
+
73
+ it 'places (posts) a message into current thread`s queue if first arg is 0' do
74
+ post_message(0, WM_USER, 33, nil).should == true
75
+ msg = get_message()
76
+ should_have msg, hwnd: 0, message: WM_USER, w_param: 33, l_param: 0
77
+ end
138
78
 
139
- it "snake_case api defaults data to 0, converts block into callback and returns true/false" do
140
- sent = send_message_callback(@app.handle, WM_USER, 0, nil){|*args| @data=args[2]}
141
- sent.should == true
142
- @data.should == nil # Callback did not fire just yet
79
+ it 'returns without waiting for the thread to process the message'
143
80
 
144
- sleep SLEEP_DELAY # Small delay to allow message delivery
145
- peek_message # Dispatching sent message (even though there is nothing in queue)
81
+ end # describe '#post_message'
146
82
 
147
- @data.should == 0
148
- end
83
+ describe '#send_message' do
84
+ after(:all) { close_test_app if @launched_test_app }
149
85
 
150
- it "can be used with :pointer as a 4th arg" do
151
- sent = send_message_callback(@app.handle, WM_USER, 0, FFI::MemoryPointer.new(:long)){|*args|}
152
- sent.should == true
86
+ spec { use { success = SendMessage(handle = 0, msg = 0, w_param = 0, l_param = nil) } }
87
+ spec { use { success = send_message(handle = 0, msg = 0, w_param = 0, l_param = nil) } }
88
+
89
+ context 'sends a message to the specified window' do
90
+ it 'with nil as last argument(lParam)' do
91
+ app = launch_test_app
92
+ send_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, nil)
93
+ sleep SLEEP_DELAY
94
+ window?(app.handle).should == false
153
95
  end
154
96
 
155
- it "can be used with :long as a 4th arg" do
156
- sent = send_message_callback(@app.handle, WM_USER, 0, 0){|*args|}
157
- sent.should == true
97
+ it 'with pointer as last argument(lParam)' do
98
+ app = launch_test_app
99
+ send_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, FFI::MemoryPointer.new(:long))
100
+ sleep SLEEP_DELAY
101
+ window?(app.handle).should == false
158
102
  end
159
103
 
160
- it "fails if unable to send message" do
161
- sent = SendMessageCallback(not_a_handle, WM_USER, 0, nil, msg_callback, 0)
162
- sent.should == 0
163
- send_message_callback(not_a_handle, WM_USER, 0, nil){|*args|@data=args[2]}
164
- sent.should == 0
165
- get_last_error.should == "Invalid window handle."
104
+ it 'with Fixnum as last argument(lParam)' do
105
+ app = launch_test_app
106
+ send_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, 0)
107
+ sleep SLEEP_DELAY
108
+ window?(app.handle).should == false
166
109
  end
167
- end # describe send_message_callback
110
+ end
111
+ end # describe '#send_message'
112
+
113
+ describe "#send_message_callback" do
114
+ before(:all) { @app=launch_test_app }
115
+ after(:all) { close_test_app if @launched_test_app }
116
+
117
+ spec { use { success = SendMessageCallback(h_wnd=0, msg=0, w_param=0, l_param=nil, msg_callback, data=0) } }
118
+ spec { use { success = send_message_callback(h_wnd=0, msg=0, w_param=0, l_param=nil, data=0, &msg_callback) } }
119
+
120
+ it "sends message to window and returns, specifying callback to be called by system after message is processed" do
121
+ sent = SendMessageCallback(@app.handle, WM_USER, 0, nil, msg_callback, data=13)
122
+ sent.should == 1
123
+ @handle.should == nil # Callback did not fire just yet
124
+ @message.should == nil
125
+ @data.should == nil
126
+ @result.should == nil
127
+
128
+ sleep SLEEP_DELAY # Small delay to allow message delivery
129
+ peek_message # Dispatching sent message (even though there is nothing in queue)
130
+
131
+ @handle.should == @app.handle
132
+ @message.should == WM_USER
133
+ @data.should == 13
134
+ @result.should == 0
135
+ end
168
136
 
169
- describe "#get_message" do
170
- before(:all){clear_thread_queue; 2.times {post_message 0, 0, 0, nil}}
137
+ it "snake_case api defaults data to 0, converts block into callback and returns true/false" do
138
+ sent = send_message_callback(@app.handle, WM_USER, 0, nil) { |*args| @data=args[2] }
139
+ sent.should == true
140
+ @data.should == nil # Callback did not fire just yet
171
141
 
172
- spec{ use{ res = GetMessage(msg, handle=0, msg_filter_min=0, msg_filter_max=0) }}
173
- spec{ use{ message = get_message(msg, handle=0, msg_filter_min=0, msg_filter_max=0) }}
142
+ sleep SLEEP_DELAY # Small delay to allow message delivery
143
+ peek_message # Dispatching sent message (even though there is nothing in queue)
174
144
 
175
- it "original api retrieves a message from the calling thread's message queue" do
176
- set_cursor_pos(x=0, y=0)
177
- post_message(0, WM_USER+1, 33, nil)
178
- res = GetMessage(msg, handle=0, msg_filter_min=0, msg_filter_max=0)
179
- res.should == 1
180
- # p msg[:hwnd], msg[:message], msg[:w_param], msg[:l_param], msg[:time], msg[:x], msg[:y]
181
- should_have msg, hwnd: 0, message: WM_USER+1, w_param: 33, x: 0, y: 0, l_param: 0, time: 1000000
182
- end
145
+ @data.should == 0
146
+ end
183
147
 
184
- it "original api returns -1 if there is an error (wrong handle, in this case)" do
185
- res = GetMessage(msg, not_a_handle, msg_filter_min=0, msg_filter_max=0)
186
- res.should == -1
187
- end
148
+ it "can be used with :pointer as a 4th arg" do
149
+ sent = send_message_callback(@app.handle, WM_USER, 0, FFI::MemoryPointer.new(:long)) { |*args|}
150
+ sent.should == true
151
+ end
188
152
 
189
- it "original api returns 0 if WM_QUIT was posted to thread`s message queue" do
190
- post_message(0, WM_QUIT, 13, nil)
191
- res = GetMessage(msg, 0, msg_filter_min=0, msg_filter_max=0)
192
- res.should == 0
193
- end
153
+ it "can be used with :long as a 4th arg" do
154
+ sent = send_message_callback(@app.handle, WM_USER, 0, 0) { |*args|}
155
+ sent.should == true
156
+ end
194
157
 
195
- it "snake_case api returns a message struct retrieved from the calling thread's message queue " do
196
- set_cursor_pos(x=99, y=99)
197
- post_message(0, WM_USER+2, 33, nil)
198
- msg = get_message()
199
- should_have msg, hwnd: 0, message: WM_USER+2, w_param: 33, x: 99, y: 99, l_param: 0, time: 1000000
200
- end
158
+ it "fails if unable to send message" do
159
+ sent = SendMessageCallback(not_a_handle, WM_USER, 0, nil, msg_callback, 0)
160
+ sent.should == 0
161
+ send_message_callback(not_a_handle, WM_USER, 0, nil) { |*args| @data=args[2] }
162
+ sent.should == 0
163
+ get_last_error.should == "Invalid window handle."
164
+ end
165
+ end # describe send_message_callback
201
166
 
202
- it "snake_case api returns nil if there is an error (wrong handle, in this case)" do
203
- get_message(msg, not_a_handle).should == nil
204
- end
167
+ describe "#get_message" do
168
+ before(:all) { clear_thread_queue; 2.times { post_message 0, 0, 0, nil } }
205
169
 
206
- it "snake_case api returns false if WM_QUIT was posted to thread`s message queue" do
207
- post_message(0, WM_QUIT, 13, nil)
208
- get_message.should == false
209
- end
210
- end # describe get_message
170
+ spec { use { res = GetMessage(msg, handle=0, msg_filter_min=0, msg_filter_max=0) } }
171
+ spec { use { message = get_message(msg, handle=0, msg_filter_min=0, msg_filter_max=0) } }
211
172
 
173
+ it "original api retrieves a message from the calling thread's message queue" do
174
+ set_cursor_pos(x=0, y=0)
175
+ post_message(0, WM_USER+1, 33, nil)
176
+ res = GetMessage(msg, handle=0, msg_filter_min=0, msg_filter_max=0)
177
+ res.should == 1
178
+ # p msg[:hwnd], msg[:message], msg[:w_param], msg[:l_param], msg[:time], msg[:x], msg[:y]
179
+ should_have msg, hwnd: 0, message: WM_USER+1, w_param: 33, x: 0, y: 0, l_param: 0, time: 1000000
180
+ end
212
181
 
213
- describe "#peek_message" do
214
- before(:all){set_cursor_pos(x=0, y=0); post_message(0, WM_USER+2, 13, nil)}
215
- spec{ use{ success = PeekMessage(msg, h_wnd=0, filter_min=0, filter_max=0, remove_msg=0) }}
216
- spec{ use{ success = peek_message(msg, h_wnd=0, filter_min=0, filter_max=0, remove_msg=0) }}
182
+ it "original api returns -1 if there is an error (wrong handle, in this case)" do
183
+ res = GetMessage(msg, not_a_handle, msg_filter_min=0, msg_filter_max=0)
184
+ res.should == -1
185
+ end
217
186
 
218
- it "original api checks the thread message queue for a posted message, retrieves it without removing" do
219
- 10.times do
220
- res = PeekMessage(msg, h_wnd=0, filter_min=0, filter_max=0, remove_msg=0)
221
- res.should == 1
222
- should_have msg, hwnd: 0, message: WM_USER+2, w_param: 13, x: 0, y: 0, l_param: 0, time: 1000000
223
- end
224
- end
187
+ it "original api returns 0 if WM_QUIT was posted to thread`s message queue" do
188
+ post_message(0, WM_QUIT, 13, nil)
189
+ res = GetMessage(msg, 0, msg_filter_min=0, msg_filter_max=0)
190
+ res.should == 0
191
+ end
192
+
193
+ it "snake_case api returns a message struct retrieved from the calling thread's message queue " do
194
+ set_cursor_pos(x=99, y=99)
195
+ post_message(0, WM_USER+2, 33, nil)
196
+ msg = get_message()
197
+ should_have msg, hwnd: 0, message: WM_USER+2, w_param: 33, x: 99, y: 99, l_param: 0, time: 1000000
198
+ end
199
+
200
+ it "snake_case api returns nil if there is an error (wrong handle, in this case)" do
201
+ get_message(msg, not_a_handle).should == nil
202
+ end
203
+
204
+ it "snake_case api returns false if WM_QUIT was posted to thread`s message queue" do
205
+ post_message(0, WM_QUIT, 13, nil)
206
+ get_message.should == false
207
+ end
208
+ end # describe get_message
225
209
 
226
- it "snake_case api checks the thread message queue for a posted message, returns it without removing" do
227
- 10.times do
228
- msg = peek_message()
229
- should_have msg, hwnd: 0, message: WM_USER+2, w_param: 13, x: 0, y: 0, l_param: 0, time: 1000000
230
- end
231
- end
232
210
 
233
- it "original api returns 0 if no message in queue" do
234
- get_message
235
- PeekMessage(msg, h_wnd=0, filter_min=0, filter_max=0, remove_msg=0).should == 0
211
+ describe "#peek_message" do
212
+ before(:all) { set_cursor_pos(x=0, y=0); post_message(0, WM_USER+2, 13, nil) }
213
+ spec { use { success = PeekMessage(msg, h_wnd=0, filter_min=0, filter_max=0, remove_msg=0) } }
214
+ spec { use { success = peek_message(msg, h_wnd=0, filter_min=0, filter_max=0, remove_msg=0) } }
215
+
216
+ it "original api checks the thread message queue for a posted message, retrieves it without removing" do
217
+ 10.times do
218
+ res = PeekMessage(msg, h_wnd=0, filter_min=0, filter_max=0, remove_msg=0)
219
+ res.should == 1
220
+ should_have msg, hwnd: 0, message: WM_USER+2, w_param: 13, x: 0, y: 0, l_param: 0, time: 1000000
236
221
  end
222
+ end
237
223
 
238
- it "snake_case api returns nil if no message in queue" do
239
- peek_message.should == nil
224
+ it "snake_case api checks the thread message queue for a posted message, returns it without removing" do
225
+ 10.times do
226
+ msg = peek_message()
227
+ should_have msg, hwnd: 0, message: WM_USER+2, w_param: 13, x: 0, y: 0, l_param: 0, time: 1000000
240
228
  end
241
- end # describe peek_message
229
+ end
242
230
 
243
- describe "#translate_message" do
244
- spec{ use{ success = TranslateMessage(msg) }}
245
- spec{ use{ success = translate_message(msg) }}
231
+ it "original api returns 0 if no message in queue" do
232
+ get_message
233
+ PeekMessage(msg, h_wnd=0, filter_min=0, filter_max=0, remove_msg=0).should == 0
234
+ end
246
235
 
247
- it "translates virtual-key message into character message which is then posted to the thread's message queue"
236
+ it "snake_case api returns nil if no message in queue" do
237
+ peek_message.should == nil
238
+ end
239
+ end # describe peek_message
248
240
 
249
- it "returns zero/false if no translation took place" do
250
- TranslateMessage(msg).should == 0
251
- translate_message(msg).should == false
252
- end
253
- end # describe translate_message
241
+ describe "#translate_message" do
242
+ spec { use { success = TranslateMessage(msg) } }
243
+ spec { use { success = translate_message(msg) } }
254
244
 
255
- describe "#dispatch_message" do
256
- spec{ use{ res = DispatchMessage(msg) }} #return value is normally ignored
257
- spec{ use{ res = dispatch_message(msg) }} #return value is normally ignored
245
+ it "translates virtual-key message into character message which is then posted to the thread's message queue" do
246
+ pending
247
+ end
258
248
 
259
- it "dispatches a message to a window procedure. Typically used to dispatch a message retrieved by GetMessage" do
260
- pending
261
- res = DispatchMessage(msg)
262
- end
249
+ it "returns zero/false if no translation took place" do
250
+ TranslateMessage(msg).should == 0
251
+ translate_message(msg).should == false
252
+ end
253
+ end # describe translate_message
263
254
 
264
- end # describe dispatch_message
255
+ describe "#dispatch_message" do
256
+ spec { use { res = DispatchMessage(msg) } } #return value is normally ignored
257
+ spec { use { res = dispatch_message(msg) } } #return value is normally ignored
265
258
 
266
- end # Win::Gui::Message, ' defines a set of API functions related to Window messaging'
267
- end
259
+ it "dispatches a message to a window procedure. Typically used to dispatch a message retrieved by GetMessage" do
260
+ pending
261
+ res = DispatchMessage(msg)
262
+ end
263
+
264
+ end # describe dispatch_message
265
+
266
+ end # Win::Gui::Message, ' defines a set of API functions related to Window messaging'
268
267
 
269
268