xref: /aosp_15_r20/external/pigweed/third_party/chre/integration_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include <chrono>
16 #include <functional>
17 #include <thread>
18 
19 #include "chre/core/event_loop_manager.h"
20 #include "chre/core/init.h"
21 #include "chre/platform/memory.h"
22 #include "chre/platform/system_timer.h"
23 #include "chre/util/fixed_size_blocking_queue.h"
24 #include "chre/util/memory.h"
25 #include "chre/util/non_copyable.h"
26 #include "chre/util/singleton.h"
27 #include "chre/util/time.h"
28 #include "chre_api/chre/version.h"
29 #include "gtest/gtest.h"
30 #include "test_base.h"
31 #include "test_util.h"
32 
33 namespace chre {
34 
SetUp()35 void TestBase::SetUp() {
36   TestEventQueueSingleton::init();
37   chre::init();
38   EventLoopManagerSingleton::get()->lateInit();
39 
40   mChreThread = std::thread(
41       []() { EventLoopManagerSingleton::get()->getEventLoop().run(); });
42 
43   auto callback = [](void*) {
44     LOGE("Test timed out ...");
45     TestEventQueueSingleton::get()->pushEvent(
46         CHRE_EVENT_SIMULATION_TEST_TIMEOUT);
47   };
48 
49   ASSERT_TRUE(mSystemTimer.init());
50   ASSERT_TRUE(mSystemTimer.set(
51       callback, nullptr /*data*/, Nanoseconds(getTimeoutNs())));
52 }
53 
TearDown()54 void TestBase::TearDown() {
55   mSystemTimer.cancel();
56   TestEventQueueSingleton::get()->flush();
57   EventLoopManagerSingleton::get()->getEventLoop().stop();
58   mChreThread.join();
59   chre::deinit();
60   TestEventQueueSingleton::deinit();
61   deleteNanoappInfos();
62   unregisterAllTestNanoapps();
63 }
64 
65 template class Singleton<TestEventQueue>;
66 
TEST_F(TestBase,CanLoadAndStartSingleNanoapp)67 TEST_F(TestBase, CanLoadAndStartSingleNanoapp) {
68   constexpr uint64_t kAppId = 0x0123456789abcdef;
69   constexpr uint32_t kAppVersion = 0;
70   constexpr uint32_t kAppPerms = 0;
71 
72   UniquePtr<Nanoapp> nanoapp = createStaticNanoapp("Test nanoapp",
73                                                    kAppId,
74                                                    kAppVersion,
75                                                    kAppPerms,
76                                                    defaultNanoappStart,
77                                                    defaultNanoappHandleEvent,
78                                                    defaultNanoappEnd);
79 
80   EventLoopManagerSingleton::get()->deferCallback(
81       SystemCallbackType::FinishLoadingNanoapp,
82       std::move(nanoapp),
83       testFinishLoadingNanoappCallback);
84   waitForEvent(CHRE_EVENT_SIMULATION_TEST_NANOAPP_LOADED);
85 }
86 
TEST_F(TestBase,CanLoadAndStartMultipleNanoapps)87 TEST_F(TestBase, CanLoadAndStartMultipleNanoapps) {
88   constexpr uint64_t kAppId1 = 0x123;
89   constexpr uint64_t kAppId2 = 0x456;
90   constexpr uint32_t kAppVersion = 0;
91   constexpr uint32_t kAppPerms = 0;
92   loadNanoapp("Test nanoapp",
93               kAppId1,
94               kAppVersion,
95               kAppPerms,
96               defaultNanoappStart,
97               defaultNanoappHandleEvent,
98               defaultNanoappEnd);
99 
100   loadNanoapp("Test nanoapp",
101               kAppId2,
102               kAppVersion,
103               kAppPerms,
104               defaultNanoappStart,
105               defaultNanoappHandleEvent,
106               defaultNanoappEnd);
107 
108   uint16_t id1;
109   EXPECT_TRUE(EventLoopManagerSingleton::get()
110                   ->getEventLoop()
111                   .findNanoappInstanceIdByAppId(kAppId1, &id1));
112   uint16_t id2;
113   EXPECT_TRUE(EventLoopManagerSingleton::get()
114                   ->getEventLoop()
115                   .findNanoappInstanceIdByAppId(kAppId2, &id2));
116 
117   EXPECT_NE(id1, id2);
118 }
119 }  // namespace chre
120