# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest

from telemetry import benchmark
from telemetry.core import browser_options
from telemetry.core.platform import android_device
from telemetry.core.platform import android_platform_backend
from telemetry.unittest_util import system_stub


class AndroidDeviceTest(unittest.TestCase):
  def setUp(self):
    self._android_device_stub = system_stub.Override(
        android_device, ['adb_commands'])

  def testGetAllAttachedAndroidDevices(self):
    self._android_device_stub.adb_commands.attached_devices = [
        '01', '02']
    self.assertEquals(
        set(['01', '02']),
        set(device.device_id for device in
            android_device.AndroidDevice.GetAllConnectedDevices()
        ))

  def tearDown(self):
    self._android_device_stub.Restore()


class GetDeviceTest(unittest.TestCase):
  def setUp(self):
    self._android_device_stub = system_stub.Override(
        android_device, ['adb_commands', 'os', 'subprocess', 'logging'])
    self._apb_stub = system_stub.Override(
        android_platform_backend, ['adb_commands'])

  def tearDown(self):
    self._android_device_stub.Restore()
    self._apb_stub.Restore()

  def testNoAdbReturnsNone(self):
    finder_options = browser_options.BrowserFinderOptions()

    def NoAdb(*_, **__):
      raise OSError('not found')
    self._android_device_stub.subprocess.Popen = NoAdb

    self.assertEquals([], self._android_device_stub.logging.warnings)
    self.assertIsNone(android_device.GetDevice(finder_options))

  def testAdbNoDevicesReturnsNone(self):
    finder_options = browser_options.BrowserFinderOptions()
    self.assertEquals([], self._android_device_stub.logging.warnings)
    self.assertIsNone(android_device.GetDevice(finder_options))

  def testAdbPermissionsErrorReturnsNone(self):
    finder_options = browser_options.BrowserFinderOptions()
    self._android_device_stub.subprocess.Popen.communicate_result = (
        'List of devices attached\n????????????\tno permissions\n',
        '* daemon not running. starting it now on port 5037 *\n'
        '* daemon started successfully *\n')
    device = android_device.GetDevice(finder_options)
    self.assertEquals([
        'adb devices gave a permissions error. Consider running adb as root:',
        '  adb kill-server',
        '  sudo `which adb` devices\n\n'],
        self._android_device_stub.logging.warnings)
    self.assertIsNone(device)

  def testAdbTwoDevicesReturnsNone(self):
    finder_options = browser_options.BrowserFinderOptions()
    self._android_device_stub.adb_commands.attached_devices = [
        '015d14fec128220c', '015d14fec128220d']
    device = android_device.GetDevice(finder_options)
    self.assertEquals([
        'Multiple devices attached. Please specify one of the following:\n'
        '  --device=015d14fec128220c\n'
        '  --device=015d14fec128220d'],
        self._android_device_stub.logging.warnings)
    self.assertIsNone(device)

  def testAdbPickOneDeviceReturnsDeviceInstance(self):
    finder_options = browser_options.BrowserFinderOptions()
    finder_options.android_device = '555d14fecddddddd'  # pick one
    self._android_device_stub.adb_commands.attached_devices = [
        '015d14fec128220c', '555d14fecddddddd']
    device = android_device.GetDevice(finder_options)
    self.assertEquals([], self._android_device_stub.logging.warnings)
    self.assertEquals('555d14fecddddddd', device.device_id)

  def testAdbOneDeviceReturnsDeviceInstance(self):
    finder_options = browser_options.BrowserFinderOptions()
    self._android_device_stub.adb_commands.attached_devices = (
        ['015d14fec128220c'])
    device = android_device.GetDevice(finder_options)
    self.assertEquals([], self._android_device_stub.logging.warnings)
    self.assertEquals('015d14fec128220c', device.device_id)

