xref: /qemu/tests/functional/test_m68k_nextcube.py (revision 200cd4b081732c27e50262287ff3f07bf0211f08)
1576fffbcSThomas Huth#!/usr/bin/env python3
2576fffbcSThomas 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é
13576fffbcSThomas Huthfrom qemu_test import QemuSystemTest, Asset
14576fffbcSThomas Huthfrom unittest import skipUnless
15162127f2SPhilippe Mathieu-Daudé
16576fffbcSThomas 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é
29576fffbcSThomas Huth    ASSET_ROM = Asset(('https://sourceforge.net/p/previous/code/1350/tree/'
30576fffbcSThomas Huth                       'trunk/src/Rev_2.5_v66.BIN?format=raw'),
31576fffbcSThomas Huth                      '1b753890b67095b73e104c939ddf62eca9e7d0aedde5108e3893b0ed9d8000a4')
32576fffbcSThomas Huth
33ca2e7e46SPhilippe Mathieu-Daudé    def check_bootrom_framebuffer(self, screenshot_path):
34576fffbcSThomas 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')
40*200cd4b0SDaniel P. Berrangé        # TODO: wait for the 'displaysurface_create 1120x832' trace-event.
41ca2e7e46SPhilippe Mathieu-Daudé        time.sleep(2)
42ca2e7e46SPhilippe Mathieu-Daudé
43684750abSVladimir Sementsov-Ogievskiy        self.vm.cmd('human-monitor-command',
44ca2e7e46SPhilippe Mathieu-Daudé                    command_line='screendump %s' % screenshot_path)
45ca2e7e46SPhilippe Mathieu-Daudé
46ca2e7e46SPhilippe Mathieu-Daudé    @skipUnless(PIL_AVAILABLE, 'Python PIL not installed')
47ca2e7e46SPhilippe Mathieu-Daudé    def test_bootrom_framebuffer_size(self):
48576fffbcSThomas Huth        self.set_machine('next-cube')
4928bbe20cSPhilippe Mathieu-Daudé        screenshot_path = os.path.join(self.workdir, "dump.ppm")
50ca2e7e46SPhilippe Mathieu-Daudé        self.check_bootrom_framebuffer(screenshot_path)
51ca2e7e46SPhilippe Mathieu-Daudé
52ca2e7e46SPhilippe Mathieu-Daudé        width, height = Image.open(screenshot_path).size
53ca2e7e46SPhilippe Mathieu-Daudé        self.assertEqual(width, 1120)
54ca2e7e46SPhilippe Mathieu-Daudé        self.assertEqual(height, 832)
55ca2e7e46SPhilippe Mathieu-Daudé
56ca2e7e46SPhilippe Mathieu-Daudé    # Tesseract 4 adds a new OCR engine based on LSTM neural networks. The
57ca2e7e46SPhilippe Mathieu-Daudé    # new version is faster and more accurate than version 3. The drawback is
58ca2e7e46SPhilippe Mathieu-Daudé    # that it is still alpha-level software.
59645198d5SThomas Huth    @skipUnless(tesseract_available(4), 'tesseract OCR tool not available')
60645198d5SThomas Huth    def test_bootrom_framebuffer_ocr_with_tesseract(self):
61576fffbcSThomas Huth        self.set_machine('next-cube')
6228bbe20cSPhilippe Mathieu-Daudé        screenshot_path = os.path.join(self.workdir, "dump.ppm")
63ca2e7e46SPhilippe Mathieu-Daudé        self.check_bootrom_framebuffer(screenshot_path)
64576fffbcSThomas Huth        lines = tesseract_ocr(screenshot_path)
65ca822449SPhilippe Mathieu-Daudé        text = '\n'.join(lines)
66645198d5SThomas Huth        self.assertIn('Testing the FPU', text)
6720869f98SCleber Rosa        self.assertIn('System test failed. Error code', text)
68ca2e7e46SPhilippe Mathieu-Daudé        self.assertIn('Boot command', text)
69ca2e7e46SPhilippe Mathieu-Daudé        self.assertIn('Next>', text)
70576fffbcSThomas Huth
71576fffbcSThomas Huthif __name__ == '__main__':
72576fffbcSThomas Huth    QemuSystemTest.main()
73