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