end_to_end_tests.cpp revision 80ada3d405cbf76797e007d5fc265d17315aa05c
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
21856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley#include <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("");
778f8cc9b91a8d0b4942a978e9d2edc5337503d583Christopher Wiley  options.input_file_name_ =
78a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley      CanonicalNameToPath(kCanonicalName, ".aidl").value();
79a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  options.output_file_name_ = kJavaOutputPath;
80a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  options.dep_file_name_ = "an/arbitrary/path/to/deps.P";
81ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley
82ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  // Load up our fake file system with data.
83a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  io_delegate_.SetFileContents(options.input_file_name_, kInterfaceDefinition);
84a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  io_delegate_.AddCompoundParcelable("android.test.CompoundParcelable",
85a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley                                     {"Subclass1", "Subclass2"});
86a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  AddStubAidls(kImportedParcelables, kImportedInterfaces);
87ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley
88ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  // Check that we parse correctly.
89a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  EXPECT_EQ(android::aidl::compile_aidl_to_java(options, io_delegate_), 0);
90a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  CheckFileContents(kJavaOutputPath, kExpectedJavaOutput);
91a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  CheckFileContents(options.dep_file_name_, kExpectedJavaDepsOutput);
92856a869ccab7efe1bc73311d1ee8843cee2f705fChristopher Wiley}
934427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley
94af2d510018c546961e502b61682eaf8a019cc974Christopher WileyTEST_F(EndToEndTest, IPingResponderCpp) {
95af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  using namespace ::android::aidl::test_data::ping_responder;
96af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
97af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  const string input_path =
98af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley      CanonicalNameToPath(kCanonicalName, ".aidl").value();
99af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  const string output_file = "path/to/output_file.cpp";
100af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  const size_t argc = 5;
101af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  const char* cmdline[argc + 1] = {
102af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley      "aidl-cpp", "-I.", input_path.c_str(), kGenHeaderDir,
103af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley      output_file.c_str(), nullptr
104af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  };
105af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  auto options = CppOptions::Parse(argc, cmdline);
106af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
107af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  // Set up input paths.
108af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  io_delegate_.SetFileContents(input_path, kInterfaceDefinition);
109af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
110af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  // Check that we parse and generate code correctly.
111af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  EXPECT_EQ(android::aidl::compile_aidl_to_cpp(*options, io_delegate_), 0);
112af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(output_file, kExpectedCppOutput);
113af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(kGenInterfaceHeaderPath, kExpectedIHeaderOutput);
114af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(kGenClientHeaderPath, kExpectedBpHeaderOutput);
115af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley  CheckFileContents(kGenServerHeaderPath, kExpectedBnHeaderOutput);
116af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley}
117af2d510018c546961e502b61682eaf8a019cc974Christopher Wiley
1184427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley}  // namespace android
1194427d8654985209c1ebf2fb4705bb3e1a4262be2Christopher Wiley}  // namespace aidl
120