summaryrefslogtreecommitdiff
path: root/emu_test/test_console/testcase_orientation.py
blob: a694627366d2ee19da093a9aa5aa662fd4ea4d87 (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
"""Tests for rotate-related commands."""

import inspect
import json
import os
import time
import unittest

import requests
import testcase_base
from utils import util

TESTCASE_CALL_DIR = os.path.dirname(os.path.realpath(__file__))
SERVLET_ORIENTATION = 'http://localhost:8080/OrientationManagerService'

MAX_TRIES = 3

ROTATION_0 = 0
ROTATION_270 = 3
ROTATION_180 = 2
ROTATION_90 = 1

ORIENTATION_PORTRAIT = 1
ORIENTATION_LANDSCAPE = 2


class OrientationTest(testcase_base.BaseConsoleTest):
  """This class aims to test rotate-related emulator console commands."""

  def __init__(self, method_name=None, avd=None):
    if method_name:
      super(OrientationTest, self).__init__(method_name)
    else:
      super(OrientationTest, self).__init__()
    self.avd = avd

  @classmethod
  def setUpClass(cls):
    util.run_script_run_adb_shell(TESTCASE_CALL_DIR)

  def _process_request_orientation_service(self, payload):
    """Processes post request to orientation service.

    Sends post request to sms service, gets the orientation and rotation.

    Args:
        payload: The payload for sending POST request to sms server.

    Returns:
        orientation: The orientation of the screen.
        rotation: The rotation of the screen.
    """
    r = requests.post(SERVLET_ORIENTATION, data=json.dumps(payload))

    if r.raise_for_status():
      error_msg = ('Servlet Error: Post request to %s failed' %
                   SERVLET_ORIENTATION)
      print error_msg
      return False, error_msg

    r_json = r.json()

    if r_json['isFail']:
      error_msg = ('Servlet Error: Failure occurred in servlet side => %s'
                   % SERVLET_ORIENTATION)
      print error_msg
      return False, error_msg

    return int(r_json['screenOrientation']), int(r_json['screenRotation'])

  def _poll_orientation_rotation_and_verify(self, expected_orientation,
                                            expected_rotation):
    """Polls orientation/rotation information from emulator and verifies them.

    Args:
      expected_orientation: Expected orientation to get.
      expected_rotation: Expected rotation to get.
    """
    got_expected = False
    for i in range(MAX_TRIES):
      got_orientation, got_rotation = self._process_request_orientation_service(
        {})
      print ('got_orientation = %s, expected_orientation = %s' %
             (got_orientation, expected_orientation))
      print ('got_rotation = %s, expected_rotation = %s' %
             (got_rotation, expected_rotation))
      if (got_orientation == expected_orientation and
            got_rotation == expected_rotation):
        got_expected = True
        break
      else:
        time.sleep(util.TRIAL_WAIT_TIMEOUT_S)

    self.assertTrue(got_expected,
                    'Max tries reached, failed to get expected values.')

  def _execute_rotate_command_and_verify(self, expected_orientation,
                                         expected_rotation):
    print '\n-------------------------'
    is_command_successful, output = util.execute_console_command(
      self.telnet, util.CMD_ROTATE, '')
    self.assert_cmd_successful(
      is_command_successful, 'Failed to properly get orientation/rotation.',
      False, '', '', output)
    self._poll_orientation_rotation_and_verify(expected_orientation,
                                               expected_rotation)

  def test_orientation(self):
    """Test command for: rotate

    Test Rail ID: C14595295
    Test steps:
      1. Launch an emulator avd
      2. Open any app, say Calculator, or maps
      3. From command prompt, run: telnet localhost <port>
      4. Copy the auth_token value from ~/.emulator_console_auth_token
      5. Run: auth auth_token
      6. Run: rotate, and verify
      7. Run: rotate, and verify
      8. Run: rotate, and verify
      9. Run: rotate, and verify
    Verify:
      Check to orientation and rotation of the launched app.
    """
    print 'Running test: %s' % (inspect.stack()[0][3])
    self._poll_orientation_rotation_and_verify(ORIENTATION_PORTRAIT, ROTATION_0)
    self._execute_rotate_command_and_verify(ORIENTATION_LANDSCAPE, ROTATION_270)
    self._execute_rotate_command_and_verify(ORIENTATION_PORTRAIT, ROTATION_0)
    self._execute_rotate_command_and_verify(ORIENTATION_LANDSCAPE, ROTATION_90)
    self._execute_rotate_command_and_verify(ORIENTATION_PORTRAIT, ROTATION_0)


if __name__ == '__main__':
  print '======= rotate Test ======='
  unittest.main()