xref: /qemu/tests/functional/test_m68k_nextcube.py (revision 576fffbc8eefd806c47850f8e1c60fdcb37733e3)
1*576fffbcSThomas Huth#!/usr/bin/env python3
2*576fffbcSThomas Huth#
3ca2e7e46SPhilippe Mathieu-Daudé# Functional test that boots a VM and run OCR on the framebuffer
4ca2e7e46SPhilippe Mathieu-Daudé#
5162127f2SPhilippe Mathieu-Daudé# Copyright (c) 2019 Philippe Mathieu-Daudé <f4bug@amsat.org>
6ca2e7e46SPhilippe Mathieu-Daudé#
7ca2e7e46SPhilippe Mathieu-Daudé# This work is licensed under the terms of the GNU GPL, version 2 or
8ca2e7e46SPhilippe Mathieu-Daudé# later.  See the COPYING file in the top-level directory.
9ca2e7e46SPhilippe Mathieu-Daudé
10ca2e7e46SPhilippe Mathieu-Daudéimport os
11ca2e7e46SPhilippe Mathieu-Daudéimport time
12ca2e7e46SPhilippe Mathieu-Daudé
13*576fffbcSThomas Huthfrom qemu_test import QemuSystemTest, Asset
14*576fffbcSThomas Huthfrom unittest import skipUnless
15162127f2SPhilippe Mathieu-Daudé
16*576fffbcSThomas Huthfrom qemu_test.tesseract import tesseract_available, tesseract_ocr
17ca2e7e46SPhilippe Mathieu-Daudé
18ca2e7e46SPhilippe Mathieu-DaudéPIL_AVAILABLE = True
19ca2e7e46SPhilippe Mathieu-Daudétry:
20ca2e7e46SPhilippe Mathieu-Daudé    from PIL import Image
21ca2e7e46SPhilippe Mathieu-Daudéexcept ImportError:
22ca2e7e46SPhilippe Mathieu-Daudé    PIL_AVAILABLE = False
23ca2e7e46SPhilippe Mathieu-Daudé
24ca2e7e46SPhilippe Mathieu-Daudé
252283b627SPhilippe Mathieu-Daudéclass NextCubeMachine(QemuSystemTest):
26ca2e7e46SPhilippe Mathieu-Daudé
27ca2e7e46SPhilippe Mathieu-Daudé    timeout = 15
28ca2e7e46SPhilippe Mathieu-Daudé
29*576fffbcSThomas Huth    ASSET_ROM = Asset(('https://sourceforge.net/p/previous/code/1350/tree/'
30*576fffbcSThomas Huth                       'trunk/src/Rev_2.5_v66.BIN?format=raw'),
31*576fffbcSThomas Huth                      '1b753890b67095b73e104c939ddf62eca9e7d0aedde5108e3893b0ed9d8000a4')
32*576fffbcSThomas Huth
33ca2e7e46SPhilippe Mathieu-Daudé    def check_bootrom_framebuffer(self, screenshot_path):
34*576fffbcSThomas Huth        rom_path = self.ASSET_ROM.fetch()
35ca2e7e46SPhilippe Mathieu-Daudé
36ca2e7e46SPhilippe Mathieu-Daudé        self.vm.add_args('-bios', rom_path)
37ca2e7e46SPhilippe Mathieu-Daudé        self.vm.launch()
38ca2e7e46SPhilippe Mathieu-Daudé
39ca2e7e46SPhilippe Mathieu-Daudé        self.log.info('VM launched, waiting for display')
40ca2e7e46SPhilippe Mathieu-Daudé        # TODO: Use avocado.utils.wait.wait_for to catch the
41ca2e7e46SPhilippe Mathieu-Daudé        #       'displaysurface_create 1120x832' trace-event.
42ca2e7e46SPhilippe Mathieu-Daudé        time.sleep(2)
43ca2e7e46SPhilippe Mathieu-Daudé
44684750abSVladimir Sementsov-Ogievskiy        self.vm.cmd('human-monitor-command',
45ca2e7e46SPhilippe Mathieu-Daudé                    command_line='screendump %s' % screenshot_path)
46ca2e7e46SPhilippe Mathieu-Daudé
47ca2e7e46SPhilippe Mathieu-Daudé    @skipUnless(PIL_AVAILABLE, 'Python PIL not installed')
48ca2e7e46SPhilippe Mathieu-Daudé    def test_bootrom_framebuffer_size(self):
49*576fffbcSThomas Huth        self.set_machine('next-cube')
5028bbe20cSPhilippe Mathieu-Daudé        screenshot_path = os.path.join(self.workdir, "dump.ppm")
51ca2e7e46SPhilippe Mathieu-Daudé        self.check_bootrom_framebuffer(screenshot_path)
52ca2e7e46SPhilippe Mathieu-Daudé
53ca2e7e46SPhilippe Mathieu-Daudé        width, height = Image.open(screenshot_path).size
54ca2e7e46SPhilippe Mathieu-Daudé        self.assertEqual(width, 1120)
55ca2e7e46SPhilippe Mathieu-Daudé        self.assertEqual(height, 832)
56ca2e7e46SPhilippe Mathieu-Daudé
57ca2e7e46SPhilippe Mathieu-Daudé    # Tesseract 4 adds a new OCR engine based on LSTM neural networks. The
58ca2e7e46SPhilippe Mathieu-Daudé    # new version is faster and more accurate than version 3. The drawback is
59ca2e7e46SPhilippe Mathieu-Daudé    # that it is still alpha-level software.
60645198d5SThomas Huth    @skipUnless(tesseract_available(4), 'tesseract OCR tool not available')
61645198d5SThomas Huth    def test_bootrom_framebuffer_ocr_with_tesseract(self):
62*576fffbcSThomas Huth        self.set_machine('next-cube')
6328bbe20cSPhilippe Mathieu-Daudé        screenshot_path = os.path.join(self.workdir, "dump.ppm")
64ca2e7e46SPhilippe Mathieu-Daudé        self.check_bootrom_framebuffer(screenshot_path)
65*576fffbcSThomas Huth        lines = tesseract_ocr(screenshot_path)
66ca822449SPhilippe Mathieu-Daudé        text = '\n'.join(lines)
67645198d5SThomas Huth        self.assertIn('Testing the FPU', text)
6820869f98SCleber Rosa        self.assertIn('System test failed. Error code', text)
69ca2e7e46SPhilippe Mathieu-Daudé        self.assertIn('Boot command', text)
70ca2e7e46SPhilippe Mathieu-Daudé        self.assertIn('Next>', text)
71*576fffbcSThomas Huth
72*576fffbcSThomas Huthif __name__ == '__main__':
73*576fffbcSThomas Huth    QemuSystemTest.main()
74