1 /*
2 * Copyright (C) 2024 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 #include <cstdint>
18 #include <mutex>
19
20 #include "chre/platform/linux/system_time.h"
21
22 #include "gtest/gtest.h"
23 #include "inc/test_util.h"
24 #include "test_base.h"
25 #include "test_event.h"
26 #include "test_event_queue.h"
27 #include "test_util.h"
28
29 using ::chre::platform_linux::overrideMonotonicTime;
30 using ::chre::platform_linux::SystemTimeOverride;
31
32 namespace chre {
33 namespace {
34
35 CREATE_CHRE_TEST_EVENT(DELAY_EVENT, 0);
36
37 constexpr Seconds kDelayEventInterval(2);
38 std::mutex gMutex;
39
40 class DelayEventNanoapp : public TestNanoapp {
41 public:
start()42 bool start() override {
43 return true;
44 }
45
handleEvent(uint32_t,uint16_t eventType,const void * eventData)46 void handleEvent(uint32_t, uint16_t eventType,
47 const void *eventData) override {
48 switch (eventType) {
49 case CHRE_EVENT_TEST_EVENT: {
50 auto event = static_cast<const TestEvent *>(eventData);
51 switch (event->type) {
52 case DELAY_EVENT: {
53 std::lock_guard<std::mutex> lock(gMutex);
54
55 if (!hasSeenDelayEvent) {
56 overrideMonotonicTime(SystemTime::getMonotonicTime() +
57 kDelayEventInterval);
58 hasSeenDelayEvent = true;
59 }
60 TestEventQueueSingleton::get()->pushEvent(DELAY_EVENT);
61 break;
62 }
63 }
64 }
65 }
66 }
67
68 private:
69 bool hasSeenDelayEvent = false;
70 };
71
TEST_F(TestBase,DelayedEventIsFlagged)72 TEST_F(TestBase, DelayedEventIsFlagged) {
73 constexpr uint32_t kDelayEventCount = 3;
74 SystemTimeOverride override(0);
75 uint64_t appId = loadNanoapp(MakeUnique<DelayEventNanoapp>());
76
77 testing::internal::CaptureStdout();
78 {
79 std::lock_guard<std::mutex> lock(gMutex);
80 for (uint32_t i = 0; i < kDelayEventCount; ++i) {
81 overrideMonotonicTime(SystemTime::getMonotonicTime() + Nanoseconds(1));
82 sendEventToNanoapp(appId, DELAY_EVENT);
83 }
84 }
85
86 for (uint32_t i = 0; i < kDelayEventCount; ++i) {
87 waitForEvent(DELAY_EVENT);
88 }
89
90 std::string output = testing::internal::GetCapturedStdout();
91 EXPECT_NE(output.find("Delayed event"), std::string::npos);
92 }
93
94 } // namespace
95 } // namespace chre