class TestFixture

Defined at line 46 of file ../../src/media/audio/lib/test/test_fixture.h

TestFixture wraps a RealLoopFixture with methods to check for FIDL errors and callbacks.

For example, to check for disconnection:

SomeInterfacePtr ptr;

environment->ConnectToService(ptr.NewRequest());

AddErrorHandler(ptr, "SomeInterface");

... do something that should disconnect ptr ...

ExpectDisconnect(ptr);

Or, to check that a sequence of callbacks are executed as expected:

SomeInterfacePtr ptr;

environment->ConnectToService(ptr.NewRequest());

AddErrorHandler(ptr, "SomeInterface");

int b;

ptr.events().OnA = AddCallback("A")

ptr.events().OnB = AddCallback("B", [

&b

](int x) { b = x; });

// This verifies that callbacks A and B are executed, in that order, that B

// is called with the correct argument, and that the ErrorHandler is not called.

ExpectCallbacks();

EXPECT_EQ(b, 42);

Public Methods

template <class T>
void AddErrorHandler (fidl::InterfacePtr<T> & ptr, const std::string & name)

Add a new ErrorHandler for the given protocol. If this ErrorHandler triggers unexpectedly,

the given name will be included in the test failure message. The InterfacePtr must

live for the duration of this TestFixture.

Defined at line 58 of file ../../src/media/audio/lib/test/test_fixture.h

template <class T>
void AddErrorHandler (fidl::Binding<T> & binding, const std::string & name)

Defined at line 64 of file ../../src/media/audio/lib/test/test_fixture.h

template <class T>
std::shared_ptr<ErrorHandler> ErrorHandlerFor (fidl::InterfacePtr<T> & ptr)

Retrieves a previously-added error handler.

Useful for direct calls to ExpectErrors or ExpectDisconnects. Tests that

use ExpectError or ExpectDisconnect won't need this.

Defined at line 74 of file ../../src/media/audio/lib/test/test_fixture.h

template <class T>
std::shared_ptr<ErrorHandler> ErrorHandlerFor (fidl::Binding<T> & binding)

Defined at line 80 of file ../../src/media/audio/lib/test/test_fixture.h

(lambda at ../../src/media/audio/lib/test/test_fixture.h:100:12) AddUnexpectedCallback (const std::string & name)

Add an unexpected callback. The test will fail if this callback is triggered.

Defined at line 99 of file ../../src/media/audio/lib/test/test_fixture.h

(lambda at ../../src/media/audio/lib/test/test_fixture.h:173:12) AddCallback (const std::string & name)

Add an expected callback to the pending set.

Callbacks are expected to occur in the order in which they are added.

Optionally, provide a custom function to invoke when the expected callback is triggered.

Defined at line 199 of file ../../src/media/audio/lib/test/test_fixture.h

template <typename Callable>
auto AddCallback (const std::string & name, Callable callback)

Defined at line 204 of file ../../src/media/audio/lib/test/test_fixture.h

(lambda at ../../src/media/audio/lib/test/test_fixture.h:173:12) AddCallbackUnordered (const std::string & name)

Like AddCallback, but allow the callback to happen in any order.

Defined at line 208 of file ../../src/media/audio/lib/test/test_fixture.h

template <typename Callable>
auto AddCallbackUnordered (const std::string & name, Callable callback)

Defined at line 213 of file ../../src/media/audio/lib/test/test_fixture.h

void ExpectCallbacks ()

Wait until all pending callbacks are drained. Fails if an error is encountered.

Callbacks are expected to occur in the order they are added. After this method

returns, the pending callback set is emptied and new callbacks may be added for

a future call to ExpectCallbacks().

Defined at line 28 of file ../../src/media/audio/lib/test/test_fixture.cc

void ExpectNoCallbacks (zx::duration timeout, const std::string & msg_for_failure)

Run loop with specified timeout, expecting to reach the timeout. Fails if an error is

encountered, with `msg_for_failure`. The callbacks themselves should include failures

such that if they trigger, they register as unexpected errors. After this method returns,

the pending callback set is emptied and new callbacks may be added for a future call to

ExpectCallback or ExpectNoCallbacks.

Defined at line 80 of file ../../src/media/audio/lib/test/test_fixture.cc

void ExpectErrors (const std::vector<std::shared_ptr<ErrorHandler>> & errors)

Wait for the given ErrorHandlers to trigger with their expected errors. Fails if

different errors are found or if errors are triggered in different ErrorHandlers.

Defined at line 86 of file ../../src/media/audio/lib/test/test_fixture.cc

void ExpectNoUnexpectedErrors (const std::string & msg_for_failure)

Verifies that no unexpected errors have occurred so far.

Defined at line 106 of file ../../src/media/audio/lib/test/test_fixture.cc

void ExpectDisconnects (const std::vector<std::shared_ptr<ErrorHandler>> & errors)

Shorthand to expect many disconnect errors.

Defined at line 121 of file ../../src/media/audio/lib/test/test_fixture.h

template <class T>
void ExpectError (fidl::InterfacePtr<T> & ptr, zx_status_t expected_error)

Shorthand to expect a single error.

Defined at line 131 of file ../../src/media/audio/lib/test/test_fixture.h

bool ErrorOccurred ()

Reports whether any ErrorHandlers have triggered.

Defined at line 134 of file ../../src/media/audio/lib/test/test_fixture.cc

template <class T>
void ExpectDisconnect (fidl::InterfacePtr<T> & ptr)

Defined at line 137 of file ../../src/media/audio/lib/test/test_fixture.h

void RunLoopUntil (fit::function<bool ()> condition, zx::duration step)

Override this method to crash if the condition is not reached within 1 minute.

This helps debug test flakes that surface as deadlocks. New tests should use

RunLoopWithTimeoutOrUntil instead of this method.

Defined at line 150 of file ../../src/media/audio/lib/test/test_fixture.h

Protected Methods

void TearDown ()

Defined at line 23 of file ../../src/media/audio/lib/test/test_fixture.cc

Records