| 1 |
import contextlib |
| 2 |
import os |
| 3 |
import random |
| 4 |
from _thread import start_new_thread |
| 5 |
from time import sleep |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from DisplayCAL import localization as lang |
| 10 |
from DisplayCAL import (config, worker) |
| 11 |
from DisplayCAL.cgats import CGATS |
| 12 |
from DisplayCAL.config import getcfg |
| 13 |
from DisplayCAL.icc_profile import ICCProfile |
| 14 |
from DisplayCAL.util_io import Files |
| 15 |
from DisplayCAL.wx_untethered_frame import UntetheredFrame |
| 16 |
from DisplayCAL.wx_addons import wx |
| 17 |
from DisplayCAL.wx_windows import BaseApp |
| 18 |
|
| 19 |
|
| 20 |
class Subprocess: |
| 21 |
def send(self, bytes_): |
| 22 |
start_new_thread(setup_app, (bytes_,)) |
| 23 |
|
| 24 |
|
| 25 |
class Worker(worker.Worker): |
| 26 |
def __init__(self): |
| 27 |
worker.Worker.__init__(self) |
| 28 |
self.finished = False |
| 29 |
self.instrument_calibration_complete = False |
| 30 |
self.instrument_place_on_screen_msg = False |
| 31 |
self.instrument_sensor_position_msg = False |
| 32 |
self.is_ambient_measuring = False |
| 33 |
self.subprocess = Subprocess() |
| 34 |
self.subprocess_abort = False |
| 35 |
|
| 36 |
def abort_subprocess(self): |
| 37 |
self.safe_send("Q") |
| 38 |
|
| 39 |
def safe_send(self, bytes_): |
| 40 |
print(f"*** Sending {bytes_!r}") |
| 41 |
self.subprocess.send(bytes_) |
| 42 |
return True |
| 43 |
|
| 44 |
|
| 45 |
@pytest.fixture(scope="function") |
| 46 |
def setup_test_app_and_data(): |
| 47 |
"""Set up the tests.""" |
| 48 |
config.initcfg() |
| 49 |
print("untethered.min_delta", getcfg("untethered.min_delta")) |
| 50 |
print("untethered.min_delta.lightness", getcfg("untethered.min_delta.lightness")) |
| 51 |
print("untethered.max_delta.chroma", getcfg("untethered.max_delta.chroma")) |
| 52 |
lang.init() |
| 53 |
lang.update_defaults() |
| 54 |
app = BaseApp(0) |
| 55 |
app.TopWindow = UntetheredFrame(start_timer=False) |
| 56 |
test_chart = getcfg("testchart.file") |
| 57 |
if os.path.splitext(test_chart)[1].lower() in (".icc", ".icm"): |
| 58 |
with contextlib.suppress(Exception): |
| 59 |
test_chart = ICCProfile(test_chart).tags.targ |
| 60 |
try: |
| 61 |
app.TopWindow.cgats = CGATS(test_chart) |
| 62 |
except Exception: |
| 63 |
app.TopWindow.cgats = CGATS( |
| 64 |
"""TI1 |
| 65 |
BEGIN_DATA_FORMAT |
| 66 |
SAMPLE_ID RGB_R RGB_G RGB_B XYZ_X XYZ_Y XYZ_Z |
| 67 |
END_DATA_FORMAT |
| 68 |
BEGIN_DATA |
| 69 |
1 0 0 0 0 0 0 |
| 70 |
END_DATA |
| 71 |
""" |
| 72 |
) |
| 73 |
app.TopWindow.worker = Worker() |
| 74 |
app.TopWindow.worker.progress_wnd = app.TopWindow |
| 75 |
app.TopWindow.Show() |
| 76 |
files = Files([app.TopWindow.worker, app.TopWindow]) |
| 77 |
yield app, files |
| 78 |
|
| 79 |
|
| 80 |
def setup_app(setup_test_app_and_data, bytes_): |
| 81 |
app, files = setup_test_app_and_data |
| 82 |
print(f"*** Received {bytes_!r}") |
| 83 |
menu = r"""Place instrument on spot to be measured, |
| 84 |
and hit [A-Z] to read white and setup FWA compensation (keyed to letter) |
| 85 |
[a-z] to read and make FWA compensated reading from keyed reference |
| 86 |
'r' to set reference, 's' to save spectrum, |
| 87 |
'h' to toggle high res., 'k' to do a calibration |
| 88 |
Hit ESC or Q to exit, any other key to take a reading:""" |
| 89 |
if not bytes_: |
| 90 |
txt = menu |
| 91 |
elif bytes_ == " ": |
| 92 |
i = app.TopWindow.index |
| 93 |
row = app.TopWindow.cgats[0].DATA[i] |
| 94 |
txt = [ |
| 95 |
f""" |
| 96 |
Result is XYZ: {row.XYZ_X:.6f} {row.XYZ_Y:.6f} {row.XYZ_Z:.6f} |
| 97 |
|
| 98 |
Place instrument on spot to be measured, |
| 99 |
and hit [A-Z] to read white and setup FWA compensation (keyed to letter) |
| 100 |
[a-z] to read and make FWA compensated reading from keyed reference |
| 101 |
'r' to set reference, 's' to save spectrum, |
| 102 |
'h' to toggle high res., 'k' to do a calibration |
| 103 |
Hit ESC or Q to exit, any other key to take a reading:""", |
| 104 |
f"""" |
| 105 |
Result is XYZ: {row.XYZ_X:.6f} {row.XYZ_Y:.6f} {row.XYZ_Z:.6f} |
| 106 |
|
| 107 |
Spot read needs a calibration before continuing |
| 108 |
Place cap on the instrument, or place on a dark surface, |
| 109 |
or place on the white calibration reference, |
| 110 |
and then hit any key to continue, |
| 111 |
or hit Esc or Q to abort:""", |
| 112 |
][random.choice([0, 1])] |
| 113 |
elif bytes_ in ("Q", "q"): |
| 114 |
wx.CallAfter(app.TopWindow.Close) |
| 115 |
return |
| 116 |
else: |
| 117 |
return |
| 118 |
for line in txt.split("\n"): |
| 119 |
sleep(0.03125) |
| 120 |
if app.TopWindow: |
| 121 |
wx.CallAfter(files.write, line) |
| 122 |
print(line) |
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.skip(reason="Not implemented yet") |
| 126 |
def test_wxuntetheredframe(setup_app): |
| 127 |
start_new_thread(setup_app, ()) |
| 128 |
app.MainLoop() |