Vaclav Brozek | cdc7defb | 2018-03-20 09:54:35 | [diff] [blame] | 1 | #!/usr/bin/env python |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 2 | # Copyright 2014 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 os |
| 7 | import sys |
| 8 | import imp |
| 9 | import tempfile |
| 10 | import unittest |
| 11 | import PRESUBMIT |
| 12 | |
| 13 | sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname( |
| 14 | os.path.abspath(__file__)))))) |
gayane | e170266 | 2014-12-13 03:48:09 | [diff] [blame] | 15 | from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi |
| 16 | from PRESUBMIT_test_mocks import MockFile, MockChange |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 17 | |
| 18 | class HTMLActionAdditionTest(unittest.TestCase): |
| 19 | |
| 20 | def testActionXMLChanged(self): |
| 21 | mock_input_api = MockInputApi() |
gayane | e170266 | 2014-12-13 03:48:09 | [diff] [blame] | 22 | lines = ['<input id="testinput" pref="testpref"', |
| 23 | 'metric="validaction" type="checkbox" dialog-pref>'] |
| 24 | mock_input_api.files = [MockFile('path/valid.html', lines)] |
| 25 | mock_input_api.change = MockChange(['path/valid.html','actions.xml']) |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 26 | action_xml_path = self._createActionXMLFile() |
| 27 | self.assertEqual([], PRESUBMIT.CheckUserActionUpdate(mock_input_api, |
| 28 | MockOutputApi(), |
| 29 | action_xml_path)) |
| 30 | |
| 31 | def testValidChange_StartOfLine(self): |
| 32 | lines = ['<input id="testinput" pref="testpref"', |
| 33 | 'metric="validaction" type="checkbox" dialog-pref>'] |
| 34 | self.assertEqual([], self._testChange(lines)) |
| 35 | |
| 36 | def testValidChange_StartsWithSpace(self): |
| 37 | lines = ['<input id="testinput" pref="testpref"', |
| 38 | ' metric="validaction" type="checkbox" dialog-pref>'] |
| 39 | self.assertEqual([], self._testChange(lines)) |
| 40 | |
| 41 | def testValidChange_Radio(self): |
| 42 | lines = ['<input id="testinput" pref="testpref"', |
| 43 | ' metric="validaction" type="radio" dialog-pref value="true">'] |
| 44 | self.assertEqual([], self._testChange(lines)) |
| 45 | |
| 46 | def testValidChange_UsingDatatype(self): |
| 47 | lines = ['<input id="testinput" pref="testpref"', |
| 48 | ' metric="validaction" datatype="boolean" dialog-pref>'] |
| 49 | self.assertEqual([], self._testChange(lines)) |
| 50 | |
| 51 | def testValidChange_NotBoolean(self): |
| 52 | lines = ['<input id="testinput" pref="testpref"', |
| 53 | ' metric="notboolean_validaction" dialog-pref>'] |
| 54 | self.assertEqual([], self._testChange(lines)) |
| 55 | |
| 56 | def testInvalidChange(self): |
| 57 | lines = ['<input id="testinput" pref="testpref"', |
| 58 | 'metric="invalidaction" type="checkbox" dialog-pref>'] |
| 59 | warnings = self._testChange(lines) |
gayane | 940df07 | 2015-02-24 14:28:30 | [diff] [blame] | 60 | self.assertEqual(1, len(warnings), warnings) |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 61 | |
| 62 | def testInValidChange_Radio(self): |
| 63 | lines = ['<input id="testinput" pref="testpref"', |
| 64 | ' metric="validaction" type="radio" dialog-pref value="string">'] |
| 65 | warnings = self._testChange(lines) |
gayane | 940df07 | 2015-02-24 14:28:30 | [diff] [blame] | 66 | self.assertEqual(1, len(warnings), warnings) |
| 67 | |
| 68 | def testValidChange_MultilineType(self): |
| 69 | lines = ['<input id="testinput" pref="testpref"\n' |
| 70 | ' metric="validaction" type=\n' |
| 71 | ' "radio" dialog-pref value=\n' |
| 72 | ' "false">'] |
| 73 | warnings = self._testChange(lines) |
| 74 | self.assertEqual([], self._testChange(lines)) |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 75 | |
| 76 | def _testChange(self, lines): |
| 77 | mock_input_api = MockInputApi() |
| 78 | mock_input_api.files = [MockFile('path/test.html', lines)] |
| 79 | |
| 80 | action_xml_path = self._createActionXMLFile() |
| 81 | return PRESUBMIT.CheckUserActionUpdate(mock_input_api, |
| 82 | MockOutputApi(), |
| 83 | action_xml_path) |
| 84 | |
| 85 | def _createActionXMLFile(self): |
| 86 | content = ('<actions>' |
gayane | 860db5c3 | 2014-12-05 16:16:46 | [diff] [blame] | 87 | '<action name="validaction_Disable">' |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 88 | ' <owner>Please list the metric\'s owners.</owner>' |
| 89 | ' <description>Enter the description of this user action.</description>' |
| 90 | '</action>' |
gayane | 860db5c3 | 2014-12-05 16:16:46 | [diff] [blame] | 91 | '<action name="validaction_Enable">' |
gayane | 3dff8c2 | 2014-12-04 17:09:51 | [diff] [blame] | 92 | ' <owner>Please list the metric\'s owners. </owner>' |
| 93 | ' <description>Enter the description of this user action.</description>' |
| 94 | '</action>' |
| 95 | '<action name="notboolean_validaction">' |
| 96 | ' <owner>Please list the metric\'s owners.</owner>' |
| 97 | ' <description>Enter the description of this user action.</description>' |
| 98 | '</action>' |
| 99 | '</actions>') |
| 100 | sys_temp = tempfile.gettempdir() |
| 101 | action_xml_path = os.path.join(sys_temp, 'actions_test.xml') |
| 102 | if not os.path.exists(action_xml_path): |
| 103 | with open(action_xml_path, 'w+') as action_file: |
| 104 | action_file.write(content) |
| 105 | |
| 106 | return action_xml_path |
| 107 | |
| 108 | |
| 109 | if __name__ == '__main__': |
| 110 | unittest.main() |