1*576fffbcSThomas Huth#!/usr/bin/env python3
2*576fffbcSThomas Huth#
3*576fffbcSThomas Huth# Functional test that boots a VM and run OCR on the framebuffer
4*576fffbcSThomas Huth#
5*576fffbcSThomas Huth# Copyright (c) 2019 Philippe Mathieu-Daudé <f4bug@amsat.org>
6*576fffbcSThomas Huth#
7*576fffbcSThomas Huth# This work is licensed under the terms of the GNU GPL, version 2 or
8*576fffbcSThomas Huth# later.  See the COPYING file in the top-level directory.
9*576fffbcSThomas Huth
10*576fffbcSThomas Huthimport os
11*576fffbcSThomas Huthimport time
12*576fffbcSThomas Huth
13*576fffbcSThomas Huthfrom qemu_test import QemuSystemTest, Asset
14*576fffbcSThomas Huthfrom unittest import skipUnless
15*576fffbcSThomas Huth
16*576fffbcSThomas Huthfrom qemu_test.tesseract import tesseract_available, tesseract_ocr
17*576fffbcSThomas Huth
18*576fffbcSThomas HuthPIL_AVAILABLE = True
19*576fffbcSThomas Huthtry:
20*576fffbcSThomas Huth    from PIL import Image
21*576fffbcSThomas Huthexcept ImportError:
22*576fffbcSThomas Huth    PIL_AVAILABLE = False
23*576fffbcSThomas Huth
24*576fffbcSThomas Huth
25*576fffbcSThomas Huthclass NextCubeMachine(QemuSystemTest):
26*576fffbcSThomas Huth
27*576fffbcSThomas Huth    timeout = 15
28*576fffbcSThomas Huth
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
33*576fffbcSThomas Huth    def check_bootrom_framebuffer(self, screenshot_path):
34*576fffbcSThomas Huth        rom_path = self.ASSET_ROM.fetch()
35*576fffbcSThomas Huth
36*576fffbcSThomas Huth        self.vm.add_args('-bios', rom_path)
37*576fffbcSThomas Huth        self.vm.launch()
38*576fffbcSThomas Huth
39*576fffbcSThomas Huth        self.log.info('VM launched, waiting for display')
40*576fffbcSThomas Huth        # TODO: Use avocado.utils.wait.wait_for to catch the
41*576fffbcSThomas Huth        #       'displaysurface_create 1120x832' trace-event.
42*576fffbcSThomas Huth        time.sleep(2)
43*576fffbcSThomas Huth
44*576fffbcSThomas Huth        self.vm.cmd('human-monitor-command',
45*576fffbcSThomas Huth                    command_line='screendump %s' % screenshot_path)
46*576fffbcSThomas Huth
47*576fffbcSThomas Huth    @skipUnless(PIL_AVAILABLE, 'Python PIL not installed')
48*576fffbcSThomas Huth    def test_bootrom_framebuffer_size(self):
49*576fffbcSThomas Huth        self.set_machine('next-cube')
50*576fffbcSThomas Huth        screenshot_path = os.path.join(self.workdir, "dump.ppm")
51*576fffbcSThomas Huth        self.check_bootrom_framebuffer(screenshot_path)
52*576fffbcSThomas Huth
53*576fffbcSThomas Huth        width, height = Image.open(screenshot_path).size
54*576fffbcSThomas Huth        self.assertEqual(width, 1120)
55*576fffbcSThomas Huth        self.assertEqual(height, 832)
56*576fffbcSThomas Huth
57*576fffbcSThomas Huth    # Tesseract 4 adds a new OCR engine based on LSTM neural networks. The
58*576fffbcSThomas Huth    # new version is faster and more accurate than version 3. The drawback is
59*576fffbcSThomas Huth    # that it is still alpha-level software.
60*576fffbcSThomas Huth    @skipUnless(tesseract_available(4), 'tesseract OCR tool not available')
61*576fffbcSThomas Huth    def test_bootrom_framebuffer_ocr_with_tesseract(self):
62*576fffbcSThomas Huth        self.set_machine('next-cube')
63*576fffbcSThomas Huth        screenshot_path = os.path.join(self.workdir, "dump.ppm")
64*576fffbcSThomas Huth        self.check_bootrom_framebuffer(screenshot_path)
65*576fffbcSThomas Huth        lines = tesseract_ocr(screenshot_path)
66*576fffbcSThomas Huth        text = '\n'.join(lines)
67*576fffbcSThomas Huth        self.assertIn('Testing the FPU', text)
68*576fffbcSThomas Huth        self.assertIn('System test failed. Error code', text)
69*576fffbcSThomas Huth        self.assertIn('Boot command', text)
70*576fffbcSThomas Huth        self.assertIn('Next>', text)
71*576fffbcSThomas Huth
72*576fffbcSThomas Huthif __name__ == '__main__':
73*576fffbcSThomas Huth    QemuSystemTest.main()
74