Home | History | Annotate | Download | only in android
      1 #!/usr/bin/env python
      2 # Copyright 2017 The Chromium Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 """
      6 Unit tests for the contents of flag_changer.py.
      7 The test will invoke real devices
      8 """
      9 
     10 import os
     11 import posixpath
     12 import sys
     13 import unittest
     14 
     15 if __name__ == '__main__':
     16   sys.path.append(
     17       os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', )))
     18 
     19 from devil.android import device_test_case
     20 from devil.android import device_utils
     21 from devil.android import flag_changer
     22 from devil.android.sdk import adb_wrapper
     23 
     24 
     25 _CMDLINE_FILE = 'dummy-command-line'
     26 
     27 
     28 class FlagChangerTest(device_test_case.DeviceTestCase):
     29 
     30   def setUp(self):
     31     super(FlagChangerTest, self).setUp()
     32     self.adb = adb_wrapper.AdbWrapper(self.serial)
     33     self.adb.WaitForDevice()
     34     self.device = device_utils.DeviceUtils(
     35         self.adb, default_timeout=10, default_retries=0)
     36     # pylint: disable=protected-access
     37     self.cmdline_path = posixpath.join(flag_changer._CMDLINE_DIR, _CMDLINE_FILE)
     38     self.cmdline_path_legacy = posixpath.join(
     39         flag_changer._CMDLINE_DIR_LEGACY, _CMDLINE_FILE)
     40 
     41   def tearDown(self):
     42     super(FlagChangerTest, self).tearDown()
     43     self.device.RemovePath(
     44         [self.cmdline_path, self.cmdline_path_legacy], force=True, as_root=True)
     45 
     46   def testFlagChanger_restoreFlags(self):
     47     if not self.device.HasRoot():
     48       self.skipTest('Test needs a rooted device')
     49 
     50     # Write some custom chrome command line flags.
     51     self.device.WriteFile(
     52         self.cmdline_path, 'chrome --some --old --flags')
     53 
     54     # Write some more flags on a command line file in the legacy location.
     55     self.device.WriteFile(
     56         self.cmdline_path_legacy, 'some --stray --flags', as_root=True)
     57     self.assertTrue(self.device.PathExists(self.cmdline_path_legacy))
     58 
     59     changer = flag_changer.FlagChanger(self.device, _CMDLINE_FILE)
     60 
     61     # Legacy command line file is removed, ensuring Chrome picks up the
     62     # right file.
     63     self.assertFalse(self.device.PathExists(self.cmdline_path_legacy))
     64 
     65     # Write some new files, and check they are set.
     66     new_flags = ['--my', '--new', '--flags=with special value']
     67     self.assertItemsEqual(
     68         changer.ReplaceFlags(new_flags),
     69         new_flags)
     70 
     71     # Restore and go back to the old flags.
     72     self.assertItemsEqual(
     73         changer.Restore(),
     74         ['--some', '--old', '--flags'])
     75 
     76   def testFlagChanger_removeFlags(self):
     77     self.device.RemovePath(self.cmdline_path, force=True)
     78     self.assertFalse(self.device.PathExists(self.cmdline_path))
     79 
     80     with flag_changer.CustomCommandLineFlags(
     81         self.device, _CMDLINE_FILE, ['--some', '--flags']):
     82       self.assertTrue(self.device.PathExists(self.cmdline_path))
     83 
     84     self.assertFalse(self.device.PathExists(self.cmdline_path))
     85 
     86 
     87 if __name__ == '__main__':
     88   unittest.main()
     89