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