aboutsummaryrefslogtreecommitdiff
path: root/catapult/devil/devil/android/app_ui_test.py
blob: 3472985118531eecdb88ba9b949155f13fb9eaec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# Copyright 2015 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.

"""Unit tests for the app_ui module."""

import unittest
from xml.etree import ElementTree as element_tree

from devil import devil_env
from devil.android import app_ui
from devil.android import device_errors
from devil.utils import geometry

with devil_env.SysPath(devil_env.PYMOCK_PATH):
  import mock  # pylint: disable=import-error


MOCK_XML_LOADING = '''
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<hierarchy rotation="0">
  <node bounds="[0,50][1536,178]" content-desc="Loading"
      resource-id="com.example.app:id/spinner"/>
</hierarchy>
'''.strip()


MOCK_XML_LOADED = '''
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<hierarchy rotation="0">
  <node bounds="[0,50][1536,178]" content-desc=""
      resource-id="com.example.app:id/toolbar">
    <node bounds="[0,58][112,170]" content-desc="Open navigation drawer"/>
    <node bounds="[121,50][1536,178]"
        resource-id="com.example.app:id/actionbar_custom_view">
      <node bounds="[121,50][1424,178]"
          resource-id="com.example.app:id/actionbar_title" text="Primary"/>
      <node bounds="[1424,50][1536,178]" content-desc="Search"
          resource-id="com.example.app:id/actionbar_search_button"/>
    </node>
  </node>
  <node bounds="[0,178][576,1952]" resource-id="com.example.app:id/drawer">
    <node bounds="[0,178][144,1952]"
        resource-id="com.example.app:id/mini_drawer">
      <node bounds="[40,254][104,318]" resource-id="com.example.app:id/avatar"/>
      <node bounds="[16,354][128,466]" content-desc="Primary"
          resource-id="com.example.app:id/image_view"/>
      <node bounds="[16,466][128,578]" content-desc="Social"
          resource-id="com.example.app:id/image_view"/>
      <node bounds="[16,578][128,690]" content-desc="Promotions"
          resource-id="com.example.app:id/image_view"/>
    </node>
  </node>
</hierarchy>
'''.strip()


