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