diff --git a/lib/icuke/cucumber.rb b/lib/icuke/cucumber.rb index 0a56666..322cab1 100644 --- a/lib/icuke/cucumber.rb +++ b/lib/icuke/cucumber.rb @@ -83,6 +83,7 @@ def swipe(direction, options = {}) y = 480 / 2 x2 = x y2 = y + hold_for = 0.015 if [:up, :down].include?(direction) y2 = y + (y * modifier) @@ -90,7 +91,7 @@ def swipe(direction, options = {}) x2 = x + (x * modifier) end - @simulator.fire_event(Swipe.new(x, y, x2, y2, options)) + @simulator.fire_event(Swipe.new(x, y, x2, y2, hold_for, options)) sleep(1) diff --git a/lib/icuke/simulate.rb b/lib/icuke/simulate.rb index b037874..09c0133 100644 --- a/lib/icuke/simulate.rb +++ b/lib/icuke/simulate.rb @@ -93,23 +93,24 @@ def to_json(*a) end class Swipe - attr_accessor :x, :y, :x2, :y2, :options + attr_accessor :x, :y, :x2, :y2, :options, :hold_for - def initialize(x, y, x2, y2, options) + def initialize(x, y, x2, y2, hold_for, options) @options = options @x = x @y = y @x2 = x2 @y2 = y2 + @hold_for = hold_for end def to_json(*a) - events = [ICuke::Simulate::Events::Touch.new(:down, [[x, y]], options.merge(:hold_for => 0.015))] + events = [ICuke::Simulate::Events::Touch.new(:down, [[x, y]], options.merge(:hold_for => hold_for))] each_point([x, y], [x2, y2], 25) do |ix, iy| - events << ICuke::Simulate::Events::Touch.new(:moved, [[ix, iy]], :hold_for => 0.015) + events << ICuke::Simulate::Events::Touch.new(:moved, [[ix, iy]], :hold_for => hold_for) end - events << ICuke::Simulate::Events::Touch.new(:up, [[x2, y2]], :hold_for => 0.015) + events << ICuke::Simulate::Events::Touch.new(:up, [[x2, y2]], :hold_for => hold_for) events.to_json(*a) end diff --git a/spec/simulate_spec.rb b/spec/simulate_spec.rb index d4b6e31..ff91d49 100644 --- a/spec/simulate_spec.rb +++ b/spec/simulate_spec.rb @@ -41,7 +41,7 @@ describe ICuke::Simulate::Gestures::Swipe do before(:each) do - @swipe = ICuke::Simulate::Gestures::Swipe.new(40, 60, 20, 30, {}) + @swipe = ICuke::Simulate::Gestures::Swipe.new(40, 60, 20, 30, 0.015, {}) end it "should generate a down and up touch" do @@ -56,12 +56,17 @@ it "should generate multiple move touches when moving over distance" do mx1, my1 = calculate_move(40, 60, 120, 130, 1) - @swipe = ICuke::Simulate::Gestures::Swipe.new(40, 60, 120, 130, {}) + @swipe = ICuke::Simulate::Gestures::Swipe.new(40, 60, 120, 130, 0.015, {}) 4.times do |i| x, y = calculate_move(40, 60, 120, 130, i+1) @swipe.to_json.should include touch_output(2, x, y) end end + + it "should include the appropriate hold_for value in the output" do + times = timestamps(@swipe.to_json) + times[0].should == times[1] - 15000000 + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5d58a49..d1ffaa6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,3 +23,13 @@ def touch_output(type, x, y) type.to_s + '}' end + +def timestamps(json) + segments = json.split('"Time":') + segments.delete_at 0 + timestamps = [] + segments.each do |segment| + timestamps << segment.split(',')[0].to_i + end + timestamps +end