package v2 import ( "fmt" "testing" "github.com/stretchr/testify/require" ) var ( ncr1234 = MPID("NCR-1234") ncr5678 = MPID("NCR-5678") ncr30578 = MPID("NCR-30578") ncr22800 = MPID("NCR-22800") disabledDPMS = &DPMS{ Enabled: &dpmsDisabled, BlankTime: &zeroSeconds, StandbyTime: &zeroSeconds, SuspendTime: &zeroSeconds, OffTime: &zeroSeconds, } ) func TestApplyDefaults(t *testing.T) { tcs := []struct { displayConfig *DisplayConfig inputDevices []InputDeviceName expectedResult *DisplayConfig }{ { // test case 1: // default DPMS should be added displayConfig: &DisplayConfig{}, inputDevices: []InputDeviceName{}, expectedResult: &DisplayConfig{ DPMS: disabledDPMS, }, }, { // test case 2: // DPMS should be overwritten with defaults displayConfig: &DisplayConfig{ DPMS: &DPMS{ BlankTime: &thirtySeconds, StandbyTime: &sixtySeconds, OffTime: &oneTwentySeconds, }, }, inputDevices: []InputDeviceName{}, expectedResult: &DisplayConfig{ DPMS: disabledDPMS, }, }, { // test case 3: // default display should overwrite orientation // first display should be primary displayConfig: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr1234, Orientation: &NormalOrientation, }, { DisplayPort: "card0-HDMI2", MPID: &ncr5678, Orientation: &InvertedOrientation, }, }, }, inputDevices: []InputDeviceName{}, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr1234, Primary: &primary, Orientation: &NormalOrientation, }, { DisplayPort: "card0-HDMI2", MPID: &ncr5678, Primary: ¬Primary, Orientation: &NormalOrientation, }, }, DPMS: disabledDPMS, }, }, { // test case 4: // first display in layout should be primary displayConfig: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr1234, Primary: &primary, }, { DisplayPort: "card0-HDMI2", MPID: &ncr5678, Primary: ¬Primary, }, }, Layout: Layout{"card0-HDMI2", "card0-HDMI1"}, }, inputDevices: []InputDeviceName{}, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr1234, Primary: ¬Primary, Orientation: &NormalOrientation, }, { DisplayPort: "card0-HDMI2", MPID: &ncr5678, Primary: &primary, Orientation: &NormalOrientation, }, }, Layout: Layout{"card0-HDMI2", "card0-HDMI1"}, DPMS: disabledDPMS, }, }, { // test case 5: // match for single input device in list displayConfig: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &wnx32, Primary: &primary, Orientation: &InvertedOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card0-DP1", MPID: &ncr1234, Orientation: &LeftOrientation, Resolution: &Resolution{Width: 800, Height: 600}, }, }, Layout: Layout{"card0-DP1"}, }, inputDevices: []InputDeviceName{"Beijing IRTOUCH SYSTEMS Co.,LtD IRTOUCH InfraRed TouchScreen Mouse", "eGalax Inc. eGalaxTouch P80H84 0716 v00T3 k03_216", "Advanced Silicon S.A. CoolTouch® System"}, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &wnx32, Primary: ¬Primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ "Beijing IRTOUCH SYSTEMS Co.,LtD IRTOUCH InfraRed TouchScreen Mouse", }, }, { DisplayPort: "card0-DP1", MPID: &ncr1234, Primary: &primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 800, Height: 600}, }, }, Layout: Layout{"card0-DP1"}, DPMS: disabledDPMS, }, }, { // test case 6: // match for multiple input devices in list displayConfig: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr30578, Primary: &primary, Orientation: &InvertedOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card0-DP1", MPID: &ncr1234, Orientation: &LeftOrientation, Resolution: &Resolution{Width: 800, Height: 600}, }, }, Layout: Layout{"card0-DP1"}, }, inputDevices: []InputDeviceName{"Beijing IRTOUCH SYSTEMS Co.,LtD IRTOUCH InfraRed TouchScreen Mouse", "Advanced Silicon S.A. CoolTouch® System", "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146", "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146 Mouse"}, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr30578, Primary: ¬Primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146", "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146 Mouse", }, }, { DisplayPort: "card0-DP1", MPID: &ncr1234, Primary: &primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 800, Height: 600}, }, }, Layout: Layout{"card0-DP1"}, DPMS: disabledDPMS, }, }, { // test case 7: // no matches for devices in list displayConfig: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &wnx32, Primary: &primary, Orientation: &InvertedOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card0-DP1", MPID: &ncr1234, Orientation: &LeftOrientation, Resolution: &Resolution{Width: 800, Height: 600}, }, }, Layout: Layout{"card0-DP1"}, }, inputDevices: []InputDeviceName{"eGalax Inc. eGalaxTouch P80H84 0716 v00T3 k03_216", "Advanced Silicon S.A. CoolTouch® System"}, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &wnx32, Primary: ¬Primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{}, }, { DisplayPort: "card0-DP1", MPID: &ncr1234, Primary: &primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 800, Height: 600}, }, }, Layout: Layout{"card0-DP1"}, DPMS: disabledDPMS, }, }, { // test case 8: // display config already has input device before defaults are applied - no additional input added displayConfig: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &wnx32, Primary: &primary, Orientation: &InvertedOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{"Beijing IRTOUCH SYSTEMS Co.,LtD IRTOUCH InfraRed TouchScreen Mouse"}, }, { DisplayPort: "card0-DP1", MPID: &ncr1234, Orientation: &LeftOrientation, Resolution: &Resolution{Width: 800, Height: 600}, }, }, Layout: Layout{"card0-DP1"}, }, inputDevices: []InputDeviceName{"Beijing IRTOUCH SYSTEMS Co.,LtD IRTOUCH InfraRed TouchScreen Mouse", "eGalax Inc. eGalaxTouch P80H84 0716 v00T3 k03_216", "Advanced Silicon S.A. CoolTouch® System"}, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &wnx32, Primary: ¬Primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{"Beijing IRTOUCH SYSTEMS Co.,LtD IRTOUCH InfraRed TouchScreen Mouse"}, }, { DisplayPort: "card0-DP1", MPID: &ncr1234, Primary: &primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 800, Height: 600}, }, }, Layout: Layout{"card0-DP1"}, DPMS: disabledDPMS, }, }, { // test case 9: // display config already has input device before defaults are applied - additional inputs are added displayConfig: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr30578, Primary: &primary, Orientation: &InvertedOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146", }, }, { DisplayPort: "card0-DP1", MPID: &ncr1234, Orientation: &LeftOrientation, Resolution: &Resolution{Width: 800, Height: 600}, }, }, Layout: Layout{"card0-DP1"}, }, inputDevices: []InputDeviceName{"Beijing IRTOUCH SYSTEMS Co.,LtD IRTOUCH InfraRed TouchScreen Mouse", "Advanced Silicon S.A. CoolTouch® System", "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146", "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146 Mouse"}, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr30578, Primary: ¬Primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146", "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146 Mouse", }, }, { DisplayPort: "card0-DP1", MPID: &ncr1234, Primary: &primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 800, Height: 600}, }, }, Layout: Layout{"card0-DP1"}, DPMS: disabledDPMS, }, }, { // test case 10: // multiple displays and input devices - single match displayConfig: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr30578, Primary: &primary, Orientation: &InvertedOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card0-DP1", MPID: &wnx32, Orientation: &InvertedOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, }, }, Layout: Layout{"card0-DP1"}, }, inputDevices: []InputDeviceName{"Beijing IRTOUCH SYSTEMS Co.,LtD IRTOUCH InfraRed TouchScreen Mouse", "Advanced Silicon S.A. CoolTouch® System", "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146"}, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr30578, Primary: ¬Primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{"eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146"}, }, { DisplayPort: "card0-DP1", MPID: &wnx32, Primary: &primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{"Beijing IRTOUCH SYSTEMS Co.,LtD IRTOUCH InfraRed TouchScreen Mouse"}, }, }, Layout: Layout{"card0-DP1"}, DPMS: disabledDPMS, }, }, { // test case 11: // multiple displays and input devices - multiple matches displayConfig: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr30578, Primary: &primary, Orientation: &InvertedOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, }, { DisplayPort: "card0-DP1", MPID: &ncr22800, Orientation: &InvertedOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, }, }, Layout: Layout{"card0-DP1"}, }, inputDevices: []InputDeviceName{"Beijing IRTOUCH SYSTEMS Co.,LtD IRTOUCH InfraRed TouchScreen Mouse", "eGalax Inc. eGalaxTouch EXC3160-3079-08.00.00", "eGalax Inc. eGalaxTouch EXC3160-3079-08.00.00 UNKNOWN", "Advanced Silicon S.A. CoolTouch® System", "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146", "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146 Mouse"}, expectedResult: &DisplayConfig{ Displays: []Display{ { DisplayPort: "card0-HDMI1", MPID: &ncr30578, Primary: ¬Primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146", "eGalax Inc. eGalaxTouch P80H84 0490 v00_test2 k4.02.146 Mouse", }, }, { DisplayPort: "card0-DP1", MPID: &ncr22800, Primary: &primary, Orientation: &NormalOrientation, Resolution: &Resolution{Width: 1920, Height: 1080}, InputDeviceMappings: []InputDeviceName{ "eGalax Inc. eGalaxTouch EXC3160-3079-08.00.00", "eGalax Inc. eGalaxTouch EXC3160-3079-08.00.00 UNKNOWN", }, }, }, Layout: Layout{"card0-DP1"}, DPMS: disabledDPMS, }, }, } for idx, tc := range tcs { result, err := tc.displayConfig.GenerateDefaultDisplayConfig(Defaults, tc.inputDevices) 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)) } }