class UiAppTest(unittest.TestCase):

  def setUp(self):
    self.device = mock.Mock()
    self.device.pixel_density = 320  # Each dp pixel is 2 real pixels.
    self.app = app_ui.AppUi(self.device, package='com.example.app')
    self._setMockXmlScreenshots([MOCK_XML_LOADED])

  def _setMockXmlScreenshots(self, xml_docs):
    """Mock self.app._GetRootUiNode to load nodes from some test xml_docs.

    Each time the method is called it will return a UI node for each string
    given in |xml_docs|, or rise a time out error when the list is exhausted.
    """
    # pylint: disable=protected-access
    def get_mock_root_ui_node(value):
      if isinstance(value, Exception):
        raise value
      return app_ui._UiNode(
          self.device, element_tree.fromstring(value), self.app.package)

    xml_docs.append(device_errors.CommandTimeoutError('Timed out!'))

    self.app._GetRootUiNode = mock.Mock(
        side_effect=(get_mock_root_ui_node(doc) for doc in xml_docs))

  def assertNodeHasAttribs(self, node, attr):
    # pylint: disable=protected-access
    for key, value in attr.iteritems():
      self.assertEquals(node._GetAttribute(key), value)

  def assertTappedOnceAt(self, x, y):
    self.device.RunShellCommand.assert_called_once_with(
        ['input', 'tap', str(x), str(y)], check_return=True)

  def testFind_byText(self):
    node = self.app.GetUiNode(text='Primary')
    self.assertNodeHasAttribs(node, {
        'text': 'Primary',
        'content-desc': None,
        'resource-id': 'com.example.app:id/actionbar_title',
    })
    self.assertEquals(node.bounds, geometry.Rectangle([121, 50], [1424, 178]))

  def testFind_byContentDesc(self):
    node = self.app.GetUiNode(content_desc='Social')
    self.assertNodeHasAttribs(node, {
        'text': None,
        'content-desc': 'Social',
        'resource-id': 'com.example.app:id/image_view',
    })
    self.assertEquals(node.bounds, geometry.Rectangle([16, 466], [128, 578]))

  def testFind_byResourceId_autocompleted(self):
    node = self.app.GetUiNode(resource_id='image_view')
    self.assertNodeHasAttribs(node, {
        'content-desc': 'Primary',
        'resource-id': 'com.example.app:id/image_view',
    })

  def testFind_byResourceId_absolute(self):
    node = self.app.GetUiNode(resource_id='com.example.app:id/image_view')
    self.assertNodeHasAttribs(node, {
        'content-desc': 'Primary',
        'resource-id': 'com.example.app:id/image_view',
    })

  def testFind_byMultiple(self):
    node = self.app.GetUiNode(resource_id='image_view',
                              content_desc='Promotions')
    self.assertNodeHasAttribs(node, {
        'content-desc': 'Promotions',
        'resource-id': 'com.example.app:id/image_view',
    })
    self.assertEquals(node.bounds, geometry.Rectangle([16, 578], [128, 690]))

  def testFind_notFound(self):
    node = self.app.GetUiNode(resource_id='does_not_exist')
    self.assertIsNone(node)

  def testFind_noArgsGiven(self):
    # Same exception given by Python for a function call with not enough args.
    with self.assertRaises(TypeError):
      self.app.GetUiNode()

  def testGetChildren(self):
    node = self.app.GetUiNode(resource_id='mini_drawer')
    self.assertNodeHasAttribs(
        node[0], {'resource-id': 'com.example.app:id/avatar'})
    self.assertNodeHasAttribs(node[1], {'content-desc': 'Primary'})
    self.assertNodeHasAttribs(node[2], {'content-desc': 'Social'})
    self.assertNodeHasAttribs(node[3], {'content-desc': 'Promotions'})
    with self.assertRaises(IndexError):
      # pylint: disable=pointless-statement
      node[4]

  def testTap_center(self):
    node = self.app.GetUiNode(content_desc='Open navigation drawer')
    node.Tap()
    self.assertTappedOnceAt(56, 114)

  def testTap_topleft(self):
    node = self.app.GetUiNode(content_desc='Open navigation drawer')
    node.Tap(geometry.Point(0, 0))
    self.assertTappedOnceAt(0, 58)

  def testTap_withOffset(self):
    node = self.app.GetUiNode(content_desc='Open navigation drawer')
    node.Tap(geometry.Point(10, 20))
    self.assertTappedOnceAt(10, 78)

  def testTap_withOffsetInDp(self):
    node = self.app.GetUiNode(content_desc='Open navigation drawer')
    node.Tap(geometry.Point(10, 20), dp_units=True)
    self.assertTappedOnceAt(20, 98)

  def testTap_dpUnitsIgnored(self):
    node = self.app.GetUiNode(content_desc='Open navigation drawer')
    node.Tap(dp_units=True)
    self.assertTappedOnceAt(56, 114)  # Still taps at center.

  @mock.patch('time.sleep', mock.Mock())
  def testWaitForUiNode_found(self):
    self._setMockXmlScreenshots(
        [MOCK_XML_LOADING, MOCK_XML_LOADING, MOCK_XML_LOADED])
    node = self.app.WaitForUiNode(resource_id='actionbar_title')
    self.assertNodeHasAttribs(node, {'text': 'Primary'})

  @mock.patch('time.sleep', mock.Mock())
  def testWaitForUiNode_notFound(self):
    self._setMockXmlScreenshots(
        [MOCK_XML_LOADING, MOCK_XML_LOADING, MOCK_XML_LOADING])
    with self.assertRaises(device_errors.CommandTimeoutError):
      self.app.WaitForUiNode(resource_id='actionbar_title')