| Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame] | 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 | #undef LOG_TAG | 
|  | 18 | #define LOG_TAG "RegionSamplingTest" | 
|  | 19 |  | 
| Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 20 | #include <ui/Transform.h> | 
|  | 21 |  | 
| Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame] | 22 | #include <gmock/gmock.h> | 
|  | 23 | #include <gtest/gtest.h> | 
|  | 24 | #include <array> | 
|  | 25 | #include <limits> | 
|  | 26 |  | 
|  | 27 | #include "RegionSamplingThread.h" | 
|  | 28 |  | 
|  | 29 | namespace android { | 
|  | 30 |  | 
|  | 31 | struct RegionSamplingTest : testing::Test { | 
|  | 32 | public: | 
|  | 33 | static uint32_t constexpr kBlack = 0; | 
|  | 34 | static uint32_t constexpr kWhite = std::numeric_limits<uint32_t>::max(); | 
|  | 35 | static int constexpr kWidth = 98; | 
|  | 36 | static int constexpr kStride = 100; | 
|  | 37 | static int constexpr kHeight = 29; | 
| Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 38 | static int constexpr kOrientation = ui::Transform::ROT_0; | 
| Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame] | 39 | std::array<uint32_t, kHeight * kStride> buffer; | 
|  | 40 | Rect const whole_area{0, 0, kWidth, kHeight}; | 
|  | 41 | }; | 
|  | 42 |  | 
|  | 43 | TEST_F(RegionSamplingTest, calculate_mean_white) { | 
|  | 44 | std::fill(buffer.begin(), buffer.end(), kWhite); | 
| Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 45 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, whole_area), | 
|  | 46 | testing::FloatEq(1.0f)); | 
| Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
|  | 49 | TEST_F(RegionSamplingTest, calculate_mean_black) { | 
|  | 50 | std::fill(buffer.begin(), buffer.end(), kBlack); | 
| Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 51 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, whole_area), | 
|  | 52 | testing::FloatEq(0.0f)); | 
| Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
|  | 55 | TEST_F(RegionSamplingTest, calculate_mean_partial_region) { | 
|  | 56 | auto const halfway_down = kHeight >> 1; | 
|  | 57 | auto const half = halfway_down * kStride; | 
|  | 58 | Rect const partial_region = {whole_area.left, whole_area.top, whole_area.right, | 
|  | 59 | whole_area.top + halfway_down}; | 
|  | 60 | std::fill(buffer.begin(), buffer.begin() + half, 0); | 
|  | 61 | std::fill(buffer.begin() + half, buffer.end(), kWhite); | 
| Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 62 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, partial_region), | 
|  | 63 | testing::FloatEq(0.0f)); | 
| Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
|  | 66 | TEST_F(RegionSamplingTest, calculate_mean_mixed_values) { | 
|  | 67 | std::generate(buffer.begin(), buffer.end(), [n = 0]() mutable { | 
|  | 68 | uint32_t const pixel = (n % std::numeric_limits<uint8_t>::max()) << ((n % 3) * CHAR_BIT); | 
|  | 69 | n++; | 
|  | 70 | return pixel; | 
|  | 71 | }); | 
| Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 72 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, whole_area), | 
|  | 73 | testing::FloatNear(0.083f, 0.01f)); | 
| Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
|  | 76 | TEST_F(RegionSamplingTest, bimodal_tiebreaker) { | 
|  | 77 | std::generate(buffer.begin(), buffer.end(), | 
|  | 78 | [n = 0]() mutable { return (n++ % 2) ? kBlack : kWhite; }); | 
|  | 79 | // presently there's no tiebreaking strategy in place, accept either of the means | 
| Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 80 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, whole_area), | 
| Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame] | 81 | testing::AnyOf(testing::FloatEq(1.0), testing::FloatEq(0.0f))); | 
|  | 82 | } | 
|  | 83 |  | 
| Kevin DuBois | b325c93 | 2019-05-21 08:34:09 -0700 | [diff] [blame] | 84 | TEST_F(RegionSamplingTest, bounds_checking) { | 
|  | 85 | std::generate(buffer.begin(), buffer.end(), | 
|  | 86 | [n = 0]() mutable { return (n++ > (kStride * kHeight >> 1)) ? kBlack : kWhite; }); | 
|  | 87 |  | 
|  | 88 | Rect invalid_region{0, 0, 4, kHeight + 1}; | 
|  | 89 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, invalid_region), | 
|  | 90 | testing::Eq(0.0)); | 
|  | 91 |  | 
|  | 92 | invalid_region = Rect{0, 0, -4, kHeight}; | 
|  | 93 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, invalid_region), | 
|  | 94 | testing::Eq(0.0)); | 
|  | 95 |  | 
|  | 96 | invalid_region = Rect{3, 0, 2, 0}; | 
|  | 97 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, invalid_region), | 
|  | 98 | testing::Eq(0.0)); | 
|  | 99 |  | 
|  | 100 | invalid_region = Rect{0, 3, 0, 2}; | 
|  | 101 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, kOrientation, invalid_region), | 
|  | 102 | testing::Eq(0.0)); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | // workaround for b/133849373 | 
|  | 106 | TEST_F(RegionSamplingTest, orientation_90) { | 
|  | 107 | std::generate(buffer.begin(), buffer.end(), | 
|  | 108 | [n = 0]() mutable { return (n++ > (kStride * kHeight >> 1)) ? kBlack : kWhite; }); | 
|  | 109 |  | 
|  | 110 | Rect tl_region{0, 0, 4, 4}; | 
|  | 111 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, ui::Transform::ROT_0, | 
|  | 112 | tl_region), | 
|  | 113 | testing::Eq(1.0)); | 
|  | 114 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, ui::Transform::ROT_180, | 
|  | 115 | tl_region), | 
|  | 116 | testing::Eq(1.0)); | 
|  | 117 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, ui::Transform::ROT_90, | 
|  | 118 | tl_region), | 
|  | 119 | testing::Eq(0.0)); | 
|  | 120 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, ui::Transform::ROT_270, | 
|  | 121 | tl_region), | 
|  | 122 | testing::Eq(0.0)); | 
|  | 123 |  | 
|  | 124 | Rect br_region{kWidth - 4, kHeight - 4, kWidth, kHeight}; | 
|  | 125 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, ui::Transform::ROT_0, | 
|  | 126 | br_region), | 
|  | 127 | testing::Eq(0.0)); | 
|  | 128 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, ui::Transform::ROT_180, | 
|  | 129 | br_region), | 
|  | 130 | testing::Eq(0.0)); | 
|  | 131 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, ui::Transform::ROT_90, | 
|  | 132 | br_region), | 
|  | 133 | testing::Eq(1.0)); | 
|  | 134 | EXPECT_THAT(sampleArea(buffer.data(), kWidth, kHeight, kStride, ui::Transform::ROT_270, | 
|  | 135 | br_region), | 
|  | 136 | testing::Eq(1.0)); | 
|  | 137 | } | 
|  | 138 |  | 
| Kevin DuBois | bb27bcd | 2019-04-02 14:34:35 -0700 | [diff] [blame] | 139 | } // namespace android |