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.
9 from devil
.android
import flag_changer
12 _CMDLINE_FILE
= 'chrome-command-line'
15 class _FakeDevice(object):
17 self
.build_type
= 'user'
24 def PathExists(self
, filepath
):
25 return filepath
in self
.file_system
27 def RemovePath(self
, path
, **_kwargs
):
28 self
.file_system
.pop(path
)
30 def WriteFile(self
, path
, contents
, **_kwargs
):
31 self
.file_system
[path
] = contents
33 def ReadFile(self
, path
, **_kwargs
):
34 return self
.file_system
[path
]
37 class FlagChangerTest(unittest
.TestCase
):
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
)
45 def testFlagChanger_removeLegacyCmdLine(self
):
46 self
.device
.WriteFile(self
.cmdline_path_legacy
, 'chrome --old --stuff')
47 self
.assertTrue(self
.device
.PathExists(self
.cmdline_path_legacy
))
49 changer
= flag_changer
.FlagChanger(self
.device
, 'chrome-command-line')
51 changer
._cmdline
_path
, # pylint: disable=protected-access
53 self
.assertFalse(self
.device
.PathExists(self
.cmdline_path_legacy
))
55 def testFlagChanger_mustBeFileName(self
):
56 with self
.assertRaises(ValueError):
57 flag_changer
.FlagChanger(self
.device
, '/data/local/chrome-command-line')
60 class ParseSerializeFlagsTest(unittest
.TestCase
):
61 def _testQuoteFlag(self
, flag
, expected_quoted_flag
):
62 # Start with an unquoted flag, check that it's quoted as expected.
63 # pylint: disable=protected-access
64 quoted_flag
= flag_changer
._QuoteFlag
(flag
)
65 self
.assertEqual(quoted_flag
, expected_quoted_flag
)
66 # Check that it survives a round-trip.
67 parsed_flags
= flag_changer
._ParseFlags
('_ %s' % quoted_flag
)
68 self
.assertEqual(len(parsed_flags
), 1)
69 self
.assertEqual(flag
, parsed_flags
[0])
71 def testQuoteFlag_simple(self
):
72 self
._testQuoteFlag
('--simple-flag', '--simple-flag')
74 def testQuoteFlag_withSimpleValue(self
):
75 self
._testQuoteFlag
('--key=value', '--key=value')
77 def testQuoteFlag_withQuotedValue1(self
):
78 self
._testQuoteFlag
('--key=valueA valueB', '--key="valueA valueB"')
80 def testQuoteFlag_withQuotedValue2(self
):
82 '--key=this "should" work', r
'--key="this \"should\" work"')
84 def testQuoteFlag_withQuotedValue3(self
):
86 "--key=this is 'fine' too", '''--key="this is 'fine' too"''')
88 def testQuoteFlag_withQuotedValue4(self
):
90 "--key='I really want to keep these quotes'",
91 '''--key="'I really want to keep these quotes'"''')
93 def testQuoteFlag_withQuotedValue5(self
):
95 "--this is a strange=flag", '"--this is a strange=flag"')
97 def testQuoteFlag_withEmptyValue(self
):
98 self
._testQuoteFlag
('--some-flag=', '--some-flag=')
100 def _testParseCmdLine(self
, command_line
, expected_flags
):
101 # Start with a command line, check that flags are parsed as expected.
102 # pylint: disable=protected-access
103 flags
= flag_changer
._ParseFlags
(command_line
)
104 self
.assertItemsEqual(flags
, expected_flags
)
106 # Check that flags survive a round-trip.
107 # Note: Although new_command_line and command_line may not match, they
108 # should describe the same set of flags.
109 new_command_line
= flag_changer
._SerializeFlags
(flags
)
110 new_flags
= flag_changer
._ParseFlags
(new_command_line
)
111 self
.assertItemsEqual(new_flags
, expected_flags
)
113 def testParseCmdLine_simple(self
):
114 self
._testParseCmdLine
(
115 'chrome --foo --bar="a b" --baz=true --fine="ok"',
116 ['--foo', '--bar=a b', '--baz=true', '--fine=ok'])
118 def testParseCmdLine_withFancyQuotes(self
):
119 self
._testParseCmdLine
(
120 r
'''_ --foo="this 'is' ok"
121 --bar='this \'is\' too
'
122 --baz="this \'is\' tricky"
124 ["--foo=this 'is' ok",
125 "--bar=this 'is' too",
126 r"--baz=this \'is\' tricky"])
128 def testParseCmdLine_withUnterminatedQuote(self):
129 self._testParseCmdLine(
130 '_
--foo
--bar
="I forgot something',
131 ['--foo', '--bar=I forgot something'])
134 if __name__ == '__main__':
135 unittest.main(verbosity=2)