File: display_manager_test_util_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 4,411 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/display/manager/util/display_manager_test_util.h"

#include <stdint.h>

#include "base/test/scoped_feature_list.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/display/display_features.h"

namespace display {

namespace {

// Use larger than max int to catch overflow early. This should be equal to the
// same name constant in display_util.cc.
constexpr int64_t kSynthesizedDisplayIdStart = 2200000000LL;

}  // namespace

TEST(DisplayManagerTestUtilTest, TestResetDisplayIdForTest) {
  ResetDisplayIdForTest();

  const int64_t kFirstId = GetASynthesizedDisplayId();
  EXPECT_EQ(kSynthesizedDisplayIdStart, kFirstId);
  const int64_t kFirstEdidConnectorIndex =
      GetNextSynthesizedEdidDisplayConnectorIndex();
  EXPECT_EQ(1L, kFirstEdidConnectorIndex);

  // Synthesize a hundred different display IDs and EDID-based display connector
  // indices.
  for (size_t i = 0; i < 100; ++i) {
    GetASynthesizedDisplayId();
    GetNextSynthesizedEdidDisplayConnectorIndex();
  }
  ResetDisplayIdForTest();

  EXPECT_EQ(kFirstId, GetASynthesizedDisplayId());
  EXPECT_EQ(kFirstEdidConnectorIndex,
            GetNextSynthesizedEdidDisplayConnectorIndex());
}

TEST(DisplayManagerTestUtilTest, TestGetASynthesizedDisplayId) {
  ResetDisplayIdForTest();

  // Test port-based synthesized IDs, which increment by 1 after the initial
  // value of kSynthesizedDisplayIdStart.
  EXPECT_EQ(kSynthesizedDisplayIdStart, GetASynthesizedDisplayId());
  EXPECT_EQ(2200000257, GetASynthesizedDisplayId());
  EXPECT_EQ(2200000258, GetASynthesizedDisplayId());
  EXPECT_EQ(2200000259, GetASynthesizedDisplayId());
  EXPECT_EQ(2200000260, GetASynthesizedDisplayId());
  EXPECT_EQ(2200000261, GetASynthesizedDisplayId());
  EXPECT_EQ(2200000262, GetASynthesizedDisplayId());

  // Test EDID-based synthesized IDs, which are incremented by 0x100 after the
  // initial value of kSynthesizedDisplayIdStart.
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(
      display::features::kEnableEdidBasedDisplayIds);
  ResetDisplayIdForTest();

  EXPECT_EQ(kSynthesizedDisplayIdStart, GetASynthesizedDisplayId());
  EXPECT_EQ(2200000256, GetASynthesizedDisplayId());
  EXPECT_EQ(2200000512, GetASynthesizedDisplayId());
  EXPECT_EQ(2200000768, GetASynthesizedDisplayId());
  EXPECT_EQ(2200001024, GetASynthesizedDisplayId());
  EXPECT_EQ(2200001280, GetASynthesizedDisplayId());
  EXPECT_EQ(2200001536, GetASynthesizedDisplayId());
}

TEST(DisplayManagerTestUtilTest, TestSynthesizeDisplayIdFromSeed) {
  constexpr int64_t kFirstIdBase = 1000000000;
  constexpr int64_t kFirstIdConnectorIndex = 1;
  constexpr int64_t kFirstId = kFirstIdBase + kFirstIdConnectorIndex;

  const int64_t kSecondId = SynthesizeDisplayIdFromSeed(kFirstId);
  EXPECT_EQ(1000000002, kSecondId);

  // Connector index is stored in the first 8 bits for port-base display IDs.
  const int64_t kSecondIdConnectorIndex = kSecondId & 0xFF;
  EXPECT_EQ(kFirstIdConnectorIndex + 1, kSecondIdConnectorIndex);

  const int64_t kSecondIdBase =
      static_cast<int64_t>(~static_cast<uint64_t>(0xFF) & kSecondId);
  EXPECT_EQ(kFirstIdBase, kSecondIdBase);

  // Make sure the returned synthesized ID doesn't change for the same seed.
  for (size_t i = 0; i < 10; ++i) {
    EXPECT_EQ(kSecondId, SynthesizeDisplayIdFromSeed(kFirstId));
  }

  // Test when the seed is equal to initial synthesized display ID
  // kSynthesizedDisplayIdStart.
  constexpr int64_t kExpectedIdAfterInitialValue =
      kSynthesizedDisplayIdStart + 0x101;
  EXPECT_EQ(kExpectedIdAfterInitialValue,
            SynthesizeDisplayIdFromSeed(kSynthesizedDisplayIdStart));
}

TEST(DisplayManagerTestUtilTest, TestProduceAlternativeSchemeIdForId) {
  EXPECT_EQ(1200000000,
            ProduceAlternativeSchemeIdForId(kSynthesizedDisplayIdStart));
  EXPECT_EQ(1200000257, ProduceAlternativeSchemeIdForId(2200000257));
  EXPECT_EQ(1000000000, ProduceAlternativeSchemeIdForId(2000000000));

  EXPECT_EQ(234, ProduceAlternativeSchemeIdForId(1234));
  EXPECT_EQ(111, ProduceAlternativeSchemeIdForId(1111));
  EXPECT_EQ(100, ProduceAlternativeSchemeIdForId(1100));

  EXPECT_EQ(20, ProduceAlternativeSchemeIdForId(10));
  EXPECT_EQ(17, ProduceAlternativeSchemeIdForId(7));
}

}  // namespace display