• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 
21 #undef LOG_TAG
22 #define LOG_TAG "RegionSamplingTest"
23 
24 #include <ui/Transform.h>
25 
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
28 #include <array>
29 #include <limits>
30 
31 #include "RegionSamplingThread.h"
32 
33 namespace android {
34 
35 struct RegionSamplingTest : testing::Test {
36 public:
37     static uint32_t constexpr kBlack = 0;
38     static uint32_t constexpr kWhite = std::numeric_limits<uint32_t>::max();
39     static int constexpr kWidth = 98;
40     static int constexpr kStride = 100;
41     static int constexpr kHeight = 29;
42     static int constexpr kOrientation = ui::Transform::ROT_0;
43     std::array<uint32_t, kHeight * kStride> buffer;
44     Rect const whole_area{0, 0, kWidth, kHeight};
45 };
46 
TEST_F(RegionSamplingTest,calculate_mean_white)47 TEST_F(RegionSamplingTest, calculate_mean_white) {
48     std::fill(buffer.begin(), buffer.end(), kWhite);
49     EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, whole_area),
50                 testing::FloatEq(1.0f));
51 }
52 
TEST_F(RegionSamplingTest,calculate_mean_black)53 TEST_F(RegionSamplingTest, calculate_mean_black) {
54     std::fill(buffer.begin(), buffer.end(), kBlack);
55     EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, whole_area),
56                 testing::FloatEq(0.0f));
57 }
58 
TEST_F(RegionSamplingTest,calculate_mean_partial_region)59 TEST_F(RegionSamplingTest, calculate_mean_partial_region) {
60     auto const halfway_down = kHeight >> 1;
61     auto const half = halfway_down * kStride;
62     Rect const partial_region = {whole_area.left, whole_area.top, whole_area.right,
63                                  whole_area.top + halfway_down};
64     std::fill(buffer.begin(), buffer.begin() + half, 0);
65     std::fill(buffer.begin() + half, buffer.end(), kWhite);
66     EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, partial_region),
67                 testing::FloatEq(0.0f));
68 }
69 
TEST_F(RegionSamplingTest,calculate_mean_mixed_values)70 TEST_F(RegionSamplingTest, calculate_mean_mixed_values) {
71     std::generate(buffer.begin(), buffer.end(), [n = 0]() mutable {
72         uint32_t const pixel = (n % std::numeric_limits<uint8_t>::max()) << ((n % 3) * CHAR_BIT);
73         n++;
74         return pixel;
75     });
76 
77     EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, whole_area),
78                 testing::FloatNear(0.16f, 0.01f));
79 }
80 
TEST_F(RegionSamplingTest,bimodal_tiebreaker)81 TEST_F(RegionSamplingTest, bimodal_tiebreaker) {
82     std::generate(buffer.begin(), buffer.end(),
83                   [n = 0]() mutable { return (n++ % 2) ? kBlack : kWhite; });
84     EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, whole_area),
85                 testing::FloatEq(0.5f));
86 }
87 
TEST_F(RegionSamplingTest,bounds_checking)88 TEST_F(RegionSamplingTest, bounds_checking) {
89     std::generate(buffer.begin(), buffer.end(),
90                   [n = 0]() mutable { return (n++ > (kStride * kHeight >> 1)) ? kBlack : kWhite; });
91 
92     Rect invalid_region{0, 0, 4, kHeight + 1};
93     EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, invalid_region),
94                 testing::Eq(0.0));
95 
96     invalid_region = Rect{0, 0, -4, kHeight};
97     EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, invalid_region),
98                 testing::Eq(0.0));
99 
100     invalid_region = Rect{3, 0, 2, 0};
101     EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, invalid_region),
102                 testing::Eq(0.0));
103 
104     invalid_region = Rect{0, 3, 0, 2};
105     EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, invalid_region),
106                 testing::Eq(0.0));
107 }
108 
109 } // namespace android
110 
111 // TODO(b/129481165): remove the #pragma below and fix conversion issues
112 #pragma clang diagnostic pop // ignored "-Wconversion"
113