def test_find_best_mode(self): outputs = [ XrandrConnection("LVDS1", Display()), XrandrConnection("DP1", Display(["1920x1080"], edid="office")) ] best = self.matcher.find_best(self.profiles, outputs) self.assertEqual(self.profiles[1], best)
def test_find_best_ambiguous(self): """ Test matching for similarly scored profiles """ edid = "office" edidhash = profile.md5(edid) connected_outputs = [ XrandrConnection("LVDS1", Display()), XrandrConnection("DP1", Display(["1920x1080"], edid=edid)) ] profile_outputs = [ Output("LVDS1", Viewport('1366x768'), True), Output("DP1", Viewport('1920x1080')) ] p1 = Profile("p1", profile_outputs, { "LVDS1": Rule(), "DP1": Rule(edidhash) }) p2 = Profile("p2", profile_outputs, { "LVDS1": Rule(), "DP1": Rule(edidhash) }) best = self.matcher.find_best([p1, p2], connected_outputs) self.assertEqual(p1, best)
def test_find_best_no_match(self): outputs = [ XrandrConnection("LVDS1", Display()), XrandrConnection("DP1", Display(["1280x1024"], edid="guest")) ] best = self.matcher.find_best(self.profiles, outputs) self.assertIsNone(best)
def test_should_match_profiles_and_list_descending(self): # given edid = "office" edidhash = hash(edid) profiles = [ profile("match4", {"LVDS1": Rule(), "DP1": Rule()}), profile("match1", {"LVDS1": Rule(), "DP1": Rule(edidhash)}), profile("match3", {"LVDS1": Rule(), "DP1": Rule(supports="1920x1080")}), profile("match2", {"LVDS1": Rule(), "DP1": Rule(prefers="1920x1080")}), profile("match5", {"LVDS1": Rule()}), profile("missing_output", {"LVDS1": Rule(), "DP1": Rule(), "HDMI1": Rule()}), profile("no_rules") ] outputs = [ XrandrConnection("LVDS1", Display()), XrandrConnection("DP1", Display(["1920x1080"], "1920x1080", edid=edid)) ] # when matches = self.matcher.match(profiles, outputs) # then self.assertEqual(5, len(matches)) self.assertEqual("match1", matches[0][1].name) self.assertEqual("match2", matches[1][1].name) self.assertEqual("match3", matches[2][1].name) self.assertEqual("match4", matches[3][1].name) self.assertEqual("match5", matches[4][1].name)
def test_find_best_default2(self): outputs = [ XrandrConnection("LVDS1", Display()), XrandrConnection("DP1", Display(["1280x1024"], edid="guest")) ] best = self.matcher.find_best(self.profiles, outputs) self.assertEqual(self.profiles[0], best)
def test_profile_from_xrandr(self): xc = [XrandrConnection("LVDS1", Display(), Viewport("1366x768"), False), XrandrConnection("DP1", Display(), Viewport("1920x1080", pos="1366x0"), True), XrandrConnection("HDMI1", None, Viewport("1366x768"), False)] p = self.manager.profile_from_xrandr(xc) self.assertEqual("profile", p.name) self.assertEqual(2, len(p.outputs))
def test_find_best_prefers_over_supports(self): outputs = [ XrandrConnection("LVDS1", Display()), XrandrConnection( "DP1", Display(["1920x1080", "1920x1200"], "1920x1200", edid="office")) ] best = self.matcher.find_best(self.profiles, outputs) self.assertEqual(self.profiles[2], best)
def _parse_xrandr_connection(self, item_lines: list): """ Creates XrandrConnection from lines returned by xrandr --query. Example: LVDS1 connected primary 1366x768+0+312 (normal left inverted right x axis y axis) 277mm x 156mm 1366x768 60.02*+ 1024x768 60.00 """ connection_info = item_lines[0] name, status, state = connection_info.split(' ', 2) if status != 'connected': # We are not connected, do not parse the rest. return XrandrConnection(name) # We are connected parse connected display. display = self._parse_display(item_lines[1:]) if not display.is_on(): # inactive output return XrandrConnection(name, display) parsed = self.OUTPUT_DETAILS_REGEX.match(state) if parsed is None: raise ParseException(name, status, state) primary = parsed.group('primary') is not None rotate = parsed.group('rotate') panning = parsed.group('panning') geometry = parsed.group('geometry') size, pos = self._parse_geometry(geometry) is_rotated = rotate in ['left', 'right'] if is_rotated: size = 'x'.join(size.split('x')[::-1]) scale = '1x1' if size != display.mode: dw, dh = map(lambda s: int(s), display.mode.split('x')) vw, vh = map(lambda s: int(s), size.split('x')) sw, sh = vw / dw, vh / dh if is_rotated: sw, sh = sh, sw scale = "{}x{}".format(sw, sh) viewport = Viewport(size, pos, rotate, panning, scale) return XrandrConnection(name, display, viewport, primary)
def test_should_pick_first_profile_if_same_score(self): # given edid = "office" edidhash = hash(edid) profiles = [ profile("p1", {"LVDS1": Rule(), "DP1": Rule(edidhash)}), profile("p2", {"LVDS1": Rule(), "DP1": Rule(edidhash)}) ] outputs = [ XrandrConnection("LVDS1", Display()), XrandrConnection("DP1", Display(["1920x1080"], edid=edid)) ] # when best = self.matcher.find_best(profiles, outputs) # then self.assertEqual(profiles[0], best)
def test_compose_mode_args_exact_line(self): xrandr = Xrandr() xrandr.EXECUTABLE = "stub" outputs = [Output("LVDS1", mode='1366x768')] p = Profile("default", outputs, primary="LVDS1") xrandr_connections = [ XrandrConnection("LVDS1"), XrandrConnection("HDMI1") ] command = xrandr._compose_mode_args(p, xrandr_connections) self.assertListEqual([ '--output', 'LVDS1', '--mode', '1366x768', '--pos', '0x0', '--rotate', 'normal', '--panning', '0x0', '--scale', '1x1', '--primary', '--output', 'HDMI1', '--off' ], command)
def test_priority(self): outputs_default = [ XrandrConnection("LVDS1", Display()), ] outputs_office = [ XrandrConnection("LVDS1", Display()), XrandrConnection("HDMI1", Display(["1920x1080"], edid="office")) ] profiles = [ Profile("default", [Output("LVDS1", "1366x768")], {"LVDS1": Rule()}, priority=50), Profile("office", [Output("HDMI1", "1920x1080")], {"HDMI1": Rule()}, priority=100) # default priority ] best_default = self.matcher.find_best(profiles, outputs_default) best_office = self.matcher.find_best(profiles, outputs_office) self.assertEqual(profiles[0], best_default) self.assertEqual(profiles[1], best_office)
def test_compose_mode_args(self): xrandr = Xrandr(":0", None) xrandr.EXECUTABLE = "stub" outputs = { "LVDS1": Output(mode='1366x768'), "DP1": Output(mode='1920x1080', pos='1366x0', scale='1.5x1.5', panning='1920x1080'), "VGA1": Output(mode='800x600', pos='0x768') } p = Profile("default", outputs, primary="LVDS1") xrandr_connections = [ XrandrConnection("HDMI1"), XrandrConnection("HDMI2") ] command = xrandr._compose_mode_args(p, xrandr_connections) num_of_outputs = len(outputs) + len(xrandr_connections) self.assertEqual(num_of_outputs, command.count(xrandr.OUTPUT_KEY)) self.assertEqual(len(outputs), command.count(xrandr.POS_KEY)) self.assertEqual(len(outputs), command.count(xrandr.MODE_KEY)) self.assertEqual(len(outputs), command.count(xrandr.PANNING_KEY)) self.assertEqual(len(outputs), command.count(xrandr.ROTATE_KEY)) self.assertEqual(len(outputs), command.count(xrandr.SCALE_KEY)) self.assertEqual(len(xrandr_connections), command.count(xrandr.OFF_KEY)) self.assertEqual(1, command.count(xrandr.PRIMARY_KEY)) self.assertEqual(1, command.count("LVDS1")) self.assertEqual(1, command.count("DP1")) self.assertEqual(1, command.count("VGA1")) self.assertEqual(1, command.count("HDMI1")) self.assertEqual(1, command.count("HDMI2"))
def test_should_pick_profile_with_higher_prio_if_same_score(self): # given expected = profile("highprio", {"LVDS1": Rule()}, prio=999) profiles = [profile("default", {"LVDS1": Rule()}), expected] outputs = [ XrandrConnection("LVDS1", Display()), ] # when best = self.matcher.find_best(profiles, outputs) # then self.assertEqual(expected, best)
def test_should_not_match_profile_without_rules(self): # given profiles = [ profile("no_rules1"), profile("no_rules2"), profile("no_rules3") ] outputs = [ XrandrConnection("LVDS1", Display(preferred_mode="1920x1080")) ] # when best = self.matcher.find_best(profiles, outputs) # then self.assertIsNone(best)
def test_should_prefer_rule_prefers_over_supports(self): # given expected = profile("with_prefers", {"LVDS1": Rule(prefers="1920x1080")}) profiles = [ expected, profile("with_supports", {"LVDS1": Rule(supports="1920x1080")}) ] outputs = [ XrandrConnection("LVDS1", Display(["1920x1080"], "1920x1080")) ] # when best = self.matcher.find_best(profiles, outputs) # then self.assertEqual(expected, best)
def test_should_match_profile_with_empty_rule(self): # given expected = profile("should_match", {"LVDS1": Rule()}) profiles = [ profile("different_output_in_rule", {"DP1": Rule(prefers="1920x1080")}), profile("no_rules"), expected ] outputs = [ XrandrConnection("LVDS1", Display(preferred_mode="1920x1080")) ] # when best = self.matcher.find_best(profiles, outputs) # then self.assertEqual(expected, best)
def test_should_prefer_edid_over_mode(self): # given edid = "some_edid" expected = profile("with_edid", {"LVDS1": Rule(hash(edid))}) profiles = [ expected, profile("with_supported_mode", {"LVDS1": Rule(supports="1920x1080")}), profile("with_preferred_mode", {"LVDS1": Rule(prefers="1920x1080")}) ] outputs = [ XrandrConnection("LVDS1", Display(["1920x1080"], "1920x1080", edid=edid)) ] # when best = self.matcher.find_best(profiles, outputs) # then self.assertEqual(expected, best)
def test_find_best_default(self): outputs = [XrandrConnection("LVDS1", Display())] best = self.matcher.find_best(self.profiles, outputs) self.assertEqual(self.profiles[0], best)