end_to_end_tests.cpp revision 0a62067f35e957493bc37c4b42dfdcfc16353831
1856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley/*
2856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * Copyright (C) 2015, The Android Open Source Project
3856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley *
4856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * Licensed under the Apache License, Version 2.0 (the "License");
5856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * you may not use this file except in compliance with the License.
6856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * You may obtain a copy of the License at
7856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley *
8856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley *     http://www.apache.org/licenses/LICENSE-2.0
9856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley *
10856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * Unless required by applicable law or agreed to in writing, software
11856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * distributed under the License is distributed on an "AS IS" BASIS,
12856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * See the License for the specific language governing permissions and
14856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley * limitations under the License.
15856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley */
16856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
17856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include <memory>
18856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include <string>
19856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include <vector>
20856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
210a62067f35e957493bc37c4b42dfdcfc16353831Elliott Hughes#include <android-base/logging.h>
22856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include <gtest/gtest.h>
23856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
24856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include "aidl.h"
25856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include "options.h"
26ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley#include "tests/fake_io_delegate.h"
27856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include "tests/test_data.h"
28ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley#include "tests/test_util.h"
29856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
30ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wileyusing android::aidl::test::CanonicalNameToPath;
31ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wileyusing android::aidl::test::FakeIoDelegate;
32856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wileyusing std::string;
33856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wileyusing std::unique_ptr;
34856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wileyusing std::vector;
35856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
364427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wileynamespace android {
374427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wileynamespace aidl {
38856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
39856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wileyclass EndToEndTest : public ::testing::Test {
40856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley protected:
41856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley  virtual void SetUp() {
42856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley  }
43856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
44a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  void AddStubAidls(const char** parcelables, const char** interfaces) {
45ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley    for ( ; *parcelables; ++parcelables) {
46a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley      io_delegate_.AddStubParcelable(*parcelables);
478f3e0f5d67690ce8ea22a967b0cefcc8b1b01cfaChristopher Wiley    }
48ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley    for ( ; *interfaces; ++interfaces) {
49a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley      io_delegate_.AddStubInterface(*interfaces);
50856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley    }
51856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley  }
52856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
53a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  void CheckFileContents(const string& rel_path,
54856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley                         const string& expected_content) {
55a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley    string actual_content;
56a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley    ASSERT_TRUE(io_delegate_.GetWrittenContents(rel_path, &actual_content))
57a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley        << "Expected aidl to write to " << rel_path << " but it did not.";
580edf34239909bbc16e884ea2517e5f3398185d0fCasey Dahlin
59a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley    if (actual_content == expected_content) {
60a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley      return;  // success!
61856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley    }
62a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley
6380ada3d405cbf76797e007d5fc265d17315aa05cCasey Dahlin    test::PrintDiff(expected_content, actual_content);
64a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley    FAIL() << "Actual contents of " << rel_path
65a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley           << " did not match expected content";
66856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley  }
67856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
68a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  FakeIoDelegate io_delegate_;
69856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley};
70856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley
71856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher WileyTEST_F(EndToEndTest, IExampleInterface) {
72a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  using namespace ::android::aidl::test_data::example_interface;
73a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley
7489eaab56f3ff913c247397b858fac3c8d3d4a206Christopher Wiley  JavaOptions options;
758f8cc9b91a8d0b4942a978e9d2edc5337503d583Christopher Wiley  options.fail_on_parcelable_ = true;
76ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  options.import_paths_.push_back("");
77c5442de40df22a763804648f5ee1d16ec28a7cd2Christopher Wiley  options.input_file_name_ = CanonicalNameToPath(kCanonicalName, ".aidl");
78a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  options.output_file_name_ = kJavaOutputPath;
79a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  options.dep_file_name_ = "an/arbitrary/path/to/deps.P";
80ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley
81ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  // Load up our fake file system with data.
82a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  io_delegate_.SetFileContents(options.input_file_name_, kInterfaceDefinition);
83a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  io_delegate_.AddCompoundParcelable("android.test.CompoundParcelable",
84a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley                                     {"Subclass1", "Subclass2"});
85a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  AddStubAidls(kImportedParcelables, kImportedInterfaces);
86ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley
87ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  // Check that we parse correctly.
88a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  EXPECT_EQ(android::aidl::compile_aidl_to_java(options, io_delegate_), 0);
89a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  CheckFileContents(kJavaOutputPath, kExpectedJavaOutput);
9019059cb70c5330787ba88f3860444e6da3ef3ca0Christopher Wiley  CheckFileContents(options.DependencyFilePath(), kExpectedJavaDepsOutput);
91856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley}
924427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley
93af2d510018c546961e502b61682eaf8a019cc974Christopher WileyTEST_F(EndToEndTest, IPingResponderCpp) {
94af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  using namespace ::android::aidl::test_data::ping_responder;
95af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
96c5442de40df22a763804648f5ee1d16ec28a7cd2Christopher Wiley  const string input_path = CanonicalNameToPath(kCanonicalName, ".aidl");
9719059cb70c5330787ba88f3860444e6da3ef3ca0Christopher Wiley  const string output_file = kCppOutputPath;
9819059cb70c5330787ba88f3860444e6da3ef3ca0Christopher Wiley  const size_t argc = 6;
99af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  const char* cmdline[argc + 1] = {
10019059cb70c5330787ba88f3860444e6da3ef3ca0Christopher Wiley      "aidl-cpp", "-ddeps.P", "-I.", input_path.c_str(), kGenHeaderDir,
101af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley      output_file.c_str(), nullptr
102af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  };
103af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  auto options = CppOptions::Parse(argc, cmdline);
104af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
105af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  // Set up input paths.
106af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  io_delegate_.SetFileContents(input_path, kInterfaceDefinition);
10719059cb70c5330787ba88f3860444e6da3ef3ca0Christopher Wiley  AddStubAidls(kImportedParcelables, kImportedInterfaces);
108af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
109af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  // Check that we parse and generate code correctly.
110af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  EXPECT_EQ(android::aidl::compile_aidl_to_cpp(*options, io_delegate_), 0);
111af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(output_file, kExpectedCppOutput);
112af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(kGenInterfaceHeaderPath, kExpectedIHeaderOutput);
113af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(kGenClientHeaderPath, kExpectedBpHeaderOutput);
114af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(kGenServerHeaderPath, kExpectedBnHeaderOutput);
11519059cb70c5330787ba88f3860444e6da3ef3ca0Christopher Wiley  CheckFileContents(options->DependencyFilePath(), kExpectedCppDepsOutput);
116af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley}
117af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
1184427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley}  // namespace android
1194427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley}  // namespace aidl
120