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.



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

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.



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

def touches
  @touches
end

Class Method Details

.double_tap(opt = {}) ⇒ Object



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

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



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

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



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

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



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

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

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



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

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/calabash-android/gestures.rb', line 185

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



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

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



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

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



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

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

#<<(touch) ⇒ Object



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

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

#add_touch(touch) ⇒ Object



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

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

#from(touch) ⇒ Object



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

def from(touch)
  to(touch)
end

#max_execution_timeObject



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

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

#offset=(offset) ⇒ Object



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

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

#query_string=(query_string) ⇒ Object



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

def query_string=(query_string)
  @query_string = query_string
end

#reset_query_stringObject



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

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

#to(touch) ⇒ Object



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

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



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

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