Class: Mobo::Android::Emulator

Inherits:
Object
  • Object
show all
Defined in:
lib/mobo/android.rb

Constant Summary collapse

PORT_START =
5600
BOOT_SLEEP =
5
BOOT_WAIT_ATTEMPTS =
30
UNLOCK_KEY_EVENT =
82
BACK_KEY_EVENT =
4

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device) ⇒ Emulator



131
132
133
# File 'lib/mobo/android.rb', line 131

def initialize(device)
  @device = device
end

Class Attribute Details

.last_portObject

Returns the value of attribute last_port.



128
129
130
# File 'lib/mobo/android.rb', line 128

def last_port
  @last_port
end

Instance Method Details

#booted?Boolean



210
211
212
213
214
215
216
217
# File 'lib/mobo/android.rb', line 210

def booted?
  bootanim = Mobo.cmd_out("adb -s #{@device["id"]} shell 'getprop init.svc.bootanim'")
  if bootanim.match(/stopped/)
    return true
  else
    return false
  end
end

#create_cacheObject



153
154
155
# File 'lib/mobo/android.rb', line 153

def create_cache
  @device["cache"] = @device["name"] + "_cache.img"
end

#create_sdcardObject



144
145
146
147
# File 'lib/mobo/android.rb', line 144

def create_sdcard
  @device["sdcard"] = "#{@device["name"]}_sdcard.img"
  Mobo.cmd("mksdcard -l e #{@device["sdcard_size"]} #{@device["sdcard"]}")
end

#destroyObject



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/mobo/android.rb', line 232

def destroy
  (0..10).each do 
    if booted?
      Mobo.cmd("adb -s #{@device["id"]} emu kill")
      sleep 1
    else
      Mobo.log.info("#{@device["name"]} (#{@device["id"]}) is shutdown")
      destroy_cache
      destroy_sdcard
      break
    end
  end
end

#destroy_cacheObject



157
158
159
# File 'lib/mobo/android.rb', line 157

def destroy_cache
  Mobo.cmd("rm -f #{@device["cache"]}")
end

#destroy_sdcardObject



149
150
151
# File 'lib/mobo/android.rb', line 149

def destroy_sdcard
  Mobo.cmd("rm -f #{@device["sdcard"]}")
end

#find_open_portObject



135
136
137
138
139
140
141
142
# File 'lib/mobo/android.rb', line 135

def find_open_port
  port = (Emulator.last_port || PORT_START) + 2
  until not Mobo.cmd("netstat -an | grep 127.0.0.1.#{port}")
    port += 2
  end
  Emulator.last_port = port
  @device["port"] = port
end

#running?Boolean



201
202
203
204
205
206
207
208
# File 'lib/mobo/android.rb', line 201

def running?
  begin
    Process.getpgid( @device["pid"] )
    true
  rescue Errno::ESRCH
    false
  end
end

#startObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/mobo/android.rb', line 161

def start
  find_open_port
  create_sdcard
  create_cache

  cmd ="emulator @#{@device["name"]} \
    -port #{@device["port"]} \
    -sdcard #{@device["sdcard"]} \
    -cache #{@device["cache"]}"
  pid = Process.fork {
    Mobo.cmd(cmd)
    Mobo.log.info("Emulator #{@device["name"]} has stopped")
  }

  @device["pid"] = pid
  @device["id"] = "emulator-#{@device["port"]}"
  return @device
end

#statusObject



228
229
230
# File 'lib/mobo/android.rb', line 228

def status
  Mobo.log.info("#{@device["name"]} (#{@device["id"]}) is running: #{booted?}")
end

#unlockObject



219
220
221
222
223
224
225
226
# File 'lib/mobo/android.rb', line 219

def unlock
  # unlock the emulator, so it can be used for UI testing
  # then, pressing back because sometimes a menu appears
  [UNLOCK_KEY_EVENT, BACK_KEY_EVENT].each do |key|
    sleep(BOOT_SLEEP)
    Mobo.cmd("adb -s #{@device["id"]} shell input keyevent #{key}")
  end
end

#unlock_when_bootedObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/mobo/android.rb', line 180

def unlock_when_booted
  Process.fork do
    BOOT_WAIT_ATTEMPTS.times do |attempt|
      if booted?
        Mobo.log.info("#{@device["id"]} has booted")
        break
      elsif !running?
        Mobo.log.error("Emulator #{@device["name"]} has stopped")
        break
      else
        Mobo.log.debug("waiting for #{@device["id"]} to boot...")
        sleep(BOOT_SLEEP)
      end
    end

    if running? && booted?
      unlock
    end
  end
end