summaryrefslogtreecommitdiff
path: root/services/surfaceflinger/tests/unittests/LayerMetadataTest.cpp
blob: 373fd74020d55867bf8fdf7813285b9ed946072d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// TODO(b/129481165): remove the #pragma below and fix conversion issues
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wextra"

#undef LOG_TAG
#define LOG_TAG "LibSurfaceFlingerUnittests"

#include <binder/Parcel.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <gui/LayerMetadata.h>
#include <log/log.h>

namespace android {
namespace {

class LayerMetadataTest : public testing::Test {
public:
    LayerMetadataTest();
    ~LayerMetadataTest() override;
};

LayerMetadataTest::LayerMetadataTest() {
    const ::testing::TestInfo* const test_info =
            ::testing::UnitTest::GetInstance()->current_test_info();
    ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
}

LayerMetadataTest::~LayerMetadataTest() {
    const ::testing::TestInfo* const test_info =
            ::testing::UnitTest::GetInstance()->current_test_info();
    ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
}

TEST_F(LayerMetadataTest, testLayerMetadata) {
    LayerMetadata metadata;

    ASSERT_EQ(0, metadata.mMap.size());

    // Test non-set
    ASSERT_EQ(3, metadata.getInt32(4, 3));

    // Make sure it's still unset
    ASSERT_EQ(5, metadata.getInt32(4, 5));

    metadata.setInt32(4, 2);
    ASSERT_EQ(2, metadata.getInt32(4, 0));

    // data is too small
    metadata.mMap[2] = std::vector<uint8_t>{'a', 'b'};
    ASSERT_EQ(0, metadata.getInt32(2, 0));

    Parcel p;
    metadata.writeToParcel(&p);
    LayerMetadata reconstructed;
    reconstructed.setInt32(3, 1); // to make sure it gets replaced
    p.setDataPosition(0);
    reconstructed.readFromParcel(&p);
    ASSERT_EQ(metadata.mMap, reconstructed.mMap);
}

TEST_F(LayerMetadataTest, merge) {
    LayerMetadata metadata;
    metadata.setInt32(4, 2);
    metadata.mMap[2] = std::vector<uint8_t>{'a', 'b'};

    LayerMetadata second;
    std::vector<uint8_t> someData{'c', 'd', '\0'};
    second.mMap[2] = someData;
    second.setInt32(6, 5);
    second.mMap[4].clear(); // will not delete if eraseEmpty is false
    bool changed = metadata.merge(second);

    ASSERT_TRUE(changed);
    ASSERT_EQ(3, metadata.mMap.size());
    ASSERT_EQ(someData, second.mMap[2]);
    ASSERT_EQ(5, metadata.getInt32(6, 0));
    ASSERT_TRUE(metadata.mMap.at(4).empty());

    LayerMetadata withErase;
    withErase.mMap[6].clear();
    changed = metadata.merge(withErase, true /* eraseEmpty */);
    ASSERT_TRUE(changed);
    ASSERT_EQ(2, metadata.mMap.size());
    ASSERT_EQ(someData, second.mMap[2]);
    ASSERT_EQ(true, metadata.has(4));

    // test for change detection
    LayerMetadata third;
    third.mMap[2] = someData;
    third.mMap[5].clear();
    changed = metadata.merge(third);
    ASSERT_FALSE(changed);
}

} // namespace
} // namespace android

// TODO(b/129481165): remove the #pragma below and fix conversion issues
#pragma clang diagnostic pop // ignored "-Wextra"