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_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_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 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_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_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 _parse_display(self, lines: list): supported_modes = [] preferred_mode = None current_mode = None current_rate = None for mode_line in lines: mode_line = mode_line.strip() (mode, rate, extra) = self.CURRENT_MODE_REGEX.match(mode_line).groups() current = (extra.find("*") >= 0) preferred = (extra.find("+") >= 0) supported_modes.append(mode) if current: current_mode = mode current_rate = int(round(float(rate))) if preferred: preferred_mode = mode return Display(supported_modes, preferred_mode, current_mode, current_rate)
def test_find_best_default(self): outputs = [XrandrConnection("LVDS1", Display())] best = self.matcher.find_best(self.profiles, outputs) self.assertEqual(self.profiles[0], best)