Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 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 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 17 | #include <memory> |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 18 | #include <set> |
| 19 | #include <string> |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | #include "aidl.h" |
| 26 | #include "aidl_language.h" |
| 27 | #include "tests/fake_io_delegate.h" |
| 28 | #include "type_cpp.h" |
| 29 | #include "type_java.h" |
| 30 | #include "type_namespace.h" |
| 31 | |
| 32 | using android::aidl::test::FakeIoDelegate; |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 33 | using android::base::StringPrintf; |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 34 | using std::set; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 35 | using std::string; |
| 36 | using std::unique_ptr; |
Christopher Wiley | 12e894a | 2016-01-29 11:55:07 -0800 | [diff] [blame] | 37 | using std::vector; |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 38 | using android::aidl::internals::parse_preprocessed_file; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | namespace aidl { |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | const char kExpectedDepFileContents[] = |
| 45 | R"(place/for/output/p/IFoo.java : \ |
| 46 | p/IFoo.aidl |
| 47 | |
| 48 | p/IFoo.aidl : |
| 49 | )"; |
| 50 | |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 51 | const char kExpectedParcelableDepFileContents[] = |
| 52 | R"( : \ |
| 53 | p/Foo.aidl |
| 54 | |
| 55 | p/Foo.aidl : |
| 56 | )"; |
| 57 | |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 58 | } // namespace |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 59 | |
| 60 | class AidlTest : public ::testing::Test { |
| 61 | protected: |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 62 | void SetUp() override { |
| 63 | java_types_.Init(); |
| 64 | cpp_types_.Init(); |
| 65 | } |
| 66 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 67 | unique_ptr<AidlInterface> Parse(const string& path, |
| 68 | const string& contents, |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 69 | TypeNamespace* types, |
| 70 | AidlError* error = nullptr) { |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 71 | io_delegate_.SetFileContents(path, contents); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 72 | unique_ptr<AidlInterface> ret; |
| 73 | std::vector<std::unique_ptr<AidlImport>> imports; |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 74 | AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl( |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 75 | preprocessed_files_, |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 76 | import_paths_, |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 77 | path, |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 78 | io_delegate_, |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 79 | types, |
| 80 | &ret, |
| 81 | &imports); |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 82 | if (error != nullptr) { |
| 83 | *error = actual_error; |
| 84 | } |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 85 | return ret; |
| 86 | } |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 87 | |
| 88 | FakeIoDelegate io_delegate_; |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 89 | vector<string> preprocessed_files_; |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 90 | vector<string> import_paths_; |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 91 | java::JavaTypeNamespace java_types_; |
| 92 | cpp::TypeNamespace cpp_types_; |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | TEST_F(AidlTest, JavaAcceptsMissingPackage) { |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 96 | EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &java_types_)); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 99 | TEST_F(AidlTest, RejectsArraysOfBinders) { |
| 100 | import_paths_.push_back(""); |
| 101 | io_delegate_.SetFileContents("bar/IBar.aidl", |
| 102 | "package bar; interface IBar {}"); |
| 103 | string path = "foo/IFoo.aidl"; |
| 104 | string contents = "package foo;\n" |
| 105 | "import bar.IBar;\n" |
| 106 | "interface IFoo { void f(in IBar[] input); }"; |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 107 | EXPECT_EQ(nullptr, Parse(path, contents, &java_types_)); |
| 108 | EXPECT_EQ(nullptr, Parse(path, contents, &cpp_types_)); |
Christopher Wiley | 0522cd5 | 2015-10-28 15:39:44 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 111 | TEST_F(AidlTest, CppRejectsMissingPackage) { |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 112 | EXPECT_EQ(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &cpp_types_)); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 113 | EXPECT_NE(nullptr, |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 114 | Parse("a/IFoo.aidl", "package a; interface IFoo { }", &cpp_types_)); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST_F(AidlTest, RejectsOnewayOutParameters) { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 118 | string oneway_interface = |
| 119 | "package a; oneway interface IFoo { void f(out int bar); }"; |
| 120 | string oneway_method = |
| 121 | "package a; interface IBar { oneway void f(out int bar); }"; |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 122 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &cpp_types_)); |
| 123 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &java_types_)); |
| 124 | EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &cpp_types_)); |
| 125 | EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &java_types_)); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_F(AidlTest, RejectsOnewayNonVoidReturn) { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 129 | string oneway_method = "package a; interface IFoo { oneway int f(); }"; |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 130 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_)); |
| 131 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_)); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 134 | TEST_F(AidlTest, RejectsNullablePrimitive) { |
| 135 | string oneway_method = "package a; interface IFoo { @nullable int f(); }"; |
| 136 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_)); |
| 137 | EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_)); |
| 138 | } |
| 139 | |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 140 | TEST_F(AidlTest, ParsesNullableAnnotation) { |
| 141 | for (auto is_nullable: {true, false}) { |
| 142 | auto parse_result = Parse( |
| 143 | "a/IFoo.aidl", |
| 144 | StringPrintf( "package a; interface IFoo {%s String f(); }", |
| 145 | (is_nullable) ? "@nullable" : ""), |
| 146 | &cpp_types_); |
| 147 | ASSERT_NE(nullptr, parse_result); |
| 148 | ASSERT_FALSE(parse_result->GetMethods().empty()); |
| 149 | EXPECT_EQ(parse_result->GetMethods()[0]->GetType().IsNullable(), |
| 150 | is_nullable); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | TEST_F(AidlTest, ParsesUtf8Annotations) { |
| 155 | for (auto is_utf8: {true, false}) { |
| 156 | auto parse_result = Parse( |
| 157 | "a/IFoo.aidl", |
| 158 | StringPrintf( "package a; interface IFoo {%s String f(); }", |
Christopher Wiley | 9f40372 | 2016-01-27 16:04:11 -0800 | [diff] [blame] | 159 | (is_utf8) ? "@utf8InCpp" : ""), |
Christopher Wiley | b7e0117 | 2016-01-28 16:32:34 -0800 | [diff] [blame] | 160 | &cpp_types_); |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 161 | ASSERT_NE(nullptr, parse_result); |
| 162 | ASSERT_FALSE(parse_result->GetMethods().empty()); |
Christopher Wiley | 9f40372 | 2016-01-27 16:04:11 -0800 | [diff] [blame] | 163 | EXPECT_EQ(parse_result->GetMethods()[0]->GetType().IsUtf8InCpp(), |
Christopher Wiley | ec31a05 | 2016-01-25 07:28:51 -0800 | [diff] [blame] | 164 | is_utf8); |
| 165 | } |
| 166 | } |
| 167 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 168 | TEST_F(AidlTest, AcceptsOneway) { |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 169 | string oneway_method = "package a; interface IFoo { oneway void f(int a); }"; |
| 170 | string oneway_interface = |
| 171 | "package a; oneway interface IBar { void f(int a); }"; |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 172 | EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_)); |
| 173 | EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_)); |
| 174 | EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &cpp_types_)); |
| 175 | EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &java_types_)); |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 176 | } |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 177 | |
| 178 | TEST_F(AidlTest, ParsesPreprocessedFile) { |
| 179 | string simple_content = "parcelable a.Foo;\ninterface b.IBar;"; |
| 180 | io_delegate_.SetFileContents("path", simple_content); |
Christopher Wiley | 9ab0623 | 2016-01-27 14:55:18 -0800 | [diff] [blame] | 181 | EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo")); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 182 | EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_)); |
Christopher Wiley | 9ab0623 | 2016-01-27 14:55:18 -0800 | [diff] [blame] | 183 | EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo")); |
| 184 | EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar")); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) { |
| 188 | string simple_content = "parcelable a.Foo;\n interface b.IBar ;\t"; |
| 189 | io_delegate_.SetFileContents("path", simple_content); |
Christopher Wiley | 9ab0623 | 2016-01-27 14:55:18 -0800 | [diff] [blame] | 190 | EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo")); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 191 | EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_)); |
Christopher Wiley | 9ab0623 | 2016-01-27 14:55:18 -0800 | [diff] [blame] | 192 | EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo")); |
| 193 | EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar")); |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 196 | TEST_F(AidlTest, PreferImportToPreprocessed) { |
| 197 | io_delegate_.SetFileContents("preprocessed", "interface another.IBar;"); |
| 198 | io_delegate_.SetFileContents("one/IBar.aidl", "package one; " |
| 199 | "interface IBar {}"); |
| 200 | preprocessed_files_.push_back("preprocessed"); |
| 201 | import_paths_.push_back(""); |
| 202 | auto parse_result = Parse( |
| 203 | "p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}", |
| 204 | &java_types_); |
| 205 | EXPECT_NE(nullptr, parse_result); |
| 206 | // We expect to know about both kinds of IBar |
Christopher Wiley | 9ab0623 | 2016-01-27 14:55:18 -0800 | [diff] [blame] | 207 | EXPECT_TRUE(java_types_.HasTypeByCanonicalName("one.IBar")); |
| 208 | EXPECT_TRUE(java_types_.HasTypeByCanonicalName("another.IBar")); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 209 | // But if we request just "IBar" we should get our imported one. |
Christopher Wiley | 9ab0623 | 2016-01-27 14:55:18 -0800 | [diff] [blame] | 210 | AidlType ambiguous_type("IBar", 0, "", false /* not an array */); |
| 211 | const java::Type* type = java_types_.Find(ambiguous_type); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 212 | ASSERT_TRUE(type); |
Christopher Wiley | d21bfee | 2016-01-29 15:11:38 -0800 | [diff] [blame] | 213 | EXPECT_EQ("one.IBar", type->CanonicalName()); |
Christopher Wiley | 4154437 | 2015-11-03 14:52:29 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 216 | TEST_F(AidlTest, WritePreprocessedFile) { |
| 217 | io_delegate_.SetFileContents("p/Outer.aidl", |
| 218 | "package p; parcelable Outer.Inner;"); |
| 219 | io_delegate_.SetFileContents("one/IBar.aidl", "package one; import p.Outer;" |
| 220 | "interface IBar {}"); |
| 221 | |
| 222 | JavaOptions options; |
| 223 | options.output_file_name_ = "preprocessed"; |
| 224 | options.files_to_preprocess_.resize(2); |
| 225 | options.files_to_preprocess_[0] = "p/Outer.aidl"; |
| 226 | options.files_to_preprocess_[1] = "one/IBar.aidl"; |
| 227 | EXPECT_TRUE(::android::aidl::preprocess_aidl(options, io_delegate_)); |
| 228 | |
| 229 | string output; |
| 230 | EXPECT_TRUE(io_delegate_.GetWrittenContents("preprocessed", &output)); |
| 231 | EXPECT_EQ("parcelable p.Outer.Inner;\ninterface one.IBar;\n", output); |
| 232 | } |
| 233 | |
Christopher Wiley | 63bce2a | 2015-11-03 14:55:03 -0800 | [diff] [blame] | 234 | TEST_F(AidlTest, RequireOuterClass) { |
| 235 | io_delegate_.SetFileContents("p/Outer.aidl", |
| 236 | "package p; parcelable Outer.Inner;"); |
| 237 | import_paths_.push_back(""); |
| 238 | auto parse_result = Parse( |
| 239 | "p/IFoo.aidl", |
| 240 | "package p; import p.Outer; interface IFoo { void f(in Inner c); }", |
| 241 | &java_types_); |
| 242 | EXPECT_EQ(nullptr, parse_result); |
| 243 | } |
| 244 | |
| 245 | TEST_F(AidlTest, ParseCompoundParcelableFromPreprocess) { |
| 246 | io_delegate_.SetFileContents("preprocessed", |
| 247 | "parcelable p.Outer.Inner;"); |
| 248 | preprocessed_files_.push_back("preprocessed"); |
| 249 | auto parse_result = Parse( |
| 250 | "p/IFoo.aidl", |
| 251 | "package p; interface IFoo { void f(in Inner c); }", |
| 252 | &java_types_); |
| 253 | // TODO(wiley): This should actually return nullptr because we require |
| 254 | // the outer class name. However, for legacy reasons, |
| 255 | // this behavior must be maintained. b/17415692 |
| 256 | EXPECT_NE(nullptr, parse_result); |
| 257 | } |
| 258 | |
Christopher Wiley | 632801d | 2015-11-05 14:15:49 -0800 | [diff] [blame] | 259 | TEST_F(AidlTest, FailOnParcelable) { |
| 260 | JavaOptions options; |
| 261 | options.input_file_name_ = "p/IFoo.aidl"; |
| 262 | io_delegate_.SetFileContents(options.input_file_name_, |
| 263 | "package p; parcelable IFoo;"); |
| 264 | // By default, we shouldn't fail on parcelable. |
| 265 | EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_)); |
| 266 | options.fail_on_parcelable_ = true; |
| 267 | EXPECT_NE(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_)); |
| 268 | } |
| 269 | |
Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 270 | TEST_F(AidlTest, FailOnDuplicateConstantNames) { |
| 271 | AidlError reported_error; |
| 272 | EXPECT_EQ(nullptr, |
| 273 | Parse("p/IFoo.aidl", |
| 274 | R"(package p; |
| 275 | interface IFoo { |
| 276 | const String DUPLICATED = "d"; |
| 277 | const int DUPLICATED = 1; |
| 278 | } |
| 279 | )", |
| 280 | &cpp_types_, |
| 281 | &reported_error)); |
| 282 | EXPECT_EQ(AidlError::BAD_CONSTANTS, reported_error); |
| 283 | } |
| 284 | |
Roshan Pius | 3b2203d | 2016-07-22 16:13:20 -0700 | [diff] [blame^] | 285 | TEST_F(AidlTest, FailOnMalformedConstHexValue) { |
| 286 | AidlError reported_error; |
| 287 | EXPECT_EQ(nullptr, |
| 288 | Parse("p/IFoo.aidl", |
| 289 | R"(package p; |
| 290 | interface IFoo { |
| 291 | const int BAD_HEX_VALUE = 0xffffffffffffffffff; |
| 292 | } |
| 293 | )", |
| 294 | &cpp_types_, |
| 295 | &reported_error)); |
| 296 | EXPECT_EQ(AidlError::BAD_CONSTANTS, reported_error); |
| 297 | } |
| 298 | |
| 299 | TEST_F(AidlTest, ParsePositiveConstHexValue) { |
| 300 | AidlError reported_error; |
| 301 | auto cpp_parse_result = |
| 302 | Parse("p/IFoo.aidl", |
| 303 | R"(package p; |
| 304 | interface IFoo { |
| 305 | const int POSITIVE_HEX_VALUE = 0xf5; |
| 306 | } |
| 307 | )", |
| 308 | &cpp_types_, |
| 309 | &reported_error); |
| 310 | EXPECT_NE(nullptr, cpp_parse_result); |
| 311 | const auto& cpp_int_constants = cpp_parse_result->GetIntConstants(); |
| 312 | EXPECT_EQ((size_t)1, cpp_int_constants.size()); |
| 313 | EXPECT_EQ("POSITIVE_HEX_VALUE", cpp_int_constants[0]->GetName()); |
| 314 | EXPECT_EQ(245, cpp_int_constants[0]->GetValue()); |
| 315 | } |
| 316 | |
| 317 | TEST_F(AidlTest, ParseNegativeConstHexValue) { |
| 318 | AidlError reported_error; |
| 319 | auto cpp_parse_result = |
| 320 | Parse("p/IFoo.aidl", |
| 321 | R"(package p; |
| 322 | interface IFoo { |
| 323 | const int NEGATIVE_HEX_VALUE = 0xffffffff; |
| 324 | } |
| 325 | )", |
| 326 | &cpp_types_, |
| 327 | &reported_error); |
| 328 | EXPECT_NE(nullptr, cpp_parse_result); |
| 329 | const auto& cpp_int_constants = cpp_parse_result->GetIntConstants(); |
| 330 | EXPECT_EQ((size_t)1, cpp_int_constants.size()); |
| 331 | EXPECT_EQ("NEGATIVE_HEX_VALUE", cpp_int_constants[0]->GetName()); |
| 332 | EXPECT_EQ(-1, cpp_int_constants[0]->GetValue()); |
| 333 | } |
| 334 | |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 335 | TEST_F(AidlTest, UnderstandsNativeParcelables) { |
| 336 | io_delegate_.SetFileContents( |
| 337 | "p/Bar.aidl", |
Casey Dahlin | cd63921 | 2015-12-15 12:51:04 -0800 | [diff] [blame] | 338 | "package p; parcelable Bar cpp_header \"baz/header\";"); |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 339 | import_paths_.push_back(""); |
| 340 | const string input_path = "p/IFoo.aidl"; |
| 341 | const string input = "package p; import p.Bar; interface IFoo { }"; |
| 342 | |
| 343 | // C++ understands C++ specific stuff |
| 344 | auto cpp_parse_result = Parse(input_path, input, &cpp_types_); |
| 345 | EXPECT_NE(nullptr, cpp_parse_result); |
Christopher Wiley | 9ab0623 | 2016-01-27 14:55:18 -0800 | [diff] [blame] | 346 | auto cpp_type = cpp_types_.FindTypeByCanonicalName("p.Bar"); |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 347 | ASSERT_NE(nullptr, cpp_type); |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 348 | EXPECT_EQ("::p::Bar", cpp_type->CppType()); |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 349 | set<string> headers; |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 350 | cpp_type->GetHeaders(&headers); |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 351 | EXPECT_EQ(1u, headers.size()); |
| 352 | EXPECT_EQ(1u, headers.count("baz/header")); |
| 353 | |
| 354 | // Java ignores C++ specific stuff |
| 355 | auto java_parse_result = Parse(input_path, input, &java_types_); |
| 356 | EXPECT_NE(nullptr, java_parse_result); |
Christopher Wiley | 9ab0623 | 2016-01-27 14:55:18 -0800 | [diff] [blame] | 357 | auto java_type = java_types_.FindTypeByCanonicalName("p.Bar"); |
Christopher Wiley | 9078d72 | 2015-11-17 10:23:49 -0800 | [diff] [blame] | 358 | ASSERT_NE(nullptr, java_type); |
| 359 | EXPECT_EQ("p.Bar", java_type->InstantiableName()); |
| 360 | } |
| 361 | |
Christopher Wiley | f813619 | 2016-04-12 14:19:35 -0700 | [diff] [blame] | 362 | TEST_F(AidlTest, WritesCorrectDependencyFile) { |
| 363 | // While the in tree build system always gives us an output file name, |
| 364 | // other android tools take advantage of our ability to infer the intended |
| 365 | // file name. This test makes sure we handle this correctly. |
| 366 | JavaOptions options; |
| 367 | options.input_file_name_ = "p/IFoo.aidl"; |
| 368 | options.output_base_folder_ = "place/for/output"; |
| 369 | options.dep_file_name_ = "dep/file/path"; |
| 370 | io_delegate_.SetFileContents(options.input_file_name_, |
| 371 | "package p; interface IFoo {}"); |
| 372 | EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_)); |
| 373 | string actual_dep_file_contents; |
| 374 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.dep_file_name_, |
| 375 | &actual_dep_file_contents)); |
| 376 | EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents); |
| 377 | } |
| 378 | |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 379 | TEST_F(AidlTest, WritesTrivialDependencyFileForParcelable) { |
| 380 | // The SDK uses aidl to decide whether a .aidl file is a parcelable. It does |
| 381 | // this by calling aidl with every .aidl file it finds, then parsing the |
| 382 | // generated dependency files. Those that reference .java output files are |
| 383 | // for interfaces and those that do not are parcelables. However, for both |
| 384 | // parcelables and interfaces, we *must* generate a non-empty dependency file. |
| 385 | JavaOptions options; |
| 386 | options.input_file_name_ = "p/Foo.aidl"; |
| 387 | options.output_base_folder_ = "place/for/output"; |
| 388 | options.dep_file_name_ = "dep/file/path"; |
| 389 | io_delegate_.SetFileContents(options.input_file_name_, |
| 390 | "package p; parcelable Foo;"); |
| 391 | EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_)); |
| 392 | string actual_dep_file_contents; |
| 393 | EXPECT_TRUE(io_delegate_.GetWrittenContents(options.dep_file_name_, |
| 394 | &actual_dep_file_contents)); |
| 395 | EXPECT_EQ(actual_dep_file_contents, kExpectedParcelableDepFileContents); |
| 396 | } |
| 397 | |
Christopher Wiley | 90be4e3 | 2015-10-20 14:55:25 -0700 | [diff] [blame] | 398 | } // namespace aidl |
| 399 | } // namespace android |