1576fffbcSThomas Huth#!/usr/bin/env python3
2576fffbcSThomas Huth#
3576fffbcSThomas Huth# Functional test that boots a VM and run OCR on the framebuffer
4576fffbcSThomas Huth#
5576fffbcSThomas Huth# Copyright (c) 2019 Philippe Mathieu-Daudé <f4bug@amsat.org>
6576fffbcSThomas Huth#
7576fffbcSThomas Huth# This work is licensed under the terms of the GNU GPL, version 2 or
8576fffbcSThomas Huth# later.  See the COPYING file in the top-level directory.
9576fffbcSThomas Huth
10576fffbcSThomas Huthimport os
11576fffbcSThomas Huthimport time
12576fffbcSThomas Huth
13576fffbcSThomas Huthfrom qemu_test import QemuSystemTest, Asset
14576fffbcSThomas Huthfrom unittest import skipUnless
15576fffbcSThomas Huth
16576fffbcSThomas Huthfrom qemu_test.tesseract import tesseract_available, tesseract_ocr
17576fffbcSThomas Huth
18576fffbcSThomas HuthPIL_AVAILABLE = True
19576fffbcSThomas Huthtry:
20576fffbcSThomas Huth    from PIL import Image
21576fffbcSThomas Huthexcept ImportError:
22576fffbcSThomas Huth    PIL_AVAILABLE = False
23576fffbcSThomas Huth
24576fffbcSThomas Huth
25576fffbcSThomas Huthclass NextCubeMachine(QemuSystemTest):
26576fffbcSThomas Huth
27576fffbcSThomas Huth    timeout = 15
28576fffbcSThomas Huth
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
33576fffbcSThomas Huth    def check_bootrom_framebuffer(self, screenshot_path):
34576fffbcSThomas Huth        rom_path = self.ASSET_ROM.fetch()
35576fffbcSThomas Huth
36576fffbcSThomas Huth        self.vm.add_args('-bios', rom_path)
37576fffbcSThomas Huth        self.vm.launch()
38576fffbcSThomas Huth
39576fffbcSThomas Huth        self.log.info('VM launched, waiting for display')
40*200cd4b0SDaniel P. Berrangé        # TODO: wait for the 'displaysurface_create 1120x832' trace-event.
41576fffbcSThomas Huth        time.sleep(2)
42576fffbcSThomas Huth
43576fffbcSThomas Huth        self.vm.cmd('human-monitor-command',
44576fffbcSThomas Huth                    command_line='screendump %s' % screenshot_path)
45576fffbcSThomas Huth
46576fffbcSThomas Huth    @skipUnless(PIL_AVAILABLE, 'Python PIL not installed')
47576fffbcSThomas Huth    def test_bootrom_framebuffer_size(self):
48576fffbcSThomas Huth        self.set_machine('next-cube')
49576fffbcSThomas Huth        screenshot_path = os.path.join(self.workdir, "dump.ppm")
50576fffbcSThomas Huth        self.check_bootrom_framebuffer(screenshot_path)
51576fffbcSThomas Huth
52576fffbcSThomas Huth        width, height = Image.open(screenshot_path).size
53576fffbcSThomas Huth        self.assertEqual(width, 1120)
54576fffbcSThomas Huth        self.assertEqual(height, 832)
55576fffbcSThomas Huth
56576fffbcSThomas Huth    # Tesseract 4 adds a new OCR engine based on LSTM neural networks. The
57576fffbcSThomas Huth    # new version is faster and more accurate than version 3. The drawback is
58576fffbcSThomas Huth    # that it is still alpha-level software.
59576fffbcSThomas Huth    @skipUnless(tesseract_available(4), 'tesseract OCR tool not available')
60576fffbcSThomas Huth    def test_bootrom_framebuffer_ocr_with_tesseract(self):
61576fffbcSThomas Huth        self.set_machine('next-cube')
62576fffbcSThomas Huth        screenshot_path = os.path.join(self.workdir, "dump.ppm")
63576fffbcSThomas Huth        self.check_bootrom_framebuffer(screenshot_path)
64576fffbcSThomas Huth        lines = tesseract_ocr(screenshot_path)
65576fffbcSThomas Huth        text = '\n'.join(lines)
66576fffbcSThomas Huth        self.assertIn('Testing the FPU', text)
67576fffbcSThomas Huth        self.assertIn('System test failed. Error code', text)
68576fffbcSThomas Huth        self.assertIn('Boot command', text)
69576fffbcSThomas Huth        self.assertIn('Next>', text)
70576fffbcSThomas Huth
71576fffbcSThomas Huthif __name__ == '__main__':
72576fffbcSThomas Huth    QemuSystemTest.main()
73