summaryrefslogtreecommitdiff
path: root/systrace/catapult/devil/devil/android/flag_changer_devicetest.py
blob: f5d19d6007a138c7d6b7bedf6ddd04319a6b2ff4 (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
#!/usr/bin/env python
# Copyright 2017 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 contents of flag_changer.py.
The test will invoke real devices
"""

import os
import posixpath
import sys
import unittest

if __name__ == '__main__':
  sys.path.append(
      os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', )))

from devil.android import device_test_case
from devil.android import device_utils
from devil.android import flag_changer
from devil.android.sdk import adb_wrapper


_CMDLINE_FILE = 'dummy-command-line'


class FlagChangerTest(device_test_case.DeviceTestCase):

  def setUp(self):
    super(FlagChangerTest, self).setUp()
    self.adb = adb_wrapper.AdbWrapper(self.serial)
    self.adb.WaitForDevice()
    self.device = device_utils.DeviceUtils(
        self.adb, default_timeout=10, default_retries=0)
    # pylint: disable=protected-access
    self.cmdline_path = posixpath.join(flag_changer._CMDLINE_DIR, _CMDLINE_FILE)
    self.cmdline_path_legacy = posixpath.join(
        flag_changer._CMDLINE_DIR_LEGACY, _CMDLINE_FILE)

  def tearDown(self):
    self.device.RemovePath(
        [self.cmdline_path, self.cmdline_path_legacy], force=True, as_root=True)

  def testFlagChanger(self):
    if not self.device.HasRoot():
      self.skipTest('Test needs a rooted device')

    # Write some custom chrome command line flags.
    self.device.WriteFile(
        self.cmdline_path, 'chrome --some --old --flags')

    # Write some more flags on a command line file in the legacy location.
    self.device.WriteFile(
        self.cmdline_path_legacy, 'some --stray --flags', as_root=True)
    self.assertTrue(self.device.PathExists(self.cmdline_path_legacy))

    changer = flag_changer.FlagChanger(self.device, _CMDLINE_FILE)

    # Legacy command line file is removed, ensuring Chrome picks up the
    # right file.
    self.assertFalse(self.device.PathExists(self.cmdline_path_legacy))

    # Write some new files, and check they are set.
    new_flags = ['--my', '--new', '--flags=with special value']
    self.assertItemsEqual(
        changer.ReplaceFlags(new_flags),
        new_flags)

    # Restore and go back to the old flags.
    self.assertItemsEqual(
        changer.Restore(),
        ['--some', '--old', '--flags'])


if __name__ == '__main__':
  unittest.main()