15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#!/usr/bin/env python
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Copyright 2014 The Chromium Authors. All rights reserved.
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)# found in the LICENSE file.
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""Client side of the mock adb (i.e. the one called instead of the actual adb).
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)This file is meant to be put in front of the PATH during integration tests, in
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)order to route all the adb calls here and serve them using a pre-configured
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)dictionary (epxected commands -> planned responses).
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)mock_adb.py is the counterpart of this file, and is meant to be used in the
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)unittests for configuring the behavior (i.e. the dictionary) of this script.
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)"""
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import json
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import optparse
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import os
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)import sys
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)def main(argv):
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  # Load the dictionary of expected_cmd -> planned_response from the json file
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  # which mock_adb.py creates.
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  with open(os.environ['MOCK_ADB_CFG']) as f:
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    responses = json.load(f)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  # Swallow the irrelevant adb extra arguments (e.g., device id).
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  parser = optparse.OptionParser()
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  parser.add_option('-s')
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  options, args = parser.parse_args(argv[1:])
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  adb_args = ' '.join(args)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  response = ''
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  for (cmd, planned_response) in responses.iteritems():
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if adb_args.startswith(cmd):
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      response = planned_response
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  print response
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)if __name__ == '__main__':
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  main(sys.argv)