[((16, 32, 17, 41), 'telemetry.unittest_util.system_stub.Override', 'system_stub.Override', ({(17, 8, 17, 22): 'android_device', (17, 24, 17, 40): "['adb_commands']"}, {}), "(android_device, ['adb_commands'])", False, 'from telemetry.unittest_util import system_stub\n'), ((34, 32, 35, 72), 'telemetry.unittest_util.system_stub.Override', 'system_stub.Override', ({(35, 8, 35, 22): 'android_device', (35, 24, 35, 71): "['adb_commands', 'os', 'subprocess', 'logging']"}, {}), "(android_device, ['adb_commands', 'os', 'subprocess',\n    'logging'])", False, 'from telemetry.unittest_util import system_stub\n'), ((36, 21, 37, 51), 'telemetry.unittest_util.system_stub.Override', 'system_stub.Override', ({(37, 8, 37, 32): 'android_platform_backend', (37, 34, 37, 50): "['adb_commands']"}, {}), "(android_platform_backend, ['adb_commands'])", False, 'from telemetry.unittest_util import system_stub\n'), ((44, 21, 44, 59), 'telemetry.core.browser_options.BrowserFinderOptions', 'browser_options.BrowserFinderOptions', ({}, {}), '()', False, 'from telemetry.core import browser_options\n'), ((54, 21, 54, 59), 'telemetry.core.browser_options.BrowserFinderOptions', 'browser_options.BrowserFinderOptions', ({}, {}), '()', False, 'from telemetry.core import browser_options\n'), ((59, 21, 59, 59), 'telemetry.core.browser_options.BrowserFinderOptions', 'browser_options.BrowserFinderOptions', ({}, {}), '()', False, 'from telemetry.core import browser_options\n'), ((64, 13, 64, 53), 'telemetry.core.platform.android_device.GetDevice', 'android_device.GetDevice', ({(64, 38, 64, 52): 'finder_options'}, {}), '(finder_options)', False, 'from telemetry.core.platform import android_device\n'), ((73, 21, 73, 59), 'telemetry.core.browser_options.BrowserFinderOptions', 'browser_options.BrowserFinderOptions', ({}, {}), '()', False, 'from telemetry.core import browser_options\n'), ((76, 13, 76, 53), 'telemetry.core.platform.android_device.GetDevice', 'android_device.GetDevice', ({(76, 38, 76, 52): 'finder_options'}, {}), '(finder_options)', False, 'from telemetry.core.platform import android_device\n'), ((85, 21, 85, 59), 'telemetry.core.browser_options.BrowserFinderOptions', 'browser_options.BrowserFinderOptions', ({}, {}), '()', False, 'from telemetry.core import browser_options\n'), ((89, 13, 89, 53), 'telemetry.core.platform.android_device.GetDevice', 'android_device.GetDevice', ({(89, 38, 89, 52): 'finder_options'}, {}), '(finder_options)', False, 'from telemetry.core.platform import android_device\n'), ((94, 21, 94, 59), 'telemetry.core.browser_options.BrowserFinderOptions', 'browser_options.BrowserFinderOptions', ({}, {}), '()', False, 'from telemetry.core import browser_options\n'), ((97, 13, 97, 53), 'telemetry.core.platform.android_device.GetDevice', 'android_device.GetDevice', ({(97, 38, 97, 52): 'finder_options'}, {}), '(finder_options)', False, 'from telemetry.core.platform import android_device\n'), ((51, 22, 51, 62), 'telemetry.core.platform.android_device.GetDevice', 'android_device.GetDevice', ({(51, 47, 51, 61): 'finder_options'}, {}), '(finder_options)', False, 'from telemetry.core.platform import android_device\n'), ((56, 22, 56, 62), 'telemetry.core.platform.android_device.GetDevice', 'android_device.GetDevice', ({(56, 47, 56, 61): 'finder_options'}, {}), '(finder_options)', False, 'from telemetry.core.platform import android_device\n'), ((25, 12, 25, 65), 'telemetry.core.platform.android_device.AndroidDevice.GetAllConnectedDevices', 'android_device.AndroidDevice.GetAllConnectedDevices', ({}, {}), '()', False, 'from telemetry.core.platform import android_device\n')]