Skip to content
This repository has been archived by the owner on Sep 4, 2018. It is now read-only.

Commit

Permalink
passing delay value into Swipe so it can be used to implement drag
Browse files Browse the repository at this point in the history
  • Loading branch information
cheezy committed May 20, 2010
1 parent 83d0915 commit b22a821
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/icuke/cucumber.rb
Expand Up @@ -83,14 +83,15 @@ def swipe(direction, options = {})
y = 480 / 2
x2 = x
y2 = y
hold_for = 0.015

if [:up, :down].include?(direction)
y2 = y + (y * modifier)
else
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)

Expand Down
11 changes: 6 additions & 5 deletions lib/icuke/simulate.rb
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions spec/simulate_spec.rb
Expand Up @@ -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
Expand All @@ -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

Expand Down
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -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

0 comments on commit b22a821

Please sign in to comment.