1 /*
2  * Copyright (C) 2023 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 <stdint.h>
18 
19 #include <HeifCleanAperture.h>
20 #include <gtest/gtest.h>
21 
22 namespace {
23 
24 using android::heif::CleanAperture;
25 using android::heif::convertCleanApertureToRect;
26 using android::heif::Fraction;
27 
28 struct InvalidClapPropertyParam {
29     uint32_t width;
30     uint32_t height;
31     CleanAperture clap;
32 };
33 
34 const InvalidClapPropertyParam kInvalidClapPropertyTestParams[] = {
35         // Zero or negative denominators.
36         {120, 160, {Fraction(96, 0), Fraction(132, 1), Fraction(0, 1), Fraction(0, 1)}},
37         {120, 160, {Fraction(96, -1), Fraction(132, 1), Fraction(0, 1), Fraction(0, 1)}},
38         {120, 160, {Fraction(96, 1), Fraction(132, 0), Fraction(0, 1), Fraction(0, 1)}},
39         {120, 160, {Fraction(96, 1), Fraction(132, -1), Fraction(0, 1), Fraction(0, 1)}},
40         {120, 160, {Fraction(96, 1), Fraction(132, 1), Fraction(0, 0), Fraction(0, 1)}},
41         {120, 160, {Fraction(96, 1), Fraction(132, 1), Fraction(0, -1), Fraction(0, 1)}},
42         {120, 160, {Fraction(96, 1), Fraction(132, 1), Fraction(0, 1), Fraction(0, 0)}},
43         {120, 160, {Fraction(96, 1), Fraction(132, 1), Fraction(0, 1), Fraction(0, -1)}},
44         // Zero or negative clean aperture width or height.
45         {120, 160, {Fraction(-96, 1), Fraction(132, 1), Fraction(0, 1), Fraction(0, 1)}},
46         {120, 160, {Fraction(0, 1), Fraction(132, 1), Fraction(0, 1), Fraction(0, 1)}},
47         {120, 160, {Fraction(96, 1), Fraction(-132, 1), Fraction(0, 1), Fraction(0, 1)}},
48         {120, 160, {Fraction(96, 1), Fraction(0, 1), Fraction(0, 1), Fraction(0, 1)}},
49         // Clean aperture width or height is not an integer.
50         {120, 160, {Fraction(96, 5), Fraction(132, 1), Fraction(0, 1), Fraction(0, 1)}},
51         {120, 160, {Fraction(96, 1), Fraction(132, 5), Fraction(0, 1), Fraction(0, 1)}},
52         {722, 1024, {Fraction(385, 1), Fraction(330, 1), Fraction(103, 1), Fraction(-308, 1)}},
53         {1024, 722, {Fraction(330, 1), Fraction(385, 1), Fraction(-308, 1), Fraction(103, 1)}},
54 };
55 
56 using InvalidClapPropertyTest = ::testing::TestWithParam<InvalidClapPropertyParam>;
57 
58 INSTANTIATE_TEST_SUITE_P(Parameterized, InvalidClapPropertyTest,
59                          ::testing::ValuesIn(kInvalidClapPropertyTestParams));
60 
61 // Negative tests for the convertCleanApertureToRect() function.
TEST_P(InvalidClapPropertyTest,ValidateClapProperty)62 TEST_P(InvalidClapPropertyTest, ValidateClapProperty) {
63     const InvalidClapPropertyParam& param = GetParam();
64     int32_t left, top, right, bottom;
65     EXPECT_FALSE(convertCleanApertureToRect(param.width, param.height, param.clap, &left, &top,
66                                             &right, &bottom));
67 }
68 
69 struct ValidClapPropertyParam {
70     uint32_t width;
71     uint32_t height;
72     CleanAperture clap;
73 
74     int32_t left;
75     int32_t top;
76     int32_t right;
77     int32_t bottom;
78 };
79 
80 const ValidClapPropertyParam kValidClapPropertyTestParams[] = {
81         {120,
82          160,
83          {Fraction(96, 1), Fraction(132, 1), Fraction(0, 1), Fraction(0, 1)},
84          12,
85          14,
86          108,
87          146},
88         {120,
89          160,
90          {Fraction(60, 1), Fraction(80, 1), Fraction(-30, 1), Fraction(-40, 1)},
91          0,
92          0,
93          60,
94          80},
95 };
96 
97 using ValidClapPropertyTest = ::testing::TestWithParam<ValidClapPropertyParam>;
98 
99 INSTANTIATE_TEST_SUITE_P(Parameterized, ValidClapPropertyTest,
100                          ::testing::ValuesIn(kValidClapPropertyTestParams));
101 
102 // Positive tests for the convertCleanApertureToRect() function.
TEST_P(ValidClapPropertyTest,ValidateClapProperty)103 TEST_P(ValidClapPropertyTest, ValidateClapProperty) {
104     const ValidClapPropertyParam& param = GetParam();
105     int32_t left, top, right, bottom;
106     EXPECT_TRUE(convertCleanApertureToRect(param.width, param.height, param.clap, &left, &top,
107                                            &right, &bottom));
108     EXPECT_EQ(left, param.left);
109     EXPECT_EQ(top, param.top);
110     EXPECT_EQ(right, param.right);
111     EXPECT_EQ(bottom, param.bottom);
112 }
113 
114 }  // namespace
115