Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / resources / PRESUBMIT_test.py
blob923f2be1f4cad180e691f1c2265a9ab3a7397bb3
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.
5 import os
6 import sys
7 import imp
8 import tempfile
9 import unittest
10 import PRESUBMIT
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 MockInputApi, MockOutputApi
15 from PRESUBMIT_test_mocks import MockFile, MockChange
17 class HTMLActionAdditionTest(unittest.TestCase):
19 def testActionXMLChanged(self):
20 mock_input_api = MockInputApi()
21 lines = ['<input id="testinput" pref="testpref"',
22 'metric="validaction" type="checkbox" dialog-pref>']
23 mock_input_api.files = [MockFile('path/valid.html', lines)]
24 mock_input_api.change = MockChange(['path/valid.html','actions.xml'])
25 action_xml_path = self._createActionXMLFile()
26 self.assertEqual([], PRESUBMIT.CheckUserActionUpdate(mock_input_api,
27 MockOutputApi(),
28 action_xml_path))
30 def testValidChange_StartOfLine(self):
31 lines = ['<input id="testinput" pref="testpref"',
32 'metric="validaction" type="checkbox" dialog-pref>']
33 self.assertEqual([], self._testChange(lines))
35 def testValidChange_StartsWithSpace(self):
36 lines = ['<input id="testinput" pref="testpref"',
37 ' metric="validaction" type="checkbox" dialog-pref>']
38 self.assertEqual([], self._testChange(lines))
40 def testValidChange_Radio(self):
41 lines = ['<input id="testinput" pref="testpref"',
42 ' metric="validaction" type="radio" dialog-pref value="true">']
43 self.assertEqual([], self._testChange(lines))
45 def testValidChange_UsingDatatype(self):
46 lines = ['<input id="testinput" pref="testpref"',
47 ' metric="validaction" datatype="boolean" dialog-pref>']
48 self.assertEqual([], self._testChange(lines))
50 def testValidChange_NotBoolean(self):
51 lines = ['<input id="testinput" pref="testpref"',
52 ' metric="notboolean_validaction" dialog-pref>']
53 self.assertEqual([], self._testChange(lines))
55 def testInvalidChange(self):
56 lines = ['<input id="testinput" pref="testpref"',
57 'metric="invalidaction" type="checkbox" dialog-pref>']
58 warnings = self._testChange(lines)
59 self.assertEqual(1, len(warnings), warnings)
61 def testInValidChange_Radio(self):
62 lines = ['<input id="testinput" pref="testpref"',
63 ' metric="validaction" type="radio" dialog-pref value="string">']
64 warnings = self._testChange(lines)
65 self.assertEqual(1, len(warnings), warnings)
67 def testValidChange_MultilineType(self):
68 lines = ['<input id="testinput" pref="testpref"\n'
69 ' metric="validaction" type=\n'
70 ' "radio" dialog-pref value=\n'
71 ' "false">']
72 warnings = self._testChange(lines)
73 self.assertEqual([], self._testChange(lines))
75 def _testChange(self, lines):
76 mock_input_api = MockInputApi()
77 mock_input_api.files = [MockFile('path/test.html', lines)]
79 action_xml_path = self._createActionXMLFile()
80 return PRESUBMIT.CheckUserActionUpdate(mock_input_api,
81 MockOutputApi(),
82 action_xml_path)
84 def _createActionXMLFile(self):
85 content = ('<actions>'
86 '<action name="validaction_Disable">'
87 ' <owner>Please list the metric\'s owners.</owner>'
88 ' <description>Enter the description of this user action.</description>'
89 '</action>'
90 '<action name="validaction_Enable">'
91 ' <owner>Please list the metric\'s owners. </owner>'
92 ' <description>Enter the description of this user action.</description>'
93 '</action>'
94 '<action name="notboolean_validaction">'
95 ' <owner>Please list the metric\'s owners.</owner>'
96 ' <description>Enter the description of this user action.</description>'
97 '</action>'
98 '</actions>')
99 sys_temp = tempfile.gettempdir()
100 action_xml_path = os.path.join(sys_temp, 'actions_test.xml')
101 if not os.path.exists(action_xml_path):
102 with open(action_xml_path, 'w+') as action_file:
103 action_file.write(content)
105 return action_xml_path
108 if __name__ == '__main__':
109 unittest.main()