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/core/event_loop_manager.h" 25 #include "chre/core/nanoapp.h" 26 #include "chre/platform/system_time.h" 27 #include "chre/platform/system_timer.h" 28 #include "chre/util/system/message_router.h" 29 #include "chre/util/time.h" 30 #include "test_event_queue.h" 31 32 namespace chre { 33 34 // TODO(b/346903946): remove these extra debug logs once issue resolved 35 #define CHRE_TEST_DEBUG(fmt, ...) \ 36 do { \ 37 fprintf(stderr, "%" PRIu64 "ns %s: " fmt "\n", \ 38 SystemTime::getMonotonicTime().toRawNanoseconds(), __func__, \ 39 ##__VA_ARGS__); \ 40 fprintf(stdout, "%" PRIu64 "ns %s: " fmt "\n", \ 41 SystemTime::getMonotonicTime().toRawNanoseconds(), __func__, \ 42 ##__VA_ARGS__); \ 43 } while (0) 44 45 /* 46 * A base class for all CHRE simulated tests. 47 */ 48 class TestBase : public testing::Test { 49 protected: TestBase()50 TestBase() { 51 CHRE_TEST_DEBUG("Constructed %p", this); 52 } ~TestBase()53 ~TestBase() { 54 CHRE_TEST_DEBUG("Destroying %p", this); 55 } 56 57 void SetUp() override; 58 void TearDown() override; 59 60 /** 61 * This method can be overridden in a derived class if desired. 62 * 63 * @return The total runtime allowed for the entire test. 64 */ getTimeoutNs()65 virtual uint64_t getTimeoutNs() const { 66 return 5 * kOneSecondInNanoseconds; 67 } 68 69 /** 70 * A convenience method to invoke waitForEvent() for the TestEventQueue 71 * singleton. 72 * 73 * Note: Events that are intended to be delivered to a nanoapp as a result of 74 * asynchronous APIs invoked in a nanoappEnd() functions may not be delivered 75 * to the nanoapp through nanoappHandleEvent() (since they are already 76 * unloaded by the time it receives the event), so users of the TestEventQueue 77 * should not wait for such events in their test flow. 78 * 79 * @param eventType The event type to wait for. 80 */ waitForEvent(uint16_t eventType)81 void waitForEvent(uint16_t eventType) { 82 TestEventQueueSingleton::get()->waitForEvent(eventType); 83 } 84 85 /** 86 * A convenience method to invoke waitForEvent() for the TestEventQueue 87 * singleton. 88 * 89 * @see waitForEvent(eventType) 90 * 91 * @param eventType The event type to wait for. 92 * @param eventData Populated with the data attached to the event. 93 */ 94 template <class T> waitForEvent(uint16_t eventType,T * eventData)95 void waitForEvent(uint16_t eventType, T *eventData) { 96 TestEventQueueSingleton::get()->waitForEvent(eventType, eventData); 97 } 98 99 /** 100 * Retrieves the Nanoapp instance from its ID. 101 * 102 * @param id Nanoapp ID 103 * @return A pointer to the Nanoapp instance or nullptr if not found. 104 */ getNanoappByAppId(uint64_t id)105 Nanoapp *getNanoappByAppId(uint64_t id) { 106 uint16_t instanceId; 107 EXPECT_TRUE(EventLoopManagerSingleton::get() 108 ->getEventLoop() 109 .findNanoappInstanceIdByAppId(id, &instanceId)); 110 Nanoapp *nanoapp = EventLoopManagerSingleton::get() 111 ->getEventLoop() 112 .findNanoappByInstanceId(instanceId); 113 EXPECT_NE(nanoapp, nullptr); 114 return nanoapp; 115 } 116 117 class MemberInitLogger { 118 public: MemberInitLogger()119 MemberInitLogger() { 120 CHRE_TEST_DEBUG("Construction start"); 121 } ~MemberInitLogger()122 ~MemberInitLogger() { 123 CHRE_TEST_DEBUG("Destruction finished"); 124 } 125 }; 126 127 MemberInitLogger mInitLogger; 128 std::thread mChreThread; 129 SystemTimer mSystemTimer; 130 message::MessageRouter::MessageHub mChreMessageHub; 131 }; 132 133 } // namespace chre 134 135 #endif // CHRE_SIMULATION_TEST_BASE_H_ 136