1*84e33947SAndroid Build Coastguard Worker# Testing the CHRE Framework 2*84e33947SAndroid Build Coastguard Worker 3*84e33947SAndroid Build Coastguard Worker[TOC] 4*84e33947SAndroid Build Coastguard Worker 5*84e33947SAndroid Build Coastguard WorkerThe CHRE framework can be tested at various levels to ensure that 6*84e33947SAndroid Build Coastguard Workercomponents/modules components are working correctly and API contracts are being 7*84e33947SAndroid Build Coastguard Workermet. Below are some examples of what the team currently does to verify new 8*84e33947SAndroid Build Coastguard Workerchanges. 9*84e33947SAndroid Build Coastguard Worker 10*84e33947SAndroid Build Coastguard Worker## Unit tests 11*84e33947SAndroid Build Coastguard Worker 12*84e33947SAndroid Build Coastguard Worker### Tests run on a host machine 13*84e33947SAndroid Build Coastguard Worker 14*84e33947SAndroid Build Coastguard WorkerCurrently, unit tests exist for various core components and utilities capable 15*84e33947SAndroid Build Coastguard Workerof running on a Linux host machine. Since 16*84e33947SAndroid Build Coastguard Workerplatform-specific components likely aren’t compilable/available on a host 17*84e33947SAndroid Build Coastguard Workermachine, only components that are OS independent can be tested via this path. 18*84e33947SAndroid Build Coastguard Worker 19*84e33947SAndroid Build Coastguard WorkerIn order to write new tests, add a test source file under the test directory in 20*84e33947SAndroid Build Coastguard Workerthe appropriate subdirectory. e.g. `util/tests`. Then, add the file to the 21*84e33947SAndroid Build Coastguard Worker`GOOGLETEST_SRCS` variable in the appropriate .mk file for that subdir, 22*84e33947SAndroid Build Coastguard Worker`util/util.mk` for example. 23*84e33947SAndroid Build Coastguard Worker 24*84e33947SAndroid Build Coastguard WorkerUnit tests can be built and executed using `run_tests.sh`. 25*84e33947SAndroid Build Coastguard Worker 26*84e33947SAndroid Build Coastguard Worker 27*84e33947SAndroid Build Coastguard Worker### On-device unit tests 28*84e33947SAndroid Build Coastguard Worker 29*84e33947SAndroid Build Coastguard Worker#### Background 30*84e33947SAndroid Build Coastguard Worker 31*84e33947SAndroid Build Coastguard WorkerThe framework aims to provide an environment to test CHRE (and its users) code 32*84e33947SAndroid Build Coastguard Workeron-device, using [Pigweed's][PW_URL] Unit Test [Framework][PW_UT_URL]. Test 33*84e33947SAndroid Build Coastguard Workerinstantiations are syntactically identical to [Googletest][GT_URL], so 34*84e33947SAndroid Build Coastguard Workermodifications to on-host unit tests to run on-device are easier. 35*84e33947SAndroid Build Coastguard Worker 36*84e33947SAndroid Build Coastguard WorkerCHRE recommends running the same host-side gtests on-device using this 37*84e33947SAndroid Build Coastguard Workerframework, to catch subtle bugs. For example, the target CPU may raise an 38*84e33947SAndroid Build Coastguard Workerexception on unaligned access, when the same code would run without any 39*84e33947SAndroid Build Coastguard Workerproblems on a local x86-based machine. 40*84e33947SAndroid Build Coastguard Worker 41*84e33947SAndroid Build Coastguard Worker#### Use Cases 42*84e33947SAndroid Build Coastguard Worker 43*84e33947SAndroid Build Coastguard WorkerExample use cases of the framework include: 44*84e33947SAndroid Build Coastguard Worker 45*84e33947SAndroid Build Coastguard Worker* In continuous integration frameworks with device farms 46*84e33947SAndroid Build Coastguard Worker* As a superior alternative to logging and/or flag based debugging to quickly test a feature 47*84e33947SAndroid Build Coastguard Worker* As a modular set of tests to test feature or peripheral functioning (eg: a system timer implementation) during device bringup. 48*84e33947SAndroid Build Coastguard Worker 49*84e33947SAndroid Build Coastguard Worker###### Note 50*84e33947SAndroid Build Coastguard Worker 51*84e33947SAndroid Build Coastguard WorkerOne key difference is to run the tests via a call to `chre::runAllTests` in 52*84e33947SAndroid Build Coastguard Worker_chre/test/common/unit_test.h_, which basically wraps the gtest `RUN_ALL_TESTS` 53*84e33947SAndroid Build Coastguard Workermacro, and implements CHRE specific event handlers for Pigweed's UT Framework. 54*84e33947SAndroid Build Coastguard Worker 55*84e33947SAndroid Build Coastguard Worker#### Running Tests 56*84e33947SAndroid Build Coastguard Worker 57*84e33947SAndroid Build Coastguard WorkerUnder the current incarnation of the CHRE Unit Test Framework, the following 58*84e33947SAndroid Build Coastguard Workersteps need to be taken to run the on-device tests: 59*84e33947SAndroid Build Coastguard Worker* Set to true and export an environment variable called `CHRE_ON_DEVICE_TESTS_ENABLED` 60*84e33947SAndroid Build Coastguard Workerfrom your Makefile invocation before CHRE is built. 61*84e33947SAndroid Build Coastguard Worker * Ensure that this flag is not always set to avoid codesize bloat. 62*84e33947SAndroid Build Coastguard Worker* Append your test source file to `COMMON_SRCS` either in _test/test.mk_ or in 63*84e33947SAndroid Build Coastguard Workeryour own Makefile. 64*84e33947SAndroid Build Coastguard Worker* Call `chre::runAllTests` from somewhere in your code. 65*84e33947SAndroid Build Coastguard Worker 66*84e33947SAndroid Build Coastguard Worker##### Sample code 67*84e33947SAndroid Build Coastguard Worker 68*84e33947SAndroid Build Coastguard WorkerIn _math_test.cc_ 69*84e33947SAndroid Build Coastguard Worker```cpp 70*84e33947SAndroid Build Coastguard Worker#include <gtest/gtest.h> 71*84e33947SAndroid Build Coastguard Worker 72*84e33947SAndroid Build Coastguard WorkerTEST(MyMath, Add) { 73*84e33947SAndroid Build Coastguard Worker int x = 1, y = 2; 74*84e33947SAndroid Build Coastguard Worker int result = myAdd(x, y); 75*84e33947SAndroid Build Coastguard Worker EXPECT_EQ(result, 3); 76*84e33947SAndroid Build Coastguard Worker} 77*84e33947SAndroid Build Coastguard Worker``` 78*84e33947SAndroid Build Coastguard Worker 79*84e33947SAndroid Build Coastguard WorkerIn _some_source.cc_ 80*84e33947SAndroid Build Coastguard Worker```cpp 81*84e33947SAndroid Build Coastguard Worker#include "chre/test/common/unit_test.h" 82*84e33947SAndroid Build Coastguard Worker 83*84e33947SAndroid Build Coastguard Workervoid utEntryFunc() { 84*84e33947SAndroid Build Coastguard Worker chre::runAllTests(); 85*84e33947SAndroid Build Coastguard Worker} 86*84e33947SAndroid Build Coastguard Worker``` 87*84e33947SAndroid Build Coastguard Worker 88*84e33947SAndroid Build Coastguard Worker#### Caveats 89*84e33947SAndroid Build Coastguard Worker 90*84e33947SAndroid Build Coastguard WorkerSome advanced features of gtest (SCOPED_TRACE, etc.) are unsupported by Pigweed. 91*84e33947SAndroid Build Coastguard Worker 92*84e33947SAndroid Build Coastguard Worker#### Compatibility 93*84e33947SAndroid Build Coastguard Worker 94*84e33947SAndroid Build Coastguard WorkerThe framework has been tested with Pigweed's git revision ee460238b8a7ec0a6b4f61fe7e67a12231db6d3e. 95*84e33947SAndroid Build Coastguard Worker 96*84e33947SAndroid Build Coastguard Worker## PAL implementation tests 97*84e33947SAndroid Build Coastguard Worker 98*84e33947SAndroid Build Coastguard WorkerPAL implementation tests verify implementations of PAL interfaces adhere to the 99*84e33947SAndroid Build Coastguard Workerrequirements of that interface, and are intended primarily to support 100*84e33947SAndroid Build Coastguard Workerdevelopment of PAL implementations, typically done by a chip vendor partner. 101*84e33947SAndroid Build Coastguard WorkerAdditional setup may be required to integrate with the PAL under test and supply 102*84e33947SAndroid Build Coastguard Workernecessary dependencies. The source code is in the files under `pal/tests/src` 103*84e33947SAndroid Build Coastguard Workerand follows the naming scheme `*_pal_impl_test.cc`. 104*84e33947SAndroid Build Coastguard Worker 105*84e33947SAndroid Build Coastguard WorkerIn order to run PAL tests, run: `run_pal_impl_tests.sh`. Note that there are 106*84e33947SAndroid Build Coastguard Workeralso PAL unit tests in the same directory. The unit tests are added to the 107*84e33947SAndroid Build Coastguard Worker`GOOGLETEST_SRCS` target while PAL tests are added to the 108*84e33947SAndroid Build Coastguard Worker`GOOGLETEST_PAL_IMPL_SRCS` target. 109*84e33947SAndroid Build Coastguard Worker 110*84e33947SAndroid Build Coastguard Worker## FeatureWorld nanoapps 111*84e33947SAndroid Build Coastguard Worker 112*84e33947SAndroid Build Coastguard WorkerLocated under the `apps/` directory, FeatureWorld nanoapps interact with the set 113*84e33947SAndroid Build Coastguard Workerof CHRE APIs that they are named after, and can be useful during framework 114*84e33947SAndroid Build Coastguard Workerdevelopment for manual verification of a feature area. For example, SensorWorld 115*84e33947SAndroid Build Coastguard Workerattempts to samples from sensors and outputs to the log. It also offers a 116*84e33947SAndroid Build Coastguard Workerbreak-it mode that randomly changes which sensors are being sampled in an 117*84e33947SAndroid Build Coastguard Workerattempt to point out stress points in the framework or platform implementation. 118*84e33947SAndroid Build Coastguard Worker 119*84e33947SAndroid Build Coastguard WorkerThese apps are usually built into the CHRE framework binary as static nanoapps 120*84e33947SAndroid Build Coastguard Workerto facilitate easy development. See the Deploying Nanoapps section for more 121*84e33947SAndroid Build Coastguard Workerinformation on static nanoapps. 122*84e33947SAndroid Build Coastguard Worker 123*84e33947SAndroid Build Coastguard Worker## CHQTS 124*84e33947SAndroid Build Coastguard Worker 125*84e33947SAndroid Build Coastguard WorkerThe Context Hub Qualification Test Suite (CHQTS) tests perform end-to-end 126*84e33947SAndroid Build Coastguard Workervalidation of a CHRE implementation, by using the Java APIs in Android to load 127*84e33947SAndroid Build Coastguard Workerand interact with test nanoapps which then exercise the CHRE API. While this 128*84e33947SAndroid Build Coastguard Workercode is nominally integrated in another test suite, the source code is available 129*84e33947SAndroid Build Coastguard Workerunder `java/test/chqts/` for the Java side code and `apps/test/chqts/` for the 130*84e33947SAndroid Build Coastguard WorkerCHQTS-only nanoapp code and `apps/test/common/` for the nanoapp code shared by 131*84e33947SAndroid Build Coastguard WorkerCHQTS and other test suites. 132*84e33947SAndroid Build Coastguard Worker 133*84e33947SAndroid Build Coastguard Worker[PW_URL]: https://pigweed.dev 134*84e33947SAndroid Build Coastguard Worker[PW_UT_URL]: https://pigweed.googlesource.com/pigweed/pigweed/+/refs/heads/master/pw_unit_test 135*84e33947SAndroid Build Coastguard Worker[GT_URL]: https://github.com/google/googletest 136