This tutorial builds on the HLCPP getting started tutorials.
This tutorial walks through the process of writing a test for the Echo.EchoString
method. This tutorial demonstrates the usage of two utilities available for testing FIDL protocols implemented in HLCPP:
gtest
test loop fixturesys::testing::ComponentContextProvider
fidl_test_base.h
file provided by the HLCPP bindingsIf you would like to write the code as you follow along, feel free to delete the existing example code:
rm -r examples/fidl/hlcpp/testing/*
The test will be written in examples/fidl/hlcpp/testing/main.cc
.
{%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/testing/main.cc" region_tag="includes" %}
examples/fidl/hlcpp/testing/BUILD.gn
:{%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/testing/BUILD.gn" %}
Add an implementation for the Echo
protocol that is tested:
{%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/testing/main.cc" region_tag="impl" %}
Rather than inheriting from fuchsia::examples::Echo
, this implementation inherits from the corresponding test base class. This means that the implementation only needs to override the methods are being tested (in this case, EchoString
), as well as the NotImplemented_
method, which is called if any of the request handler methods that are not overriden gets called.
Create a test class that wraps the logic of publishing the echo protocol:
{%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/testing/main.cc" region_tag="wrapper" %}
This is similar to the code that is explained in the server tutorial, but the fidl::Binding
is owned by the class so that the binding's destructor gets called when the class is destroyed. This enables the code to publish the echo protocol on each test case given a new instance of the test component context.
{%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/testing/main.cc" region_tag="fixture" %}
The test fixture does the following:
ComponentContextProvider
. Each test, it uses it to create a new test context, and binds the Echo implementation to it using the EchoServerInstance
class.GetProxy()
method initializes a proxy to the current test component context and returns it.Here is an example test that can be written using the text fixture:
{%includecode gerrit_repo="fuchsia/fuchsia" gerrit_path="examples/fidl/hlcpp/testing/main.cc" region_tag="test" %}
Configure your gn build to include the test:
fx set core.x64 --with //examples/fidl/hlcpp/testing
Run the test:
fx test -vo example-hlcpp-protocol-test
You should see the test output indicating a success.
gtest::TestLoopFixture
removes the need for boilerplate async loop setup code. Each test case can simply call RunLoopUntilIdle()
instead of manually managing an async::Loop
.ComponentContextProvider
makes it easy to mock the component context during a test. This is useful to e.g. provide specific capabilities to the a component.