blob: 73b594e9c2952adb5856b9776bfa163141c85335 [file] [log] [blame]
Christopher Wiley90be4e32015-10-20 14:55:25 -07001/*
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
Christopher Wiley90be4e32015-10-20 14:55:25 -070017#include <memory>
Christopher Wiley12e894a2016-01-29 11:55:07 -080018#include <set>
19#include <string>
Christopher Wiley90be4e32015-10-20 14:55:25 -070020#include <vector>
21
Christopher Wileyec31a052016-01-25 07:28:51 -080022#include <android-base/stringprintf.h>
Christopher Wiley90be4e32015-10-20 14:55:25 -070023#include <gtest/gtest.h>
24
25#include "aidl.h"
Jiyong Park3656c3c2018-08-01 20:02:01 +090026#include "aidl_apicheck.h"
Christopher Wiley90be4e32015-10-20 14:55:25 -070027#include "aidl_language.h"
Steven Moreland860b1942018-08-16 14:59:28 -070028#include "aidl_to_cpp.h"
Jeongik Cha047c5ee2019-08-07 23:16:49 +090029#include "aidl_to_java.h"
30#include "options.h"
Christopher Wiley90be4e32015-10-20 14:55:25 -070031#include "tests/fake_io_delegate.h"
Christopher Wiley90be4e32015-10-20 14:55:25 -070032
Jeongik Cha2a5b4d82019-08-06 19:37:59 +090033using android::aidl::internals::parse_preprocessed_file;
Christopher Wiley90be4e32015-10-20 14:55:25 -070034using android::aidl::test::FakeIoDelegate;
Christopher Wileyec31a052016-01-25 07:28:51 -080035using android::base::StringPrintf;
Christopher Wiley12e894a2016-01-29 11:55:07 -080036using std::set;
Christopher Wiley90be4e32015-10-20 14:55:25 -070037using std::string;
38using std::unique_ptr;
Christopher Wiley12e894a2016-01-29 11:55:07 -080039using std::vector;
Jeongik Cha2a5b4d82019-08-06 19:37:59 +090040using testing::internal::CaptureStderr;
41using testing::internal::GetCapturedStderr;
Christopher Wiley90be4e32015-10-20 14:55:25 -070042
43namespace android {
44namespace aidl {
Christopher Wileyf8136192016-04-12 14:19:35 -070045namespace {
46
47const char kExpectedDepFileContents[] =
48R"(place/for/output/p/IFoo.java : \
49 p/IFoo.aidl
50
51p/IFoo.aidl :
52)";
53
Dan Willemsen93298ee2016-11-10 23:55:55 -080054const char kExpectedNinjaDepFileContents[] =
55R"(place/for/output/p/IFoo.java : \
56 p/IFoo.aidl
57)";
58
Christopher Wileyb1bbdf82016-04-21 11:43:45 -070059const char kExpectedParcelableDepFileContents[] =
Steven Moreland2a9a7d62019-02-05 16:11:54 -080060 R"(place/for/output/p/Foo.java : \
Christopher Wileyb1bbdf82016-04-21 11:43:45 -070061 p/Foo.aidl
62
63p/Foo.aidl :
64)";
65
Jiyong Parkd4e73c82019-07-24 21:39:15 +090066const char kExpectedJavaParcelableOutputContests[] =
Jeongik Chaa755c2a2018-12-12 16:28:23 +090067 R"(/*
68 * This file is auto-generated. DO NOT MODIFY.
Jeongik Chaa755c2a2018-12-12 16:28:23 +090069 */
70@android.annotation.SystemApi
71public class Rect implements android.os.Parcelable
72{
73 // Comment
74
75 @android.annotation.SystemApi
76 public int x = 5;
77
Andrei Onea9445fc62019-06-27 18:11:59 +010078 @dalvik.annotation.compat.UnsupportedAppUsage(expectedSignature = "dummy", implicitMember = "dummy", maxTargetSdk = 28, publicAlternatives = "dummy", trackingBug = 42)
Jeongik Chaa755c2a2018-12-12 16:28:23 +090079 @android.annotation.SystemApi
80 public int y;
Jiyong Parkd4e73c82019-07-24 21:39:15 +090081
82 public android.os.ParcelFileDescriptor fd;
Jeongik Chaa755c2a2018-12-12 16:28:23 +090083 public static final android.os.Parcelable.Creator<Rect> CREATOR = new android.os.Parcelable.Creator<Rect>() {
84 @Override
85 public Rect createFromParcel(android.os.Parcel _aidl_source) {
86 Rect _aidl_out = new Rect();
87 _aidl_out.readFromParcel(_aidl_source);
88 return _aidl_out;
89 }
90 @Override
91 public Rect[] newArray(int _aidl_size) {
92 return new Rect[_aidl_size];
93 }
94 };
95 @Override public final void writeToParcel(android.os.Parcel _aidl_parcel, int _aidl_flag)
96 {
97 int _aidl_start_pos = _aidl_parcel.dataPosition();
98 _aidl_parcel.writeInt(0);
99 _aidl_parcel.writeInt(x);
100 _aidl_parcel.writeInt(y);
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900101 if ((fd!=null)) {
102 _aidl_parcel.writeInt(1);
103 fd.writeToParcel(_aidl_parcel, 0);
104 }
105 else {
106 _aidl_parcel.writeInt(0);
107 }
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900108 int _aidl_end_pos = _aidl_parcel.dataPosition();
109 _aidl_parcel.setDataPosition(_aidl_start_pos);
110 _aidl_parcel.writeInt(_aidl_end_pos - _aidl_start_pos);
111 _aidl_parcel.setDataPosition(_aidl_end_pos);
112 }
113 public final void readFromParcel(android.os.Parcel _aidl_parcel)
114 {
115 int _aidl_start_pos = _aidl_parcel.dataPosition();
116 int _aidl_parcelable_size = _aidl_parcel.readInt();
117 if (_aidl_parcelable_size < 0) return;
118 try {
119 x = _aidl_parcel.readInt();
120 if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;
121 y = _aidl_parcel.readInt();
122 if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900123 if ((0!=_aidl_parcel.readInt())) {
124 fd = android.os.ParcelFileDescriptor.CREATOR.createFromParcel(_aidl_parcel);
125 }
126 else {
127 fd = null;
128 }
129 if (_aidl_parcel.dataPosition() - _aidl_start_pos >= _aidl_parcelable_size) return;
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900130 } finally {
131 _aidl_parcel.setDataPosition(_aidl_start_pos + _aidl_parcelable_size);
132 }
133 }
134 @Override public int describeContents()
135 {
Jeongik Cha372a8c82018-12-12 16:31:11 +0900136 return 0;
137 }
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900138}
139)";
140
Christopher Wileyf8136192016-04-12 14:19:35 -0700141} // namespace
Christopher Wiley90be4e32015-10-20 14:55:25 -0700142
143class AidlTest : public ::testing::Test {
144 protected:
Christopher Wiley56799522015-10-31 10:17:04 -0700145 void SetUp() override {
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900146 CaptureStderr();
Christopher Wiley56799522015-10-31 10:17:04 -0700147 }
148
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900149 void TearDown() override {
150 auto actual_stderr = GetCapturedStderr();
151 if (expected_stderr_.size() > 0) {
152 EXPECT_EQ(android::base::Join(expected_stderr_, ""), actual_stderr);
153 }
154 }
155
156 void AddExpectedStderr(string expected) { expected_stderr_.push_back(expected); }
157
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900158 AidlDefinedType* Parse(const string& path, const string& contents, AidlTypenames& typenames_,
159 Options::Language lang, AidlError* error = nullptr,
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700160 const vector<string> additional_arguments = {}) {
Christopher Wiley0522cd52015-10-28 15:39:44 -0700161 io_delegate_.SetFileContents(path, contents);
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900162 vector<string> args;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900163 if (lang == Options::Language::CPP) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900164 args.emplace_back("aidl-cpp");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900165 } else {
166 args.emplace_back("aidl");
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900167 }
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700168 for (const string& s : additional_arguments) {
169 args.emplace_back(s);
170 }
171 for (const string& f : preprocessed_files_) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900172 args.emplace_back("--preprocessed=" + f);
173 }
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700174 for (const string& i : import_paths_) {
Jiyong Parkfbbfa932018-07-30 21:44:10 +0900175 args.emplace_back("--include=" + i);
176 }
177 args.emplace_back(path);
178 Options options = Options::From(args);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900179 vector<AidlDefinedType*> defined_types;
180 vector<string> imported_files;
Jiyong Park8c380532018-08-30 14:55:26 +0900181 ImportResolver import_resolver{io_delegate_, path, import_paths_, {}};
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700182 AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl(
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900183 path, options, io_delegate_, &typenames_, &defined_types, &imported_files);
Jiyong Parkb034bf02018-07-30 17:44:33 +0900184
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700185 if (error != nullptr) {
186 *error = actual_error;
187 }
Jiyong Parkb034bf02018-07-30 17:44:33 +0900188
189 if (actual_error != AidlError::OK) {
190 return nullptr;
191 }
192
193 EXPECT_EQ(1ul, defined_types.size());
194
195 return defined_types.front();
Christopher Wiley90be4e32015-10-20 14:55:25 -0700196 }
Christopher Wiley0522cd52015-10-28 15:39:44 -0700197
198 FakeIoDelegate io_delegate_;
Christopher Wiley41544372015-11-03 14:52:29 -0800199 vector<string> preprocessed_files_;
Jiyong Park8c380532018-08-30 14:55:26 +0900200 set<string> import_paths_;
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900201 vector<string> expected_stderr_;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900202 AidlTypenames typenames_;
Christopher Wiley90be4e32015-10-20 14:55:25 -0700203};
204
Steven Morelandf3da0892018-10-05 14:52:01 -0700205TEST_F(AidlTest, AcceptMissingPackage) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900206 EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", typenames_, Options::Language::JAVA));
207 typenames_.Reset();
208 EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", typenames_, Options::Language::CPP));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700209}
210
Steven Moreland9c2988f2019-07-17 17:49:10 -0700211TEST_F(AidlTest, EndsInSingleLineComment) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900212 EXPECT_NE(nullptr,
213 Parse("IFoo.aidl", "interface IFoo { } // foo", typenames_, Options::Language::JAVA));
214 typenames_.Reset();
215 EXPECT_NE(nullptr,
216 Parse("IFoo.aidl", "interface IFoo { } // foo", typenames_, Options::Language::CPP));
Steven Moreland9c2988f2019-07-17 17:49:10 -0700217}
218
Christopher Wiley0522cd52015-10-28 15:39:44 -0700219TEST_F(AidlTest, RejectsArraysOfBinders) {
Jiyong Park8c380532018-08-30 14:55:26 +0900220 import_paths_.emplace("");
Christopher Wiley0522cd52015-10-28 15:39:44 -0700221 io_delegate_.SetFileContents("bar/IBar.aidl",
222 "package bar; interface IBar {}");
223 string path = "foo/IFoo.aidl";
224 string contents = "package foo;\n"
225 "import bar.IBar;\n"
226 "interface IFoo { void f(in IBar[] input); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900227 EXPECT_EQ(nullptr, Parse(path, contents, typenames_, Options::Language::JAVA));
228 typenames_.Reset();
229 EXPECT_EQ(nullptr, Parse(path, contents, typenames_, Options::Language::CPP));
Christopher Wiley0522cd52015-10-28 15:39:44 -0700230}
231
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900232TEST_F(AidlTest, SupportOnlyOutParameters) {
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900233 string interface_list = "package a; interface IBar { void f(out List bar); }";
234 string interface_ibinder = "package a; interface IBaz { void f(out IBinder bar); }";
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900235 // List without type isn't supported in cpp.
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900236 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", interface_list, typenames_, Options::Language::CPP));
237 typenames_.Reset();
238 EXPECT_NE(nullptr, Parse("a/IBar.aidl", interface_list, typenames_, Options::Language::JAVA));
239 typenames_.Reset();
240 EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, typenames_, Options::Language::CPP));
241 typenames_.Reset();
242 EXPECT_EQ(nullptr, Parse("a/IBaz.aidl", interface_ibinder, typenames_, Options::Language::JAVA));
Jeongik Chaa2080bf2019-06-18 16:44:29 +0900243}
244
Christopher Wiley90be4e32015-10-20 14:55:25 -0700245TEST_F(AidlTest, RejectsOnewayOutParameters) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700246 string oneway_interface =
247 "package a; oneway interface IFoo { void f(out int bar); }";
248 string oneway_method =
249 "package a; interface IBar { oneway void f(out int bar); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900250 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, typenames_, Options::Language::CPP));
251 typenames_.Reset();
252 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, typenames_, Options::Language::JAVA));
253 typenames_.Reset();
254 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, typenames_, Options::Language::CPP));
255 typenames_.Reset();
256 EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, typenames_, Options::Language::JAVA));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700257}
258
259TEST_F(AidlTest, RejectsOnewayNonVoidReturn) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700260 string oneway_method = "package a; interface IFoo { oneway int f(); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900261 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
262 typenames_.Reset();
263 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700264}
265
Casey Dahlin57dbe242015-12-04 11:44:02 -0800266TEST_F(AidlTest, RejectsNullablePrimitive) {
267 string oneway_method = "package a; interface IFoo { @nullable int f(); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900268 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
269 typenames_.Reset();
270 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
Casey Dahlin57dbe242015-12-04 11:44:02 -0800271}
272
Steven Morelandb3cd3c72018-10-11 12:37:45 -0700273TEST_F(AidlTest, RejectsDuplicatedArgumentNames) {
274 string method = "package a; interface IFoo { void f(int a, int a); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900275 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::CPP));
276 typenames_.Reset();
277 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::JAVA));
Steven Morelandb3cd3c72018-10-11 12:37:45 -0700278}
279
Andrei Onea9445fc62019-06-27 18:11:59 +0100280TEST_F(AidlTest, RejectsDuplicatedAnnotationParams) {
281 string method = "package a; interface IFoo { @UnsupportedAppUsage(foo=1, foo=2)void f(); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900282 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::CPP));
283 typenames_.Reset();
284 EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", method, typenames_, Options::Language::JAVA));
Andrei Onea9445fc62019-06-27 18:11:59 +0100285}
286
Christopher Wileyec31a052016-01-25 07:28:51 -0800287TEST_F(AidlTest, ParsesNullableAnnotation) {
288 for (auto is_nullable: {true, false}) {
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900289 auto parse_result = Parse("a/IFoo.aidl",
290 StringPrintf("package a; interface IFoo {%s String f(); }",
291 (is_nullable) ? "@nullable" : ""),
292 typenames_, Options::Language::CPP);
Christopher Wileyec31a052016-01-25 07:28:51 -0800293 ASSERT_NE(nullptr, parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700294 const AidlInterface* interface = parse_result->AsInterface();
295 ASSERT_NE(nullptr, interface);
296 ASSERT_FALSE(interface->GetMethods().empty());
297 EXPECT_EQ(interface->GetMethods()[0]->GetType().IsNullable(), is_nullable);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900298 typenames_.Reset();
Christopher Wileyec31a052016-01-25 07:28:51 -0800299 }
300}
301
302TEST_F(AidlTest, ParsesUtf8Annotations) {
303 for (auto is_utf8: {true, false}) {
304 auto parse_result = Parse(
305 "a/IFoo.aidl",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900306 StringPrintf("package a; interface IFoo {%s String f(); }", (is_utf8) ? "@utf8InCpp" : ""),
307 typenames_, Options::Language::CPP);
Christopher Wileyec31a052016-01-25 07:28:51 -0800308 ASSERT_NE(nullptr, parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700309 const AidlInterface* interface = parse_result->AsInterface();
310 ASSERT_NE(nullptr, interface);
311 ASSERT_FALSE(interface->GetMethods().empty());
312 EXPECT_EQ(interface->GetMethods()[0]->GetType().IsUtf8InCpp(), is_utf8);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900313 typenames_.Reset();
Christopher Wileyec31a052016-01-25 07:28:51 -0800314 }
315}
316
Steven Morelanda57d0a62019-07-30 09:41:14 -0700317TEST_F(AidlTest, VintfRequiresStructuredAndStability) {
318 AidlError error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900319 auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
320 Options::Language::CPP, &error);
Steven Morelanda57d0a62019-07-30 09:41:14 -0700321 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
322 ASSERT_EQ(nullptr, parse_result);
323}
324
325TEST_F(AidlTest, VintfRequiresStructured) {
326 AidlError error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900327 auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
328 Options::Language::CPP, &error, {"--stability", "vintf"});
Steven Morelanda57d0a62019-07-30 09:41:14 -0700329 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
330 ASSERT_EQ(nullptr, parse_result);
331}
332
333TEST_F(AidlTest, VintfRequiresSpecifiedStability) {
334 AidlError error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900335 auto parse_result = Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_,
336 Options::Language::CPP, &error, {"--structured"});
Steven Morelanda57d0a62019-07-30 09:41:14 -0700337 ASSERT_EQ(AidlError::NOT_STRUCTURED, error);
338 ASSERT_EQ(nullptr, parse_result);
339}
340
341TEST_F(AidlTest, ParsesStabilityAnnotations) {
342 AidlError error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900343 auto parse_result =
344 Parse("IFoo.aidl", "@VintfStability interface IFoo {}", typenames_, Options::Language::CPP,
345 &error, {"--structured", "--stability", "vintf"});
Steven Morelanda57d0a62019-07-30 09:41:14 -0700346 ASSERT_EQ(AidlError::OK, error);
347 ASSERT_NE(nullptr, parse_result);
348 const AidlInterface* interface = parse_result->AsInterface();
349 ASSERT_NE(nullptr, interface);
350 ASSERT_TRUE(interface->IsVintfStability());
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900351 typenames_.Reset();
Steven Morelanda57d0a62019-07-30 09:41:14 -0700352}
353
Jeongik Cha64783ed2019-06-07 18:30:54 +0900354TEST_F(AidlTest, ParsesJavaOnlyStableParcelable) {
355 Options java_options = Options::From("aidl -o out --structured a/Foo.aidl");
356 Options cpp_options =
357 Options::From("aidl --lang=cpp --structured -o out -h out/include a/Foo.aidl");
358 io_delegate_.SetFileContents(
359 "a/Foo.aidl", StringPrintf("package a; @JavaOnlyStableParcelable parcelable Foo;"));
360
361 EXPECT_EQ(0, ::android::aidl::compile_aidl(java_options, io_delegate_));
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900362 AddExpectedStderr(
363 "ERROR: a/Foo.aidl:1.48-52: @JavaOnlyStableParcelable supports only Java target.\n");
Jeongik Cha64783ed2019-06-07 18:30:54 +0900364 EXPECT_NE(0, ::android::aidl::compile_aidl(cpp_options, io_delegate_));
365}
366
Christopher Wiley90be4e32015-10-20 14:55:25 -0700367TEST_F(AidlTest, AcceptsOneway) {
Christopher Wiley90be4e32015-10-20 14:55:25 -0700368 string oneway_method = "package a; interface IFoo { oneway void f(int a); }";
369 string oneway_interface =
370 "package a; oneway interface IBar { void f(int a); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900371 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
372 typenames_.Reset();
373 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
374 typenames_.Reset();
375 EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, Options::Language::CPP));
376 typenames_.Reset();
377 EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, typenames_, Options::Language::JAVA));
Christopher Wiley90be4e32015-10-20 14:55:25 -0700378}
Christopher Wileyef140932015-11-03 09:29:19 -0800379
Artur Satayev91fe8712019-07-29 13:06:01 +0100380TEST_F(AidlTest, AcceptsAnnotatedOnewayMethod) {
381 string oneway_method = "package a; interface IFoo { @UnsupportedAppUsage oneway void f(int a); }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900382 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::CPP));
383 typenames_.Reset();
384 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, typenames_, Options::Language::JAVA));
Artur Satayev91fe8712019-07-29 13:06:01 +0100385}
386
387TEST_F(AidlTest, WritesComments) {
388 string foo_interface =
389 "package a; /* foo */ interface IFoo {"
390 " /* i */ int i();"
391 " /* j */ @nullable String j();"
392 " /* k */ @UnsupportedAppUsage oneway void k(int a); }";
393
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900394 auto parse_result = Parse("a/IFoo.aidl", foo_interface, typenames_, Options::Language::JAVA);
Artur Satayev91fe8712019-07-29 13:06:01 +0100395 EXPECT_NE(nullptr, parse_result);
396 EXPECT_EQ("/* foo */", parse_result->GetComments());
397
398 const AidlInterface* interface = parse_result->AsInterface();
399 EXPECT_EQ("/* i */", interface->GetMethods()[0]->GetComments());
400 EXPECT_EQ("/* j */", interface->GetMethods()[1]->GetComments());
401 EXPECT_EQ("/* k */", interface->GetMethods()[2]->GetComments());
402}
403
Christopher Wileyef140932015-11-03 09:29:19 -0800404TEST_F(AidlTest, ParsesPreprocessedFile) {
405 string simple_content = "parcelable a.Foo;\ninterface b.IBar;";
406 io_delegate_.SetFileContents("path", simple_content);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900407 EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").second);
408 EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_));
409 EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").second);
410 EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").second);
Christopher Wileyef140932015-11-03 09:29:19 -0800411}
412
413TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) {
414 string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t";
415 io_delegate_.SetFileContents("path", simple_content);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900416
417 EXPECT_FALSE(typenames_.ResolveTypename("a.Foo").second);
418 EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &typenames_));
419 EXPECT_TRUE(typenames_.ResolveTypename("a.Foo").second);
420 EXPECT_TRUE(typenames_.ResolveTypename("b.IBar").second);
Christopher Wileyef140932015-11-03 09:29:19 -0800421}
422
Christopher Wiley41544372015-11-03 14:52:29 -0800423TEST_F(AidlTest, PreferImportToPreprocessed) {
424 io_delegate_.SetFileContents("preprocessed", "interface another.IBar;");
425 io_delegate_.SetFileContents("one/IBar.aidl", "package one; "
426 "interface IBar {}");
427 preprocessed_files_.push_back("preprocessed");
Jiyong Park8c380532018-08-30 14:55:26 +0900428 import_paths_.emplace("");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900429 auto parse_result = Parse("p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
430 typenames_, Options::Language::JAVA);
Christopher Wiley41544372015-11-03 14:52:29 -0800431 EXPECT_NE(nullptr, parse_result);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900432
Christopher Wiley41544372015-11-03 14:52:29 -0800433 // We expect to know about both kinds of IBar
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900434 EXPECT_TRUE(typenames_.ResolveTypename("one.IBar").second);
435 EXPECT_TRUE(typenames_.ResolveTypename("another.IBar").second);
Christopher Wiley41544372015-11-03 14:52:29 -0800436 // But if we request just "IBar" we should get our imported one.
Steven Moreland02e012e2018-08-02 14:58:10 -0700437 AidlTypeSpecifier ambiguous_type(AIDL_LOCATION_HERE, "IBar", false, nullptr, "");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900438 ambiguous_type.Resolve(typenames_);
439 EXPECT_EQ("one.IBar", ambiguous_type.GetName());
Christopher Wiley41544372015-11-03 14:52:29 -0800440}
441
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800442TEST_F(AidlTest, WritePreprocessedFile) {
443 io_delegate_.SetFileContents("p/Outer.aidl",
444 "package p; parcelable Outer.Inner;");
445 io_delegate_.SetFileContents("one/IBar.aidl", "package one; import p.Outer;"
446 "interface IBar {}");
447
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900448 vector<string> args {
449 "aidl",
450 "--preprocess",
451 "preprocessed",
452 "p/Outer.aidl",
453 "one/IBar.aidl"};
454 Options options = Options::From(args);
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800455 EXPECT_TRUE(::android::aidl::preprocess_aidl(options, io_delegate_));
456
457 string output;
458 EXPECT_TRUE(io_delegate_.GetWrittenContents("preprocessed", &output));
459 EXPECT_EQ("parcelable p.Outer.Inner;\ninterface one.IBar;\n", output);
460}
461
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900462TEST_F(AidlTest, JavaParcelableOutput) {
Andrei Onea9445fc62019-06-27 18:11:59 +0100463 io_delegate_.SetFileContents(
464 "Rect.aidl",
465 "@SystemApi\n"
466 "parcelable Rect {\n"
467 " // Comment\n"
468 " @SystemApi\n"
469 " int x=5;\n"
470 " @SystemApi\n"
471 " @UnsupportedAppUsage(maxTargetSdk = 28, trackingBug = 42, implicitMember = \"dummy\", "
Andrei Onea07fa3e02019-07-04 17:55:19 +0100472 "expectedSignature = \"dummy\", publicAlternatives = \"d\" \n + \"u\" + \n \"m\" \n + \"m\" "
473 "+ \"y\")\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100474 " int y;\n"
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900475 " ParcelFileDescriptor fd;\n"
Andrei Onea9445fc62019-06-27 18:11:59 +0100476 "}");
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900477
478 vector<string> args{"aidl", "Rect.aidl"};
479 Options options = Options::From(args);
480 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
481
482 string output;
483 EXPECT_TRUE(io_delegate_.GetWrittenContents("Rect.java", &output));
Jiyong Parkd4e73c82019-07-24 21:39:15 +0900484 EXPECT_EQ(kExpectedJavaParcelableOutputContests, output);
Jeongik Chaa755c2a2018-12-12 16:28:23 +0900485}
486
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800487TEST_F(AidlTest, RequireOuterClass) {
488 io_delegate_.SetFileContents("p/Outer.aidl",
489 "package p; parcelable Outer.Inner;");
Jiyong Park8c380532018-08-30 14:55:26 +0900490 import_paths_.emplace("");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900491 auto parse_result =
492 Parse("p/IFoo.aidl", "package p; import p.Outer; interface IFoo { void f(in Inner c); }",
493 typenames_, Options::Language::JAVA);
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800494 EXPECT_EQ(nullptr, parse_result);
495}
496
497TEST_F(AidlTest, ParseCompoundParcelableFromPreprocess) {
498 io_delegate_.SetFileContents("preprocessed",
499 "parcelable p.Outer.Inner;");
500 preprocessed_files_.push_back("preprocessed");
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900501 auto parse_result = Parse("p/IFoo.aidl", "package p; interface IFoo { void f(in Inner c); }",
502 typenames_, Options::Language::JAVA);
Christopher Wiley63bce2a2015-11-03 14:55:03 -0800503 // TODO(wiley): This should actually return nullptr because we require
504 // the outer class name. However, for legacy reasons,
505 // this behavior must be maintained. b/17415692
506 EXPECT_NE(nullptr, parse_result);
507}
508
Christopher Wiley632801d2015-11-05 14:15:49 -0800509TEST_F(AidlTest, FailOnParcelable) {
Steven Morelande2c64b42018-09-18 15:06:37 -0700510 io_delegate_.SetFileContents("p/IFoo.aidl", "package p; parcelable IFoo;");
511
Christopher Wiley632801d2015-11-05 14:15:49 -0800512 // By default, we shouldn't fail on parcelable.
Steven Morelande2c64b42018-09-18 15:06:37 -0700513 Options options1 = Options::From("aidl p/IFoo.aidl");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900514 EXPECT_EQ(0, ::android::aidl::compile_aidl(options1, io_delegate_));
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900515
Steven Morelande2c64b42018-09-18 15:06:37 -0700516 // -b considers this an error
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900517 Options options2 = Options::From("aidl -b p/IFoo.aidl");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900518 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
Steven Morelande2c64b42018-09-18 15:06:37 -0700519
520 io_delegate_.SetFileContents("p/IBar.aidl", "package p; parcelable Foo; interface IBar{}");
521
Jiyong Parkda8c6932019-08-12 19:56:08 +0900522 // With '-b' option, a parcelable and an interface should fail.
Steven Morelande2c64b42018-09-18 15:06:37 -0700523 Options options3 = Options::From("aidl p/IBar.aidl");
Jiyong Parkda8c6932019-08-12 19:56:08 +0900524 EXPECT_EQ(0, ::android::aidl::compile_aidl(options3, io_delegate_));
Steven Morelande2c64b42018-09-18 15:06:37 -0700525 Options options4 = Options::From("aidl -b p/IBar.aidl");
526 EXPECT_NE(0, ::android::aidl::compile_aidl(options4, io_delegate_));
Christopher Wiley632801d2015-11-05 14:15:49 -0800527}
528
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700529TEST_F(AidlTest, StructuredFailOnUnstructuredParcelable) {
530 io_delegate_.SetFileContents("o/WhoKnowsWhat.aidl", "package o; parcelable WhoKnowsWhat;");
531 import_paths_.emplace("");
532 AidlError reported_error;
533 auto parse_result =
534 Parse("p/IFoo.aidl",
535 "package p; import o.WhoKnowsWhat; interface IFoo { void f(in WhoKnowsWhat thisIs); }",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900536 typenames_, Options::Language::JAVA, &reported_error, {"--structured"});
Steven Moreland1eac5fa2018-08-27 19:35:05 -0700537 EXPECT_EQ(nullptr, parse_result);
538 EXPECT_EQ(AidlError::NOT_STRUCTURED, reported_error);
539}
540
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700541TEST_F(AidlTest, FailOnDuplicateConstantNames) {
542 AidlError reported_error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900543 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
544 R"(package p;
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700545 interface IFoo {
546 const String DUPLICATED = "d";
547 const int DUPLICATED = 1;
548 }
549 )",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900550 typenames_, Options::Language::CPP, &reported_error));
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700551 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
Christopher Wiley69b44cf2016-05-03 13:43:33 -0700552}
553
Steven Morelandc258abc2018-07-10 14:03:38 -0700554TEST_F(AidlTest, FailOnManyDefinedTypes) {
555 AidlError reported_error;
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900556 AddExpectedStderr("ERROR: p/IFoo.aidl: You must declare only one type per a file.\n");
Steven Morelandc258abc2018-07-10 14:03:38 -0700557 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
558 R"(package p;
559 interface IFoo {}
Steven Morelandc258abc2018-07-10 14:03:38 -0700560 parcelable IBar {}
561 parcelable StructuredParcelable {}
562 interface IBaz {}
Jeongik Cha2a5b4d82019-08-06 19:37:59 +0900563 )",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900564 typenames_, Options::Language::CPP, &reported_error));
Steven Morelandc258abc2018-07-10 14:03:38 -0700565 // Parse success is important for clear error handling even if the cases aren't
566 // actually supported in code generation.
567 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
568}
569
570TEST_F(AidlTest, FailOnNoDefinedTypes) {
571 AidlError reported_error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900572 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl", R"(package p;)", typenames_, Options::Language::CPP,
573 &reported_error));
Jiyong Parkb034bf02018-07-30 17:44:33 +0900574 EXPECT_EQ(AidlError::PARSE_ERROR, reported_error);
Steven Morelandc258abc2018-07-10 14:03:38 -0700575}
576
Roshan Pius3b2203d2016-07-22 16:13:20 -0700577TEST_F(AidlTest, FailOnMalformedConstHexValue) {
578 AidlError reported_error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900579 EXPECT_EQ(nullptr, Parse("p/IFoo.aidl",
580 R"(package p;
Roshan Pius3b2203d2016-07-22 16:13:20 -0700581 interface IFoo {
582 const int BAD_HEX_VALUE = 0xffffffffffffffffff;
583 }
584 )",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900585 typenames_, Options::Language::CPP, &reported_error));
Steven Moreland4d12f9a2018-10-31 14:30:55 -0700586 EXPECT_EQ(AidlError::BAD_TYPE, reported_error);
Roshan Pius3b2203d2016-07-22 16:13:20 -0700587}
588
589TEST_F(AidlTest, ParsePositiveConstHexValue) {
590 AidlError reported_error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900591 auto cpp_parse_result = Parse("p/IFoo.aidl",
592 R"(package p;
Roshan Pius3b2203d2016-07-22 16:13:20 -0700593 interface IFoo {
594 const int POSITIVE_HEX_VALUE = 0xf5;
595 }
596 )",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900597 typenames_, Options::Language::CPP, &reported_error);
Roshan Pius3b2203d2016-07-22 16:13:20 -0700598 EXPECT_NE(nullptr, cpp_parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700599 const AidlInterface* interface = cpp_parse_result->AsInterface();
600 ASSERT_NE(nullptr, interface);
Steven Moreland693640b2018-07-19 13:46:27 -0700601 const auto& cpp_constants = interface->GetConstantDeclarations();
602 EXPECT_EQ((size_t)1, cpp_constants.size());
603 EXPECT_EQ("POSITIVE_HEX_VALUE", cpp_constants[0]->GetName());
Steven Moreland860b1942018-08-16 14:59:28 -0700604 EXPECT_EQ("245", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator));
Roshan Pius3b2203d2016-07-22 16:13:20 -0700605}
606
607TEST_F(AidlTest, ParseNegativeConstHexValue) {
608 AidlError reported_error;
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900609 auto cpp_parse_result = Parse("p/IFoo.aidl",
610 R"(package p;
Roshan Pius3b2203d2016-07-22 16:13:20 -0700611 interface IFoo {
612 const int NEGATIVE_HEX_VALUE = 0xffffffff;
613 }
614 )",
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900615 typenames_, Options::Language::CPP, &reported_error);
Roshan Pius3b2203d2016-07-22 16:13:20 -0700616 EXPECT_NE(nullptr, cpp_parse_result);
Steven Moreland5557f1c2018-07-02 13:50:23 -0700617 const AidlInterface* interface = cpp_parse_result->AsInterface();
618 ASSERT_NE(nullptr, interface);
Steven Moreland693640b2018-07-19 13:46:27 -0700619 const auto& cpp_constants = interface->GetConstantDeclarations();
620 EXPECT_EQ((size_t)1, cpp_constants.size());
621 EXPECT_EQ("NEGATIVE_HEX_VALUE", cpp_constants[0]->GetName());
Steven Moreland860b1942018-08-16 14:59:28 -0700622 EXPECT_EQ("-1", cpp_constants[0]->ValueString(cpp::ConstantValueDecorator));
Roshan Pius3b2203d2016-07-22 16:13:20 -0700623}
624
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700625TEST_F(AidlTest, UnderstandsNestedParcelables) {
626 io_delegate_.SetFileContents(
627 "p/Outer.aidl",
628 "package p; parcelable Outer.Inner cpp_header \"baz/header\";");
Jiyong Park8c380532018-08-30 14:55:26 +0900629 import_paths_.emplace("");
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700630 const string input_path = "p/IFoo.aidl";
631 const string input = "package p; import p.Outer; interface IFoo"
632 " { Outer.Inner get(); }";
633
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900634 auto cpp_parse_result = Parse(input_path, input, typenames_, Options::Language::CPP);
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700635 EXPECT_NE(nullptr, cpp_parse_result);
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900636
637 auto pair = typenames_.ResolveTypename("p.Outer.Inner");
638 EXPECT_TRUE(pair.second);
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700639 // C++ uses "::" instead of "." to refer to a inner class.
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900640 AidlTypeSpecifier nested_type(AIDL_LOCATION_HERE, "p.Outer.Inner", false, nullptr, "");
641 EXPECT_EQ("::p::Outer::Inner", cpp::CppNameOf(nested_type, typenames_));
Ningyuan Wangd17c58b2016-09-29 14:33:14 -0700642}
643
Christopher Wiley9078d722015-11-17 10:23:49 -0800644TEST_F(AidlTest, UnderstandsNativeParcelables) {
645 io_delegate_.SetFileContents(
646 "p/Bar.aidl",
Casey Dahlincd639212015-12-15 12:51:04 -0800647 "package p; parcelable Bar cpp_header \"baz/header\";");
Jiyong Park8c380532018-08-30 14:55:26 +0900648 import_paths_.emplace("");
Christopher Wiley9078d722015-11-17 10:23:49 -0800649 const string input_path = "p/IFoo.aidl";
650 const string input = "package p; import p.Bar; interface IFoo { }";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900651 {
652 // C++ understands C++ specific stuff
653 auto cpp_parse_result = Parse(input_path, input, typenames_, Options::Language::CPP);
654 EXPECT_NE(nullptr, cpp_parse_result);
655 auto pair = typenames_.ResolveTypename("p.Bar");
656 EXPECT_TRUE(pair.second);
657 AidlTypeSpecifier native_type(AIDL_LOCATION_HERE, "p.Bar", false, nullptr, "");
658 native_type.Resolve(typenames_);
659 EXPECT_EQ("::p::Bar", cpp::CppNameOf(native_type, typenames_));
660 set<string> headers;
661 cpp::AddHeaders(native_type, typenames_, headers);
662 EXPECT_EQ(1u, headers.size());
663 EXPECT_EQ(1u, headers.count("baz/header"));
664 }
665 typenames_.Reset();
666 {
667 // Java ignores C++ specific stuff
668 auto java_parse_result = Parse(input_path, input, typenames_, Options::Language::JAVA);
669 EXPECT_NE(nullptr, java_parse_result);
670 auto pair = typenames_.ResolveTypename("p.Bar");
671 EXPECT_TRUE(pair.second);
672 AidlTypeSpecifier native_type(AIDL_LOCATION_HERE, "p.Bar", false, nullptr, "");
673 native_type.Resolve(typenames_);
674 EXPECT_EQ("p.Bar", java::InstantiableJavaSignatureOf(native_type));
675 }
Christopher Wiley9078d722015-11-17 10:23:49 -0800676}
677
Christopher Wileyf8136192016-04-12 14:19:35 -0700678TEST_F(AidlTest, WritesCorrectDependencyFile) {
679 // While the in tree build system always gives us an output file name,
680 // other android tools take advantage of our ability to infer the intended
681 // file name. This test makes sure we handle this correctly.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900682 vector<string> args = {
683 "aidl",
684 "-d dep/file/path",
685 "-o place/for/output",
686 "p/IFoo.aidl"};
687 Options options = Options::From(args);
688 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900689 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Christopher Wileyf8136192016-04-12 14:19:35 -0700690 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900691 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Christopher Wileyf8136192016-04-12 14:19:35 -0700692 EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents);
693}
694
Dan Willemsen93298ee2016-11-10 23:55:55 -0800695TEST_F(AidlTest, WritesCorrectDependencyFileNinja) {
696 // While the in tree build system always gives us an output file name,
697 // other android tools take advantage of our ability to infer the intended
698 // file name. This test makes sure we handle this correctly.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900699 vector<string> args = {
700 "aidl",
701 "-d dep/file/path",
702 "--ninja",
703 "-o place/for/output",
704 "p/IFoo.aidl"};
705 Options options = Options::From(args);
706 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; interface IFoo {}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900707 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Dan Willemsen93298ee2016-11-10 23:55:55 -0800708 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900709 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Dan Willemsen93298ee2016-11-10 23:55:55 -0800710 EXPECT_EQ(actual_dep_file_contents, kExpectedNinjaDepFileContents);
711}
712
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700713TEST_F(AidlTest, WritesTrivialDependencyFileForParcelable) {
714 // The SDK uses aidl to decide whether a .aidl file is a parcelable. It does
715 // this by calling aidl with every .aidl file it finds, then parsing the
716 // generated dependency files. Those that reference .java output files are
717 // for interfaces and those that do not are parcelables. However, for both
718 // parcelables and interfaces, we *must* generate a non-empty dependency file.
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900719 vector<string> args = {
720 "aidl",
721 "-o place/for/output",
722 "-d dep/file/path",
723 "p/Foo.aidl"};
724 Options options = Options::From(args);
725 io_delegate_.SetFileContents(options.InputFiles().front(), "package p; parcelable Foo;");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900726 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700727 string actual_dep_file_contents;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900728 EXPECT_TRUE(io_delegate_.GetWrittenContents(options.DependencyFile(), &actual_dep_file_contents));
Christopher Wileyb1bbdf82016-04-21 11:43:45 -0700729 EXPECT_EQ(actual_dep_file_contents, kExpectedParcelableDepFileContents);
730}
731
Jiyong Parkccf00f82018-07-17 01:39:23 +0900732/* not working until type_namespace.h is fixed
733TEST_F(AidlTest, AcceptsNestedContainerType) {
734 string nested_in_iface = "package a; interface IFoo {\n"
735 " List<int, List<String, bool>> foo(); }";
736 string nested_in_parcelable = "package a; parcelable IData {\n"
737 " List<int, List<String, bool>> foo;}";
Jeongik Cha047c5ee2019-08-07 23:16:49 +0900738 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_iface, typenames_, Options::Language::JAVA));
739 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_iface, typenames_, Options::Language::CPP));
740 EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_parcelable, typenames_,
741Options::Language::JAVA)); EXPECT_NE(nullptr, Parse("a/IFoo.aidl", nested_in_parcelable, typenames_,
742Options::Language::CPP));
Jiyong Parkccf00f82018-07-17 01:39:23 +0900743}
744*/
745
Jiyong Park02da7422018-07-16 16:00:26 +0900746TEST_F(AidlTest, ApiDump) {
747 io_delegate_.SetFileContents(
748 "foo/bar/IFoo.aidl",
749 "package foo.bar;\n"
750 "import foo.bar.Data;\n"
Jiyong Parke59c3682018-09-11 23:10:25 +0900751 "// comment\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900752 "interface IFoo {\n"
753 " int foo(out int[] a, String b, boolean c, inout List<String> d);\n"
754 " int foo2(@utf8InCpp String x, inout List<String> y);\n"
755 " IFoo foo3(IFoo foo);\n"
756 " Data getData();\n"
Jiyong Parka428d212018-08-29 22:26:30 +0900757 " const int A = 1;\n"
758 " const String STR = \"Hello\";\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900759 "}\n");
760 io_delegate_.SetFileContents("foo/bar/Data.aidl",
761 "package foo.bar;\n"
762 "import foo.bar.IFoo;\n"
763 "parcelable Data {\n"
Jiyong Parka468e2a2018-08-29 21:25:18 +0900764 " int x = 10;\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900765 " int y;\n"
766 " IFoo foo;\n"
767 " List<IFoo> a;\n"
768 " List<foo.bar.IFoo> b;\n"
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900769 " @nullable String[] c;\n"
Jiyong Park02da7422018-07-16 16:00:26 +0900770 "}\n");
771 io_delegate_.SetFileContents("api.aidl", "");
Jiyong Parke59c3682018-09-11 23:10:25 +0900772 vector<string> args = {"aidl", "--dumpapi", "--out=dump", "foo/bar/IFoo.aidl",
773 "foo/bar/Data.aidl"};
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900774 Options options = Options::From(args);
Jiyong Park02da7422018-07-16 16:00:26 +0900775 bool result = dump_api(options, io_delegate_);
776 ASSERT_TRUE(result);
777 string actual;
Jiyong Parke59c3682018-09-11 23:10:25 +0900778 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual));
779 EXPECT_EQ(actual, R"(package foo.bar;
780interface IFoo {
781 int foo(out int[] a, String b, boolean c, inout List<String> d);
782 int foo2(@utf8InCpp String x, inout List<String> y);
783 foo.bar.IFoo foo3(foo.bar.IFoo foo);
784 foo.bar.Data getData();
785 const int A = 1;
786 const String STR = "Hello";
787}
788)");
Jiyong Park02da7422018-07-16 16:00:26 +0900789
Jiyong Parke59c3682018-09-11 23:10:25 +0900790 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/Data.aidl", &actual));
791 EXPECT_EQ(actual, R"(package foo.bar;
792parcelable Data {
793 int x = 10;
794 int y;
795 foo.bar.IFoo foo;
796 List<foo.bar.IFoo> a;
797 List<foo.bar.IFoo> b;
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900798 @nullable String[] c;
Jiyong Park02da7422018-07-16 16:00:26 +0900799}
800)");
801}
802
Jiyong Parked65bf42018-08-28 15:43:27 +0900803TEST_F(AidlTest, ApiDumpWithManualIds) {
804 io_delegate_.SetFileContents(
805 "foo/bar/IFoo.aidl",
806 "package foo.bar;\n"
807 "interface IFoo {\n"
808 " int foo() = 1;\n"
809 " int bar() = 2;\n"
810 " int baz() = 10;\n"
811 "}\n");
812
Jiyong Parke59c3682018-09-11 23:10:25 +0900813 vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"};
Jiyong Parked65bf42018-08-28 15:43:27 +0900814 Options options = Options::From(args);
815 bool result = dump_api(options, io_delegate_);
816 ASSERT_TRUE(result);
817 string actual;
Jiyong Parke59c3682018-09-11 23:10:25 +0900818 EXPECT_TRUE(io_delegate_.GetWrittenContents("dump/foo/bar/IFoo.aidl", &actual));
819 EXPECT_EQ(actual, R"(package foo.bar;
820interface IFoo {
821 int foo() = 1;
822 int bar() = 2;
823 int baz() = 10;
Jiyong Parked65bf42018-08-28 15:43:27 +0900824}
825)");
826}
827
828TEST_F(AidlTest, ApiDumpWithManualIdsOnlyOnSomeMethods) {
829 io_delegate_.SetFileContents(
830 "foo/bar/IFoo.aidl",
831 "package foo.bar;\n"
832 "interface IFoo {\n"
833 " int foo() = 1;\n"
834 " int bar();\n"
835 " int baz() = 10;\n"
836 "}\n");
837
Jiyong Parke59c3682018-09-11 23:10:25 +0900838 vector<string> args = {"aidl", "--dumpapi", "-o dump", "foo/bar/IFoo.aidl"};
Jiyong Parked65bf42018-08-28 15:43:27 +0900839 Options options = Options::From(args);
840 EXPECT_FALSE(dump_api(options, io_delegate_));
841}
842
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900843TEST_F(AidlTest, CheckNumGenericTypeSecifier) {
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900844 Options options = Options::From("aidl p/IFoo.aidl IFoo.java");
845 io_delegate_.SetFileContents(options.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900846 "package p; interface IFoo {"
847 "void foo(List<String, String> a);}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900848 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900849
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900850 io_delegate_.SetFileContents(options.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900851 "package p; interface IFoo {"
852 "void foo(Map<String> a);}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900853 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900854
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900855 Options options2 = Options::From("aidl p/Data.aidl Data.java");
856 io_delegate_.SetFileContents(options2.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900857 "package p; parcelable Data {"
858 "List<String, String> foo;}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900859 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900860
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900861 io_delegate_.SetFileContents(options2.InputFiles().front(),
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900862 "package p; parcelable Data {"
863 "Map<String> foo;}");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900864 EXPECT_NE(0, ::android::aidl::compile_aidl(options2, io_delegate_));
865}
866
Jeongik Cha0e426012019-07-29 15:57:02 +0900867TEST_F(AidlTest, FailOnMultipleTypesInSingleFile) {
868 std::vector<std::string> rawOptions{"aidl --lang=java -o out foo/bar/Foo.aidl",
869 "aidl --lang=cpp -o out -h out/include foo/bar/Foo.aidl"};
870 for (auto& rawOption : rawOptions) {
871 Options options = Options::From(rawOption);
872 io_delegate_.SetFileContents(options.InputFiles().front(),
873 "package foo.bar;\n"
874 "interface IFoo1 { int foo(); }\n"
875 "interface IFoo2 { int foo(); }\n"
876 "parcelable Data1 { int a; int b;}\n"
877 "parcelable Data2 { int a; int b;}\n");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900878
Jeongik Cha0e426012019-07-29 15:57:02 +0900879 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Parkb034bf02018-07-30 17:44:33 +0900880
Jeongik Cha0e426012019-07-29 15:57:02 +0900881 io_delegate_.SetFileContents(options.InputFiles().front(),
882 "package foo.bar;\n"
883 "interface IFoo1 { int foo(); }\n"
884 "interface IFoo2 { int foo(); }\n");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900885
Jeongik Cha0e426012019-07-29 15:57:02 +0900886 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Parkb034bf02018-07-30 17:44:33 +0900887
Jeongik Cha0e426012019-07-29 15:57:02 +0900888 io_delegate_.SetFileContents(options.InputFiles().front(),
889 "package foo.bar;\n"
890 "parcelable Data1 { int a; int b;}\n"
891 "parcelable Data2 { int a; int b;}\n");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900892
Jeongik Cha0e426012019-07-29 15:57:02 +0900893 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Parkb034bf02018-07-30 17:44:33 +0900894 }
895}
896
897TEST_F(AidlTest, MultipleInputFiles) {
898 Options options = Options::From(
899 "aidl --lang=java -o out foo/bar/IFoo.aidl foo/bar/Data.aidl");
900
901 io_delegate_.SetFileContents(options.InputFiles().at(0),
902 "package foo.bar;\n"
903 "import foo.bar.Data;\n"
904 "interface IFoo { Data getData(); }\n");
905
906 io_delegate_.SetFileContents(options.InputFiles().at(1),
907 "package foo.bar;\n"
908 "import foo.bar.IFoo;\n"
909 "parcelable Data { IFoo foo; }\n");
910
911 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
912
913 string content;
914 for (const auto file : {
915 "out/foo/bar/IFoo.java", "out/foo/bar/Data.java"}) {
916 content.clear();
917 EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content));
918 EXPECT_FALSE(content.empty());
919 }
920}
921
922TEST_F(AidlTest, MultipleInputFilesCpp) {
923 Options options = Options::From("aidl --lang=cpp -o out -h out/include "
924 "foo/bar/IFoo.aidl foo/bar/Data.aidl");
925
926 io_delegate_.SetFileContents(options.InputFiles().at(0),
927 "package foo.bar;\n"
928 "import foo.bar.Data;\n"
929 "interface IFoo { Data getData(); }\n");
930
931 io_delegate_.SetFileContents(options.InputFiles().at(1),
932 "package foo.bar;\n"
933 "import foo.bar.IFoo;\n"
934 "parcelable Data { IFoo foo; }\n");
935
936 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
937
938 string content;
939 for (const auto file : {
Jiyong Parkb03551f2018-08-06 19:20:51 +0900940 "out/foo/bar/IFoo.cpp", "out/foo/bar/Data.cpp",
Jiyong Parkb034bf02018-07-30 17:44:33 +0900941 "out/include/foo/bar/IFoo.h", "out/include/foo/bar/Data.h",
942 "out/include/foo/bar/BpFoo.h", "out/include/foo/bar/BpData.h",
943 "out/include/foo/bar/BnFoo.h", "out/include/foo/bar/BnData.h"}) {
944 content.clear();
945 EXPECT_TRUE(io_delegate_.GetWrittenContents(file, &content));
946 EXPECT_FALSE(content.empty());
947 }
Jiyong Park1d2df7d2018-07-23 15:22:50 +0900948}
949
Jiyong Park309668e2018-07-28 16:55:44 +0900950TEST_F(AidlTest, ConflictWithMetaTransactions) {
951 Options options = Options::From("aidl --lang=java -o place/for/output p/IFoo.aidl");
952 // int getInterfaceVersion() is one of the meta transactions
953 io_delegate_.SetFileContents(options.InputFiles().front(),
954 "package p; interface IFoo {"
955 "int getInterfaceVersion(); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900956 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +0900957
958 // boolean getInterfaceVersion() is not, but should be prevented
959 // because return type is not part of a method signature
960 io_delegate_.SetFileContents(options.InputFiles().front(),
961 "package p; interface IFoo {"
962 "boolean getInterfaceVersion(); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900963 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +0900964
965 // this is another reserved name
966 io_delegate_.SetFileContents(options.InputFiles().front(),
967 "package p; interface IFoo {"
968 "String getTransactionName(int code); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900969 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +0900970
971 // this is not a meta interface method as it differs type arguments
972 io_delegate_.SetFileContents(options.InputFiles().front(),
973 "package p; interface IFoo {"
974 "String getTransactionName(); }");
Jiyong Parkb034bf02018-07-30 17:44:33 +0900975 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
Jiyong Park309668e2018-07-28 16:55:44 +0900976}
977
Daniel Norman85aed542019-08-21 12:01:14 -0700978TEST_F(AidlTest, DifferentOrderAnnotationsInCheckAPI) {
Jeongik Cha3271ffa2018-12-04 15:19:20 +0900979 Options options = Options::From("aidl --checkapi old new");
980 io_delegate_.SetFileContents("old/p/IFoo.aidl",
981 "package p; interface IFoo{ @utf8InCpp @nullable String foo();}");
982 io_delegate_.SetFileContents("new/p/IFoo.aidl",
983 "package p; interface IFoo{ @nullable @utf8InCpp String foo();}");
984
985 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
986}
987
Jiyong Park3656c3c2018-08-01 20:02:01 +0900988TEST_F(AidlTest, SuccessOnIdenticalApiDumps) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900989 Options options = Options::From("aidl --checkapi old new");
990 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo();}");
991 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo();}");
Jiyong Park3656c3c2018-08-01 20:02:01 +0900992
993 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
994}
995
Daniel Norman85aed542019-08-21 12:01:14 -0700996class AidlTestCompatibleChanges : public AidlTest {
997 protected:
998 Options options_ = Options::From("aidl --checkapi old new");
999};
1000
1001TEST_F(AidlTestCompatibleChanges, NewType) {
1002 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1003 "package p;"
1004 "interface IFoo {"
1005 " void foo(int a);"
1006 "}");
1007 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1008 "package p;"
1009 "interface IFoo {"
1010 " void foo(int a);"
1011 "}");
1012 io_delegate_.SetFileContents("new/p/IBar.aidl",
1013 "package p;"
1014 "interface IBar {"
1015 " void bar();"
1016 "}");
1017 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1018}
1019
1020TEST_F(AidlTestCompatibleChanges, NewMethod) {
1021 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1022 "package p;"
1023 "interface IFoo {"
1024 " void foo(int a);"
1025 "}");
1026 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1027 "package p;"
1028 "interface IFoo {"
1029 " void foo(int a);"
1030 " void bar();"
1031 "}");
1032 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1033}
1034
1035TEST_F(AidlTestCompatibleChanges, NewField) {
1036 io_delegate_.SetFileContents("old/p/Data.aidl",
1037 "package p;"
1038 "parcelable Data {"
1039 " int foo;"
1040 "}");
1041 io_delegate_.SetFileContents("new/p/Data.aidl",
1042 "package p;"
1043 "parcelable Data {"
1044 " int foo;"
1045 " int bar;"
1046 "}");
1047 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1048}
1049
1050TEST_F(AidlTestCompatibleChanges, NewEnumerator) {
1051 io_delegate_.SetFileContents("old/p/Enum.aidl",
1052 "package p;"
1053 "enum Enum {"
1054 " FOO = 1,"
1055 "}");
1056 io_delegate_.SetFileContents("new/p/Enum.aidl",
1057 "package p;"
1058 "enum Enum {"
1059 " FOO = 1,"
1060 " BAR = 2,"
1061 "}");
1062 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1063}
1064
1065TEST_F(AidlTestCompatibleChanges, ReorderedEnumerator) {
1066 io_delegate_.SetFileContents("old/p/Enum.aidl",
1067 "package p;"
1068 "enum Enum {"
1069 " FOO = 1,"
1070 " BAR = 2,"
1071 "}");
1072 io_delegate_.SetFileContents("new/p/Enum.aidl",
1073 "package p;"
1074 "enum Enum {"
1075 " BAR = 2,"
1076 " FOO = 1,"
1077 "}");
1078 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1079}
1080
1081TEST_F(AidlTestCompatibleChanges, NewPackage) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001082 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1083 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001084 "interface IFoo {"
1085 " void foo(int a);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001086 "}");
1087 io_delegate_.SetFileContents("old/p/Data.aidl",
1088 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001089 "parcelable Data {"
1090 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001091 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001092 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1093 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001094 "interface IFoo {"
1095 " void foo(int a);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001096 "}");
1097 io_delegate_.SetFileContents("new/p/Data.aidl",
1098 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001099 "parcelable Data {"
1100 " int foo;"
Jiyong Parke59c3682018-09-11 23:10:25 +09001101 "}");
1102 io_delegate_.SetFileContents("new/q/IFoo.aidl",
1103 "package q;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001104 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +09001105 " void foo(int a);"
1106 "}");
1107 io_delegate_.SetFileContents("new/q/Data.aidl",
1108 "package q;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001109 "parcelable Data {"
1110 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001111 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001112 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1113}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001114
Daniel Norman85aed542019-08-21 12:01:14 -07001115TEST_F(AidlTestCompatibleChanges, ArgNameChange) {
1116 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1117 "package p;"
1118 "interface IFoo {"
1119 " void foo(int a);"
1120 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001121 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1122 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001123 "interface IFoo {"
1124 " void foo(int b);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001125 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001126 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1127}
Jiyong Parke59c3682018-09-11 23:10:25 +09001128
Daniel Norman85aed542019-08-21 12:01:14 -07001129TEST_F(AidlTestCompatibleChanges, AddedConstValue) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001130 io_delegate_.SetFileContents("old/p/I.aidl",
1131 "package p; interface I {"
1132 "const int A = 1; }");
1133 io_delegate_.SetFileContents("new/p/I.aidl",
1134 "package p ; interface I {"
1135 "const int A = 1; const int B = 2;}");
Daniel Norman85aed542019-08-21 12:01:14 -07001136 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
1137}
Jiyong Parka428d212018-08-29 22:26:30 +09001138
Daniel Norman85aed542019-08-21 12:01:14 -07001139TEST_F(AidlTestCompatibleChanges, ChangedConstValueOrder) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001140 io_delegate_.SetFileContents("old/p/I.aidl",
1141 "package p; interface I {"
1142 "const int A = 1; const int B = 2;}");
1143 io_delegate_.SetFileContents("new/p/I.aidl",
1144 "package p ; interface I {"
1145 "const int B = 2; const int A = 1;}");
Daniel Norman85aed542019-08-21 12:01:14 -07001146 EXPECT_TRUE(::android::aidl::check_api(options_, io_delegate_));
Jiyong Park3656c3c2018-08-01 20:02:01 +09001147}
1148
Daniel Norman85aed542019-08-21 12:01:14 -07001149class AidlTestIncompatibleChanges : public AidlTest {
1150 protected:
1151 Options options_ = Options::From("aidl --checkapi old new");
1152};
1153
1154TEST_F(AidlTestIncompatibleChanges, RemovedType) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001155 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1156 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001157 "interface IFoo {"
1158 " void foo(in String[] str);"
1159 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001160 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001161 io_delegate_.SetFileContents("new/p/IFoo.aidl", "");
1162 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1163}
1164
1165TEST_F(AidlTestIncompatibleChanges, RemovedMethod) {
1166 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1167 "package p;"
1168 "interface IFoo {"
1169 " void foo(in String[] str);"
1170 " void bar(@utf8InCpp String str);"
1171 "}");
1172 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1173 "package p;"
1174 "interface IFoo {"
1175 " void foo(in String[] str);"
1176 "}");
1177 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1178}
1179
1180TEST_F(AidlTestIncompatibleChanges, RemovedField) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001181 io_delegate_.SetFileContents("old/p/Data.aidl",
1182 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001183 "parcelable Data {"
1184 " int foo;"
1185 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001186 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001187 io_delegate_.SetFileContents("new/p/Data.aidl",
1188 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001189 "parcelable Data {"
1190 " int foo;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001191 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001192 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1193}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001194
Daniel Norman85aed542019-08-21 12:01:14 -07001195TEST_F(AidlTestIncompatibleChanges, RemovedEnumerator) {
1196 io_delegate_.SetFileContents("old/p/Enum.aidl",
1197 "package p;"
1198 "enum Enum {"
1199 " FOO = 1,"
1200 " BAR = 2,"
1201 "}");
1202 io_delegate_.SetFileContents("new/p/Enum.aidl",
1203 "package p;"
1204 "enum Enum {"
1205 " BAR = 2,"
1206 "}");
1207 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1208}
1209
1210TEST_F(AidlTestIncompatibleChanges, RenamedMethod) {
1211 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1212 "package p;"
1213 "interface IFoo {"
1214 " void foo(in String[] str);"
1215 " void bar(@utf8InCpp String str);"
1216 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001217 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1218 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001219 "interface IFoo {"
1220 " void foo(in String[] str);"
1221 " void bar2(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001222 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001223 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1224}
1225
1226TEST_F(AidlTestIncompatibleChanges, RenamedField) {
1227 io_delegate_.SetFileContents("old/p/Data.aidl",
Jiyong Parke59c3682018-09-11 23:10:25 +09001228 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001229 "parcelable Data {"
1230 " int foo;"
1231 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001232 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001233 io_delegate_.SetFileContents("new/p/Data.aidl",
1234 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001235 "parcelable Data {"
1236 " int foo;"
1237 " int bar2;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001238 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001239 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1240}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001241
Daniel Norman85aed542019-08-21 12:01:14 -07001242TEST_F(AidlTestIncompatibleChanges, RenamedType) {
1243 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1244 "package p;"
1245 "interface IFoo {"
1246 " void foo(in String[] str);"
1247 " void bar(@utf8InCpp String str);"
1248 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001249 io_delegate_.SetFileContents("new/p/IFoo2.aidl",
1250 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001251 "interface IFoo2 {"
1252 " void foo(in String[] str);"
1253 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001254 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001255 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1256}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001257
Daniel Norman85aed542019-08-21 12:01:14 -07001258TEST_F(AidlTestIncompatibleChanges, ChangedEnumerator) {
1259 io_delegate_.SetFileContents("old/p/Enum.aidl",
1260 "package p;"
1261 "enum Enum {"
1262 " FOO = 1,"
1263 " BAR = 2,"
1264 "}");
1265 io_delegate_.SetFileContents("new/p/Enum.aidl",
1266 "package p;"
1267 "enum Enum {"
1268 " FOO = 3,"
1269 " BAR = 2,"
1270 "}");
1271 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1272}
1273
1274TEST_F(AidlTestIncompatibleChanges, ReorderedMethod) {
1275 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1276 "package p;"
1277 "interface IFoo {"
1278 " void foo(in String[] str);"
1279 " void bar(@utf8InCpp String str);"
1280 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001281 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1282 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001283 "interface IFoo {"
1284 " void bar(@utf8InCpp String str);"
1285 " void foo(in String[] str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001286 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001287 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1288}
1289
1290TEST_F(AidlTestIncompatibleChanges, ReorderedField) {
1291 io_delegate_.SetFileContents("old/p/Data.aidl",
Jiyong Parke59c3682018-09-11 23:10:25 +09001292 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001293 "parcelable Data {"
1294 " int foo;"
1295 " int bar;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001296 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001297 io_delegate_.SetFileContents("new/p/Data.aidl",
1298 "package p;"
1299 "parcelable Data {"
1300 " int bar;"
1301 " int foo;"
1302 "}");
1303 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1304}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001305
Daniel Norman85aed542019-08-21 12:01:14 -07001306TEST_F(AidlTestIncompatibleChanges, ChangedDirectionSpecifier) {
1307 io_delegate_.SetFileContents("old/p/IFoo.aidl",
Jiyong Parke59c3682018-09-11 23:10:25 +09001308 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001309 "interface IFoo {"
1310 " void foo(in String[] str);"
1311 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001312 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001313 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1314 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001315 "interface IFoo {"
1316 " void foo(out String[] str);"
1317 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001318 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001319 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1320}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001321
Daniel Norman85aed542019-08-21 12:01:14 -07001322TEST_F(AidlTestIncompatibleChanges, AddedAnnotation) {
1323 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1324 "package p;"
1325 "interface IFoo {"
1326 " void foo(in String[] str);"
1327 " void bar(@utf8InCpp String str);"
1328 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001329 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1330 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001331 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +09001332 " void foo(in @utf8InCpp String[] str);"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001333 " void bar(@utf8InCpp String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001334 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001335 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1336}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001337
Daniel Norman85aed542019-08-21 12:01:14 -07001338TEST_F(AidlTestIncompatibleChanges, RemovedAnnotation) {
1339 io_delegate_.SetFileContents("old/p/IFoo.aidl",
1340 "package p;"
1341 "interface IFoo {"
1342 " void foo(in String[] str);"
1343 " void bar(@utf8InCpp String str);"
1344 "}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001345 io_delegate_.SetFileContents("new/p/IFoo.aidl",
1346 "package p;"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001347 "interface IFoo {"
Jiyong Parke59c3682018-09-11 23:10:25 +09001348 " void foo(in String[] str);"
Jiyong Park3656c3c2018-08-01 20:02:01 +09001349 " void bar(String str);"
Jiyong Parke59c3682018-09-11 23:10:25 +09001350 "}");
Daniel Norman85aed542019-08-21 12:01:14 -07001351 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1352}
Jiyong Park3656c3c2018-08-01 20:02:01 +09001353
Daniel Norman85aed542019-08-21 12:01:14 -07001354TEST_F(AidlTestIncompatibleChanges, RemovedPackage) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001355 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{}");
1356 io_delegate_.SetFileContents("old/q/IFoo.aidl", "package q; interface IFoo{}");
Jiyong Parke59c3682018-09-11 23:10:25 +09001357 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{}");
Daniel Norman85aed542019-08-21 12:01:14 -07001358 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1359}
Jiyong Parka468e2a2018-08-29 21:25:18 +09001360
Daniel Norman85aed542019-08-21 12:01:14 -07001361TEST_F(AidlTestIncompatibleChanges, ChangedDefaultValue) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001362 io_delegate_.SetFileContents("old/p/D.aidl", "package p; parcelable D { int a = 1; }");
1363 io_delegate_.SetFileContents("new/p/D.aidl", "package p; parcelable D { int a = 2; }");
Daniel Norman85aed542019-08-21 12:01:14 -07001364 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1365}
Jiyong Parka428d212018-08-29 22:26:30 +09001366
Daniel Norman85aed542019-08-21 12:01:14 -07001367TEST_F(AidlTestIncompatibleChanges, RemovedConstValue) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001368 io_delegate_.SetFileContents("old/p/I.aidl",
1369 "package p; interface I {"
1370 "const int A = 1; const int B = 2;}");
1371 io_delegate_.SetFileContents("new/p/I.aidl", "package p; interface I { const int A = 1; }");
Daniel Norman85aed542019-08-21 12:01:14 -07001372 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
1373}
Jiyong Parka428d212018-08-29 22:26:30 +09001374
Daniel Norman85aed542019-08-21 12:01:14 -07001375TEST_F(AidlTestIncompatibleChanges, ChangedConstValue) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001376 io_delegate_.SetFileContents("old/p/I.aidl", "package p; interface I { const int A = 1; }");
1377 io_delegate_.SetFileContents("new/p/I.aidl", "package p; interface I { const int A = 2; }");
Daniel Norman85aed542019-08-21 12:01:14 -07001378 EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
Jiyong Park3c35e392018-08-30 13:10:30 +09001379}
1380
Jiyong Park8c380532018-08-30 14:55:26 +09001381TEST_F(AidlTest, RejectAmbiguousImports) {
1382 Options options = Options::From("aidl --lang=java -o out -I dir1 -I dir2 p/IFoo.aidl");
1383 io_delegate_.SetFileContents("p/IFoo.aidl", "package p; import q.IBar; interface IFoo{}");
1384 io_delegate_.SetFileContents("dir1/q/IBar.aidl", "package q; interface IBar{}");
1385 io_delegate_.SetFileContents("dir2/q/IBar.aidl", "package q; interface IBar{}");
1386
1387 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1388}
1389
Jiyong Parked65bf42018-08-28 15:43:27 +09001390TEST_F(AidlTest, HandleManualIdAssignments) {
Jiyong Parke59c3682018-09-11 23:10:25 +09001391 Options options = Options::From("aidl --checkapi old new");
1392 io_delegate_.SetFileContents("old/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}");
1393 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 10;}");
Jiyong Parked65bf42018-08-28 15:43:27 +09001394
1395 EXPECT_TRUE(::android::aidl::check_api(options, io_delegate_));
1396
Jiyong Parke59c3682018-09-11 23:10:25 +09001397 io_delegate_.SetFileContents("new/p/IFoo.aidl", "package p; interface IFoo{ void foo() = 11;}");
Jiyong Parked65bf42018-08-28 15:43:27 +09001398 EXPECT_FALSE(::android::aidl::check_api(options, io_delegate_));
1399}
1400
Jiyong Parke05195e2018-10-08 18:24:23 +09001401TEST_F(AidlTest, ParcelFileDescriptorIsBuiltinType) {
1402 Options javaOptions = Options::From("aidl --lang=java -o out p/IFoo.aidl");
1403 Options cppOptions = Options::From("aidl --lang=cpp -h out -o out p/IFoo.aidl");
1404
1405 // use without import
1406 io_delegate_.SetFileContents("p/IFoo.aidl",
1407 "package p; interface IFoo{ void foo(in ParcelFileDescriptor fd);}");
1408 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1409 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1410
1411 // use without impot but with full name
1412 io_delegate_.SetFileContents(
1413 "p/IFoo.aidl",
1414 "package p; interface IFoo{ void foo(in android.os.ParcelFileDescriptor fd);}");
1415 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1416 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1417
1418 // use with import (as before)
1419 io_delegate_.SetFileContents("p/IFoo.aidl",
1420 "package p;"
1421 "import android.os.ParcelFileDescriptor;"
1422 "interface IFoo{"
1423 " void foo(in ParcelFileDescriptor fd);"
1424 "}");
1425 EXPECT_EQ(0, ::android::aidl::compile_aidl(javaOptions, io_delegate_));
1426 EXPECT_EQ(0, ::android::aidl::compile_aidl(cppOptions, io_delegate_));
1427}
Jiyong Parked65bf42018-08-28 15:43:27 +09001428
Jiyong Park3633b722019-04-11 15:38:26 +09001429TEST_F(AidlTest, ManualIds) {
1430 Options options = Options::From("aidl --lang=java -o out IFoo.aidl");
1431 io_delegate_.SetFileContents("IFoo.aidl",
1432 "interface IFoo {\n"
1433 " void foo() = 0;\n"
1434 " void bar() = 1;\n"
1435 "}");
1436 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1437}
1438
1439TEST_F(AidlTest, ManualIdsWithMetaTransactions) {
1440 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1441 io_delegate_.SetFileContents("IFoo.aidl",
1442 "interface IFoo {\n"
1443 " void foo() = 0;\n"
1444 " void bar() = 1;\n"
1445 "}");
1446 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1447}
1448
1449TEST_F(AidlTest, FailOnDuplicatedIds) {
1450 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1451 io_delegate_.SetFileContents("IFoo.aidl",
1452 "interface IFoo {\n"
1453 " void foo() = 3;\n"
1454 " void bar() = 3;\n"
1455 "}");
1456 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1457}
1458
1459TEST_F(AidlTest, FailOnOutOfRangeIds) {
1460 // 16777115 is kLastMetaMethodId + 1
1461 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1462 io_delegate_.SetFileContents("IFoo.aidl",
1463 "interface IFoo {\n"
1464 " void foo() = 3;\n"
1465 " void bar() = 16777115;\n"
1466 "}");
1467 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1468}
1469
1470TEST_F(AidlTest, FailOnPartiallyAssignedIds) {
1471 Options options = Options::From("aidl --lang=java --version 10 -o out IFoo.aidl");
1472 io_delegate_.SetFileContents("IFoo.aidl",
1473 "interface IFoo {\n"
1474 " void foo() = 3;\n"
1475 " void bar();\n"
1476 "}");
1477 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1478}
1479
Jiyong Parkc6816252019-07-08 08:12:28 +09001480TEST_F(AidlTest, AllowDuplicatedImportPaths) {
1481 Options options = Options::From("aidl --lang=java -I dir -I dir IFoo.aidl");
1482 io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}");
1483 io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}");
1484 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1485}
1486
1487TEST_F(AidlTest, FailOnAmbiguousImports) {
1488 Options options = Options::From("aidl --lang=java -I dir -I dir2 IFoo.aidl");
1489 io_delegate_.SetFileContents("dir/IBar.aidl", "interface IBar{}");
1490 io_delegate_.SetFileContents("dir2/IBar.aidl", "interface IBar{}");
1491 io_delegate_.SetFileContents("IFoo.aidl", "import IBar; interface IFoo{}");
1492 EXPECT_NE(0, ::android::aidl::compile_aidl(options, io_delegate_));
1493}
1494
Jiyong Park56f73d72019-06-11 12:20:28 +09001495class AidlOutputPathTest : public AidlTest {
1496 protected:
1497 void SetUp() override {
1498 AidlTest::SetUp();
1499 io_delegate_.SetFileContents("sub/dir/foo/bar/IFoo.aidl", "package foo.bar; interface IFoo {}");
1500 }
1501
1502 void Test(const Options& options, const std::string expected_output_path) {
1503 EXPECT_EQ(0, ::android::aidl::compile_aidl(options, io_delegate_));
1504 // check the existence
1505 EXPECT_TRUE(io_delegate_.GetWrittenContents(expected_output_path, nullptr));
1506 }
1507};
1508
1509TEST_F(AidlOutputPathTest, OutDirWithNoOutputFile) {
1510 // <out_dir> / <package_name> / <type_name>.java
1511 Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl"), "out/foo/bar/IFoo.java");
1512}
1513
1514TEST_F(AidlOutputPathTest, OutDirWithOutputFile) {
1515 // when output file is explicitly set, it is always respected. -o option is
1516 // ignored.
1517 Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java");
1518}
1519
1520TEST_F(AidlOutputPathTest, NoOutDirWithOutputFile) {
1521 Test(Options::From("aidl -o out sub/dir/foo/bar/IFoo.aidl output/IFoo.java"), "output/IFoo.java");
1522}
1523
1524TEST_F(AidlOutputPathTest, NoOutDirWithNoOutputFile) {
1525 // output is the same as the input file except for the suffix
1526 Test(Options::From("aidl sub/dir/foo/bar/IFoo.aidl"), "sub/dir/foo/bar/IFoo.java");
1527}
1528
Christopher Wiley90be4e32015-10-20 14:55:25 -07001529} // namespace aidl
1530} // namespace android