1*84e33947SAndroid Build Coastguard Worker### CHRE Unit Test Framework 2*84e33947SAndroid Build Coastguard Worker 3*84e33947SAndroid Build Coastguard Worker#### Background 4*84e33947SAndroid Build Coastguard Worker 5*84e33947SAndroid Build Coastguard WorkerThe framework aims to provide an environment to test CHRE (and its users) code 6*84e33947SAndroid Build Coastguard Workeron-device, using [Pigweed's][PW_URL] Unit Test [Framework][PW_UT_URL]. Test 7*84e33947SAndroid Build Coastguard Workerinstantiations are syntactically identical to [Googletest][GT_URL], so 8*84e33947SAndroid Build Coastguard Workermodifications to on-host unit tests to run on-device are easier. 9*84e33947SAndroid Build Coastguard Worker 10*84e33947SAndroid Build Coastguard WorkerCHRE recommends running the same host-side gtests on-device using this 11*84e33947SAndroid Build Coastguard Workerframework, to catch subtle bugs. For example, the target CPU may raise an 12*84e33947SAndroid Build Coastguard Workerexception on unaligned access, when the same code would run without any 13*84e33947SAndroid Build Coastguard Workerproblems on a local x86-based machine. 14*84e33947SAndroid Build Coastguard Worker 15*84e33947SAndroid Build Coastguard Worker###### Note 16*84e33947SAndroid Build Coastguard Worker 17*84e33947SAndroid Build Coastguard WorkerOne key difference is to run the tests via a call to `chre::runAllTests` in 18*84e33947SAndroid Build Coastguard Worker_chre/test/common/unit_test.h_, which basically wraps the gtest `RUN_ALL_TESTS` 19*84e33947SAndroid Build Coastguard Workermacro, and implements CHRE specific event handlers for Pigweed's UT Framework. 20*84e33947SAndroid Build Coastguard Worker 21*84e33947SAndroid Build Coastguard Worker#### Running Tests 22*84e33947SAndroid Build Coastguard Worker 23*84e33947SAndroid Build Coastguard WorkerUnder the current incarnation of the CHRE Unit Test Framework, the following 24*84e33947SAndroid Build Coastguard Workersteps need to be taken to run the on-device tests: 25*84e33947SAndroid Build Coastguard Worker* Set to true and export an environment variable called `CHRE_ON_DEVICE_TESTS_ENABLED` 26*84e33947SAndroid Build Coastguard Workerfrom your Makefile invocation before CHRE is built. 27*84e33947SAndroid Build Coastguard Worker * Ensure that this flag is not always set to avoid codesize bloat. 28*84e33947SAndroid Build Coastguard Worker* Append your test source file to `COMMON_SRCS` either in _test/test.mk_ or in 29*84e33947SAndroid Build Coastguard Workeryour own Makefile. 30*84e33947SAndroid Build Coastguard Worker* Call `chre::runAllTests` from somewhere in your code. 31*84e33947SAndroid Build Coastguard Worker 32*84e33947SAndroid Build Coastguard Worker##### Sample code 33*84e33947SAndroid Build Coastguard Worker 34*84e33947SAndroid Build Coastguard WorkerIn _math_test.cc_ 35*84e33947SAndroid Build Coastguard Worker```cpp 36*84e33947SAndroid Build Coastguard Worker#include <gtest/gtest.h> 37*84e33947SAndroid Build Coastguard Worker 38*84e33947SAndroid Build Coastguard WorkerTEST(MyMath, Add) { 39*84e33947SAndroid Build Coastguard Worker int x = 1, y = 2; 40*84e33947SAndroid Build Coastguard Worker int result = myAdd(x, y); 41*84e33947SAndroid Build Coastguard Worker EXPECT_EQ(result, 3); 42*84e33947SAndroid Build Coastguard Worker} 43*84e33947SAndroid Build Coastguard Worker``` 44*84e33947SAndroid Build Coastguard Worker 45*84e33947SAndroid Build Coastguard WorkerIn _some_source.cc_ 46*84e33947SAndroid Build Coastguard Worker```cpp 47*84e33947SAndroid Build Coastguard Worker#include "chre/test/common/unit_test.h" 48*84e33947SAndroid Build Coastguard Worker 49*84e33947SAndroid Build Coastguard Workervoid utEntryFunc() { 50*84e33947SAndroid Build Coastguard Worker chre::runAllTests(); 51*84e33947SAndroid Build Coastguard Worker} 52*84e33947SAndroid Build Coastguard Worker``` 53*84e33947SAndroid Build Coastguard Worker 54*84e33947SAndroid Build Coastguard Worker#### Caveats 55*84e33947SAndroid Build Coastguard Worker 56*84e33947SAndroid Build Coastguard WorkerSome advanced features of gtest (SCOPED_TRACE, etc.) are unsupported by Pigweed. 57*84e33947SAndroid Build Coastguard Worker 58*84e33947SAndroid Build Coastguard Worker#### Compatibility 59*84e33947SAndroid Build Coastguard Worker 60*84e33947SAndroid Build Coastguard WorkerThe framework has been tested with Pigweed's git revision ee460238b8a7ec0a6b4f61fe7e67a12231db6d3e 61*84e33947SAndroid Build Coastguard Worker 62*84e33947SAndroid Build Coastguard Worker[PW_URL]: https://pigweed.dev 63*84e33947SAndroid Build Coastguard Worker[PW_UT_URL]: https://pigweed.googlesource.com/pigweed/pigweed/+/refs/heads/master/pw_unit_test 64*84e33947SAndroid Build Coastguard Worker[GT_URL]: https://github.com/google/googletest 65