blob: 392e0187352e11acbfd600156dc210d04d05722e [file] [log] [blame]
Abhishek Gadewar76f00822024-05-09 17:02:18 -07001/*
2 * Copyright (C) 2024 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#include <binder/Parcel.h>
18#include <binder/PersistableBundle.h>
19#include <gtest/gtest.h>
20#include <numeric>
21
22using android::OK;
23using android::Parcel;
24using android::status_t;
25using android::String16;
26using android::String8;
27using android::os::PersistableBundle;
28
29namespace android {
30
31inline std::string to_string(String16 const& str) {
32 return String8{str}.c_str();
33}
34
35namespace os {
36
37template <typename T>
38inline std::ostream& operator<<(std::ostream& out, std::vector<T> const& vec) {
39 using std::to_string;
40 auto str =
41 std::accumulate(vec.begin(), vec.end(), std::string{},
42 [](std::string const& a, auto const& b) { return a + to_string(b); });
43 return out << str;
44}
45
46inline std::ostream& operator<<(std::ostream& out, PersistableBundle const& pb) {
47#define PRINT(TYPENAME, TYPE) \
48 for (auto const& key : pb.get##TYPENAME##Keys()) { \
49 TYPE val{}; \
50 pb.get##TYPENAME(key, &val); \
51 out << #TYPE " " << key << ": " << val << std::endl; \
52 }
53
54 out << "size: " << pb.size() << std::endl;
55 PRINT(Boolean, bool);
56 PRINT(Int, int);
57 PRINT(Long, int64_t);
58 PRINT(Double, double);
59 PRINT(String, String16);
60 PRINT(BooleanVector, std::vector<bool>);
61 PRINT(IntVector, std::vector<int32_t>);
62 PRINT(LongVector, std::vector<int64_t>);
63 PRINT(DoubleVector, std::vector<double>);
64 PRINT(StringVector, std::vector<String16>);
65 PRINT(PersistableBundle, PersistableBundle);
66
67#undef PRINT
68
69 return out;
70}
71
72} // namespace os
73} // namespace android
74
75static const String16 kKey{"key"};
76
77static PersistableBundle createSimplePersistableBundle() {
78 PersistableBundle pb{};
79 pb.putInt(kKey, 64);
80 return pb;
81}
82
83#define TEST_PUT_AND_GET(TYPENAME, TYPE, ...) \
84 TEST(PersistableBundle, PutAndGet##TYPENAME) { \
85 TYPE const expected{__VA_ARGS__}; \
86 PersistableBundle pb{}; \
87 \
88 pb.put##TYPENAME(kKey, expected); \
89 \
90 std::set<String16> expectedKeys{kKey}; \
91 EXPECT_EQ(pb.get##TYPENAME##Keys(), expectedKeys); \
92 \
93 TYPE val{}; \
94 EXPECT_TRUE(pb.get##TYPENAME(kKey, &val)); \
95 EXPECT_EQ(val, expected); \
96 }
97
98TEST_PUT_AND_GET(Boolean, bool, true);
99TEST_PUT_AND_GET(Int, int, 64);
100TEST_PUT_AND_GET(Long, int64_t, 42);
101TEST_PUT_AND_GET(Double, double, 42.64);
102TEST_PUT_AND_GET(String, String16, String16{"foo"});
103TEST_PUT_AND_GET(BooleanVector, std::vector<bool>, true, true);
104TEST_PUT_AND_GET(IntVector, std::vector<int32_t>, 1, 2);
105TEST_PUT_AND_GET(LongVector, std::vector<int64_t>, 1, 2);
106TEST_PUT_AND_GET(DoubleVector, std::vector<double>, 4.2, 5.9);
107TEST_PUT_AND_GET(StringVector, std::vector<String16>, String16{"foo"}, String16{"bar"});
108TEST_PUT_AND_GET(PersistableBundle, PersistableBundle, createSimplePersistableBundle());
109
110TEST(PersistableBundle, ParcelAndUnparcel) {
111 PersistableBundle expected = createSimplePersistableBundle();
112 PersistableBundle out{};
113
114 Parcel p{};
115 EXPECT_EQ(expected.writeToParcel(&p), 0);
116 p.setDataPosition(0);
117 EXPECT_EQ(out.readFromParcel(&p), 0);
118
119 EXPECT_EQ(expected, out);
120}
121
122TEST(PersistableBundle, OverwriteKey) {
123 PersistableBundle pb{};
124
125 pb.putInt(kKey, 64);
126 pb.putDouble(kKey, 0.5);
127
128 EXPECT_EQ(pb.getIntKeys().size(), 0);
129 EXPECT_EQ(pb.getDoubleKeys().size(), 1);
130
131 double out;
132 EXPECT_TRUE(pb.getDouble(kKey, &out));
133 EXPECT_EQ(out, 0.5);
134}