1 /*
2 * Copyright (C) 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 "-Wextra"
20
21 #undef LOG_TAG
22 #define LOG_TAG "LibSurfaceFlingerUnittests"
23
24 #include <binder/Parcel.h>
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
27 #include <gui/LayerMetadata.h>
28 #include <log/log.h>
29
30 using android::gui::LayerMetadata;
31
32 namespace android {
33 namespace {
34
35 class LayerMetadataTest : public testing::Test {
36 public:
37 LayerMetadataTest();
38 ~LayerMetadataTest() override;
39 };
40
LayerMetadataTest()41 LayerMetadataTest::LayerMetadataTest() {
42 const ::testing::TestInfo* const test_info =
43 ::testing::UnitTest::GetInstance()->current_test_info();
44 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
45 }
46
~LayerMetadataTest()47 LayerMetadataTest::~LayerMetadataTest() {
48 const ::testing::TestInfo* const test_info =
49 ::testing::UnitTest::GetInstance()->current_test_info();
50 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
51 }
52
TEST_F(LayerMetadataTest,testLayerMetadata)53 TEST_F(LayerMetadataTest, testLayerMetadata) {
54 LayerMetadata metadata;
55
56 ASSERT_EQ(0, metadata.mMap.size());
57
58 // Test non-set
59 ASSERT_EQ(3, metadata.getInt32(4, 3));
60
61 // Make sure it's still unset
62 ASSERT_EQ(5, metadata.getInt32(4, 5));
63
64 metadata.setInt32(4, 2);
65 ASSERT_EQ(2, metadata.getInt32(4, 0));
66
67 // data is too small
68 metadata.mMap[2] = std::vector<uint8_t>{'a', 'b'};
69 ASSERT_EQ(0, metadata.getInt32(2, 0));
70
71 Parcel p;
72 metadata.writeToParcel(&p);
73 LayerMetadata reconstructed;
74 reconstructed.setInt32(3, 1); // to make sure it gets replaced
75 p.setDataPosition(0);
76 reconstructed.readFromParcel(&p);
77 ASSERT_EQ(metadata.mMap, reconstructed.mMap);
78 }
79
TEST_F(LayerMetadataTest,merge)80 TEST_F(LayerMetadataTest, merge) {
81 LayerMetadata metadata;
82 metadata.setInt32(4, 2);
83 metadata.mMap[2] = std::vector<uint8_t>{'a', 'b'};
84
85 LayerMetadata second;
86 std::vector<uint8_t> someData{'c', 'd', '\0'};
87 second.mMap[2] = someData;
88 second.setInt32(6, 5);
89 second.mMap[4].clear(); // will not delete if eraseEmpty is false
90 bool changed = metadata.merge(second);
91
92 ASSERT_TRUE(changed);
93 ASSERT_EQ(3, metadata.mMap.size());
94 ASSERT_EQ(someData, second.mMap[2]);
95 ASSERT_EQ(5, metadata.getInt32(6, 0));
96 ASSERT_TRUE(metadata.mMap.at(4).empty());
97
98 LayerMetadata withErase;
99 withErase.mMap[6].clear();
100 changed = metadata.merge(withErase, true /* eraseEmpty */);
101 ASSERT_TRUE(changed);
102 ASSERT_EQ(2, metadata.mMap.size());
103 ASSERT_EQ(someData, second.mMap[2]);
104 ASSERT_EQ(true, metadata.has(4));
105
106 // test for change detection
107 LayerMetadata third;
108 third.mMap[2] = someData;
109 third.mMap[5].clear();
110 changed = metadata.merge(third);
111 ASSERT_FALSE(changed);
112 }
113
114 } // namespace
115 } // namespace android
116
117 // TODO(b/129481165): remove the #pragma below and fix conversion issues
118 #pragma clang diagnostic pop // ignored "-Wextra"
119