190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley/*
290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * Copyright (C) 2015, The Android Open Source Project
390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley *
490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * Licensed under the Apache License, Version 2.0 (the "License");
590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * you may not use this file except in compliance with the License.
690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * You may obtain a copy of the License at
790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley *
890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley *     http://www.apache.org/licenses/LICENSE-2.0
990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley *
1090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * Unless required by applicable law or agreed to in writing, software
1190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * distributed under the License is distributed on an "AS IS" BASIS,
1290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * See the License for the specific language governing permissions and
1490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley * limitations under the License.
1590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley */
1690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley
1790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include <memory>
1812e894a3b69f045f104a9026765ecbc60ec5967eChristopher Wiley#include <set>
1912e894a3b69f045f104a9026765ecbc60ec5967eChristopher Wiley#include <string>
2090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include <vector>
2190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley
22ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley#include <android-base/stringprintf.h>
2390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include <gtest/gtest.h>
2490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley
2590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "aidl.h"
2690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "aidl_language.h"
2790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "tests/fake_io_delegate.h"
2890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "type_cpp.h"
2990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "type_java.h"
3090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley#include "type_namespace.h"
3190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley
3290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileyusing android::aidl::test::FakeIoDelegate;
33ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wileyusing android::base::StringPrintf;
3412e894a3b69f045f104a9026765ecbc60ec5967eChristopher Wileyusing std::set;
3590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileyusing std::string;
3690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileyusing std::unique_ptr;
3712e894a3b69f045f104a9026765ecbc60ec5967eChristopher Wileyusing std::vector;
38ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wileyusing android::aidl::internals::parse_preprocessed_file;
3990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley
4090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileynamespace android {
4190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileynamespace aidl {
42f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wileynamespace {
43f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley
44f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wileyconst char kExpectedDepFileContents[] =
45f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher WileyR"(place/for/output/p/IFoo.java : \
46f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  p/IFoo.aidl
47f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley
48f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wileyp/IFoo.aidl :
49f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley)";
50f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley
5193298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsenconst char kExpectedNinjaDepFileContents[] =
5293298ee273b14bb658500fa1c87286bb9ae7aabbDan WillemsenR"(place/for/output/p/IFoo.java : \
5393298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  p/IFoo.aidl
5493298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen)";
5593298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen
56b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wileyconst char kExpectedParcelableDepFileContents[] =
57b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher WileyR"( : \
58b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  p/Foo.aidl
59b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley
60b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wileyp/Foo.aidl :
61b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley)";
62b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley
63f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley}  // namespace
6490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley
6590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wileyclass AidlTest : public ::testing::Test {
6690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley protected:
675679952c38925a54b75b087068887002e57f3174Christopher Wiley  void SetUp() override {
685679952c38925a54b75b087068887002e57f3174Christopher Wiley    java_types_.Init();
695679952c38925a54b75b087068887002e57f3174Christopher Wiley    cpp_types_.Init();
705679952c38925a54b75b087068887002e57f3174Christopher Wiley  }
715679952c38925a54b75b087068887002e57f3174Christopher Wiley
7290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley  unique_ptr<AidlInterface> Parse(const string& path,
7390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley                                  const string& contents,
7469b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley                                  TypeNamespace* types,
7569b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley                                  AidlError* error = nullptr) {
760522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley    io_delegate_.SetFileContents(path, contents);
7790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley    unique_ptr<AidlInterface> ret;
7890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley    std::vector<std::unique_ptr<AidlImport>> imports;
7969b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley    AidlError actual_error = ::android::aidl::internals::load_and_validate_aidl(
8041544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley        preprocessed_files_,
810522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley        import_paths_,
8290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley        path,
830522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley        io_delegate_,
8490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley        types,
8590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley        &ret,
8690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley        &imports);
8769b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley    if (error != nullptr) {
8869b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley      *error = actual_error;
8969b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley    }
9090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley    return ret;
9190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley  }
920522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley
930522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley  FakeIoDelegate io_delegate_;
9441544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley  vector<string> preprocessed_files_;
950522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley  vector<string> import_paths_;
965679952c38925a54b75b087068887002e57f3174Christopher Wiley  java::JavaTypeNamespace java_types_;
975679952c38925a54b75b087068887002e57f3174Christopher Wiley  cpp::TypeNamespace cpp_types_;
9890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley};
9990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley
10090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher WileyTEST_F(AidlTest, JavaAcceptsMissingPackage) {
1015679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_NE(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &java_types_));
10290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley}
10390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley
1040522cd58a55ebe894092aef9d42fd4337371de2dChristopher WileyTEST_F(AidlTest, RejectsArraysOfBinders) {
1050522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley  import_paths_.push_back("");
1060522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley  io_delegate_.SetFileContents("bar/IBar.aidl",
1070522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley                               "package bar; interface IBar {}");
1080522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley  string path = "foo/IFoo.aidl";
1090522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley  string contents = "package foo;\n"
1100522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley                    "import bar.IBar;\n"
1110522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley                    "interface IFoo { void f(in IBar[] input); }";
1125679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_EQ(nullptr, Parse(path, contents, &java_types_));
1135679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_EQ(nullptr, Parse(path, contents, &cpp_types_));
1140522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley}
1150522cd58a55ebe894092aef9d42fd4337371de2dChristopher Wiley
11690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher WileyTEST_F(AidlTest, CppRejectsMissingPackage) {
1175679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_EQ(nullptr, Parse("IFoo.aidl", "interface IFoo { }", &cpp_types_));
11890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley  EXPECT_NE(nullptr,
1195679952c38925a54b75b087068887002e57f3174Christopher Wiley            Parse("a/IFoo.aidl", "package a; interface IFoo { }", &cpp_types_));
12090be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley}
12190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley
12290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher WileyTEST_F(AidlTest, RejectsOnewayOutParameters) {
12390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley  string oneway_interface =
12490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley      "package a; oneway interface IFoo { void f(out int bar); }";
12590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley  string oneway_method =
12690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley      "package a; interface IBar { oneway void f(out int bar); }";
1275679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &cpp_types_));
1285679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_interface, &java_types_));
1295679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &cpp_types_));
1305679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_EQ(nullptr, Parse("a/IBar.aidl", oneway_method, &java_types_));
13190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley}
13290be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley
13390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher WileyTEST_F(AidlTest, RejectsOnewayNonVoidReturn) {
13490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley  string oneway_method = "package a; interface IFoo { oneway int f(); }";
1355679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
1365679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
13790be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley}
13890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley
13957dbe24bcb8f4509a876e8445c78d9504d7e616aCasey DahlinTEST_F(AidlTest, RejectsNullablePrimitive) {
14057dbe24bcb8f4509a876e8445c78d9504d7e616aCasey Dahlin  string oneway_method = "package a; interface IFoo { @nullable int f(); }";
14157dbe24bcb8f4509a876e8445c78d9504d7e616aCasey Dahlin  EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
14257dbe24bcb8f4509a876e8445c78d9504d7e616aCasey Dahlin  EXPECT_EQ(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
14357dbe24bcb8f4509a876e8445c78d9504d7e616aCasey Dahlin}
14457dbe24bcb8f4509a876e8445c78d9504d7e616aCasey Dahlin
145ec31a050fd861c5d17f3cb092492243945e865fdChristopher WileyTEST_F(AidlTest, ParsesNullableAnnotation) {
146ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley  for (auto is_nullable: {true, false}) {
147ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley    auto parse_result = Parse(
148ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley        "a/IFoo.aidl",
149ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley        StringPrintf( "package a; interface IFoo {%s String f(); }",
150ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley                     (is_nullable) ? "@nullable" : ""),
151ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley        &cpp_types_);
152ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley    ASSERT_NE(nullptr, parse_result);
153ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley    ASSERT_FALSE(parse_result->GetMethods().empty());
154ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley    EXPECT_EQ(parse_result->GetMethods()[0]->GetType().IsNullable(),
155ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley              is_nullable);
156ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley  }
157ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley}
158ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley
159ec31a050fd861c5d17f3cb092492243945e865fdChristopher WileyTEST_F(AidlTest, ParsesUtf8Annotations) {
160ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley  for (auto is_utf8: {true, false}) {
161ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley    auto parse_result = Parse(
162ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley        "a/IFoo.aidl",
163ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley        StringPrintf( "package a; interface IFoo {%s String f(); }",
1649f40372e71c5e2fdf840eeabb5531f12665e9a91Christopher Wiley                     (is_utf8) ? "@utf8InCpp" : ""),
165b7e0117281fde01c6fca3a26f1daed800ff80fa8Christopher Wiley        &cpp_types_);
166ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley    ASSERT_NE(nullptr, parse_result);
167ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley    ASSERT_FALSE(parse_result->GetMethods().empty());
1689f40372e71c5e2fdf840eeabb5531f12665e9a91Christopher Wiley    EXPECT_EQ(parse_result->GetMethods()[0]->GetType().IsUtf8InCpp(),
169ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley              is_utf8);
170ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley  }
171ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley}
172ec31a050fd861c5d17f3cb092492243945e865fdChristopher Wiley
17390be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher WileyTEST_F(AidlTest, AcceptsOneway) {
17490be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley  string oneway_method = "package a; interface IFoo { oneway void f(int a); }";
17590be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley  string oneway_interface =
17690be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley      "package a; oneway interface IBar { void f(int a); }";
1775679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &cpp_types_));
1785679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_NE(nullptr, Parse("a/IFoo.aidl", oneway_method, &java_types_));
1795679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &cpp_types_));
1805679952c38925a54b75b087068887002e57f3174Christopher Wiley  EXPECT_NE(nullptr, Parse("a/IBar.aidl", oneway_interface, &java_types_));
18190be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley}
182ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley
183ef1409302aa97b1807e55c63588ac0a428f41a06Christopher WileyTEST_F(AidlTest, ParsesPreprocessedFile) {
184ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley  string simple_content = "parcelable a.Foo;\ninterface b.IBar;";
185ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley  io_delegate_.SetFileContents("path", simple_content);
1869ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo"));
187ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley  EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_));
1889ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo"));
1899ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar"));
190ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley}
191ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley
192ef1409302aa97b1807e55c63588ac0a428f41a06Christopher WileyTEST_F(AidlTest, ParsesPreprocessedFileWithWhitespace) {
193ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley  string simple_content = "parcelable    a.Foo;\n  interface b.IBar  ;\t";
194ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley  io_delegate_.SetFileContents("path", simple_content);
1959ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  EXPECT_FALSE(java_types_.HasTypeByCanonicalName("a.Foo"));
196ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley  EXPECT_TRUE(parse_preprocessed_file(io_delegate_, "path", &java_types_));
1979ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  EXPECT_TRUE(java_types_.HasTypeByCanonicalName("a.Foo"));
1989ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  EXPECT_TRUE(java_types_.HasTypeByCanonicalName("b.IBar"));
199ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley}
200ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley
20141544376dc44bfb01309e76557be92e23dbf8d22Christopher WileyTEST_F(AidlTest, PreferImportToPreprocessed) {
20241544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley  io_delegate_.SetFileContents("preprocessed", "interface another.IBar;");
20341544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley  io_delegate_.SetFileContents("one/IBar.aidl", "package one; "
20441544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley                                                "interface IBar {}");
20541544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley  preprocessed_files_.push_back("preprocessed");
20641544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley  import_paths_.push_back("");
20741544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley  auto parse_result = Parse(
20841544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley      "p/IFoo.aidl", "package p; import one.IBar; interface IFoo {}",
20941544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley      &java_types_);
21041544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley  EXPECT_NE(nullptr, parse_result);
21141544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley  // We expect to know about both kinds of IBar
2129ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  EXPECT_TRUE(java_types_.HasTypeByCanonicalName("one.IBar"));
2139ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  EXPECT_TRUE(java_types_.HasTypeByCanonicalName("another.IBar"));
21441544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley  // But if we request just "IBar" we should get our imported one.
2159ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  AidlType ambiguous_type("IBar", 0, "", false /* not an array */);
2169ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  const java::Type* type = java_types_.Find(ambiguous_type);
21741544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley  ASSERT_TRUE(type);
218d21bfee0d45a5eb8ffb09eaae3c6d81f3dd292f7Christopher Wiley  EXPECT_EQ("one.IBar", type->CanonicalName());
21941544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley}
22041544376dc44bfb01309e76557be92e23dbf8d22Christopher Wiley
221c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey DahlinTEST_F(AidlTest, WritePreprocessedFile) {
222c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  io_delegate_.SetFileContents("p/Outer.aidl",
223c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin                               "package p; parcelable Outer.Inner;");
224c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  io_delegate_.SetFileContents("one/IBar.aidl", "package one; import p.Outer;"
225c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin                                                "interface IBar {}");
226c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin
227c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  JavaOptions options;
228c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  options.output_file_name_ = "preprocessed";
229c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  options.files_to_preprocess_.resize(2);
230c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  options.files_to_preprocess_[0] = "p/Outer.aidl";
231c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  options.files_to_preprocess_[1] = "one/IBar.aidl";
232c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  EXPECT_TRUE(::android::aidl::preprocess_aidl(options, io_delegate_));
233c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin
234c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  string output;
235c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  EXPECT_TRUE(io_delegate_.GetWrittenContents("preprocessed", &output));
236c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin  EXPECT_EQ("parcelable p.Outer.Inner;\ninterface one.IBar;\n", output);
237c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin}
238c1f39b4ea30e7313eeb7f3d3fd5ec66ba00f2bb5Casey Dahlin
23963bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher WileyTEST_F(AidlTest, RequireOuterClass) {
24063bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley  io_delegate_.SetFileContents("p/Outer.aidl",
24163bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley                               "package p; parcelable Outer.Inner;");
24263bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley  import_paths_.push_back("");
24363bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley  auto parse_result = Parse(
24463bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley      "p/IFoo.aidl",
24563bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley      "package p; import p.Outer; interface IFoo { void f(in Inner c); }",
24663bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley      &java_types_);
24763bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley  EXPECT_EQ(nullptr, parse_result);
24863bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley}
24963bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley
25063bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher WileyTEST_F(AidlTest, ParseCompoundParcelableFromPreprocess) {
25163bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley  io_delegate_.SetFileContents("preprocessed",
25263bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley                               "parcelable p.Outer.Inner;");
25363bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley  preprocessed_files_.push_back("preprocessed");
25463bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley  auto parse_result = Parse(
25563bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley      "p/IFoo.aidl",
25663bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley      "package p; interface IFoo { void f(in Inner c); }",
25763bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley      &java_types_);
25863bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley  // TODO(wiley): This should actually return nullptr because we require
25963bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley  //              the outer class name.  However, for legacy reasons,
26063bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley  //              this behavior must be maintained.  b/17415692
26163bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley  EXPECT_NE(nullptr, parse_result);
26263bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley}
26363bce2a1090a11d82a98d4463ddf0c82e1b8a6e6Christopher Wiley
264632801d891bd269badc780ab5f0f08e56e6df10aChristopher WileyTEST_F(AidlTest, FailOnParcelable) {
265632801d891bd269badc780ab5f0f08e56e6df10aChristopher Wiley  JavaOptions options;
266632801d891bd269badc780ab5f0f08e56e6df10aChristopher Wiley  options.input_file_name_ = "p/IFoo.aidl";
267632801d891bd269badc780ab5f0f08e56e6df10aChristopher Wiley  io_delegate_.SetFileContents(options.input_file_name_,
268632801d891bd269badc780ab5f0f08e56e6df10aChristopher Wiley                               "package p; parcelable IFoo;");
269632801d891bd269badc780ab5f0f08e56e6df10aChristopher Wiley  // By default, we shouldn't fail on parcelable.
270632801d891bd269badc780ab5f0f08e56e6df10aChristopher Wiley  EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
271632801d891bd269badc780ab5f0f08e56e6df10aChristopher Wiley  options.fail_on_parcelable_ = true;
272632801d891bd269badc780ab5f0f08e56e6df10aChristopher Wiley  EXPECT_NE(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
273632801d891bd269badc780ab5f0f08e56e6df10aChristopher Wiley}
274632801d891bd269badc780ab5f0f08e56e6df10aChristopher Wiley
27569b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher WileyTEST_F(AidlTest, FailOnDuplicateConstantNames) {
27669b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley  AidlError reported_error;
27769b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley  EXPECT_EQ(nullptr,
27869b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley            Parse("p/IFoo.aidl",
27969b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley                   R"(package p;
28069b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley                      interface IFoo {
28169b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley                        const String DUPLICATED = "d";
28269b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley                        const int DUPLICATED = 1;
28369b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley                      }
28469b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley                   )",
28569b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley                   &cpp_types_,
28669b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley                   &reported_error));
28769b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley  EXPECT_EQ(AidlError::BAD_CONSTANTS, reported_error);
28869b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley}
28969b44cf003bd5bda9dcd97a6fe872630c1b2818fChristopher Wiley
2903b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan PiusTEST_F(AidlTest, FailOnMalformedConstHexValue) {
2913b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  AidlError reported_error;
2923b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  EXPECT_EQ(nullptr,
2933b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius            Parse("p/IFoo.aidl",
2943b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius                   R"(package p;
2953b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius                      interface IFoo {
2963b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius                        const int BAD_HEX_VALUE = 0xffffffffffffffffff;
2973b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius                      }
2983b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius                   )",
2993b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius                   &cpp_types_,
3003b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius                   &reported_error));
3013b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  EXPECT_EQ(AidlError::BAD_CONSTANTS, reported_error);
3023b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius}
3033b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius
3043b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan PiusTEST_F(AidlTest, ParsePositiveConstHexValue) {
3053b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  AidlError reported_error;
3063b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  auto cpp_parse_result =
3073b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius    Parse("p/IFoo.aidl",
3083b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius           R"(package p;
3093b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius              interface IFoo {
3103b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius                const int POSITIVE_HEX_VALUE = 0xf5;
3113b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius              }
3123b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius           )",
3133b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius           &cpp_types_,
3143b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius           &reported_error);
3153b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  EXPECT_NE(nullptr, cpp_parse_result);
3163b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  const auto& cpp_int_constants = cpp_parse_result->GetIntConstants();
3173b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  EXPECT_EQ((size_t)1, cpp_int_constants.size());
3183b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  EXPECT_EQ("POSITIVE_HEX_VALUE", cpp_int_constants[0]->GetName());
3193b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  EXPECT_EQ(245, cpp_int_constants[0]->GetValue());
3203b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius}
3213b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius
3223b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan PiusTEST_F(AidlTest, ParseNegativeConstHexValue) {
3233b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  AidlError reported_error;
3243b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  auto cpp_parse_result =
3253b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius    Parse("p/IFoo.aidl",
3263b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius           R"(package p;
3273b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius              interface IFoo {
3283b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius                const int NEGATIVE_HEX_VALUE = 0xffffffff;
3293b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius              }
3303b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius           )",
3313b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius           &cpp_types_,
3323b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius           &reported_error);
3333b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  EXPECT_NE(nullptr, cpp_parse_result);
3343b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  const auto& cpp_int_constants = cpp_parse_result->GetIntConstants();
3353b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  EXPECT_EQ((size_t)1, cpp_int_constants.size());
3363b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  EXPECT_EQ("NEGATIVE_HEX_VALUE", cpp_int_constants[0]->GetName());
3373b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius  EXPECT_EQ(-1, cpp_int_constants[0]->GetValue());
3383b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius}
3393b2203ddadae8b44ebe14a882274707ba58bc9c3Roshan Pius
340d17c58babbf02253e7ed866325ea60352d637879Ningyuan WangTEST_F(AidlTest, UnderstandsNestedParcelables) {
341d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang  io_delegate_.SetFileContents(
342d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang      "p/Outer.aidl",
343d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang      "package p; parcelable Outer.Inner cpp_header \"baz/header\";");
344d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang  import_paths_.push_back("");
345d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang  const string input_path = "p/IFoo.aidl";
346d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang  const string input = "package p; import p.Outer; interface IFoo"
347d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang                       " { Outer.Inner get(); }";
348d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang
349d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang  auto cpp_parse_result = Parse(input_path, input, &cpp_types_);
350d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang  EXPECT_NE(nullptr, cpp_parse_result);
351d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang  auto cpp_type = cpp_types_.FindTypeByCanonicalName("p.Outer.Inner");
352d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang  ASSERT_NE(nullptr, cpp_type);
353d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang  // C++ uses "::" instead of "." to refer to a inner class.
354d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang  EXPECT_EQ("::p::Outer::Inner", cpp_type->CppType());
355d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang}
356d17c58babbf02253e7ed866325ea60352d637879Ningyuan Wang
3579078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher WileyTEST_F(AidlTest, UnderstandsNativeParcelables) {
3589078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  io_delegate_.SetFileContents(
3599078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley      "p/Bar.aidl",
360cd63921a973b742ae1f1b0f7ae3e0ac910aa438cCasey Dahlin      "package p; parcelable Bar cpp_header \"baz/header\";");
3619078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  import_paths_.push_back("");
3629078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  const string input_path = "p/IFoo.aidl";
3639078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  const string input = "package p; import p.Bar; interface IFoo { }";
3649078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley
3659078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  // C++ understands C++ specific stuff
3669078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  auto cpp_parse_result = Parse(input_path, input, &cpp_types_);
3679078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  EXPECT_NE(nullptr, cpp_parse_result);
3689ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  auto cpp_type = cpp_types_.FindTypeByCanonicalName("p.Bar");
3699078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  ASSERT_NE(nullptr, cpp_type);
370a2f77c40fb6287895e2030055c0104eac0a1f73aCasey Dahlin  EXPECT_EQ("::p::Bar", cpp_type->CppType());
3719078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  set<string> headers;
372a2f77c40fb6287895e2030055c0104eac0a1f73aCasey Dahlin  cpp_type->GetHeaders(&headers);
3739078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  EXPECT_EQ(1u, headers.size());
3749078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  EXPECT_EQ(1u, headers.count("baz/header"));
3759078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley
3769078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  // Java ignores C++ specific stuff
3779078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  auto java_parse_result = Parse(input_path, input, &java_types_);
3789078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  EXPECT_NE(nullptr, java_parse_result);
3799ab06237fd51e625286a62fdd4fe5f948e698131Christopher Wiley  auto java_type = java_types_.FindTypeByCanonicalName("p.Bar");
3809078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  ASSERT_NE(nullptr, java_type);
3819078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley  EXPECT_EQ("p.Bar", java_type->InstantiableName());
3829078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley}
3839078d724c0e9017e2a9bf8defb954d5620da1e8aChristopher Wiley
384f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher WileyTEST_F(AidlTest, WritesCorrectDependencyFile) {
385f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  // While the in tree build system always gives us an output file name,
386f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  // other android tools take advantage of our ability to infer the intended
387f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  // file name.  This test makes sure we handle this correctly.
388f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  JavaOptions options;
389f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  options.input_file_name_ = "p/IFoo.aidl";
390f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  options.output_base_folder_ = "place/for/output";
391f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  options.dep_file_name_ = "dep/file/path";
392f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  io_delegate_.SetFileContents(options.input_file_name_,
393f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley                               "package p; interface IFoo {}");
394f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
395f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  string actual_dep_file_contents;
396f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  EXPECT_TRUE(io_delegate_.GetWrittenContents(options.dep_file_name_,
397f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley                                              &actual_dep_file_contents));
398f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley  EXPECT_EQ(actual_dep_file_contents, kExpectedDepFileContents);
399f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley}
400f813619816eb56fd852b6e567bf8b1e79442e3e1Christopher Wiley
40193298ee273b14bb658500fa1c87286bb9ae7aabbDan WillemsenTEST_F(AidlTest, WritesCorrectDependencyFileNinja) {
40293298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  // While the in tree build system always gives us an output file name,
40393298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  // other android tools take advantage of our ability to infer the intended
40493298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  // file name.  This test makes sure we handle this correctly.
40593298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  JavaOptions options;
40693298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  options.input_file_name_ = "p/IFoo.aidl";
40793298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  options.output_base_folder_ = "place/for/output";
40893298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  options.dep_file_name_ = "dep/file/path";
40993298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  options.dep_file_ninja_ = true;
41093298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  io_delegate_.SetFileContents(options.input_file_name_,
41193298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen                               "package p; interface IFoo {}");
41293298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
41393298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  string actual_dep_file_contents;
41493298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  EXPECT_TRUE(io_delegate_.GetWrittenContents(options.dep_file_name_,
41593298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen                                              &actual_dep_file_contents));
41693298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen  EXPECT_EQ(actual_dep_file_contents, kExpectedNinjaDepFileContents);
41793298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen}
41893298ee273b14bb658500fa1c87286bb9ae7aabbDan Willemsen
419b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher WileyTEST_F(AidlTest, WritesTrivialDependencyFileForParcelable) {
420b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  // The SDK uses aidl to decide whether a .aidl file is a parcelable.  It does
421b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  // this by calling aidl with every .aidl file it finds, then parsing the
422b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  // generated dependency files.  Those that reference .java output files are
423b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  // for interfaces and those that do not are parcelables.  However, for both
424b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  // parcelables and interfaces, we *must* generate a non-empty dependency file.
425b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  JavaOptions options;
426b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  options.input_file_name_ = "p/Foo.aidl";
427b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  options.output_base_folder_ = "place/for/output";
428b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  options.dep_file_name_ = "dep/file/path";
429b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  io_delegate_.SetFileContents(options.input_file_name_,
430b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley                               "package p; parcelable Foo;");
431b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  EXPECT_EQ(0, ::android::aidl::compile_aidl_to_java(options, io_delegate_));
432b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  string actual_dep_file_contents;
433b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  EXPECT_TRUE(io_delegate_.GetWrittenContents(options.dep_file_name_,
434b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley                                              &actual_dep_file_contents));
435b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley  EXPECT_EQ(actual_dep_file_contents, kExpectedParcelableDepFileContents);
436b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley}
437b1bbdf80cb9fcd8181aa2cb326317929cfac890cChristopher Wiley
43890be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley}  // namespace aidl
43990be4e3fe6d16ea8e8263b80989ef2ab4e6ef9c6Christopher Wiley}  // namespace android
440