summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/runtime/cases/login.py
blob: 175a60c48ca6b5bd0289b6e8b42ccd3d37a67e73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

import shutil
import subprocess
import tempfile
import time
import os
from datetime import datetime
from oeqa.runtime.case import OERuntimeTestCase
from oeqa.runtime.decorator.package import OEHasPackage

### Status of qemu images.
#   - runqemu qemuppc64 comes up blank. (skip)
#   - qemuarmv5 comes up with multiple heads but sending "head" to screendump.
#     seems to create a png with a bad header? (skip for now, but come back to fix)
#   - qemuriscv32 and qemuloongarch64 doesn't work with testimage apparently? (skip)
#   - qemumips64 is missing mouse icon.
#   - qemumips takes forever to render and is missing mouse icon.
#   - qemuarm and qemuppc are odd as they don't resize so we need to just set width.
#   - All images have home and screen flipper icons not always rendered fully at first.
#     the sleep seems to help this out some, depending on machine load.
###

class LoginTest(OERuntimeTestCase):
    @OEHasPackage(['matchbox-desktop'])
    def test_screenshot(self):
        if self.td.get('MACHINE') in ("qemuppc64", "qemuarmv5", "qemuriscv32", "qemuloongarch64"):
            self.skipTest("{0} is not currently supported.".format(self.td.get('MACHINE')))

        pn = self.td.get('PN')

        ourenv = os.environ.copy()
        origpath = self.td.get("ORIGPATH")
        if origpath:
            ourenv['PATH'] = ourenv['PATH'] + ":" + origpath

        for cmd in ["identify.im7", "convert.im7", "compare.im7"]:
            try:
                subprocess.check_output(["which", cmd], env=ourenv)
            except subprocess.CalledProcessError:
                self.skipTest("%s (from imagemagick) not available" % cmd)


        # Store images so we can debug them if needed
        saved_screenshots_dir = self.td.get('T') + "/saved-screenshots/"

        ###
        # This is a really horrible way of doing this but I've not found the
        # right event to determine "The system is loaded and screen is rendered"
        #
        # Using dbus-wait for matchbox is the wrong answer because while it
        # ensures the system is up, it doesn't mean the screen is rendered.
        #
        # Checking the qmp socket doesn't work afaik either.
        #
        # One way to do this is to do compares of known good screendumps until
        # we either get expected or close to expected or we time out. Part of the
        # issue here with that is that there is a very fine difference in the
        # diff between a screendump where the icons haven't loaded yet and
        # one where they won't load. I'll look at that next, but, for now, this.
        #
        # Which is ugly and I hate it but it 'works' for various definitions of
        # 'works'.
        ###

        # qemumips takes forever to render. We could probably get away with 20
        # here were it not for that.
        time.sleep(40)

        with tempfile.NamedTemporaryFile(prefix="oeqa-screenshot-login", suffix=".png") as t:
            ret = self.target.runner.run_monitor("screendump", args={"filename": t.name, "format":"png"})

            # Find out size of image so we can determine where to blank out clock.
            # qemuarm and qemuppc are odd as it doesn't resize the window and returns
            # incorrect widths
            if self.td.get('MACHINE') == "qemuarm" or self.td.get('MACHINE') == "qemuppc":
                width = "640"
            else:
                cmd = "identify.im7 -ping -format '%w' {0}".format(t.name)
                width = subprocess.check_output(cmd, shell=True, env=ourenv).decode()

            rblank = int(float(width))
            lblank = rblank-40

            # Use the meta-oe version of convert, along with it's suffix. This blanks out the clock.
            cmd = "convert.im7 {0} -fill white -draw 'rectangle {1},10 {2},22' {3}".format(t.name, str(rblank), str(lblank), t.name)
            convert_out=subprocess.check_output(cmd, shell=True, env=ourenv).decode()


            bb.utils.mkdirhier(saved_screenshots_dir)
            savedfile = "{0}/saved-{1}-{2}-{3}.png".format(saved_screenshots_dir, \
                                                                        datetime.timestamp(datetime.now()), \
                                                                        pn, \
                                                                        self.td.get('MACHINE'))
            shutil.copy2(t.name, savedfile)

            refimage = self.td.get('COREBASE') + "/meta/files/screenshot-tests/" + pn + "-" + self.td.get('MACHINE') +".png"
            if not os.path.exists(refimage):
                self.skipTest("No reference image for comparision (%s)" % refimage)

            cmd = "compare.im7 -metric MSE {0} {1} /dev/null".format(t.name, refimage)
            compare_out = subprocess.run(cmd, shell=True, capture_output=True, text=True, env=ourenv)
            diff=float(compare_out.stderr.replace("(", "").replace(")","").split()[1])
            if diff > 0:
                # Keep a copy of the failed screenshot so we can see what happened.
                self.fail("Screenshot diff is {0}. Failed image stored in {1}".format(str(diff), savedfile))
            else:
                self.assertEqual(0, diff, "Screenshot diff is {0}.".format(str(diff)))