/
form_main_spec.rb
executable file
·290 lines (270 loc) · 10.5 KB
/
form_main_spec.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
include Autogui::Input
describe "FormMain" do
before(:all) do
@debug = false
@verbose = true
@application = Quicknote.new
puts "FormMain before(:all)" if @debug
puts "application:\n#{@application.inspect}\n" if @debug && @verbose
puts "application.combined_text:\n #{@application.combined_text}\n" if @debug && @verbose
end
before(:each) do
@application = Quicknote.new unless @application.running?
@application.should be_running
@application.set_focus
puts "FormMain before(:each)" if @debug
end
after(:all) do
if @application.running?
@application.file_exit
# still running? force it to close
@application.close(:wait_for_close => true)
@application.should_not be_running
end
puts "FormMain after(:all)" if @debug
end
after(:each) do
if @application.running?
keystroke(VK_N) if @application.message_dialog_confirm(:timeout => 0) || @application.dialog_overwrite_confirm(:timeout => 0)
keystroke(VK_ESCAPE) if @application.error_dialog(:timeout => 0)
end
puts "FormMain after(:each)" if @debug
end
describe "after startup" do
it "should have the title 'QuickNote - untitled.txt'" do
@application.main_window.title.should == "QuickNote - untitled.txt"
end
it "should have no text" do
@application.edit_window.text.should == ''
end
end
describe "editing text" do
after(:each) do
@application.file_new(:save => false)
end
it "should add the '+' modified flag to the title" do
@application.main_window.title.should == "QuickNote - untitled.txt"
type_in("hello")
@application.main_window.title.should == "QuickNote - +untitled.txt"
end
it "should change the text" do
@application.edit_window.text.should == ''
type_in("hello")
@application.edit_window.text.should == 'hello'
end
end
describe "file open (VK_MENU, VK_F, VK_O)" do
before(:each) do
@filename = "input_file.txt"
@file_contents = write_file(@filename, "the quick brown fox")
@application.file_new(:save => false)
end
after(:each) do
if @application.message_dialog_confirm(:timeout => 0)
@application.message_dialog_confirm.set_focus
keystroke(VK_N)
end
if @application.file_open_dialog(:timeout => 0)
@application.file_open_dialog.set_focus
keystroke(VK_ESCAPE)
end
end
it "should prompt to save with modified text" do
type_in("foobar")
@application.main_window.title.should match(/\+/)
keystroke(VK_MENU, VK_F, VK_O)
@application.message_dialog_confirm.should_not be_nil
end
it "should not prompt to save with unmodified text" do
@application.main_window.title.should_not match(/\+/)
keystroke(VK_MENU, VK_F, VK_O)
@application.message_dialog_confirm(:timeout => 0).should be_nil
end
describe "succeeding" do
it "should add the filename to the title" do
@application.main_window.title.should == "QuickNote - untitled.txt"
@application.file_open(fullpath(@filename), :save => false)
@application.main_window.title.should == "QuickNote - #{fullpath(@filename)}"
end
it "should load the text" do
@application.file_open(fullpath(@filename), :save => false)
@application.edit_window.text.should == 'the quick brown fox'
end
end
describe "failing" do
it "should show an error dialog with message 'Cannot open file'" do
type_in("foobar")
@application.file_open(fullpath("a_bogus_filename.txt"), :save => false)
@application.error_dialog.should_not be_nil
@application.error_dialog.combined_text.should match(/Cannot open file/)
end
it "should keep existing text" do
type_in("foobar")
@application.file_open(fullpath("a_bogus_filename.txt"), :save => false)
@application.error_dialog.should_not be_nil
@application.edit_window.text.should == 'foobar'
end
end
end
describe "file new (VK_MENU, VK_F, VK_N)" do
it "should prompt to save modified text" do
type_in("hello")
@application.main_window.title.should match(/\+/)
keystroke(VK_MENU, VK_F, VK_N)
@application.message_dialog_confirm.should_not be_nil
end
it "should not prompt to save with unmodified text" do
@application.file_new(:save => false)
@application.main_window.title.should_not match(/\+/)
keystroke(VK_MENU, VK_F, VK_N)
@application.message_dialog_confirm(:timeout => 0).should be_nil
end
it "should add the filename 'untitled.txt' to the title" do
filename = "input_file.txt"
file_contents = write_file(filename, "the quick brown fox")
@application.file_open(filename, :save => false)
@application.main_window.title.downcase.should == "QuickNote - #{fullpath(filename)}".downcase
@application.file_new(:save => false)
@application.main_window.title.should == "QuickNote - untitled.txt"
end
it "should remove the '+' modified flag from the title" do
type_in("hello")
@application.main_window.title.should match(/\+/)
@application.file_new(:save => false)
@application.main_window.title.should_not match(/\+/)
end
it "should clear the existing text" do
type_in("hello")
@application.edit_window.text.should match(/hello/)
@application.file_new(:save => false)
@application.edit_window.text.should == ''
end
end
describe "file save (VK_MENU, VK_F, VK_S)" do
before(:each) do
@filename = "input_file.txt"
@file_contents = write_file(@filename, "original content")
@application.file_open(fullpath(@filename), :save => false)
@application.main_window.title.downcase.should == "QuickNote - #{fullpath(@filename)}".downcase
@application.edit_window.text.should == "original content"
@application.set_focus
end
it "should do nothing unless modified text" do
append_to_file(@filename, "sneak in extra content that shouldn't be here")
contents = get_file_contents(@filename)
contents.should match(/extra content/)
@application.file_save
contents.should match(/extra content/)
@application.edit_window.text.should_not match(/extra content/)
end
describe "succeeding" do
it "should remove the '+' modified flag from the title" do
type_in("anything")
@application.main_window.title.should == "QuickNote - +#{fullpath(@filename)}"
@application.file_save
@application.main_window.title.should == "QuickNote - #{fullpath(@filename)}"
end
it "should save the text" do
type_in("foobar")
@application.edit_window.text.should == "foobar" + "original content"
@application.file_save
get_file_contents(@filename).should == "foobar" + "original content"
end
end
describe "failing" do
before(:each) do
# set read-only to cause save failure
in_current_dir do
`attrib +R #{@filename}`
end
end
after(:each) do
# cleanup read-only file
in_current_dir do
FileUtils.rm(@filename)
end
end
it "should show an error dialog with message 'Cannot create file'" do
type_in("anything")
@application.file_save
@application.error_dialog.should_not be_nil
@application.error_dialog.combined_text.should match(/Cannot create file/)
end
it "should keep the '+' modified flag on the title" do
type_in("anything")
@application.file_save
@application.main_window.title.should == "QuickNote - +#{fullpath(@filename)}"
end
it "should keep existing text" do
type_in("anything")
@application.file_save
@application.edit_window.text.should == "anything" + "original content"
end
end
end
describe "file save as (VK_MENU, VK_F, VK_A)" do
before(:each) do
@filename = "original.txt"
@file_contents = write_file(@filename, "original content")
@saveas_filename = "newfile.txt"
write_file(@saveas_filename, "")
@application.file_open(fullpath(@filename), :save => false)
end
after(:each) do
keystroke(VK_N) if @application.dialog_overwrite_confirm(:timeout => 0)
keystroke(VK_ESCAPE) if @application.file_save_as_dialog(:timeout => 0)
end
it "should prompt for filename" do
keystroke(VK_MENU, VK_F, VK_A)
@application.file_save_as_dialog.should_not be_nil
end
it "should confirm file overwrites" do
type_in("anything")
keystroke(VK_MENU, VK_F, VK_A)
@application.clipboard.text = fullpath(@saveas_filename)
keystroke(VK_CONTROL, VK_V)
keystroke(VK_RETURN)
File.exists?(fullpath(@saveas_filename)).should be_true
@application.dialog_overwrite_confirm.should_not be_nil
end
it "should remove the '+' modified flag from the title" do
type_in("anything")
@application.main_window.title.should match(/\+/)
@application.file_save_as(fullpath(@saveas_filename), :overwrite => true)
@application.main_window.title.should_not match(/\+/)
end
it "should add the filename to the title" do
@application.file_save_as(fullpath(@saveas_filename), :overwrite => true)
@application.main_window.title.downcase.should == "QuickNote - #{fullpath(@saveas_filename)}".downcase
end
it "should save the text" do
get_file_contents(@saveas_filename).should == ''
@application.file_save_as(fullpath(@saveas_filename), :overwrite => true)
get_file_contents(@saveas_filename).should match(/original content/)
end
end
describe "file exit (VK_MENU, VK_F, VK_X)" do
it "should prompt and save with modified text" do
type_in("anything")
keystroke(VK_MENU, VK_F, VK_X)
@application.message_dialog_confirm.should_not be_nil
@application.main_window.is_window?.should == true
@application.should be_running
end
it "should not prompt to save with unmodified text" do
keystroke(VK_MENU, VK_F, VK_X)
@application.message_dialog_confirm(:timeout => 0).should be_nil
@application.main_window.is_window?.should == false
@application.should_not be_running
end
end
describe "hotkey (VK_CONTROL, VK_X)" do
it "should should exit the program" do
@application.main_window.is_window?.should == true
keystroke(VK_CONTROL, VK_X)
@application.main_window.is_window?.should == false
@application.should_not be_running
end
end
end