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_timer.h"
27 #include "chre/util/time.h"
28 #include "test_event_queue.h"
29 
30 namespace chre {
31 
32 /*
33  * A base class for all CHRE simulated tests.
34  */
35 class TestBase : public testing::Test {
36  protected:
37   void SetUp() override;
38   void TearDown() override;
39 
40   /**
41    * This method can be overridden in a derived class if desired.
42    *
43    * @return The total runtime allowed for the entire test.
44    */
getTimeoutNs()45   virtual uint64_t getTimeoutNs() const {
46     return 5 * kOneSecondInNanoseconds;
47   }
48 
49   /**
50    * A convenience method to invoke waitForEvent() for the TestEventQueue
51    * singleton.
52    *
53    * Note: Events that are intended to be delivered to a nanoapp as a result of
54    * asynchronous APIs invoked in a nanoappEnd() functions may not be delivered
55    * to the nanoapp through nanoappHandleEvent() (since they are already
56    * unloaded by the time it receives the event), so users of the TestEventQueue
57    * should not wait for such events in their test flow.
58    *
59    * @param eventType The event type to wait for.
60    */
waitForEvent(uint16_t eventType)61   void waitForEvent(uint16_t eventType) {
62     TestEventQueueSingleton::get()->waitForEvent(eventType);
63   }
64 
65   /**
66    * A convenience method to invoke waitForEvent() for the TestEventQueue
67    * singleton.
68    *
69    * @see waitForEvent(eventType)
70    *
71    * @param eventType The event type to wait for.
72    * @param eventData Populated with the data attached to the event.
73    */
74   template <class T>
waitForEvent(uint16_t eventType,T * eventData)75   void waitForEvent(uint16_t eventType, T *eventData) {
76     TestEventQueueSingleton::get()->waitForEvent(eventType, eventData);
77   }
78 
79   /**
80    * Retrieves the Nanoapp instance from its ID.
81    *
82    * @param id Nanoapp ID
83    * @return A pointer to the Nanoapp instance or nullptr if not found.
84    */
getNanoappByAppId(uint64_t id)85   Nanoapp *getNanoappByAppId(uint64_t id) {
86     uint16_t instanceId;
87     EXPECT_TRUE(EventLoopManagerSingleton::get()
88                     ->getEventLoop()
89                     .findNanoappInstanceIdByAppId(id, &instanceId));
90     Nanoapp *nanoapp = EventLoopManagerSingleton::get()
91                            ->getEventLoop()
92                            .findNanoappByInstanceId(instanceId);
93     EXPECT_NE(nanoapp, nullptr);
94     return nanoapp;
95   }
96 
97   std::thread mChreThread;
98   SystemTimer mSystemTimer;
99 };
100 
101 }  // namespace chre
102 
103 #endif  // CHRE_SIMULATION_TEST_BASE_H_
104