spec/uialertview_spec.rb in sugarcube-1.5.8 vs spec/uialertview_spec.rb in sugarcube-1.5.9

- old
+ new

@@ -1,6 +1,6 @@ -describe 'UIAlertView' do +describe UIAlertView do tests UIViewController # this is just needed so that a window is available it 'should have :show option (show: false)' do alert = UIAlertView.alert('test', show: false) proper_wait 0.6 @@ -24,17 +24,31 @@ it 'should assign the title' do alert = UIAlertView.alert('test title', show: false) alert.title.should == 'test title' end + it 'should assign the title by options' do + alert = UIAlertView.alert(title: 'test title', show: false) + alert.title.should == 'test title' + end + it 'should support three args' do alert = UIAlertView.alert('test title', 'test message', show: false) + alert.title.should == 'test title' + alert.message.should == 'test message' alert.visible?.should == false end - describe 'should assign the message' do + it 'should assign the title and message by options' do + alert = UIAlertView.alert(title: 'test title', message: 'test message', show: false) + alert.title.should == 'test title' + alert.message.should == 'test message' + alert.visible?.should == false + end + describe 'assigning a message' do + it 'should use the second arg' do alert = UIAlertView.alert('test title', 'test message', show: false) alert.message.should == 'test message' end @@ -132,32 +146,32 @@ @touched.should == 'cancel' @touched_index.should == 0 end - describe 'Should call the appropriate block when :cancel and :success handlers are used' do + describe 'when :cancel and :success handlers are used' do before do @touched = nil @alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], cancel: ->{ @touched = :cancel }, success: ->{ @touched = :success } ) end after do - @alert.dismissWithClickedButtonIndex(@alert.cancelButtonIndex, animated: false) + @alert.dismissWithClickedButtonIndex(@alert.cancelButtonIndex, animated: false) if @alert.visible? end - it 'should work for :cancel' do + it 'should call the :cancel block' do proper_wait 0.6 @alert.dismissWithClickedButtonIndex(@alert.cancelButtonIndex, animated: false) @touched.should == :cancel end - it 'should work for :success' do + it 'should call the :success block' do proper_wait 0.6 @alert.dismissWithClickedButtonIndex(@alert.firstOtherButtonIndex, animated: false) @touched.should == :success end @@ -170,60 +184,192 @@ @text = nil end it 'should work with :secure_text_input' do @called = false - alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: :secure_text_input) { |button, text| + alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: :secure_text_input) do |button, text| @called = true @text = text - } + end proper_wait 0.6 alert.textFieldAtIndex(0).text = 'test text' alert.dismissWithClickedButtonIndex(alert.firstOtherButtonIndex, animated: false) proper_wait 0.1 @called.should == true @text.should == 'test text' end + it 'should work with UIAlertViewStyleSecureTextInput' do + @called = false + alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: UIAlertViewStyleSecureTextInput) do |button, text| + @called = true + @text = text + end + proper_wait 0.6 + alert.textFieldAtIndex(0).text = 'test text' + alert.dismissWithClickedButtonIndex(alert.firstOtherButtonIndex, animated: false) + proper_wait 0.1 + + @called.should == true + @text.should == 'test text' + end + + it 'should work with :secure_text_input and pass the index' do + @called = false + alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: :secure_text_input) do |button, text, touched_index| + @called = true + @text = text + @touched_index = touched_index + end + proper_wait 0.6 + alert.textFieldAtIndex(0).text = 'test text' + alert.dismissWithClickedButtonIndex(alert.firstOtherButtonIndex, animated: false) + proper_wait 0.1 + + @called.should == true + @text.should == 'test text' + @touched_index.should == alert.firstOtherButtonIndex + end + it 'should work with :plain_text_input' do - alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: :plain_text_input) { |button, text| + alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: :plain_text_input) do |button, text| @text = text - } + end proper_wait 0.6 alert.textFieldAtIndex(0).text = 'test text' alert.dismissWithClickedButtonIndex(alert.cancelButtonIndex, animated: false) proper_wait 0.1 @text.should == 'test text' end + it 'should work with UIAlertViewStylePlainTextInput' do + alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: UIAlertViewStylePlainTextInput) do |button, text| + @text = text + end + proper_wait 0.6 + alert.textFieldAtIndex(0).text = 'test text' + alert.dismissWithClickedButtonIndex(alert.cancelButtonIndex, animated: false) + proper_wait 0.1 + + @text.should == 'test text' + end + + it 'should work with :plain_text_input and pass the index' do + alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: :plain_text_input) do |button, text, touched_index| + @text = text + @touched_index = touched_index + end + proper_wait 0.6 + alert.textFieldAtIndex(0).text = 'test text' + alert.dismissWithClickedButtonIndex(alert.cancelButtonIndex, animated: false) + proper_wait 0.1 + + @text.should == 'test text' + @touched_index.should == alert.cancelButtonIndex + end + it 'should work with :login_and_password_input' do - alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: :login_and_password_input) { |button, text1, text2| + alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: :login_and_password_input) do |button, text1, text2| @text = "#{text1} + #{text2}" - } + end proper_wait 0.6 alert.textFieldAtIndex(0).text = 'test text 1' alert.textFieldAtIndex(1).text = 'test text 2' alert.dismissWithClickedButtonIndex(alert.cancelButtonIndex, animated: false) proper_wait 0.1 @text.should == 'test text 1 + test text 2' end + it 'should work with UIAlertViewStyleLoginAndPasswordInput' do + alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: UIAlertViewStyleLoginAndPasswordInput) do |button, text1, text2| + @text = "#{text1} + #{text2}" + end + proper_wait 0.6 + alert.textFieldAtIndex(0).text = 'test text 1' + alert.textFieldAtIndex(1).text = 'test text 2' + alert.dismissWithClickedButtonIndex(alert.cancelButtonIndex, animated: false) + proper_wait 0.1 + + @text.should == 'test text 1 + test text 2' + end + it 'should work with :login_and_password_input and pass the index' do - alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: :login_and_password_input) { |button, text1, text2, index| + alert = UIAlertView.alert('test', buttons: ['cancel', 'ok'], style: :login_and_password_input) do |button, text1, text2, index| @text = "#{text1} + #{text2}" @touched_index = index - } + end proper_wait 0.6 alert.textFieldAtIndex(0).text = 'test text 1' alert.textFieldAtIndex(1).text = 'test text 2' alert.dismissWithClickedButtonIndex(alert.cancelButtonIndex, animated: false) proper_wait 0.1 @text.should == 'test text 1 + test text 2' @touched_index.should == alert.cancelButtonIndex + end + + end + + describe 'with :buttons defined as a hash' do + + before do + @touched = nil + @alert = UIAlertView.alert('test', + buttons: { + cancel: 'Cancel', + ok: 'Ok' + }) do |button| + @touched = button + end + end + + after do + @alert.dismissWithClickedButtonIndex(@alert.cancelButtonIndex, animated: false) if @alert.visible? + end + + it 'should work for :cancel' do + proper_wait 0.6 + @alert.dismissWithClickedButtonIndex(@alert.cancelButtonIndex, animated: false) + @touched.should == :cancel + end + + it 'should work for :ok' do + proper_wait 0.6 + @alert.dismissWithClickedButtonIndex(@alert.firstOtherButtonIndex, animated: false) + @touched.should == :ok + end + + end + + describe 'when :cancel and :success handlers are used and buttons as a hash' do + + before do + @touched = nil + @alert = UIAlertView.alert('test', buttons: {cancel: 'cancel', ok: 'ok'}, + cancel: ->{ @touched = :cancel }, + success: ->{ @touched = :success } + ) + end + + after do + @alert.dismissWithClickedButtonIndex(@alert.cancelButtonIndex, animated: false) if @alert.visible? + end + + it 'should call the :cancel block' do + proper_wait 0.6 + @alert.dismissWithClickedButtonIndex(@alert.cancelButtonIndex, animated: false) + + @touched.should == :cancel + end + + it 'should call the :success block' do + proper_wait 0.6 + @alert.dismissWithClickedButtonIndex(@alert.firstOtherButtonIndex, animated: false) + + @touched.should == :success end end end