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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #include <scheduler/Fps.h>
21 
22 #include "FpsOps.h"
23 
24 namespace android {
25 
TEST(FpsTest,construct)26 TEST(FpsTest, construct) {
27     EXPECT_FALSE(Fps().isValid());
28 
29     EXPECT_FALSE((0_Hz).isValid());
30     EXPECT_TRUE((120_Hz).isValid());
31     EXPECT_TRUE((0.5_Hz).isValid());
32 
33     EXPECT_FALSE(Fps::fromPeriodNsecs(0).isValid());
34 
35     const Fps fps = Fps::fromPeriodNsecs(16'666'667);
36     EXPECT_TRUE(fps.isValid());
37     EXPECT_EQ(fps, 60_Hz);
38 }
39 
TEST(FpsTest,compare)40 TEST(FpsTest, compare) {
41     EXPECT_EQ(60_Hz, 60_Hz);
42     EXPECT_EQ(60_Hz, 59.9999_Hz);
43     EXPECT_EQ(60_Hz, 60.0001_Hz);
44 
45     EXPECT_LE(60_Hz, 60_Hz);
46     EXPECT_LE(60_Hz, 59.9999_Hz);
47     EXPECT_LE(60_Hz, 60.0001_Hz);
48 
49     EXPECT_GE(60_Hz, 60_Hz);
50     EXPECT_GE(60_Hz, 59.9999_Hz);
51     EXPECT_GE(60_Hz, 60.0001_Hz);
52 
53     // Fps with difference of 1 should be different.
54     EXPECT_NE(60_Hz, 61_Hz);
55     EXPECT_LT(60_Hz, 61_Hz);
56     EXPECT_GT(60_Hz, 59_Hz);
57 
58     // These are common refresh rates which should be different.
59     EXPECT_NE(60_Hz, 59.94_Hz);
60     EXPECT_GT(60_Hz, 59.94_Hz);
61     EXPECT_NE(30_Hz, 29.97_Hz);
62     EXPECT_GT(30_Hz, 29.97_Hz);
63 }
64 
TEST(FpsTest,getIntValue)65 TEST(FpsTest, getIntValue) {
66     EXPECT_EQ(30, (30.1_Hz).getIntValue());
67     EXPECT_EQ(31, (30.9_Hz).getIntValue());
68     EXPECT_EQ(31, (30.5_Hz).getIntValue());
69 }
70 
TEST(FpsTest,range)71 TEST(FpsTest, range) {
72     const auto fps = Fps::fromPeriodNsecs(16'666'665);
73 
74     EXPECT_TRUE((FpsRange{60.000004_Hz, 60.000004_Hz}.includes(fps)));
75     EXPECT_TRUE((FpsRange{59_Hz, 60.1_Hz}.includes(fps)));
76     EXPECT_FALSE((FpsRange{75_Hz, 90_Hz}.includes(fps)));
77     EXPECT_FALSE((FpsRange{60.0011_Hz, 90_Hz}.includes(fps)));
78     EXPECT_FALSE((FpsRange{50_Hz, 59.998_Hz}.includes(fps)));
79 }
80 
81 } // namespace android
82