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 #include "test_base.h"
18
19 #include <gtest/gtest.h>
20
21 #include <cstdint>
22 #include <optional>
23 #include <thread>
24
25 #include "chre/core/event_loop_manager.h"
26 #include "chre/core/host_notifications.h"
27 #include "chre/platform/log.h"
28 #include "chre_api/chre/event.h"
29 #include "test_event_queue.h"
30 #include "test_util.h"
31
32 namespace chre {
33
34 namespace {
35
36 //! The host endpoint ID to use for this test.
37 constexpr uint16_t kHostEndpointId = 123;
38
39 /**
40 * Verifies basic functionality of chreConfigureHostEndpointNotifications.
41 */
TEST_F(TestBase,HostEndpointDisconnectedTest)42 TEST_F(TestBase, HostEndpointDisconnectedTest) {
43 CREATE_CHRE_TEST_EVENT(SETUP_NOTIFICATION, 0);
44
45 struct Config {
46 bool enable;
47 uint16_t endpointId;
48 };
49
50 struct App : public TestNanoapp {
51 void (*handleEvent)(uint32_t, uint16_t, const void *) =
52 [](uint32_t, uint16_t eventType, const void *eventData) {
53 switch (eventType) {
54 case CHRE_EVENT_HOST_ENDPOINT_NOTIFICATION: {
55 auto notification =
56 *(struct chreHostEndpointNotification *)eventData;
57 TestEventQueueSingleton::get()->pushEvent(
58 CHRE_EVENT_HOST_ENDPOINT_NOTIFICATION, notification);
59 } break;
60
61 case CHRE_EVENT_TEST_EVENT: {
62 auto event = static_cast<const TestEvent *>(eventData);
63 switch (event->type) {
64 case SETUP_NOTIFICATION: {
65 auto config = static_cast<const Config *>(event->data);
66 const bool success = chreConfigureHostEndpointNotifications(
67 config->endpointId, config->enable);
68 TestEventQueueSingleton::get()->pushEvent(SETUP_NOTIFICATION,
69 success);
70 }
71 }
72 }
73 }
74 };
75 };
76
77 struct chreHostEndpointInfo info;
78 info.hostEndpointId = kHostEndpointId;
79 info.hostEndpointType = CHRE_HOST_ENDPOINT_TYPE_FRAMEWORK;
80 info.isNameValid = true;
81 strcpy(&info.endpointName[0], "Test endpoint name");
82 info.isTagValid = true;
83 strcpy(&info.endpointTag[0], "Test tag");
84 postHostEndpointConnected(info);
85
86 auto app = loadNanoapp<App>();
87 Config config = {.enable = true, .endpointId = kHostEndpointId};
88
89 sendEventToNanoapp(app, SETUP_NOTIFICATION, config);
90 bool success;
91 waitForEvent(SETUP_NOTIFICATION, &success);
92 EXPECT_TRUE(success);
93
94 struct chreHostEndpointInfo retrievedInfo;
95 ASSERT_TRUE(getHostEndpointInfo(kHostEndpointId, &retrievedInfo));
96 ASSERT_EQ(retrievedInfo.hostEndpointId, info.hostEndpointId);
97 ASSERT_EQ(retrievedInfo.hostEndpointType, info.hostEndpointType);
98 ASSERT_EQ(retrievedInfo.isNameValid, info.isNameValid);
99 ASSERT_EQ(strcmp(&retrievedInfo.endpointName[0], &info.endpointName[0]), 0);
100 ASSERT_EQ(retrievedInfo.isTagValid, info.isTagValid);
101 ASSERT_EQ(strcmp(&retrievedInfo.endpointTag[0], &info.endpointTag[0]), 0);
102
103 struct chreHostEndpointNotification notification;
104
105 postHostEndpointDisconnected(kHostEndpointId);
106 waitForEvent(CHRE_EVENT_HOST_ENDPOINT_NOTIFICATION, ¬ification);
107
108 ASSERT_EQ(notification.hostEndpointId, kHostEndpointId);
109 ASSERT_EQ(notification.notificationType,
110 HOST_ENDPOINT_NOTIFICATION_TYPE_DISCONNECT);
111 ASSERT_EQ(notification.reserved, 0);
112
113 ASSERT_FALSE(getHostEndpointInfo(kHostEndpointId, &retrievedInfo));
114 }
115
TEST_F(TestBase,HostEndpointNotRegisteredTest)116 TEST_F(TestBase, HostEndpointNotRegisteredTest) {
117 struct chreHostEndpointInfo retrievedInfo;
118 ASSERT_FALSE(getHostEndpointInfo(kHostEndpointId, &retrievedInfo));
119 }
120
TEST_F(TestBase,HostEndpointDisconnectedTwiceTest)121 TEST_F(TestBase, HostEndpointDisconnectedTwiceTest) {
122 struct chreHostEndpointInfo info;
123 info.hostEndpointId = kHostEndpointId;
124 info.hostEndpointType = CHRE_HOST_ENDPOINT_TYPE_FRAMEWORK;
125 info.isNameValid = false;
126 info.isTagValid = false;
127 postHostEndpointConnected(info);
128
129 postHostEndpointDisconnected(kHostEndpointId);
130 // The second invocation should be a silent no-op.
131 postHostEndpointDisconnected(kHostEndpointId);
132 }
133
134 } // anonymous namespace
135 } // namespace chre
136