package v2 import ( "fmt" "testing" "github.com/stretchr/testify/require" ) var ( pln5376 = MPID("PLN-5376") wnx32 = MPID("WNX-32") tgc18464 = MPID("TGC-18464") eloTouchSolutionsUSB = InputDeviceName("Elo Touch Solutions Elo Touch Solutions Pcap USB Interface") eGalaxEXC3189 = InputDeviceName("eGalax Inc. eGalaxTouch EXC3189-2506-09.00.00.00") eGalaxEXC3189Mouse = InputDeviceName("eGalax Inc. eGalaxTouch EXC3189-2506-09.00.00.00 Mouse") thirtySeconds = 30 sixtySeconds = 60 ninetySeconds = 90 oneTwentySeconds = 120 dpmsEnabled = true primary = Primary(true) notPrimary = Primary(false) ) func TestMergeLayout(t *testing.T) { tcs := []struct { a Layout b Layout expectedResult Layout }{ { // test case 1: // merging empty into empty should set result as empty a: Layout{}, b: Layout{}, expectedResult: Layout{}, }, { // test case 2: // merging B into empty should remain empty a: Layout{}, b: Layout{"card0-DP1", "card0-HDMI1"}, expectedResult: Layout{}, }, { // test case 3: // merging empty into A should set result as A a: Layout{"card0-DP1", "card0-HDMI1"}, b: Layout{}, expectedResult: Layout{"card0-DP1", "card0-HDMI1"}, }, { // test case 4: // changing the order or displays should be reflected in result a: Layout{"card0-DP1", "card0-HDMI1"}, b: Layout{"card0-HDMI1", "card0-DP1"}, expectedResult: Layout{"card0-HDMI1", "card0-DP1"}, }, { // test case 5: // additional displays in A should be appended to B in result a: Layout{"card0-DP1", "card0-HDMI1", "card1-HDMI1"}, b: Layout{"card0-HDMI1", "card0-DP1"}, expectedResult: Layout{"card0-HDMI1", "card0-DP1", "card1-HDMI1"}, }, { // additional displays in B should not be added to result a: Layout{"card0-DP1", "card0-HDMI1"}, b: Layout{"card0-HDMI1", "card0-DP1", "card1-HDMI1"}, expectedResult: Layout{"card0-HDMI1", "card0-DP1"}, }, } for idx, tc := range tcs { result := tc.a.Merge(tc.b) require.Equal(t, tc.expectedResult, result, fmt.Sprintf("test case %d failed", idx+1)) } } func TestMergeDisplayConfig(t *testing.T) { tcs := []struct { a *DisplayConfig b *DisplayConfig expectedResult *DisplayConfig }{ { // test case 1: // merging empty into empty should set result as empty a: &DisplayConfig{}, b: &DisplayConfig{}, expectedResult: &DisplayConfig{}, }, { // test case 2: // merging B into empty should set result as empty a: &DisplayConfig{}, b: &DisplayConfig{ Layout: Layout{"card0-DP1", "card0-HDMI1"}, Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &InvertedOrientation, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, Orientation: &LeftOrientation, }, }, }, expectedResult: &DisplayConfig{}, }, { // test case 3: // merging empty into A should set result as A a: &DisplayConfig{ Layout: Layout{"card0-DP1", "card0-HDMI1"}, Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &LeftOrientation, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, Orientation: &LeftOrientation, }, }, DPMS: &DPMS{ BlankTime: &thirtySeconds, StandbyTime: &sixtySeconds, OffTime: &oneTwentySeconds, }, }, b: &DisplayConfig{}, expectedResult: &DisplayConfig{ Layout: Layout{"card0-DP1", "card0-HDMI1"}, Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &LeftOrientation, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, Orientation: &LeftOrientation, }, }, DPMS: &DPMS{ BlankTime: &thirtySeconds, StandbyTime: &sixtySeconds, OffTime: &oneTwentySeconds, }, }, }, { // test case 4: // updated attributes for display "card0-HDMI1" and DPMS in B should update in result a: &DisplayConfig{ Layout: Layout{"card0-DP1", "card0-HDMI1"}, Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &NormalOrientation, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, Orientation: &NormalOrientation, }, }, DPMS: &DPMS{ Enabled: &dpmsEnabled, BlankTime: &thirtySeconds, StandbyTime: &sixtySeconds, OffTime: &oneTwentySeconds, }, }, b: &DisplayConfig{ Layout: Layout{}, Displays: []Display{ { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &InvertedOrientation, }, }, DPMS: &DPMS{ Enabled: &dpmsDisabled, StandbyTime: &thirtySeconds, SuspendTime: &ninetySeconds, OffTime: &ninetySeconds, }, }, expectedResult: &DisplayConfig{ Layout: Layout{"card0-DP1", "card0-HDMI1"}, Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &NormalOrientation, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &InvertedOrientation, }, }, DPMS: &DPMS{ Enabled: &dpmsDisabled, BlankTime: &thirtySeconds, StandbyTime: &thirtySeconds, SuspendTime: &ninetySeconds, OffTime: &ninetySeconds, }, }, }, { // test case 5: // display "card1-HDMI2" in B but not in A should not be added into result a: &DisplayConfig{ Layout: Layout{"card0-DP1", "card0-HDMI1"}, Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &NormalOrientation, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, Orientation: &NormalOrientation, }, }, }, b: &DisplayConfig{ Layout: Layout{}, Displays: []Display{ { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &InvertedOrientation, }, { DisplayPort: "card1-HDMI2", Resolution: &Resolution{Width: 1920, Height: 1080}, }, }, DPMS: &DPMS{ Enabled: &dpmsEnabled, }, }, expectedResult: &DisplayConfig{ Layout: Layout{"card0-DP1", "card0-HDMI1"}, Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &NormalOrientation, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &InvertedOrientation, }, }, DPMS: &DPMS{ Enabled: &dpmsEnabled, }, }, }, { // test case 6: // updated layout in B should be set in result a: &DisplayConfig{ Layout: Layout{"card0-DP1", "card0-HDMI1"}, Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, }, }, }, b: &DisplayConfig{ Layout: Layout{"card0-HDMI1", "card0-DP1"}, }, expectedResult: &DisplayConfig{ Layout: Layout{"card0-HDMI1", "card0-DP1"}, Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, }, }, }, }, { // test case 7: // layout A with displays not in layout B and vice-versa should merge as expected // i.e. displays in B not present in A are removed and displays present in A not // present in B will be appended to the resulting layout. a: &DisplayConfig{ Layout: Layout{"card0-DP1", "card0-HDMI1", "VSC-3298"}, Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, }, }, }, b: &DisplayConfig{ Layout: Layout{"card0-HDMI1", "card0-DP1", "card1-HDMI2", "NCR-3455"}, Displays: []Display{ { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card1-HDMI2", Resolution: &Resolution{Width: 1920, Height: 1080}, }, }, }, expectedResult: &DisplayConfig{ Layout: Layout{"card0-HDMI1", "card0-DP1", "VSC-3298"}, Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, }, }, }, }, { // test case 8: // The Primary display in B should be respected in the result. a: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, Primary: &primary, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, }, { DisplayPort: "card1-HDMI2", Resolution: &Resolution{Width: 1920, Height: 1080}, Primary: ¬Primary, }, }, }, b: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, Primary: &primary, }, { DisplayPort: "card1-HDMI2", Orientation: &NormalOrientation, }, }, }, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, Primary: ¬Primary, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, Primary: &primary, }, { DisplayPort: "card1-HDMI2", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &NormalOrientation, Primary: ¬Primary, }, }, }, }, { // test case 9: // InputDevice mappings should be overwritten, not appended to. a: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ eloTouchSolutionsUSB, }, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, }, { DisplayPort: "card1-HDMI2", Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ eGalaxEXC3189, eGalaxEXC3189Mouse, }, }, }, }, b: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ eloTouchSolutionsUSB, }, }, { DisplayPort: "card1-HDMI2", Orientation: &NormalOrientation, InputDeviceMappings: []InputDeviceName{ eGalaxEXC3189, }, }, }, }, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ eloTouchSolutionsUSB, }, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ eloTouchSolutionsUSB, }, }, { DisplayPort: "card1-HDMI2", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &NormalOrientation, InputDeviceMappings: []InputDeviceName{ eGalaxEXC3189, }, }, }, }, }, { // test case 10: // Merged MPIDs should be ignored. a: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, MPID: &pln5376, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, MPID: &wnx32, }, { DisplayPort: "card1-HDMI2", Resolution: &Resolution{Width: 1920, Height: 1080}, }, }, }, b: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, MPID: &tgc18464, }, }, }, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, MPID: &pln5376, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, MPID: &wnx32, }, { DisplayPort: "card1-HDMI2", Resolution: &Resolution{Width: 1920, Height: 1080}, }, }, }, }, { // test case 11: // merging empty InputDeviceMappings should remove any existing InputDeviceMappings. a: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ eloTouchSolutionsUSB, }, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1260, Height: 720}, }, { DisplayPort: "card1-HDMI2", Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ eGalaxEXC3189, eGalaxEXC3189Mouse, }, }, }, }, b: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{}, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{}, }, { DisplayPort: "card1-HDMI2", Orientation: &NormalOrientation, InputDeviceMappings: []InputDeviceName{}, }, }, }, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-DP1", Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card0-HDMI1", Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card1-HDMI2", Resolution: &Resolution{Width: 1920, Height: 1080}, Orientation: &NormalOrientation, }, }, }, }, } for idx, tc := range tcs { result, err := tc.a.Merge(tc.b) require.NoError(t, err, fmt.Sprintf("test case %d failed", idx+1)) require.Equal(t, tc.expectedResult, result, fmt.Sprintf("test case %d failed", idx+1)) } }