• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class DelayEventTest : public TestBase {};
36 
37 CREATE_CHRE_TEST_EVENT(DELAY_EVENT, 0);
38 
39 constexpr Seconds kDelayEventInterval(2);
40 std::mutex gMutex;
41 
42 class DelayEventNanoapp : public TestNanoapp {
43  public:
start()44   bool start() override {
45     return true;
46   }
47 
handleEvent(uint32_t,uint16_t eventType,const void * eventData)48   void handleEvent(uint32_t, uint16_t eventType,
49                    const void *eventData) override {
50     switch (eventType) {
51       case CHRE_EVENT_TEST_EVENT: {
52         auto event = static_cast<const TestEvent *>(eventData);
53         switch (event->type) {
54           case DELAY_EVENT: {
55             std::lock_guard<std::mutex> lock(gMutex);
56 
57             if (!hasSeenDelayEvent) {
58               overrideMonotonicTime(SystemTime::getMonotonicTime() +
59                                     kDelayEventInterval);
60               hasSeenDelayEvent = true;
61             }
62             TestEventQueueSingleton::get()->pushEvent(DELAY_EVENT);
63             break;
64           }
65         }
66       }
67     }
68   }
69 
70  private:
71   bool hasSeenDelayEvent = false;
72 };
73 
TEST_F(DelayEventTest,DelayedEventIsFlagged)74 TEST_F(DelayEventTest, DelayedEventIsFlagged) {
75   constexpr uint32_t kDelayEventCount = 3;
76   SystemTimeOverride override(0);
77   uint64_t appId = loadNanoapp(MakeUnique<DelayEventNanoapp>());
78 
79   testing::internal::CaptureStdout();
80   {
81     std::lock_guard<std::mutex> lock(gMutex);
82     for (uint32_t i = 0; i < kDelayEventCount; ++i) {
83       overrideMonotonicTime(SystemTime::getMonotonicTime() + Nanoseconds(1));
84       sendEventToNanoapp(appId, DELAY_EVENT);
85     }
86   }
87 
88   for (uint32_t i = 0; i < kDelayEventCount; ++i) {
89     waitForEvent(DELAY_EVENT);
90   }
91 
92   std::string output = testing::internal::GetCapturedStdout();
93   EXPECT_NE(output.find("Delayed event"), std::string::npos);
94 }
95 
96 }  // namespace
97 }  // namespace chre