1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef CHRE_SIMULATION_TEST_BASE_H_ 18 #define CHRE_SIMULATION_TEST_BASE_H_ 19 20 #include <gtest/gtest.h> 21 #include <cstdint> 22 #include <thread> 23 24 #include "chre/platform/system_timer.h" 25 #include "chre/util/time.h" 26 #include "test_event_queue.h" 27 28 namespace chre { 29 30 /* 31 * A base class for all CHRE simulated tests. 32 */ 33 class TestBase : public testing::Test { 34 protected: 35 void SetUp() override; 36 void TearDown() override; 37 38 /** 39 * This method can be overridden in a derived class if desired. 40 * 41 * @return The total runtime allowed for the entire test. 42 */ getTimeoutNs()43 virtual uint64_t getTimeoutNs() const { 44 return 5 * kOneSecondInNanoseconds; 45 } 46 47 /** 48 * A convenience method to invoke waitForEvent() for the TestEventQueue 49 * singleton. 50 * 51 * Note: Events that are intended to be delivered to a nanoapp as a result of 52 * asynchronous APIs invoked in a nanoappEnd() functions may not be delivered 53 * to the nanoapp through nanoappHandleEvent() (since they are already 54 * unloaded by the time it receives the event), so users of the TestEventQueue 55 * should not wait for such events in their test flow. 56 * 57 * @param eventType The event type to wait for. 58 */ waitForEvent(uint16_t eventType)59 void waitForEvent(uint16_t eventType) { 60 TestEventQueueSingleton::get()->waitForEvent(eventType); 61 } 62 63 /** 64 * A convenience method to invoke waitForEvent() for the TestEventQueue 65 * singleton. 66 * 67 * @see waitForEvent(eventType) 68 * 69 * @param eventType The event type to wait for. 70 * @param eventData Populated with the data attached to the event. 71 */ 72 template <class T> waitForEvent(uint16_t eventType,T * eventData)73 void waitForEvent(uint16_t eventType, T *eventData) { 74 TestEventQueueSingleton::get()->waitForEvent(eventType, eventData); 75 } 76 77 std::thread mChreThread; 78 SystemTimer mSystemTimer; 79 }; 80 81 } // namespace chre 82 83 #endif // CHRE_SIMULATION_TEST_BASE_H_ 84