1 /*
2 * Copyright (C) 2017 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 <general_test/nanoapp_info_events_test_observer.h>
18
19 #include <shared/nano_endian.h>
20 #include <shared/nano_string.h>
21
22 namespace general_test {
23
NanoAppInfoEventsTestObserver()24 NanoAppInfoEventsTestObserver::NanoAppInfoEventsTestObserver()
25 : Test(CHRE_API_VERSION_1_1) {
26 }
27
setUp(uint32_t,const void *)28 void NanoAppInfoEventsTestObserver::setUp(uint32_t /* messageSize */,
29 const void * /* message */) {
30 chreConfigureNanoappInfoEvents(true /* enable */);
31 nanoapp_testing::sendMessageToHost(nanoapp_testing::MessageType::kContinue);
32 }
33
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)34 void NanoAppInfoEventsTestObserver::handleEvent(uint32_t senderInstanceId,
35 uint16_t eventType,
36 const void *eventData) {
37 if ((senderInstanceId == CHRE_INSTANCE_ID)
38 && ((eventType == CHRE_EVENT_NANOAPP_STARTED)
39 || (eventType == CHRE_EVENT_NANOAPP_STOPPED))) {
40
41 const struct chreNanoappInfo *nanoAppInfo =
42 static_cast<const struct chreNanoappInfo *>(eventData);
43
44 mStartStopHistory[mHistoryIndex].instanceId =
45 nanoAppInfo->instanceId;
46
47 mStartStopHistory[mHistoryIndex].eventType = eventType;
48 mHistoryIndex = (mHistoryIndex + 1) % kHistorySize;
49 } else if ((senderInstanceId == CHRE_INSTANCE_ID)
50 && (eventType == CHRE_EVENT_MESSAGE_FROM_HOST)) {
51 uint32_t performerInstanceId;
52
53 const void *message = getMessageDataFromHostEvent(
54 senderInstanceId, eventType, eventData,
55 nanoapp_testing::MessageType::kContinue, sizeof(performerInstanceId));
56
57 nanoapp_testing::memcpy(&performerInstanceId, message,
58 sizeof(performerInstanceId));
59 performerInstanceId =
60 nanoapp_testing::littleEndianToHost(performerInstanceId);
61
62 processStartStopHistory(performerInstanceId);
63 } else {
64 unexpectedEvent(eventType);
65 }
66 }
67
processStartStopHistory(uint32_t performerInstanceId)68 void NanoAppInfoEventsTestObserver::processStartStopHistory(
69 uint32_t performerInstanceId) {
70 uint32_t startCount = 0;
71 uint32_t stopCount = 0;
72 bool seenFirstEvent = false;
73 bool eventsOrdered = false;
74
75 // The oldest data (if present) is at the insertion point in the
76 // circular array (i.e. mHistoryIndex)
77 for (uint32_t i = 0; i < kHistorySize; ++i) {
78 HostActionMetadata& data =
79 mStartStopHistory[(mHistoryIndex + i) % kHistorySize];
80
81 if (data.instanceId == performerInstanceId) {
82 if (data.eventType == CHRE_EVENT_NANOAPP_STARTED) {
83 ++startCount;
84 } else {
85 ++stopCount;
86 }
87
88 if (!seenFirstEvent) {
89 eventsOrdered = (data.eventType == CHRE_EVENT_NANOAPP_STARTED);
90 seenFirstEvent = true;
91 }
92 }
93 }
94
95 if (startCount > 1) {
96 nanoapp_testing::sendFatalFailureToHost(
97 "Received too many Start events");
98 } else if (startCount == 0) {
99 nanoapp_testing::sendFatalFailureToHost(
100 "Did not receive Start event");
101 } else if (stopCount > 1) {
102 nanoapp_testing::sendFatalFailureToHost(
103 "Received too many Stop events");
104 } else if (stopCount == 0) {
105 nanoapp_testing::sendFatalFailureToHost(
106 "Did not receive Stop event");
107 } else if (!eventsOrdered) {
108 nanoapp_testing::sendFatalFailureToHost(
109 "Start and Stop events were not in order");
110 } else {
111 nanoapp_testing::sendSuccessToHost();
112 }
113 }
114
115 } // namespace general_test
116