15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import json
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import os
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import tempfile
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class MockAdb(object):
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  """Simple and effective mock for Android adb.
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  This module is meant to be used in integration tests in conjunction with the
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  other script named 'adb' living in the same directory.
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  The purpose of this class is preparing a dictionary of mocked responses to adb
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  commands. The dictionary is stored into a json file and read by the mock 'adb'
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  script. The path of the json dict is held in the MOCK_ADB_CFG env var.
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  """
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def __init__(self):
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    self._responses = {}  # Maps expected_cmd (str) -> prepared_response (str).
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    self._cfg_file = None
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def Start(self):
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    (fd_num, self._cfg_file) = tempfile.mkstemp()
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    with os.fdopen(fd_num, 'w') as f:
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      json.dump(self._responses, f)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      f.close()
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    os.environ['MOCK_ADB_CFG'] = self._cfg_file
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def Stop(self):
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    assert(self._cfg_file), 'Stop called before Start.'
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    os.unlink(self._cfg_file)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def PrepareResponse(self, cmd, response):
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    self._responses[cmd] = response
37