...

Source file src/edge-infra.dev/pkg/sds/display/k8s/apis/v2/displayconfig_merge_test.go

Documentation: edge-infra.dev/pkg/sds/display/k8s/apis/v2

     1  package v2
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  var (
    11  	pln5376  = MPID("PLN-5376")
    12  	wnx32    = MPID("WNX-32")
    13  	tgc18464 = MPID("TGC-18464")
    14  
    15  	eloTouchSolutionsUSB = InputDeviceName("Elo Touch Solutions Elo Touch Solutions Pcap USB Interface")
    16  	eGalaxEXC3189        = InputDeviceName("eGalax Inc. eGalaxTouch EXC3189-2506-09.00.00.00")
    17  	eGalaxEXC3189Mouse   = InputDeviceName("eGalax Inc. eGalaxTouch EXC3189-2506-09.00.00.00 Mouse")
    18  
    19  	thirtySeconds    = 30
    20  	sixtySeconds     = 60
    21  	ninetySeconds    = 90
    22  	oneTwentySeconds = 120
    23  
    24  	dpmsEnabled = true
    25  
    26  	primary    = Primary(true)
    27  	notPrimary = Primary(false)
    28  )
    29  
    30  func TestMergeLayout(t *testing.T) {
    31  	tcs := []struct {
    32  		a              Layout
    33  		b              Layout
    34  		expectedResult Layout
    35  	}{
    36  		{
    37  			// test case 1:
    38  			// merging empty into empty should set result as empty
    39  			a:              Layout{},
    40  			b:              Layout{},
    41  			expectedResult: Layout{},
    42  		},
    43  		{
    44  			// test case 2:
    45  			// merging B into empty should remain empty
    46  			a:              Layout{},
    47  			b:              Layout{"card0-DP1", "card0-HDMI1"},
    48  			expectedResult: Layout{},
    49  		},
    50  		{
    51  			// test case 3:
    52  			// merging empty into A should set result as A
    53  			a:              Layout{"card0-DP1", "card0-HDMI1"},
    54  			b:              Layout{},
    55  			expectedResult: Layout{"card0-DP1", "card0-HDMI1"},
    56  		},
    57  		{
    58  			// test case 4:
    59  			// changing the order or displays should be reflected in result
    60  			a:              Layout{"card0-DP1", "card0-HDMI1"},
    61  			b:              Layout{"card0-HDMI1", "card0-DP1"},
    62  			expectedResult: Layout{"card0-HDMI1", "card0-DP1"},
    63  		},
    64  		{
    65  			// test case 5:
    66  			// additional displays in A should be appended to B in result
    67  			a:              Layout{"card0-DP1", "card0-HDMI1", "card1-HDMI1"},
    68  			b:              Layout{"card0-HDMI1", "card0-DP1"},
    69  			expectedResult: Layout{"card0-HDMI1", "card0-DP1", "card1-HDMI1"},
    70  		},
    71  		{
    72  			// additional displays in B should not be added to result
    73  			a:              Layout{"card0-DP1", "card0-HDMI1"},
    74  			b:              Layout{"card0-HDMI1", "card0-DP1", "card1-HDMI1"},
    75  			expectedResult: Layout{"card0-HDMI1", "card0-DP1"},
    76  		},
    77  	}
    78  	for idx, tc := range tcs {
    79  		result := tc.a.Merge(tc.b)
    80  		require.Equal(t, tc.expectedResult, result, fmt.Sprintf("test case %d failed", idx+1))
    81  	}
    82  }
    83  
    84  func TestMergeDisplayConfig(t *testing.T) {
    85  	tcs := []struct {
    86  		a              *DisplayConfig
    87  		b              *DisplayConfig
    88  		expectedResult *DisplayConfig
    89  	}{
    90  		{ // test case 1:
    91  			// merging empty into empty should set result as empty
    92  			a:              &DisplayConfig{},
    93  			b:              &DisplayConfig{},
    94  			expectedResult: &DisplayConfig{},
    95  		},
    96  		{
    97  			// test case 2:
    98  			// merging B into empty should set result as empty
    99  			a: &DisplayConfig{},
   100  			b: &DisplayConfig{
   101  				Layout: Layout{"card0-DP1", "card0-HDMI1"},
   102  				Displays: []Display{
   103  					{
   104  						DisplayPort: "card0-DP1",
   105  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   106  						Orientation: &InvertedOrientation,
   107  					},
   108  					{
   109  						DisplayPort: "card0-HDMI1",
   110  						Resolution:  &Resolution{Width: 1260, Height: 720},
   111  						Orientation: &LeftOrientation,
   112  					},
   113  				},
   114  			},
   115  			expectedResult: &DisplayConfig{},
   116  		},
   117  		{
   118  			// test case 3:
   119  			// merging empty into A should set result as A
   120  			a: &DisplayConfig{
   121  				Layout: Layout{"card0-DP1", "card0-HDMI1"},
   122  				Displays: []Display{
   123  					{
   124  						DisplayPort: "card0-DP1",
   125  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   126  						Orientation: &LeftOrientation,
   127  					},
   128  					{
   129  						DisplayPort: "card0-HDMI1",
   130  						Resolution:  &Resolution{Width: 1260, Height: 720},
   131  						Orientation: &LeftOrientation,
   132  					},
   133  				},
   134  				DPMS: &DPMS{
   135  					BlankTime:   &thirtySeconds,
   136  					StandbyTime: &sixtySeconds,
   137  					OffTime:     &oneTwentySeconds,
   138  				},
   139  			},
   140  			b: &DisplayConfig{},
   141  			expectedResult: &DisplayConfig{
   142  				Layout: Layout{"card0-DP1", "card0-HDMI1"},
   143  				Displays: []Display{
   144  					{
   145  						DisplayPort: "card0-DP1",
   146  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   147  						Orientation: &LeftOrientation,
   148  					},
   149  					{
   150  						DisplayPort: "card0-HDMI1",
   151  						Resolution:  &Resolution{Width: 1260, Height: 720},
   152  						Orientation: &LeftOrientation,
   153  					},
   154  				},
   155  				DPMS: &DPMS{
   156  					BlankTime:   &thirtySeconds,
   157  					StandbyTime: &sixtySeconds,
   158  					OffTime:     &oneTwentySeconds,
   159  				},
   160  			},
   161  		},
   162  
   163  		{
   164  			// test case 4:
   165  			// updated attributes for display "card0-HDMI1" and DPMS in B should update in result
   166  			a: &DisplayConfig{
   167  				Layout: Layout{"card0-DP1", "card0-HDMI1"},
   168  				Displays: []Display{
   169  					{
   170  						DisplayPort: "card0-DP1",
   171  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   172  						Orientation: &NormalOrientation,
   173  					},
   174  					{
   175  						DisplayPort: "card0-HDMI1",
   176  						Resolution:  &Resolution{Width: 1260, Height: 720},
   177  						Orientation: &NormalOrientation,
   178  					},
   179  				},
   180  				DPMS: &DPMS{
   181  					Enabled:     &dpmsEnabled,
   182  					BlankTime:   &thirtySeconds,
   183  					StandbyTime: &sixtySeconds,
   184  					OffTime:     &oneTwentySeconds,
   185  				},
   186  			},
   187  			b: &DisplayConfig{
   188  				Layout: Layout{},
   189  				Displays: []Display{
   190  					{
   191  						DisplayPort: "card0-HDMI1",
   192  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   193  						Orientation: &InvertedOrientation,
   194  					},
   195  				},
   196  				DPMS: &DPMS{
   197  					Enabled:     &dpmsDisabled,
   198  					StandbyTime: &thirtySeconds,
   199  					SuspendTime: &ninetySeconds,
   200  					OffTime:     &ninetySeconds,
   201  				},
   202  			},
   203  			expectedResult: &DisplayConfig{
   204  				Layout: Layout{"card0-DP1", "card0-HDMI1"},
   205  				Displays: []Display{
   206  					{
   207  						DisplayPort: "card0-DP1",
   208  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   209  						Orientation: &NormalOrientation,
   210  					},
   211  					{
   212  						DisplayPort: "card0-HDMI1",
   213  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   214  						Orientation: &InvertedOrientation,
   215  					},
   216  				},
   217  				DPMS: &DPMS{
   218  					Enabled:     &dpmsDisabled,
   219  					BlankTime:   &thirtySeconds,
   220  					StandbyTime: &thirtySeconds,
   221  					SuspendTime: &ninetySeconds,
   222  					OffTime:     &ninetySeconds,
   223  				},
   224  			},
   225  		},
   226  		{
   227  			// test case 5:
   228  			// display "card1-HDMI2" in B but not in A should not be added into result
   229  			a: &DisplayConfig{
   230  				Layout: Layout{"card0-DP1", "card0-HDMI1"},
   231  				Displays: []Display{
   232  					{
   233  						DisplayPort: "card0-DP1",
   234  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   235  						Orientation: &NormalOrientation,
   236  					},
   237  					{
   238  						DisplayPort: "card0-HDMI1",
   239  						Resolution:  &Resolution{Width: 1260, Height: 720},
   240  						Orientation: &NormalOrientation,
   241  					},
   242  				},
   243  			},
   244  			b: &DisplayConfig{
   245  				Layout: Layout{},
   246  				Displays: []Display{
   247  					{
   248  						DisplayPort: "card0-HDMI1",
   249  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   250  						Orientation: &InvertedOrientation,
   251  					},
   252  					{
   253  						DisplayPort: "card1-HDMI2",
   254  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   255  					},
   256  				},
   257  				DPMS: &DPMS{
   258  					Enabled: &dpmsEnabled,
   259  				},
   260  			},
   261  			expectedResult: &DisplayConfig{
   262  				Layout: Layout{"card0-DP1", "card0-HDMI1"},
   263  				Displays: []Display{
   264  					{
   265  						DisplayPort: "card0-DP1",
   266  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   267  						Orientation: &NormalOrientation,
   268  					},
   269  					{
   270  						DisplayPort: "card0-HDMI1",
   271  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   272  						Orientation: &InvertedOrientation,
   273  					},
   274  				},
   275  				DPMS: &DPMS{
   276  					Enabled: &dpmsEnabled,
   277  				},
   278  			},
   279  		},
   280  		{
   281  			// test case 6:
   282  			// updated layout in B should be set in result
   283  			a: &DisplayConfig{
   284  				Layout: Layout{"card0-DP1", "card0-HDMI1"},
   285  				Displays: []Display{
   286  					{
   287  						DisplayPort: "card0-DP1",
   288  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   289  					},
   290  					{
   291  						DisplayPort: "card0-HDMI1",
   292  						Resolution:  &Resolution{Width: 1260, Height: 720},
   293  					},
   294  				},
   295  			},
   296  			b: &DisplayConfig{
   297  				Layout: Layout{"card0-HDMI1", "card0-DP1"},
   298  			},
   299  			expectedResult: &DisplayConfig{
   300  				Layout: Layout{"card0-HDMI1", "card0-DP1"},
   301  				Displays: []Display{
   302  					{
   303  						DisplayPort: "card0-DP1",
   304  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   305  					},
   306  					{
   307  						DisplayPort: "card0-HDMI1",
   308  						Resolution:  &Resolution{Width: 1260, Height: 720},
   309  					},
   310  				},
   311  			},
   312  		},
   313  		{
   314  			// test case 7:
   315  			// layout A with displays not in layout B and vice-versa should merge as expected
   316  			// i.e. displays in B not present in A are removed and displays present in A not
   317  			// present in B will be appended to the resulting layout.
   318  			a: &DisplayConfig{
   319  				Layout: Layout{"card0-DP1", "card0-HDMI1", "VSC-3298"},
   320  				Displays: []Display{
   321  					{
   322  						DisplayPort: "card0-DP1",
   323  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   324  					},
   325  					{
   326  						DisplayPort: "card0-HDMI1",
   327  						Resolution:  &Resolution{Width: 1260, Height: 720},
   328  					},
   329  				},
   330  			},
   331  			b: &DisplayConfig{
   332  				Layout: Layout{"card0-HDMI1", "card0-DP1", "card1-HDMI2", "NCR-3455"},
   333  				Displays: []Display{
   334  					{
   335  						DisplayPort: "card0-HDMI1",
   336  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   337  					},
   338  					{
   339  						DisplayPort: "card1-HDMI2",
   340  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   341  					},
   342  				},
   343  			},
   344  			expectedResult: &DisplayConfig{
   345  				Layout: Layout{"card0-HDMI1", "card0-DP1", "VSC-3298"},
   346  				Displays: []Display{
   347  					{
   348  						DisplayPort: "card0-DP1",
   349  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   350  					},
   351  					{
   352  						DisplayPort: "card0-HDMI1",
   353  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   354  					},
   355  				},
   356  			},
   357  		},
   358  		{
   359  			// test case 8:
   360  			// The Primary display in B should be respected in the result.
   361  			a: &DisplayConfig{
   362  				Displays: []Display{
   363  					{
   364  						DisplayPort: "card0-DP1",
   365  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   366  						Primary:     &primary,
   367  					},
   368  					{
   369  						DisplayPort: "card0-HDMI1",
   370  						Resolution:  &Resolution{Width: 1260, Height: 720},
   371  					},
   372  					{
   373  						DisplayPort: "card1-HDMI2",
   374  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   375  						Primary:     &notPrimary,
   376  					},
   377  				},
   378  			},
   379  			b: &DisplayConfig{
   380  				Displays: []Display{
   381  					{
   382  						DisplayPort: "card0-HDMI1",
   383  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   384  						Primary:     &primary,
   385  					},
   386  					{
   387  						DisplayPort: "card1-HDMI2",
   388  						Orientation: &NormalOrientation,
   389  					},
   390  				},
   391  			},
   392  			expectedResult: &DisplayConfig{
   393  				Displays: []Display{
   394  					{
   395  						DisplayPort: "card0-DP1",
   396  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   397  						Primary:     &notPrimary,
   398  					},
   399  					{
   400  						DisplayPort: "card0-HDMI1",
   401  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   402  						Primary:     &primary,
   403  					},
   404  					{
   405  						DisplayPort: "card1-HDMI2",
   406  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   407  						Orientation: &NormalOrientation,
   408  						Primary:     &notPrimary,
   409  					},
   410  				},
   411  			},
   412  		},
   413  		{
   414  			// test case 9:
   415  			// InputDevice mappings should be overwritten, not appended to.
   416  			a: &DisplayConfig{
   417  				Displays: []Display{
   418  					{
   419  						DisplayPort: "card0-DP1",
   420  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   421  						InputDeviceMappings: []InputDeviceName{
   422  							eloTouchSolutionsUSB,
   423  						},
   424  					},
   425  					{
   426  						DisplayPort: "card0-HDMI1",
   427  						Resolution:  &Resolution{Width: 1260, Height: 720},
   428  					},
   429  					{
   430  						DisplayPort: "card1-HDMI2",
   431  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   432  						InputDeviceMappings: []InputDeviceName{
   433  							eGalaxEXC3189,
   434  							eGalaxEXC3189Mouse,
   435  						},
   436  					},
   437  				},
   438  			},
   439  			b: &DisplayConfig{
   440  				Displays: []Display{
   441  					{
   442  						DisplayPort: "card0-HDMI1",
   443  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   444  						InputDeviceMappings: []InputDeviceName{
   445  							eloTouchSolutionsUSB,
   446  						},
   447  					},
   448  					{
   449  						DisplayPort: "card1-HDMI2",
   450  						Orientation: &NormalOrientation,
   451  						InputDeviceMappings: []InputDeviceName{
   452  							eGalaxEXC3189,
   453  						},
   454  					},
   455  				},
   456  			},
   457  			expectedResult: &DisplayConfig{
   458  				Displays: []Display{
   459  					{
   460  						DisplayPort: "card0-DP1",
   461  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   462  						InputDeviceMappings: []InputDeviceName{
   463  							eloTouchSolutionsUSB,
   464  						},
   465  					},
   466  					{
   467  						DisplayPort: "card0-HDMI1",
   468  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   469  						InputDeviceMappings: []InputDeviceName{
   470  							eloTouchSolutionsUSB,
   471  						},
   472  					},
   473  					{
   474  						DisplayPort: "card1-HDMI2",
   475  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   476  						Orientation: &NormalOrientation,
   477  						InputDeviceMappings: []InputDeviceName{
   478  							eGalaxEXC3189,
   479  						},
   480  					},
   481  				},
   482  			},
   483  		},
   484  		{
   485  			// test case 10:
   486  			// Merged MPIDs should be ignored.
   487  			a: &DisplayConfig{
   488  				Displays: []Display{
   489  					{
   490  						DisplayPort: "card0-DP1",
   491  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   492  						MPID:        &pln5376,
   493  					},
   494  					{
   495  						DisplayPort: "card0-HDMI1",
   496  						Resolution:  &Resolution{Width: 1260, Height: 720},
   497  						MPID:        &wnx32,
   498  					},
   499  					{
   500  						DisplayPort: "card1-HDMI2",
   501  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   502  					},
   503  				},
   504  			},
   505  			b: &DisplayConfig{
   506  				Displays: []Display{
   507  					{
   508  						DisplayPort: "card0-HDMI1",
   509  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   510  						MPID:        &tgc18464,
   511  					},
   512  				},
   513  			},
   514  			expectedResult: &DisplayConfig{
   515  				Displays: []Display{
   516  					{
   517  						DisplayPort: "card0-DP1",
   518  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   519  						MPID:        &pln5376,
   520  					},
   521  					{
   522  						DisplayPort: "card0-HDMI1",
   523  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   524  						MPID:        &wnx32,
   525  					},
   526  					{
   527  						DisplayPort: "card1-HDMI2",
   528  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   529  					},
   530  				},
   531  			},
   532  		},
   533  		{
   534  			// test case 11:
   535  			// merging empty InputDeviceMappings should remove any existing InputDeviceMappings.
   536  			a: &DisplayConfig{
   537  				Displays: []Display{
   538  					{
   539  						DisplayPort: "card0-DP1",
   540  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   541  						InputDeviceMappings: []InputDeviceName{
   542  							eloTouchSolutionsUSB,
   543  						},
   544  					},
   545  					{
   546  						DisplayPort: "card0-HDMI1",
   547  						Resolution:  &Resolution{Width: 1260, Height: 720},
   548  					},
   549  					{
   550  						DisplayPort: "card1-HDMI2",
   551  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   552  						InputDeviceMappings: []InputDeviceName{
   553  							eGalaxEXC3189,
   554  							eGalaxEXC3189Mouse,
   555  						},
   556  					},
   557  				},
   558  			},
   559  			b: &DisplayConfig{
   560  				Displays: []Display{
   561  					{
   562  						DisplayPort:         "card0-DP1",
   563  						Resolution:          &Resolution{Width: 1920, Height: 1080},
   564  						InputDeviceMappings: []InputDeviceName{},
   565  					},
   566  					{
   567  						DisplayPort:         "card0-HDMI1",
   568  						Resolution:          &Resolution{Width: 1920, Height: 1080},
   569  						InputDeviceMappings: []InputDeviceName{},
   570  					},
   571  					{
   572  						DisplayPort:         "card1-HDMI2",
   573  						Orientation:         &NormalOrientation,
   574  						InputDeviceMappings: []InputDeviceName{},
   575  					},
   576  				},
   577  			},
   578  			expectedResult: &DisplayConfig{
   579  				Displays: []Display{
   580  					{
   581  						DisplayPort: "card0-DP1",
   582  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   583  					},
   584  					{
   585  						DisplayPort: "card0-HDMI1",
   586  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   587  					},
   588  					{
   589  						DisplayPort: "card1-HDMI2",
   590  						Resolution:  &Resolution{Width: 1920, Height: 1080},
   591  						Orientation: &NormalOrientation,
   592  					},
   593  				},
   594  			},
   595  		},
   596  	}
   597  	for idx, tc := range tcs {
   598  		result, err := tc.a.Merge(tc.b)
   599  		require.NoError(t, err, fmt.Sprintf("test case %d failed", idx+1))
   600  		require.Equal(t, tc.expectedResult, result, fmt.Sprintf("test case %d failed", idx+1))
   601  	}
   602  }
   603  

View as plain text