Debian Codegraph @

debian/jstest/jstest.py

raw
1 #!/usr/bin/python3
2 # encoding=UTF-8
3
4 # Copyright © 2011 Jakub Wilk <[email protected]>
5 # © 2013-2023 Dmitry Shachnev <[email protected]>
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 #
14 # * Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 import unittest
31
32 from PyQt6.QtCore import QTimer, QUrl, QUrlQuery
33 from PyQt6.QtWidgets import QApplication
34 from PyQt6.QtWebEngineCore import QWebEnginePage
35 from PyQt6.QtWebEngineWidgets import QWebEngineView
36
37 default_time_limit = 40_000 # msecs
38 timer_step = 1_000 # msecs
39
40 # HTTP browser
41 # ============
42
43 class Page(QWebEnginePage):
44 def javaScriptConsoleMessage(self, level, message, lineNumber, sourceId):
45 print(f"[{level}] {sourceId}:{lineNumber}: {message}")
46
47
48 class View(QWebEngineView):
49 def __init__(self):
50 super().__init__()
51 self.result = None
52 self._page = Page(parent=self)
53 self._page.loadFinished.connect(self.onLoadFinished)
54 self.setPage(self._page)
55 self._timer = None
56 self._time_limit = default_time_limit
57
58 def setResult(self, result):
59 self.result = result
60
61 def onLoadFinished(self):
62 self._timer = QTimer(parent=self)
63 self._timer.timeout.connect(self.runJavaScript)
64 self._timer.start(timer_step)
65
66 def runJavaScript(self):
67 if self._time_limit <= 0:
68 raise TimeoutError
69 script = """
70 result = {
71 n_results: document.querySelector('#search-results > p').textContent.match(/found (\\d+) page/)[1],
72 n_links: document.querySelectorAll('#search-results a').length,
73 n_highlights: document.querySelectorAll('#search-results .highlighted').length,
74 };
75 result
76 """
77 self._page.runJavaScript(script, self.setResult)
78 self._time_limit -= timer_step
79
80 def waitForResult(self):
81 app = QApplication.instance()
82 while self.result is None:
83 app.processEvents()
84 self._timer.stop()
85
86
87 # Actual tests
88 # ============
89
90 def test_html(result, options):
91
92 class TestCase(unittest.TestCase):
93
94 if options.n_results is not None:
95 def test_n_results(self):
96 n_results = int(result['n_results'])
97 self.assertEqual(n_results, options.n_results)
98
99 if options.n_links is not None:
100 def test_n_links(self):
101 n_links = int(result['n_links'])
102 self.assertEqual(n_links, options.n_links)
103
104 if options.n_highlights is not None:
105 def test_n_highlights(self):
106 n_highlights = int(result['n_highlights'])
107 self.assertEqual(n_highlights, options.n_highlights)
108
109 TestCase.__name__ = 'TestCase(%r)' % options.search_term
110
111 suite = unittest.TestLoader().loadTestsFromTestCase(TestCase)
112 return unittest.TextTestRunner(verbosity=2).run(suite)
113
114 def test_directory(directory, options, time_limit=default_time_limit):
115 url = QUrl.fromLocalFile(f'{directory}/search.html')
116 query = QUrlQuery()
117 query.addQueryItem('q', options.search_term)
118 url.setQuery(query)
119 view = View()
120 view.load(url)
121 view.show()
122 view.waitForResult()
123 return test_html(view.result, options)
124
125 # vim:ts=4 sw=4 et