OLD | NEW |
(Empty) | |
| 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 import posixpath |
| 7 import unittest |
| 8 |
| 9 from devil.android import flag_changer |
| 10 |
| 11 |
| 12 _CMDLINE_FILE = 'chrome-command-line' |
| 13 |
| 14 |
| 15 class _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 |
| 37 class 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_removeLegacyCmdLine(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 |
| 56 class ParseSerializeFlagsTest(unittest.TestCase): |
| 57 def _testQuoteFlag(self, flag, expected_quoted_flag): |
| 58 # Start with an unquoted flag, check that it's quoted as expected. |
| 59 # pylint: disable=protected-access |
| 60 quoted_flag = flag_changer._QuoteFlag(flag) |
| 61 self.assertEqual(quoted_flag, expected_quoted_flag) |
| 62 # Check that it survives a round-trip. |
| 63 parsed_flags = flag_changer._ParseFlags('_ %s' % quoted_flag) |
| 64 self.assertEqual(len(parsed_flags), 1) |
| 65 self.assertEqual(flag, parsed_flags[0]) |
| 66 |
| 67 def testQuoteFlag_simple(self): |
| 68 self._testQuoteFlag('--simple-flag', '--simple-flag') |
| 69 |
| 70 def testQuoteFlag_withSimpleValue(self): |
| 71 self._testQuoteFlag('--key=value', '--key=value') |
| 72 |
| 73 def testQuoteFlag_withQuotedValue1(self): |
| 74 self._testQuoteFlag('--key=valueA valueB', '--key="valueA valueB"') |
| 75 |
| 76 def testQuoteFlag_withQuotedValue2(self): |
| 77 self._testQuoteFlag( |
| 78 '--key=this "should" work', r'--key="this \"should\" work"') |
| 79 |
| 80 def testQuoteFlag_withQuotedValue3(self): |
| 81 self._testQuoteFlag( |
| 82 "--key=this is 'fine' too", '''--key="this is 'fine' too"''') |
| 83 |
| 84 def testQuoteFlag_withQuotedValue4(self): |
| 85 with self.assertRaises(AssertionError): |
| 86 # TODO(catapult:#3112) This test is broken in the current implementation; |
| 87 # flags that appear to be quoted are left as-is and, thus, do not |
| 88 # survive the round-trip. |
| 89 self._testQuoteFlag( |
| 90 "--key='I really want to keep these quotes'", |
| 91 '''--key="'I really want to keep these quotes'"''') |
| 92 |
| 93 def testQuoteFlag_withQuotedValue5(self): |
| 94 self._testQuoteFlag( |
| 95 "--this is a strange=flag", '"--this is a strange=flag"') |
| 96 |
| 97 def _testParseCmdLine(self, command_line, expected_flags): |
| 98 # Start with a command line, check that flags are parsed as expected. |
| 99 # pylint: disable=protected-access |
| 100 flags = flag_changer._ParseFlags(command_line) |
| 101 self.assertItemsEqual(flags, expected_flags) |
| 102 |
| 103 # Check that flags survive a round-trip. |
| 104 # Note: Although new_command_line and command_line may not match, they |
| 105 # should describe the same set of flags. |
| 106 new_command_line = flag_changer._SerializeFlags(flags) |
| 107 new_flags = flag_changer._ParseFlags(new_command_line) |
| 108 self.assertItemsEqual(new_flags, expected_flags) |
| 109 |
| 110 def testParseCmdLine_simple(self): |
| 111 self._testParseCmdLine( |
| 112 'chrome --foo --bar="a b" --baz=true --fine="ok"', |
| 113 ['--foo', '--bar=a b', '--baz=true', '--fine=ok']) |
| 114 |
| 115 def testParseCmdLine_withFancyQuotes(self): |
| 116 self._testParseCmdLine( |
| 117 r'''_ --foo="this 'is' ok" |
| 118 --bar='this \'is\' too' |
| 119 --baz="this \'is\' tricky" |
| 120 ''', |
| 121 ["--foo=this 'is' ok", |
| 122 "--bar=this 'is' too", |
| 123 r"--baz=this \'is\' tricky"]) |
| 124 |
| 125 def testParseCmdLine_withUnterminatedQuote(self): |
| 126 self._testParseCmdLine( |
| 127 '_ --foo --bar="I forgot something', |
| 128 ['--foo', '--bar=I forgot something']) |
| 129 |
| 130 |
| 131 if __name__ == '__main__': |
| 132 unittest.main(verbosity=2) |
OLD | NEW |