• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 #include <ui/DisplayId.h>
19 
20 #include <algorithm>
21 #include <iterator>
22 #include <vector>
23 
24 #include "DisplayIdGenerator.h"
25 
26 namespace android {
27 
28 template <typename Id>
testGenerateId()29 void testGenerateId() {
30     DisplayIdGenerator<Id> generator;
31 
32     std::vector<std::optional<Id>> ids;
33     std::generate_n(std::back_inserter(ids), 10, [&] { return generator.generateId(); });
34 
35     // All IDs should be different.
36     for (auto it = ids.begin(); it != ids.end(); ++it) {
37         EXPECT_TRUE(*it);
38 
39         for (auto dup = it + 1; dup != ids.end(); ++dup) {
40             EXPECT_NE(*it, *dup);
41         }
42     }
43 }
44 
TEST(DisplayIdGeneratorTest,generateGpuVirtualDisplayId)45 TEST(DisplayIdGeneratorTest, generateGpuVirtualDisplayId) {
46     testGenerateId<GpuVirtualDisplayId>();
47 }
48 
TEST(DisplayIdGeneratorTest,generateHalVirtualDisplayId)49 TEST(DisplayIdGeneratorTest, generateHalVirtualDisplayId) {
50     testGenerateId<HalVirtualDisplayId>();
51 }
52 
TEST(DisplayIdGeneratorTest,releaseId)53 TEST(DisplayIdGeneratorTest, releaseId) {
54     constexpr size_t kMaxIdsCount = 5;
55     DisplayIdGenerator<GpuVirtualDisplayId> generator(kMaxIdsCount);
56 
57     const auto id = generator.generateId();
58     EXPECT_TRUE(id);
59 
60     for (size_t i = 1; i < kMaxIdsCount; i++) {
61         EXPECT_TRUE(generator.generateId());
62     }
63 
64     EXPECT_FALSE(generator.generateId());
65 
66     generator.releaseId(*id);
67     EXPECT_TRUE(generator.generateId());
68 }
69 
TEST(DisplayIdGeneratorTest,maxIdsCount)70 TEST(DisplayIdGeneratorTest, maxIdsCount) {
71     constexpr size_t kMaxIdsCount = 5;
72     DisplayIdGenerator<GpuVirtualDisplayId> generator(kMaxIdsCount);
73 
74     for (size_t i = 0; i < kMaxIdsCount; i++) {
75         EXPECT_TRUE(generator.generateId());
76     }
77 
78     EXPECT_FALSE(generator.generateId());
79 }
80 
81 } // namespace android
82