win 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,309 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'win/window'
3
+
4
+ module WinWindowTest
5
+
6
+ include WinTest
7
+ include Win::Window
8
+
9
+ def launch_test_app
10
+ system TEST_APP_START
11
+ sleep TEST_SLEEP_DELAY until (handle = find_window(nil, TEST_WIN_TITLE))
12
+ @launched_test_app = Window.new handle
13
+ end
14
+
15
+ def close_test_app(app = @launched_test_app)
16
+ while app and app.respond_to? :handle and find_window(nil, TEST_WIN_TITLE)
17
+ post_message(app.handle, WM_SYSCOMMAND, SC_CLOSE, 0)
18
+ sleep TEST_SLEEP_DELAY
19
+ end
20
+ @launched_test_app = nil
21
+ end
22
+
23
+ # Creates test app object and yields it back to the block
24
+ def test_app
25
+ app = launch_test_app
26
+
27
+ def app.textarea #define singleton method retrieving app's text area
28
+ Window.new find_window_ex(self.handle, 0, TEST_TEXTAREA_CLASS, nil)
29
+ end
30
+
31
+ yield app
32
+ close_test_app
33
+ end
34
+
35
+ describe Win::Window, ' defines a set user32 API functions related to Window manipulation' do
36
+ describe '#window?' do
37
+ spec{ use{ IsWindow(any_handle) }}
38
+ spec{ use{ is_window(any_handle) }}
39
+ spec{ use{ window?(any_handle) }}
40
+
41
+ it 'returns true if window exists' do
42
+ window?(any_handle).should == true
43
+ end
44
+
45
+ it 'returns false for invalid window handle' do
46
+ window?(not_a_handle).should == false
47
+ end
48
+
49
+ it 'changes from true to false when existing window is closed' do
50
+ test_app do |app|
51
+ @app_handle = app.handle
52
+ @ta_handle = app.textarea.handle
53
+ window?(@app_handle).should == true
54
+ window?(@ta_handle).should == true
55
+ end
56
+ window?(@app_handle).should == false
57
+ window?(@ta_handle).should == false
58
+ end
59
+ end
60
+
61
+ describe '#window_visible?' do
62
+ spec{ use{ IsWindowVisible(any_handle) }}
63
+ spec{ use{ is_window_visible(any_handle) }}
64
+ spec{ use{ window_visible?(any_handle) }}
65
+ spec{ use{ visible?(any_handle) }}
66
+
67
+ it 'returns true when window is visible, false when window is hidden' do
68
+ test_app do |app|
69
+ visible?(app.handle).should == true
70
+ window_visible?(app.handle).should == true
71
+ window_visible?(app.textarea.handle).should == true
72
+ hide_window(app.handle)
73
+ visible?(app.handle).should == false
74
+ window_visible?(app.handle).should == false
75
+ window_visible?(app.textarea.handle).should == false
76
+ end
77
+ end
78
+ end
79
+
80
+ describe '#maximized?' do
81
+ spec{ use{ IsZoomed(any_handle) }}
82
+ spec{ use{ is_zoomed(any_handle) }}
83
+ spec{ use{ zoomed?(any_handle) }}
84
+ spec{ use{ maximized?(any_handle) }}
85
+
86
+ it 'returns true if the window is maximized, false otherwise' do
87
+ test_app do |app|
88
+ zoomed?(app.handle).should == false
89
+ maximized?(app.handle).should == false
90
+ show_window(app.handle, SW_MAXIMIZE)
91
+ maximized?(app.handle).should == true
92
+ zoomed?(app.handle).should == true
93
+ end
94
+ end
95
+ end
96
+
97
+ describe '#minimized?' do
98
+ spec{ use{ IsIconic(any_handle) }}
99
+ spec{ use{ is_iconic(any_handle) }}
100
+ spec{ use{ iconic?(any_handle) }}
101
+ spec{ use{ minimized?(any_handle) }}
102
+
103
+ it 'returns true if the window is minimized, false otherwise' do
104
+ test_app do |app|
105
+ iconic?(app.handle).should == false
106
+ minimized?(app.handle).should == false
107
+ show_window(app.handle, SW_MINIMIZE)
108
+ iconic?(app.handle).should == true
109
+ minimized?(app.handle).should == true
110
+ end
111
+ end
112
+ end
113
+
114
+ describe '#child?' do
115
+ spec{ use{ IsChild(parent_handle = any_handle, handle = any_handle) }}
116
+ spec{ use{ is_child(parent_handle = any_handle, handle = any_handle) }}
117
+ spec{ use{ child?(parent_handle = any_handle, handle = any_handle) }}
118
+
119
+ it 'returns true if the window is a child of given parent, false otherwise' do
120
+ test_app do |app|
121
+ child?(app.handle, app.textarea.handle).should == true
122
+ child?(app.handle, any_handle).should == false
123
+ end
124
+ end
125
+ end
126
+
127
+ describe '#find_window(w)' do
128
+ spec{ use{ FindWindow(class_name = nil, win_name = nil) }}
129
+ spec{ use{ find_window(class_name = nil, win_name = nil) }}
130
+ # Widebyte (unicode) version
131
+ spec{ use{ FindWindowW(class_name = nil, win_name = nil) }}
132
+ spec{ use{ find_window_w(class_name = nil, win_name = nil) }}
133
+
134
+ it 'returns either Integer Window handle or nil' do
135
+ find_window(nil, nil).should be_a_kind_of Integer
136
+ find_window(TEST_IMPOSSIBLE, nil).should == nil
137
+ end
138
+
139
+ it 'returns nil if Window is not found' do
140
+ find_window(TEST_IMPOSSIBLE, nil).should == nil
141
+ find_window(nil, TEST_IMPOSSIBLE).should == nil
142
+ find_window(TEST_IMPOSSIBLE, TEST_IMPOSSIBLE).should == nil
143
+ find_window_w(TEST_IMPOSSIBLE, nil).should == nil
144
+ find_window_w(nil, TEST_IMPOSSIBLE).should == nil
145
+ find_window_w(TEST_IMPOSSIBLE, TEST_IMPOSSIBLE).should == nil
146
+ end
147
+
148
+ it 'finds at least one window if both args are nils' do
149
+ find_window(nil, nil).should_not == nil
150
+ find_window_w(nil, nil).should_not == nil
151
+ end
152
+
153
+ it 'finds top-level window by window class or title' do
154
+ test_app do |app|
155
+ find_window(TEST_WIN_CLASS, nil).should == app.handle
156
+ find_window(nil, TEST_WIN_TITLE).should == app.handle
157
+ find_window_w(TEST_WIN_CLASS.to_w, nil).should == app.handle
158
+ find_window_w(nil, TEST_WIN_TITLE.to_w).should == app.handle
159
+ end
160
+ end
161
+ end
162
+
163
+ describe '#find_window_ex' do
164
+ spec{ use{ FindWindowEx(parent = any_handle, after_child = 0, win_class = nil, win_title = nil) }}
165
+ spec{ use{ find_window_ex(parent = any_handle, after_child = 0, win_class = nil, win_title = nil) }}
166
+
167
+ it 'returns nil if wrong control is given' do
168
+ parent_handle = any_handle
169
+ find_window_ex(parent_handle, 0, TEST_IMPOSSIBLE, nil).should == nil
170
+ find_window_ex(parent_handle, 0, nil, TEST_IMPOSSIBLE).should == nil
171
+ end
172
+
173
+ it 'finds child window/control by class' do
174
+ test_app do |app|
175
+ ta_handle = find_window_ex(app.handle, 0, TEST_TEXTAREA_CLASS, nil)
176
+ ta_handle.should_not == nil
177
+ ta_handle.should == app.textarea.handle
178
+ end
179
+ end
180
+
181
+ it 'finds child window/control by text/title' do
182
+ pending 'Identify appropriate (short name) control'
183
+ test_app do |app|
184
+ keystroke(VK_CONTROL, 'A'.ord)
185
+ keystroke('1'.ord, '2'.ord)
186
+ ta_handle = find_window_ex(app.handle, 0, nil, '12')
187
+ ta_handle.should_not == 0
188
+ ta_handle.should == app.textarea.handle
189
+ end
190
+ end
191
+ end
192
+
193
+ describe '#get_foreground_window' do
194
+ # ! Different from GetActiveWindow !
195
+ spec{ use{ handle = GetForegroundWindow() }}
196
+ spec{ use{ handle = get_foreground_window }}
197
+ spec{ use{ handle = foreground_window }}
198
+
199
+ it 'returns handle to window that is currently in foreground' do
200
+ test_app do |app|
201
+ @app_handle = app.handle
202
+ fg1 = foreground_window
203
+ @app_handle.should == fg1
204
+ end
205
+ fg2 = foreground_window
206
+ @app_handle.should_not == fg2
207
+ end
208
+
209
+ it 'defines #foreground? test function ' do
210
+ test_app do |app|
211
+ @app_handle = app.handle
212
+ foreground?(@app_handle).should == true
213
+ end
214
+ foreground?(@app_handle).should == false
215
+ end
216
+ end
217
+
218
+ describe '#get_active_window' do
219
+ # ! Different from GetForegroundWindow !
220
+ spec{ use{ handle = GetActiveWindow() }}
221
+ spec{ use{ handle = get_active_window }}
222
+ spec{ use{ handle = active_window }}
223
+
224
+ it 'returns handle to the active window attached to the calling thread`s message queue' do
225
+ pending 'No idea how to test it'
226
+ test_app do |app|
227
+ @app_handle = app.handle
228
+ fg1 = active_window
229
+ @app_handle.should == fg1
230
+ end
231
+ fg2 = active_window
232
+ @app_handle.should_not == fg2
233
+ end
234
+ end
235
+
236
+ describe '#get_window_text(w)' do
237
+ spec{ use{ GetWindowText(any_handle, buffer = "\00"* 1024, buffer.size)}}
238
+ # Improved with block to accept window handle as a single arg and return (rstripped) text string
239
+ spec{ use{ text = get_window_text(handle = 0)}}
240
+ # Unicode version of get_window_text (strings returned encoded as utf-8)
241
+ spec{ use{ GetWindowTextW(any_handle, buffer = "\00"* 1024, buffer.size)}}
242
+ spec{ use{ text = get_window_text_w(any_handle)}} # result encoded as utf-8
243
+
244
+ it 'returns nil if incorrect window handle given' do
245
+ get_window_text(not_a_handle).should == nil
246
+ get_window_text_w(not_a_handle).should == nil
247
+ end
248
+
249
+ it 'returns correct window text' do
250
+ test_app do |app|
251
+ get_window_text(app.handle).should == TEST_WIN_TITLE
252
+ get_window_text_w(app.handle).should == TEST_WIN_TITLE
253
+ end
254
+ end
255
+ end
256
+
257
+ describe '#get_class_name(w)' do
258
+ spec{ use{ GetClassName(any_handle, buffer = "\00"* 1024, buffer.size)}}
259
+ # Improved with block to accept window handle as a single arg and return class name string
260
+ spec{ use{ class_name = get_class_name(any_handle)}}
261
+ # Unicode version of get_class_name (strings returned encoded as utf-8)
262
+ spec{ use{ GetClassNameW(any_handle, buffer = "\00"* 1024, buffer.size)}}
263
+ spec{ use{ class_name = get_class_name_w(handle = 0)}} # result encoded as utf-8
264
+
265
+ it 'returns correct window class name' do
266
+ test_app do |app|
267
+ get_class_name(app.handle).should == TEST_WIN_CLASS
268
+ get_class_name_w(app.handle).should == TEST_WIN_CLASS
269
+ end
270
+ end
271
+ end
272
+
273
+ describe '#get_window_thread_process_id' do
274
+ spec{ use{ thread = GetWindowThreadProcessId(any_handle, process_buffer = [1].pack('L')) }}
275
+ spec{ use{ thread, process = get_window_thread_process_id(any_handle) }}
276
+ # Improved with block to accept window handle as a single arg and return a pair of [thread, process]
277
+
278
+ it 'returns a pair of nonzero Integer ids (thread and process) for valid window' do
279
+ thread, process = get_window_thread_process_id(any_handle)
280
+ thread.should be_a_kind_of Integer
281
+ thread.should be > 0
282
+ process.should be_a_kind_of Integer
283
+ process.should be > 0
284
+ end
285
+
286
+ it 'returns a pair of nils (thread and process) for invalid window' do
287
+ thread, process = get_window_thread_process_id(not_a_handle)
288
+ thread.should == nil
289
+ process.should == nil
290
+ end
291
+ end
292
+
293
+ describe '#get_window_rect' do
294
+ spec{ use{ success = GetWindowRect(any_handle, rectangle = [0, 0, 0, 0].pack('L*'))}}
295
+ spec{ use{ left, top, right, bottom = get_window_rect(any_handle)}}
296
+
297
+ it 'returns array of nils for invalid window' do
298
+ get_window_rect(not_a_handle).should == [nil, nil, nil, nil]
299
+ end
300
+
301
+ it 'returns window`s border rectangle' do
302
+ test_app do |app|
303
+ get_window_rect(app.handle).should == TEST_WIN_RECT
304
+ end
305
+ end
306
+ end
307
+
308
+ end
309
+ end
data/win.gemspec ADDED
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{win}
8
+ s.version = "0.0.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["arvicco"]
12
+ s.date = %q{2010-02-10}
13
+ s.description = %q{A collection of Windows functions predefined for you using FFI}
14
+ s.email = %q{arvitallian@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "features/step_definitions/win_steps.rb",
27
+ "features/support/env.rb",
28
+ "features/win.feature",
29
+ "lib/win/extensions.rb",
30
+ "lib/win/library.rb",
31
+ "lib/win/window.rb",
32
+ "spec/spec.opts",
33
+ "spec/spec_helper.rb",
34
+ "spec/test_apps/locknote/LockNote.exe",
35
+ "spec/win/extensions_spec.rb",
36
+ "spec/win/library_spec.rb",
37
+ "spec/win/window_spec.rb",
38
+ "win.gemspec"
39
+ ]
40
+ s.homepage = %q{http://github.com/arvicco/win}
41
+ s.rdoc_options = ["--charset=UTF-8"]
42
+ s.require_paths = ["lib"]
43
+ s.rubygems_version = %q{1.3.5}
44
+ s.summary = %q{A collection of Windows functions predefined for you using FFI}
45
+ s.test_files = [
46
+ "spec/spec_helper.rb",
47
+ "spec/win/extensions_spec.rb",
48
+ "spec/win/library_spec.rb",
49
+ "spec/win/window_spec.rb"
50
+ ]
51
+
52
+ if s.respond_to? :specification_version then
53
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
57
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
58
+ s.add_development_dependency(%q<cucumber>, [">= 0"])
59
+ else
60
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
61
+ s.add_dependency(%q<cucumber>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
65
+ s.add_dependency(%q<cucumber>, [">= 0"])
66
+ end
67
+ end
68
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: win
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - arvicco
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-08 00:00:00 +03:00
12
+ date: 2010-02-10 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -53,10 +53,14 @@ files:
53
53
  - features/win.feature
54
54
  - lib/win/extensions.rb
55
55
  - lib/win/library.rb
56
+ - lib/win/window.rb
56
57
  - spec/spec.opts
57
58
  - spec/spec_helper.rb
59
+ - spec/test_apps/locknote/LockNote.exe
58
60
  - spec/win/extensions_spec.rb
59
61
  - spec/win/library_spec.rb
62
+ - spec/win/window_spec.rb
63
+ - win.gemspec
60
64
  has_rdoc: true
61
65
  homepage: http://github.com/arvicco/win
62
66
  licenses: []
@@ -89,3 +93,4 @@ test_files:
89
93
  - spec/spec_helper.rb
90
94
  - spec/win/extensions_spec.rb
91
95
  - spec/win/library_spec.rb
96
+ - spec/win/window_spec.rb