Class: Calabash::Android::Gestures::Gesture

Inherits:
Object
  • Object
show all
Defined in:
lib/calabash-android/gestures.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(touches = [], query_string = nil) ⇒ Gesture

Returns a new instance of Gesture.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/calabash-android/gestures.rb', line 79

def initialize(touches = [], query_string = nil)
  unless touches.is_a?(Array)
    touches = [touches]
  end

  @touches = []

  touches.each do |touch|
    @touches << Touch.new(touch)
  end

  @query_string = query_string
end

Instance Attribute Details

#touchesObject (readonly)

Returns the value of attribute touches.



77
78
79
# File 'lib/calabash-android/gestures.rb', line 77

def touches
  @touches
end

Class Method Details

.double_tap(opt = {}) ⇒ Object



169
170
171
# File 'lib/calabash-android/gestures.rb', line 169

def self.double_tap(opt={})
  self.tap(opt).merge(self.tap({wait: 0.1}.merge(opt)))
end

.generate_pinch_out(from_arr, to_arr, opt = {}) ⇒ Object



212
213
214
# File 'lib/calabash-android/gestures.rb', line 212

def self.generate_pinch_out(from_arr, to_arr, opt={})
  self.generate_swipe(from_arr[0], to_arr[0], opt) + self.generate_swipe(from_arr[1], to_arr[1], opt)
end

.generate_swipe(from_hash, to_hash, opt = {}) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/calabash-android/gestures.rb', line 173

def self.generate_swipe(from_hash, to_hash, opt={})
  from_params = from_hash.merge(opt).merge(opt[:from] || {})
  to_params = {time: 0}.merge(to_hash).merge(opt[:to] || {})

  if opt[:flick]
    to_params.merge!(wait: 0)
  else
    to_params = {wait: 0.2}.merge(to_params)
  end

  self.tap({release: false}.merge(from_params)).merge(self.tap(to_params))
end

.generate_tap(touch_hash) ⇒ Object



153
154
155
# File 'lib/calabash-android/gestures.rb', line 153

def self.generate_tap(touch_hash)
  MultiTouchGesture.new(Gesture.new(Touch.new(touch_hash)))
end

.pinch(direction, opt = {}) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/calabash-android/gestures.rb', line 216

def self.pinch(direction, opt={})
  opt[:from] ||= []
  opt[:from][0] = (opt[:from][0] || {}).merge(opt)
  opt[:from][1] = (opt[:from][1] || {}).merge(opt)
  opt[:to] ||= []
  opt[:to][0] ||= {}
  opt[:to][1] ||= {}

  from = [{x: 40, y: 40}.merge(opt[:from][0]), {x: 60, y: 60}.merge(opt[:from][1])]
  to = [{x: 10, y: 10}.merge(opt[:to][0]), {x: 90, y: 90}.merge(opt[:to][1])]

  case direction
    when :out

    when :in
      from,to = to,from
    else
      raise "Cannot pinch #{direction}"
  end

  generate_pinch_out(from, to)
end

.swipe(direction, opt = {}) ⇒ Object



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
# File 'lib/calabash-android/gestures.rb', line 186

def self.swipe(direction, opt={})
  from = {x: 50, y: 50}
  to = {x: 50, y: 50}

  case direction
    when :left
      from[:x] = 90
      to[:x] = 10
    when :right
      from[:x] = 10
      to[:x] = 90
    when :up
      from[:y] = 90
      to[:y] = 10
    when :down
      from[:y] = 10
      to[:y] = 90
    else
      raise "Cannot swipe in #{direction}"
  end

  opt[:time] ||= 0.3

  generate_swipe(from, to, opt)
end

.tap(opt = {}) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/calabash-android/gestures.rb', line 157

def self.tap(opt={})
  touch = opt[:touch] || {}
  touch[:x] ||= (opt[:x] || 50)
  touch[:y] ||= (opt[:y] || 50)
  touch[:time] ||= (opt[:time] || 0.2)
  touch[:release] = touch[:release].nil? ? (opt[:release].nil? ? true : opt[:release]) : touch[:release]
  touch[:wait] ||= (opt[:wait] || 0)
  touch[:offset] ||= opt[:offset]

  generate_tap(touch)
end

.with_parameters(multi_touch_gesture, params = {}) ⇒ Object



146
147
148
149
150
151
# File 'lib/calabash-android/gestures.rb', line 146

def self.with_parameters(multi_touch_gesture, params={})
  multi_touch_gesture.query_string = params[:query_string] if params[:query_string]
  multi_touch_gesture.timeout = params[:timeout] if params[:timeout]

  multi_touch_gesture
end

Instance Method Details

#+(gesture) ⇒ Object



110
111
112
# File 'lib/calabash-android/gestures.rb', line 110

def +(gesture)
  Gesture.new(@touches + gesture.touches, @query_string)
end

#<<(touch) ⇒ Object



119
120
121
# File 'lib/calabash-android/gestures.rb', line 119

def <<(touch)
  @touches << touch
end

#add_touch(touch) ⇒ Object



114
115
116
117
# File 'lib/calabash-android/gestures.rb', line 114

def add_touch(touch)
  touches = @touches
  Gesture.new(touches << touch, @query_string)
end

#from(touch) ⇒ Object



93
94
95
# File 'lib/calabash-android/gestures.rb', line 93

def from(touch)
  to(touch)
end

#max_execution_timeObject



142
143
144
# File 'lib/calabash-android/gestures.rb', line 142

def max_execution_time
  (@touches.map {|touch| touch.wait + touch.time}).reduce(:+)
end

#offset=(offset) ⇒ Object



138
139
140
# File 'lib/calabash-android/gestures.rb', line 138

def offset=(offset)
  @touches.each {|touch| touch.offset=offset}
end

#query_string=(query_string) ⇒ Object



130
131
132
# File 'lib/calabash-android/gestures.rb', line 130

def query_string=(query_string)
  @query_string = query_string
end

#reset_query_stringObject



134
135
136
# File 'lib/calabash-android/gestures.rb', line 134

def reset_query_string
  touches.each {|touch| touch.query_string=nil}
end

#to(touch) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/calabash-android/gestures.rb', line 97

def to(touch)
  if touch.is_a?(Hash)
    touch = Touch.new(touch)
  end

  unless (last_touch = @touches.last).nil?
    touch.x ||= last_touch.x
    touch.y ||= last_touch.y
  end

  Gesture.new(@touches << touch, @query_string)
end

#to_json(*object) ⇒ Object



123
124
125
126
127
128
# File 'lib/calabash-android/gestures.rb', line 123

def to_json(*object)
  {
      query_string: @query_string,
      touches: @touches
  }.to_json(*object)
end