Google is committed to advancing racial equity for Black communities. See how.

LLCPP FIDL library

Prerequisites

This tutorial builds on the Compiling FIDL tutorial. For more information on other FIDL tutorials, see the Overview.

Overview

This tutorial details how to use the LLCPP FIDL bindings by creating a unit test that you can use as a "playground" for exploring the LLCPP bindings.

This document covers how to complete the following tasks:

The example code is located in your Fuchsia checkout in //examples/fidl/llcpp/unittests/. If you want to write all the code as you follow this tutorial, you can remove the example code:

rm -r examples/fidl/llcpp/unittests/*

Write a C++ device test

  1. Add a gtest stub to examples/fidl/llcpp/unittests/main.cc:

    #include <gtest/gtest.h>
    
    namespace {
    
    } // namespace
    
  2. Define a test target, and a package containg the test in examples/fidl/llcpp/unittests/BUILD.gn:

    import("//src/sys/build/components.gni")
    
    
    test("test") {
      testonly = true
      output_name = "fidl_example_llcpp_test"
      sources = [ "main.cc" ]
      deps = [ "//third_party/googletest:gtest_main" ]
    }
    
    fuchsia_unittest_package("fidl-example-llcpp-test") {
      executable_path = "test/fidl_example_llcpp_test"
      deps = [ ":test" ]
    }
    
    
  3. Build and run the empty test suite on a running instance of Fuchsia:

    fx set core.x64 --with //examples/fidl/llcpp/unittests:fidl-example-llcpp-test
    fx build
    

    In a separate terminal, run

    fx qemu -kN
    

    In a separate terminal, run

    fx serve
    

    Back in the original terminal (with fx build), run

    fx test -vo fidl-example-llcpp-test
    

    You should see test output indicating that zero tests have run, since no tests have been added yet.

Add the LLCPP FIDL bindings as a dependency

For each FIDL library declaration, including the one in Compiling FIDL, C++ library containing LLCPP bindings code for that library is generated under the original target name appended with _llcpp.

Add a dependency on the LLCPP bindings by referencing this target. The new test target should look like:

test("test") {
  testonly = true
  output_name = "fidl_example_llcpp_test"
  sources = [ "main.cc" ]
  deps = [
    "//examples/fidl/fuchsia.examples:fuchsia.examples_llcpp",
    "//third_party/googletest:gtest_main",
  ]
}

(Optional) To view the newly generated bindings:

  1. Rebuild using fx build.
  2. Change to the generated files directory: out/default/fidling/gen/examples/fidl/fuchsia.examples/fuchsia/examples, where the generated files are located. You may need to change out/default if you have set a different build output directory. You can check your build output directory with cat .fx-build-dir.

For more information on how to find generated bindings code, see Viewing generated bindings code.

Include the LLCPP bindings in your C++ code

To include the bindings, add the following include statement to the top of examples/fidl/llcpp/unittests/main.cc

#include <fuchsia/examples/llcpp/fidl.h>

Inspect and use the generated bindings code

You can now write some tests by referring to the generated code. For more information on the bindings, see LLCPP Bindings Reference.

To get started, you can also use some example code. You can add this inside the anonymous namespace in main.cc:

TEST(FidlExamples, Bits) {
  auto flags = llcpp::fuchsia::examples::FileMode::READ |
               llcpp::fuchsia::examples::FileMode::WRITE |
               llcpp::fuchsia::examples::FileMode::EXECUTE;
  ASSERT_EQ(flags, llcpp::fuchsia::examples::FileMode::kMask);
}

TEST(FidlExamples, Enums) {
  ASSERT_EQ(static_cast<uint32_t>(llcpp::fuchsia::examples::LocationType::MUSEUM), 1u);
}

TEST(FidlExamples, Structs) {
  llcpp::fuchsia::examples::Color default_color;
  ASSERT_EQ(default_color.id, 0u);
  // Default values are currently not supported.
  ASSERT_TRUE(default_color.name.is_null());
  ASSERT_TRUE(default_color.name.empty());

  llcpp::fuchsia::examples::Color blue = {1, "blue"};
  ASSERT_EQ(blue.id, 1u);
}

TEST(FidlExamples, Unions) {
  auto int_val = llcpp::fuchsia::examples::JsonValue::WithIntValue(std::make_unique<int32_t>(1));
  auto str_val =
      llcpp::fuchsia::examples::JsonValue::WithStringValue(std::make_unique<fidl::StringView>("1"));
  ASSERT_TRUE(int_val.is_int_value());
  ASSERT_TRUE(str_val.is_string_value());
}

TEST(FidlExamples, Tables) {
  llcpp::fuchsia::examples::User user =
      llcpp::fuchsia::examples::User::Builder(
          std::make_unique<llcpp::fuchsia::examples::User::Frame>())
          .set_age(std::make_unique<uint8_t>(30))
          .set_name(std::make_unique<fidl::StringView>("jdoe"))
          .build();
  ASSERT_FALSE(user.IsEmpty());
  ASSERT_EQ(user.age(), 30);
}

To rebuild and rerun the tests, run:

fx test -vo fidl_crates_test