1 /*
2 * Copyright (C) 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 <input/Input.h>
19 #include <ios>
20 #include <memory>
21 #include <unordered_set>
22
23 namespace android::test {
24
25 class IdGeneratorTest : public testing::TestWithParam<IdGenerator::Source> {
26 protected:
SetUp()27 void SetUp() override { mGenerator.reset(new IdGenerator(GetParam())); }
28
29 std::unique_ptr<IdGenerator> mGenerator;
30 };
31
TEST_P(IdGeneratorTest,GenerateRandomNumber)32 TEST_P(IdGeneratorTest, GenerateRandomNumber) {
33 for (int i = 0; i < 500; ++i) {
34 mGenerator->nextId();
35 }
36 }
37
TEST_P(IdGeneratorTest,GenerateRandomNumberWithProperFlag)38 TEST_P(IdGeneratorTest, GenerateRandomNumberWithProperFlag) {
39 for (int i = 0; i < 500; ++i) {
40 int32_t id = mGenerator->nextId();
41 IdGenerator::Source source = IdGenerator::getSource(id);
42 EXPECT_EQ(source, GetParam())
43 << std::hex << "Generator generated a value with wrong source. Value: 0x" << id
44 << " Source: 0x" << static_cast<int32_t>(source);
45 }
46 }
47
48 INSTANTIATE_TEST_SUITE_P(SourceInstantiation, IdGeneratorTest,
49 testing::Values(IdGenerator::Source::INPUT_READER,
50 IdGenerator::Source::INPUT_DISPATCHER,
51 IdGenerator::Source::OTHER));
52 } // namespace android::test
53