1import pytest
2from _testutils import MockPyusbDevice
3
4from collections import deque
5
6from liquidctl.driver.asetek_pro import CorsairAsetekProDriver
7
8
9@pytest.fixture
10def emulate():
11    usb_dev = MockPyusbDevice()
12    cooler = CorsairAsetekProDriver(usb_dev, 'Emulated Asetek Pro cooler', fan_count=2)
13    return (usb_dev, cooler)
14
15
16def test_not_totally_broken(emulate):
17    _, cooler = emulate
18
19    with cooler.connect():
20        cooler.initialize()
21
22        cooler.get_status()
23
24        cooler.set_color(channel='logo', mode='blinking', colors=iter([[3, 2, 1]]))
25        cooler.set_color(channel='logo', mode='pulse', colors=[[3, 2, 1]], speed='normal')
26
27        cooler.set_speed_profile(channel='fan',
28                                      profile=iter([(20, 20), (30, 50), (40, 100)]))
29        cooler.set_speed_profile(channel='fan1',
30                                      profile=iter([(20, 20), (30, 50), (40, 100)]))
31
32        cooler.set_fixed_speed(channel='fan', duty=50)
33
34
35def test_initialize_with_pump_mode(emulate):
36    usb_dev, cooler = emulate
37
38    with cooler.connect():
39        cooler.initialize(pump_mode='balanced')
40
41        _begin, set_pump = usb_dev._sent_xfers
42        assert set_pump == ('write', 1, [0x32, 0x01])
43
44
45def test_set_profile_of_all_fans(emulate):
46    usb_dev, cooler = emulate
47
48    # When setting the speed of all fans the driver first gets the speed of all
49    # fans to work out how many fans are present. Set 2 valid responses to
50    # simulate a setup with 2 fans present.  The first response is for the pump
51    # setup in the initialize function.
52
53    with cooler.connect():
54        cooler.initialize()
55
56        cooler.set_speed_profile(channel='fan', profile=[(0, 10), (25, 50), (40, 100)])
57
58        _begin, _pump, fan1, fan2 = usb_dev._sent_xfers
59        assert fan1 == ('write', 1, [0x40, 0x00, 0x00, 0x19, 0x28, 0x3c, 0x3c, 0x3c,
60                                          0x3c, 0x0a, 0x32, 0x64, 0x64, 0x64, 0x64, 0x64])
61        assert fan2 == ('write', 1, [0x40, 0x01, 0x00, 0x19, 0x28, 0x3c, 0x3c, 0x3c,
62                                          0x3c, 0x0a, 0x32, 0x64, 0x64, 0x64, 0x64, 0x64])
63
64
65def test_setting_speed_on_single_fan_2(emulate):
66    usb_dev, cooler = emulate
67
68    with cooler.connect():
69        cooler.initialize()
70
71        cooler.set_fixed_speed('fan2', 100)
72
73        _begin, _pump, fan2 = usb_dev._sent_xfers
74        assert fan2 == ('write', 1, [0x42, 0x01, 0x64])
75
76
77def test_set_pump_to_fixed_color(emulate):
78    usb_dev, cooler = emulate
79
80    with cooler.connect():
81        cooler.initialize()
82
83        cooler.set_color(channel='logo', mode='fixed', colors=iter([[0xff, 0x88, 0x44]]))
84
85        _begin, _pump, color_change, color_end = usb_dev._sent_xfers
86        assert color_change == ('write', 1, [0x56, 0x02, 0xff, 0x88,
87                                             0x44, 0xff, 0x88, 0x44])
88        assert color_end == ('write', 1, [0x55, 0x01])
89
90
91def test_set_pump_to_blinking_mode(emulate):
92    usb_dev, cooler = emulate
93
94    with cooler.connect():
95        cooler.initialize()
96
97        cooler.set_color(channel='logo', mode='blinking', speed='normal',
98            colors=iter([[0xff, 0x88, 0x44], [0xff, 0xff, 0xff], [0x00, 0x00, 0x00]]))
99
100        _begin, _pump, color_change, color_mode, color_end = usb_dev._sent_xfers
101        assert color_change == ('write', 1, [0x56, 0x03, 0xff, 0x88, 0x44, 0xff,
102                                             0xff, 0xff, 0x00, 0x00, 0x00])
103        assert color_mode == ('write', 1, [0x53, 0x0A])
104        assert color_end == ('write', 1, [0x58, 0x01])
105