• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 "aidl_test_client_parcelables.h"
18 
19 #include <iostream>
20 #include <vector>
21 
22 // libutils:
23 using android::sp;
24 
25 // libbinder:
26 using android::binder::Status;
27 
28 // generated
29 using android::aidl::tests::ITestService;
30 using android::aidl::tests::SimpleParcelable;
31 using android::os::PersistableBundle;
32 
33 using std::cout;
34 using std::endl;
35 using std::vector;
36 
37 namespace android {
38 namespace aidl {
39 namespace tests {
40 namespace client {
41 
ConfirmSimpleParcelables(const sp<ITestService> & s)42 bool ConfirmSimpleParcelables(const sp<ITestService>& s) {
43   cout << "Confirming passing and returning SimpleParcelable objects works."
44        << endl;
45 
46   SimpleParcelable input("Booya", 42);
47   SimpleParcelable out_param, returned;
48   Status status = s->RepeatSimpleParcelable(input, &out_param, &returned);
49   if (!status.isOk()) {
50     cout << "Binder call failed." << endl;
51     return false;
52   }
53   if (input != out_param || input != returned) {
54     cout << "Failed to repeat SimpleParcelable objects." << endl;
55     return false;
56   }
57 
58   cout << "Attempting to reverse an array of SimpleParcelable objects." << endl;
59   const vector<SimpleParcelable> original{SimpleParcelable("first", 0),
60                                           SimpleParcelable("second", 1),
61                                           SimpleParcelable("third", 2)};
62   vector<SimpleParcelable> repeated;
63   vector<SimpleParcelable> reversed;
64   status = s->ReverseSimpleParcelables(original, &repeated, &reversed);
65   if (!status.isOk()) {
66     cout << "Binder call failed." << endl;
67     return false;
68   }
69   std::reverse(reversed.begin(), reversed.end());
70   if (repeated != original || reversed != original) {
71     cout << "Failed to reverse an array of SimpleParcelable objects." << endl;
72     return false;
73   }
74 
75   return true;
76 }
77 
ConfirmPersistableBundles(const sp<ITestService> & s)78 bool ConfirmPersistableBundles(const sp<ITestService>& s) {
79   cout << "Confirming passing and returning PersistableBundle objects works."
80        << endl;
81 
82   PersistableBundle empty_bundle, returned;
83   Status status = s->RepeatPersistableBundle(empty_bundle, &returned);
84   if (!status.isOk()) {
85     cout << "Binder call failed for empty PersistableBundle." << endl;
86     return false;
87   }
88   if (empty_bundle != returned) {
89     cout << "Failed to repeat empty PersistableBundle." << endl;
90     return false;
91   }
92 
93   PersistableBundle non_empty_bundle;
94   non_empty_bundle.putBoolean(String16("test_bool"), false);
95   non_empty_bundle.putInt(String16("test_int"), 33);
96   non_empty_bundle.putLong(String16("test_long"), 34359738368l);
97   non_empty_bundle.putDouble(String16("test_double"), 1.1);
98   non_empty_bundle.putString(String16("test_string"), String16("Woot!"));
99   non_empty_bundle.putBooleanVector(String16("test_bool_vector"),
100                                     {true, false, true});
101   non_empty_bundle.putIntVector(String16("test_int_vector"), {33, 44, 55, 142});
102   non_empty_bundle.putLongVector(String16("test_long_vector"),
103                                  {34l, 8371l, 34359738375l});
104   non_empty_bundle.putDoubleVector(String16("test_double_vector"), {2.2, 5.4});
105   non_empty_bundle.putStringVector(String16("test_string_vector"),
106                                    {String16("hello"), String16("world!")});
107   PersistableBundle nested_bundle;
108   nested_bundle.putInt(String16("test_nested_int"), 345);
109   non_empty_bundle.putPersistableBundle(String16("test_persistable_bundle"),
110                                         nested_bundle);
111 
112   status = s->RepeatPersistableBundle(non_empty_bundle, &returned);
113   if (!status.isOk()) {
114     cout << "Binder call failed. " << endl;
115     return false;
116   }
117   if (non_empty_bundle != returned) {
118     cout << "Failed to repeat PersistableBundle object." << endl;
119     return false;
120   }
121 
122   cout << "Attempting to reverse an array of PersistableBundle objects."
123        << endl;
124   PersistableBundle first;
125   PersistableBundle second;
126   PersistableBundle third;
127   first.putInt(String16("test_int"), 1231);
128   second.putLong(String16("test_long"), 222222l);
129   third.putDouble(String16("test_double"), 10.8);
130   const vector<PersistableBundle> original{first, second, third};
131 
132   vector<PersistableBundle> repeated;
133   vector<PersistableBundle> reversed;
134   status = s->ReversePersistableBundles(original, &repeated, &reversed);
135   if (!status.isOk()) {
136     cout << "Binder call failed." << endl;
137     return false;
138   }
139   std::reverse(reversed.begin(), reversed.end());
140   if (repeated != original || reversed != original) {
141     cout << "Failed to reverse an array of PersistableBundle objects." << endl;
142     return false;
143   }
144 
145   return true;
146 }
147 
148 }  // namespace client
149 }  // namespace tests
150 }  // namespace aidl
151 }  // namespace android
152