blob: c7412927c8904de22bd0ad287204b150c3d5f0ab [file] [log] [blame]
Vaclav Brozekcdc7defb2018-03-20 09:54:351#!/usr/bin/env python
gayane3dff8c22014-12-04 17:09:512# 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
6import os
7import sys
8import imp
9import tempfile
10import unittest
11import PRESUBMIT
12
13sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(
14 os.path.abspath(__file__))))))
gayanee1702662014-12-13 03:48:0915from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi
16from PRESUBMIT_test_mocks import MockFile, MockChange
gayane3dff8c22014-12-04 17:09:5117
18class HTMLActionAdditionTest(unittest.TestCase):
19
20 def testActionXMLChanged(self):
21 mock_input_api = MockInputApi()
gayanee1702662014-12-13 03:48:0922 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'])
gayane3dff8c22014-12-04 17:09:5126 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)
gayane940df072015-02-24 14:28:3060 self.assertEqual(1, len(warnings), warnings)
gayane3dff8c22014-12-04 17:09:5161
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)
gayane940df072015-02-24 14:28:3066 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))
gayane3dff8c22014-12-04 17:09:5175
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>'
gayane860db5c32014-12-05 16:16:4687 '<action name="validaction_Disable">'
gayane3dff8c22014-12-04 17:09:5188 ' <owner>Please list the metric\'s owners.</owner>'
89 ' <description>Enter the description of this user action.</description>'
90 '</action>'
gayane860db5c32014-12-05 16:16:4691 '<action name="validaction_Enable">'
gayane3dff8c22014-12-04 17:09:5192 ' <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
109if __name__ == '__main__':
110 unittest.main()