• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7from telemetry import decorators
8from telemetry.internal.browser import browser_options
9from telemetry.internal.platform import android_device
10from telemetry.internal.platform import remote_platform_options
11from telemetry.testing import system_stub
12import mock
13
14from devil.android import device_utils
15from devil.android import device_blacklist
16
17
18class _BaseAndroidDeviceTest(unittest.TestCase):
19  def setUp(self):
20    def check_blacklist_arg(blacklist):
21      self.assertTrue(blacklist is None
22                      or isinstance(blacklist, device_blacklist.Blacklist))
23      return mock.DEFAULT
24
25    self._healthy_device_patcher = mock.patch(
26        'devil.android.device_utils.DeviceUtils.HealthyDevices')
27    self._healthy_device_mock = self._healthy_device_patcher.start()
28    self._healthy_device_mock.side_effect = check_blacklist_arg
29    self._android_device_stub = system_stub.Override(
30        android_device, ['subprocess', 'logging'])
31
32  def _GetMockDeviceUtils(self, device_serial, is_online=True):
33    device = device_utils.DeviceUtils(device_serial)
34    device.IsOnline = mock.MagicMock(return_value=is_online)
35    return device
36
37  def tearDown(self):
38    self._healthy_device_patcher.stop()
39    self._android_device_stub.Restore()
40
41
42class AndroidDeviceTest(_BaseAndroidDeviceTest):
43  @decorators.Enabled('android')
44  def testGetAllAttachedAndroidDevices(self):
45    self._healthy_device_mock.return_value = [
46        self._GetMockDeviceUtils('01'),
47        self._GetMockDeviceUtils('07', is_online=False),
48        self._GetMockDeviceUtils('02'),
49        self._GetMockDeviceUtils('03', is_online=False)]
50    self.assertEquals(
51        set(['01', '02']),
52        set(device.device_id for device in
53            android_device.AndroidDevice.GetAllConnectedDevices(None)))
54
55  @decorators.Enabled('android')
56  def testNoAdbReturnsNone(self):
57    finder_options = browser_options.BrowserFinderOptions()
58    with (
59        mock.patch('os.path.isabs', return_value=True)), (
60        mock.patch('os.path.exists', return_value=False)):
61      self.assertEquals([], self._android_device_stub.logging.warnings)
62      self.assertIsNone(android_device.GetDevice(finder_options))
63
64  @decorators.Enabled('android')
65  def testAdbNoDevicesReturnsNone(self):
66    finder_options = browser_options.BrowserFinderOptions()
67    with mock.patch('os.path.isabs', return_value=False):
68      self._healthy_device_mock.return_value = []
69      self.assertEquals([], self._android_device_stub.logging.warnings)
70      self.assertIsNone(android_device.GetDevice(finder_options))
71
72  @decorators.Enabled('android')
73  def testAdbTwoDevicesReturnsNone(self):
74    finder_options = browser_options.BrowserFinderOptions()
75    with mock.patch('os.path.isabs', return_value=False):
76      self._healthy_device_mock.return_value = [
77          self._GetMockDeviceUtils('015d14fec128220c'),
78          self._GetMockDeviceUtils('015d14fec128220d')]
79      device = android_device.GetDevice(finder_options)
80      self.assertEquals([
81          'Multiple devices attached. Please specify one of the following:\n'
82          '  --device=015d14fec128220c\n'
83          '  --device=015d14fec128220d'],
84          self._android_device_stub.logging.warnings)
85      self.assertIsNone(device)
86
87  @decorators.Enabled('android')
88  def testAdbPickOneDeviceReturnsDeviceInstance(self):
89    finder_options = browser_options.BrowserFinderOptions()
90    platform_options = remote_platform_options.AndroidPlatformOptions(
91        device='555d14fecddddddd')  # pick one
92    finder_options.remote_platform_options = platform_options
93    with mock.patch('os.path.isabs', return_value=False):
94      self._healthy_device_mock.return_value = [
95          self._GetMockDeviceUtils('015d14fec128220c'),
96          self._GetMockDeviceUtils('555d14fecddddddd')]
97      device = android_device.GetDevice(finder_options)
98      self.assertEquals([], self._android_device_stub.logging.warnings)
99      self.assertEquals('555d14fecddddddd', device.device_id)
100
101  @decorators.Enabled('android')
102  def testAdbOneDeviceReturnsDeviceInstance(self):
103    finder_options = browser_options.BrowserFinderOptions()
104    with mock.patch('os.path.isabs', return_value=False):
105      self._healthy_device_mock.return_value = [
106          self._GetMockDeviceUtils('015d14fec128220c')]
107      device = android_device.GetDevice(finder_options)
108      self.assertEquals([], self._android_device_stub.logging.warnings)
109      self.assertEquals('015d14fec128220c', device.device_id)
110
111
112class FindAllAvailableDevicesTest(_BaseAndroidDeviceTest):
113  @decorators.Enabled('android')
114  def testAdbNoDeviceReturnsEmptyList(self):
115    finder_options = browser_options.BrowserFinderOptions()
116    with mock.patch('os.path.isabs', return_value=False):
117      self._healthy_device_mock.return_value = []
118      devices = android_device.FindAllAvailableDevices(finder_options)
119      self.assertEquals([], self._android_device_stub.logging.warnings)
120      self.assertIsNotNone(devices)
121      self.assertEquals(len(devices), 0)
122
123  @decorators.Enabled('android')
124  def testAdbOneDeviceReturnsListWithOneDeviceInstance(self):
125    finder_options = browser_options.BrowserFinderOptions()
126    with mock.patch('os.path.isabs', return_value=False):
127      self._healthy_device_mock.return_value = [
128          self._GetMockDeviceUtils('015d14fec128220c')]
129      devices = android_device.FindAllAvailableDevices(finder_options)
130      self.assertEquals([], self._android_device_stub.logging.warnings)
131      self.assertIsNotNone(devices)
132      self.assertEquals(len(devices), 1)
133      self.assertEquals('015d14fec128220c', devices[0].device_id)
134
135  @decorators.Enabled('android')
136  def testAdbMultipleDevicesReturnsListWithAllDeviceInstances(self):
137    finder_options = browser_options.BrowserFinderOptions()
138    with mock.patch('os.path.isabs', return_value=False):
139      self._healthy_device_mock.return_value = [
140          self._GetMockDeviceUtils('015d14fec128220c'),
141          self._GetMockDeviceUtils('this0should0not0show', is_online=False),
142          self._GetMockDeviceUtils('015d14fec128220d'),
143          self._GetMockDeviceUtils('015d14fec128220e')]
144      devices = android_device.FindAllAvailableDevices(finder_options)
145      self.assertEquals([], self._android_device_stub.logging.warnings)
146      self.assertIsNotNone(devices)
147      self.assertEquals(len(devices), 3)
148      self.assertEquals(devices[0].guid, '015d14fec128220c')
149      self.assertEquals(devices[1].guid, '015d14fec128220d')
150      self.assertEquals(devices[2].guid, '015d14fec128220e')
151