• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <limits>
21 #include <cstddef>
22 #include <vector>
23 
24 #include "android-base/file.h"
25 #include "android-base/test_utils.h"
26 #include <gtest/gtest.h>
27 
28 #include <binder/Parcel.h>
29 #include <binder/Value.h>
30 #include <binder/Debug.h>
31 
32 using ::android::binder::Value;
33 using ::android::os::PersistableBundle;
34 using ::android::String16;
35 using ::std::vector;
36 
37 #define VALUE_TYPE_TEST(T, TYPENAME, VAL)         \
38     TEST(ValueType, Handles ## TYPENAME) {        \
39         T x = VAL;                                \
40         T y = T();                                \
41         Value value = VAL;                        \
42         ASSERT_FALSE(value.empty());              \
43         ASSERT_TRUE(value.is ## TYPENAME ());     \
44         ASSERT_TRUE(value.get ## TYPENAME (&y));  \
45         ASSERT_EQ(x, y);                          \
46         ASSERT_EQ(value, Value(y));               \
47         value.put ## TYPENAME (x);                \
48         ASSERT_EQ(value, Value(y));               \
49         value = Value();                          \
50         ASSERT_TRUE(value.empty());               \
51         ASSERT_NE(value, Value(y));               \
52         value = y;                                \
53         ASSERT_EQ(value, Value(x));               \
54     }
55 
56 #define VALUE_TYPE_VECTOR_TEST(T, TYPENAME, VAL)      \
57     TEST(ValueType, Handles ## TYPENAME ## Vector) {  \
58         vector<T> x;                                  \
59         vector<T> y;                                  \
60         x.push_back(VAL);                             \
61         x.push_back(T());                             \
62         Value value(x);                               \
63         ASSERT_FALSE(value.empty());                  \
64         ASSERT_TRUE(value.is ## TYPENAME ## Vector());    \
65         ASSERT_TRUE(value.get ## TYPENAME ## Vector(&y)); \
66         ASSERT_EQ(x, y);                              \
67         ASSERT_EQ(value, Value(y));                   \
68         value.put ## TYPENAME ## Vector(x);           \
69         ASSERT_EQ(value, Value(y));                   \
70         value = Value();                              \
71         ASSERT_TRUE(value.empty());                   \
72         ASSERT_NE(value, Value(y));                   \
73         value = y;                                    \
74         ASSERT_EQ(value, Value(x));                   \
75     }
76 
VALUE_TYPE_TEST(bool,Boolean,true)77 VALUE_TYPE_TEST(bool, Boolean, true)
78 VALUE_TYPE_TEST(int32_t, Int, 31337)
79 VALUE_TYPE_TEST(int64_t, Long, 13370133701337l)
80 VALUE_TYPE_TEST(double, Double, 3.14159265358979323846)
81 VALUE_TYPE_TEST(String16, String, String16("Lovely"))
82 
83 VALUE_TYPE_VECTOR_TEST(bool, Boolean, true)
84 VALUE_TYPE_VECTOR_TEST(int32_t, Int, 31337)
85 VALUE_TYPE_VECTOR_TEST(int64_t, Long, 13370133701337l)
86 VALUE_TYPE_VECTOR_TEST(double, Double, 3.14159265358979323846)
87 VALUE_TYPE_VECTOR_TEST(String16, String, String16("Lovely"))
88 
89 VALUE_TYPE_TEST(PersistableBundle, PersistableBundle, PersistableBundle())
90 
91 TEST(ValueType, HandlesClear) {
92     Value value;
93     ASSERT_TRUE(value.empty());
94     value.putInt(31337);
95     ASSERT_FALSE(value.empty());
96     value.clear();
97     ASSERT_TRUE(value.empty());
98 }
99 
TEST(ValueType,HandlesSwap)100 TEST(ValueType, HandlesSwap) {
101     Value value_a, value_b;
102     int32_t int_x;
103     value_a.putInt(31337);
104     ASSERT_FALSE(value_a.empty());
105     ASSERT_TRUE(value_b.empty());
106     value_a.swap(value_b);
107     ASSERT_FALSE(value_b.empty());
108     ASSERT_TRUE(value_a.empty());
109     ASSERT_TRUE(value_b.getInt(&int_x));
110     ASSERT_EQ(31337, int_x);
111 }
112