Skip to content

Commit

Permalink
Merge pull request #1204 from calabash/feature/fix-first-responder-ch…
Browse files Browse the repository at this point in the history
…eck-in-DeviceAgent

Automator::DeviceAgent: search for any first responder for return key type
  • Loading branch information
jmoody committed Oct 14, 2016
2 parents 9f973c7 + 21fdee3 commit 791e2cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
26 changes: 17 additions & 9 deletions calabash-cucumber/lib/calabash-cucumber/automator/device_agent.rb
Expand Up @@ -453,17 +453,25 @@ def mark_for_return_key_type(number)
# @!visibility private
def return_key_type_of_first_responder

['textField', 'textView'].each do |ui_class|
query = "#{ui_class} isFirstResponder:1"
raw = Calabash::Cucumber::Map.raw_map(query, :query, :returnKeyType)
results = raw["results"]
if !results.empty?
return results.first
end
query = "* isFirstResponder:1"
raw = Calabash::Cucumber::Map.raw_map(query, :query, :returnKeyType)
elements = raw["results"]
return nil if elements.count == 0

return_key_type = elements[0]

# first responder did not respond to :text selector
if return_key_type == "*****"
RunLoop.log_debug("First responder does not respond to :returnKeyType")
return nil
end

if return_key_type.nil?
RunLoop.log_debug("First responder has nil :returnKeyType")
return nil
end

RunLoop.log_debug("Cannot find keyboard first responder to ask for its returnKeyType")
nil
return_key_type
end

# @!visibility private
Expand Down
28 changes: 13 additions & 15 deletions calabash-cucumber/spec/lib/automator/device_agent_spec.rb
Expand Up @@ -622,8 +622,9 @@ def enter_text_without_keyboard_check(_); ; end
end

context "#return_key_type_of_first_responder" do
it "returns the returnKeyType of text field when it is the first responder" do
query = "textField isFirstResponder:1"
let(:query) { "* isFirstResponder:1" }

it "returns the returnKeyType of the first responder" do
expect(Calabash::Cucumber::Map).to(
receive(:raw_map).with(query, :query, :returnKeyType)
).and_return({"results" => [1]})
Expand All @@ -632,31 +633,28 @@ def enter_text_without_keyboard_check(_); ; end
expect(actual).to be == 1
end

it "returns the returnKeyType of text view when it is the first responder" do
query = "textField isFirstResponder:1"
it "returns nil if returnKeyType result is empty" do
expect(Calabash::Cucumber::Map).to(
receive(:raw_map).with(query, :query, :returnKeyType)
).and_return({"results" => []})

query = "textView isFirstResponder:1"
expect(Calabash::Cucumber::Map).to(
receive(:raw_map).with(query, :query, :returnKeyType)
).and_return({"results" => [2]})

actual = device_agent.send(:return_key_type_of_first_responder)
expect(actual).to be == 2
expect(actual).to be == nil
end

it "returns nil when no first responder can be found" do
query = "textField isFirstResponder:1"
it "returns nil if first responder does not respond to :returnKeyType" do
expect(Calabash::Cucumber::Map).to(
receive(:raw_map).with(query, :query, :returnKeyType)
).and_return({"results" => []})
).and_return({"results" => ["*****"]})

actual = device_agent.send(:return_key_type_of_first_responder)
expect(actual).to be == nil
end

query = "textView isFirstResponder:1"
it "returns nil if first responder :returnKeyType is nil" do
expect(Calabash::Cucumber::Map).to(
receive(:raw_map).with(query, :query, :returnKeyType)
).and_return({"results" => []})
).and_return({"results" => [nil]})

actual = device_agent.send(:return_key_type_of_first_responder)
expect(actual).to be == nil
Expand Down

0 comments on commit 791e2cf

Please sign in to comment.