• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6import posixpath
7import unittest
8
9from devil.android import flag_changer
10
11
12_CMDLINE_FILE = 'chrome-command-line'
13
14
15class _FakeDevice(object):
16  def __init__(self):
17    self.build_type = 'user'
18    self.has_root = True
19    self.file_system = {}
20
21  def HasRoot(self):
22    return self.has_root
23
24  def PathExists(self, filepath):
25    return filepath in self.file_system
26
27  def RemovePath(self, path, **_kwargs):
28    self.file_system.pop(path)
29
30  def WriteFile(self, path, contents, **_kwargs):
31    self.file_system[path] = contents
32
33  def ReadFile(self, path, **_kwargs):
34    return self.file_system[path]
35
36
37class FlagChangerTest(unittest.TestCase):
38  def setUp(self):
39    self.device = _FakeDevice()
40    # pylint: disable=protected-access
41    self.cmdline_path = posixpath.join(flag_changer._CMDLINE_DIR, _CMDLINE_FILE)
42    self.cmdline_path_legacy = posixpath.join(
43        flag_changer._CMDLINE_DIR_LEGACY, _CMDLINE_FILE)
44
45  def testFlagChanger_removeAlternateCmdLine(self):
46    self.device.WriteFile(self.cmdline_path_legacy, 'chrome --old --stuff')
47    self.assertTrue(self.device.PathExists(self.cmdline_path_legacy))
48
49    changer = flag_changer.FlagChanger(self.device, 'chrome-command-line')
50    self.assertEquals(
51        changer._cmdline_path,  # pylint: disable=protected-access
52        self.cmdline_path)
53    self.assertFalse(self.device.PathExists(self.cmdline_path_legacy))
54
55  def testFlagChanger_removeAlternateCmdLineLegacyPath(self):
56    self.device.WriteFile(self.cmdline_path, 'chrome --old --stuff')
57    self.assertTrue(self.device.PathExists(self.cmdline_path))
58
59    changer = flag_changer.FlagChanger(self.device, 'chrome-command-line',
60                                       use_legacy_path=True)
61    self.assertEquals(
62      changer._cmdline_path,  # pylint: disable=protected-access
63      self.cmdline_path_legacy)
64    self.assertFalse(self.device.PathExists(self.cmdline_path))
65
66  def testFlagChanger_mustBeFileName(self):
67    with self.assertRaises(ValueError):
68      flag_changer.FlagChanger(self.device, '/data/local/chrome-command-line')
69
70
71class ParseSerializeFlagsTest(unittest.TestCase):
72  def _testQuoteFlag(self, flag, expected_quoted_flag):
73    # Start with an unquoted flag, check that it's quoted as expected.
74    # pylint: disable=protected-access
75    quoted_flag = flag_changer._QuoteFlag(flag)
76    self.assertEqual(quoted_flag, expected_quoted_flag)
77    # Check that it survives a round-trip.
78    parsed_flags = flag_changer._ParseFlags('_ %s' % quoted_flag)
79    self.assertEqual(len(parsed_flags), 1)
80    self.assertEqual(flag, parsed_flags[0])
81
82  def testQuoteFlag_simple(self):
83    self._testQuoteFlag('--simple-flag', '--simple-flag')
84
85  def testQuoteFlag_withSimpleValue(self):
86    self._testQuoteFlag('--key=value', '--key=value')
87
88  def testQuoteFlag_withQuotedValue1(self):
89    self._testQuoteFlag('--key=valueA valueB', '--key="valueA valueB"')
90
91  def testQuoteFlag_withQuotedValue2(self):
92    self._testQuoteFlag(
93        '--key=this "should" work', r'--key="this \"should\" work"')
94
95  def testQuoteFlag_withQuotedValue3(self):
96    self._testQuoteFlag(
97        "--key=this is 'fine' too", '''--key="this is 'fine' too"''')
98
99  def testQuoteFlag_withQuotedValue4(self):
100    self._testQuoteFlag(
101        "--key='I really want to keep these quotes'",
102        '''--key="'I really want to keep these quotes'"''')
103
104  def testQuoteFlag_withQuotedValue5(self):
105    self._testQuoteFlag(
106        "--this is a strange=flag", '"--this is a strange=flag"')
107
108  def testQuoteFlag_withEmptyValue(self):
109    self._testQuoteFlag('--some-flag=', '--some-flag=')
110
111  def _testParseCmdLine(self, command_line, expected_flags):
112    # Start with a command line, check that flags are parsed as expected.
113    # pylint: disable=protected-access
114    flags = flag_changer._ParseFlags(command_line)
115    self.assertItemsEqual(flags, expected_flags)
116
117    # Check that flags survive a round-trip.
118    # Note: Although new_command_line and command_line may not match, they
119    # should describe the same set of flags.
120    new_command_line = flag_changer._SerializeFlags(flags)
121    new_flags = flag_changer._ParseFlags(new_command_line)
122    self.assertItemsEqual(new_flags, expected_flags)
123
124  def testParseCmdLine_simple(self):
125    self._testParseCmdLine(
126        'chrome --foo --bar="a b" --baz=true --fine="ok"',
127        ['--foo', '--bar=a b', '--baz=true', '--fine=ok'])
128
129  def testParseCmdLine_withFancyQuotes(self):
130    self._testParseCmdLine(
131        r'''_ --foo="this 'is' ok"
132              --bar='this \'is\' too'
133              --baz="this \'is\' tricky"
134        ''',
135        ["--foo=this 'is' ok",
136         "--bar=this 'is' too",
137         r"--baz=this \'is\' tricky"])
138
139  def testParseCmdLine_withUnterminatedQuote(self):
140    self._testParseCmdLine(
141        '_ --foo --bar="I forgot something',
142        ['--foo', '--bar=I forgot something'])
143
144
145if __name__ == '__main__':
146  unittest.main(verbosity=2)
147