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_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_read(self): with open(self.TEST_PROFILE_FILE) as f: p = self.manager.read_file(f) self.assertIsNotNone(p) expected = [ Output("LVDS1", mode="1366x768"), Output("DP1", "1920x1080", pos="1366x0"), Output("VGA1", "800x600", pos="3286x0", rotate="inverted", panning="800x1080", rate=80) ] self.assertOutputs(expected, p.outputs) self.assertDictEqual(Rule("d8578edf8458ce06fbc5bb76a58c5ca4", "1920x1200", "1920x1080").__dict__, p.rules["DP1"].__dict__) self.assertDictEqual(Rule().__dict__, p.rules["LVDS1"].__dict__)
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 read_file(self, profile_file_descriptor): try: result = json.load(profile_file_descriptor) rules = result.get('match') priority = int(result.get('priority', 100)) if rules: for k, v in rules.items(): # backward compatibility for match.mode if v.get('mode'): logger.warn( "%s\n\tmatch.mode is deprecated" "\n\tConsider changing to 'supports' or 'prefers'", profile_file_descriptor.name) v['supports'] = v['mode'] del v['mode'] rules[k] = Rule(**v) else: rules = {} primary = result['primary'] outputs_raw = result['outputs'] outputs = [] for name, mode_raw in outputs_raw.items(): output = Output(name, **mode_raw) outputs.append(output) name = os.path.basename(profile_file_descriptor.name) return Profile(name, outputs, rules, primary, priority) except (KeyError, ValueError): raise InvalidProfileException(profile_file_descriptor.name)
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_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_read(self): with open(self.TEST_PROFILE_FILE) as f: p = self.manager.read_file(f) self.assertIsNotNone(p) self.assertSetEqual( set([ Output("LVDS1", Geometry("1366x768"), True), Output("DP1", Geometry("1920x1080", pos="1366x0"), False), Output( "VGA1", Geometry("800x600", pos="3286x0", rotate="inverted", panning="800x1080", rate=80), False) ]), set(p.outputs)) self.assertEqual( Rule("d8578edf8458ce06fbc5bb76a58c5ca4", "1920x1200", "1920x1080"), p.rules["DP1"]) self.assertEqual(Rule(), p.rules["LVDS1"])
def profile_from_xrandr(self, xrandr_connections: list, name: str='profile'): outputs = [] rules = {} for c in xrandr_connections: if not c.connected or not c.is_active(): continue output = Output(c.name, c.current_geometry, c.primary) outputs.append(output) rule = Rule(md5(c.edid), c.preferred_mode, c.current_geometry.mode) rules[c.name] = rule logger.debug("Extracted %d outputs from %d xrandr connections", len(outputs), len(xrandr_connections)) return Profile(name, outputs, rules)
def profile_from_xrandr(self, xrandr_connections: list, name: str='profile'): outputs = [] rules = {} primary = None for c in xrandr_connections: display = c.display if not display or not c.is_active(): continue output = Output.fromconnection(c) if c.primary: primary = c.name outputs.append(output) rule = Rule(md5(display.edid), display.preferred_mode, display.mode) rules[c.name] = rule logger.debug("Extracted %d outputs from %d xrandr connections", len(outputs), len(xrandr_connections)) return Profile(name, outputs, rules, primary)
def profile_from_xrandr(self, xrandr_connections: list, profile_name: str = 'profile'): outputs = {} rules = {} primary = None for connection in xrandr_connections: output_name = connection.name display = connection.display if not display or not connection.is_active(): continue output = Output.fromconnection(connection) if connection.primary: primary = output_name outputs[output_name] = output rule = Rule(hash(display.edid), display.preferred_mode, display.mode) rules[output_name] = rule logger.debug("Extracted %d outputs from %d xrandr connections", len(outputs), len(xrandr_connections)) return Profile(profile_name, outputs, rules, primary)
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_profile_from_dict(self): # given data = [(Profile("no_rules", {"lvds1": Output("800x600")}, priority=100), { 'name': 'no_rules', 'outputs': { 'lvds1': { 'mode': '800x600', 'pos': '0x0', 'rotate': 'normal', 'panning': '0x0', 'scale': '1x1', } }, 'priority': 100 }), (Profile(name="with_rules", outputs={ "lvds1": Output("800x600", rate="60"), "vga1": Output("1024x768", pos="800x0", rate="75") }, match={"lvds1": Rule("edid", "800x600", "800x600")}, primary="lvds1", priority=100), { 'name': 'with_rules', 'match': { 'lvds1': { 'edid': 'edid', 'supports': '800x600', 'prefers': '800x600' } }, 'outputs': { 'lvds1': { 'mode': '800x600', 'pos': '0x0', 'rotate': 'normal', 'panning': '0x0', 'scale': '1x1', 'rate': '60' }, 'vga1': { 'mode': '1024x768', 'pos': '800x0', 'rotate': 'normal', 'panning': '0x0', 'scale': '1x1', 'rate': 75 } }, 'primary': 'lvds1', 'priority': 100 })] for expected_profile, dict in data: # when p = Profile.from_dict(dict) # then self.assertEqual(expected_profile, p) self.assertDictEqual(dict, p.to_dict())
class Test_ProfileMatcher(TestCase): logging.basicConfig() matcher = ProfileMatcher() HOME_MD5 = hashlib.md5("home".encode()).hexdigest() OFFICE_MD5 = hashlib.md5("office".encode()).hexdigest() profiles = [ Profile("default", [Output("LVDS1", "1366x768")], {"LVDS1": Rule()}), Profile("DP1_1920x1080", [Output("LVDS1", "1366x768"), Output("DP1", "1920x1080")], { "LVDS1": Rule(), "DP1": Rule(None, None, "1920x1080") }), Profile("DP1_1920x1200", [Output("LVDS1", "1366x768"), Output("DP1", "1920x1200")], { "LVDS1": Rule(), "DP1": Rule(None, "1920x1200", None) }), Profile("home", [Output("LVDS1", "1366x768"), Output("DP1", "1920x1080")], { "LVDS1": Rule(), "DP1": Rule(HOME_MD5) }), Profile("no_rule", [Output("LVDS1", "800x600")]), Profile("office", [Output("LVDS1", "1366x768"), Output("HDMI1", "1920x1080")], { "LVDS1": Rule(), "HDMI1": Rule(OFFICE_MD5) }) ] def test_find_best_default(self): outputs = [XrandrConnection("LVDS1", Display())] best = self.matcher.find_best(self.profiles, outputs) self.assertEqual(self.profiles[0], 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_edid_over_mode(self): outputs = [ XrandrConnection("LVDS1", Display()), XrandrConnection("DP1", Display(["1920x1080"], edid="home")) ] best = self.matcher.find_best(self.profiles, outputs) self.assertEqual(self.profiles[3], best) 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_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)