OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import os |
| 6 import sys |
| 7 import imp |
| 8 import tempfile |
| 9 import unittest |
| 10 import PRESUBMIT |
| 11 |
| 12 sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname( |
| 13 os.path.abspath(__file__)))))) |
| 14 from PRESUBMIT_test_mocks import MockFile, MockInputApi, MockOutputApi |
| 15 |
| 16 class HTMLActionAdditionTest(unittest.TestCase): |
| 17 |
| 18 def testActionXMLChanged(self): |
| 19 mock_input_api = MockInputApi() |
| 20 mock_input_api.files = [ |
| 21 MockFile('path/valid.html', ''), |
| 22 MockFile('actions.xml', '') ] |
| 23 |
| 24 action_xml_path = self._createActionXMLFile() |
| 25 self.assertEqual([], PRESUBMIT.CheckUserActionUpdate(mock_input_api, |
| 26 MockOutputApi(), |
| 27 action_xml_path)) |
| 28 |
| 29 def testValidChange_StartOfLine(self): |
| 30 lines = ['<input id="testinput" pref="testpref"', |
| 31 'metric="validaction" type="checkbox" dialog-pref>'] |
| 32 self.assertEqual([], self._testChange(lines)) |
| 33 |
| 34 def testValidChange_StartsWithSpace(self): |
| 35 lines = ['<input id="testinput" pref="testpref"', |
| 36 ' metric="validaction" type="checkbox" dialog-pref>'] |
| 37 self.assertEqual([], self._testChange(lines)) |
| 38 |
| 39 def testValidChange_Radio(self): |
| 40 lines = ['<input id="testinput" pref="testpref"', |
| 41 ' metric="validaction" type="radio" dialog-pref value="true">'] |
| 42 self.assertEqual([], self._testChange(lines)) |
| 43 |
| 44 def testValidChange_UsingDatatype(self): |
| 45 lines = ['<input id="testinput" pref="testpref"', |
| 46 ' metric="validaction" datatype="boolean" dialog-pref>'] |
| 47 self.assertEqual([], self._testChange(lines)) |
| 48 |
| 49 def testValidChange_NotBoolean(self): |
| 50 lines = ['<input id="testinput" pref="testpref"', |
| 51 ' metric="notboolean_validaction" dialog-pref>'] |
| 52 self.assertEqual([], self._testChange(lines)) |
| 53 |
| 54 def testInvalidChange(self): |
| 55 lines = ['<input id="testinput" pref="testpref"', |
| 56 'metric="invalidaction" type="checkbox" dialog-pref>'] |
| 57 warnings = self._testChange(lines) |
| 58 self.assertEqual(1, len(warnings)) |
| 59 |
| 60 def testInValidChange_Radio(self): |
| 61 lines = ['<input id="testinput" pref="testpref"', |
| 62 ' metric="validaction" type="radio" dialog-pref value="string">'] |
| 63 warnings = self._testChange(lines) |
| 64 self.assertEqual(1, len(warnings)) |
| 65 |
| 66 def _testChange(self, lines): |
| 67 mock_input_api = MockInputApi() |
| 68 mock_input_api.files = [MockFile('path/test.html', lines)] |
| 69 |
| 70 action_xml_path = self._createActionXMLFile() |
| 71 return PRESUBMIT.CheckUserActionUpdate(mock_input_api, |
| 72 MockOutputApi(), |
| 73 action_xml_path) |
| 74 |
| 75 def _createActionXMLFile(self): |
| 76 content = ('<actions>' |
| 77 '<action name="validaction_Disabled">' |
| 78 ' <owner>Please list the metric\'s owners.</owner>' |
| 79 ' <description>Enter the description of this user action.</description>' |
| 80 '</action>' |
| 81 '<action name="validaction_Enabled">' |
| 82 ' <owner>Please list the metric\'s owners. </owner>' |
| 83 ' <description>Enter the description of this user action.</description>' |
| 84 '</action>' |
| 85 '<action name="notboolean_validaction">' |
| 86 ' <owner>Please list the metric\'s owners.</owner>' |
| 87 ' <description>Enter the description of this user action.</description>' |
| 88 '</action>' |
| 89 '</actions>') |
| 90 sys_temp = tempfile.gettempdir() |
| 91 action_xml_path = os.path.join(sys_temp, 'actions_test.xml') |
| 92 if not os.path.exists(action_xml_path): |
| 93 with open(action_xml_path, 'w+') as action_file: |
| 94 action_file.write(content) |
| 95 |
| 96 return action_xml_path |
| 97 |
| 98 |
| 99 if __name__ == '__main__': |
| 100 unittest.main() |
OLD | NEW |