1 /* 2 * Copyright (C) 2016 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/test.h> 18 19 #include <shared/abort.h> 20 #include <shared/send_message.h> 21 22 #include <chre.h> 23 24 using nanoapp_testing::sendFatalFailureToHost; 25 26 namespace general_test { 27 28 Test::Test(uint32_t minSupportedVersion) 29 : mApiVersion(chreGetApiVersion()) 30 , mIsSupported(mApiVersion >= minSupportedVersion) { 31 } 32 33 void Test::testSetUp(uint32_t messageSize, const void *message) { 34 if (mIsSupported) { 35 setUp(messageSize, message); 36 } else { 37 sendMessageToHost(nanoapp_testing::MessageType::kSkipped); 38 } 39 } 40 41 void Test::testHandleEvent(uint32_t senderInstanceId, uint16_t eventType, 42 const void *eventData) { 43 if (mIsSupported) { 44 handleEvent(senderInstanceId, eventType, eventData); 45 } 46 } 47 48 void Test::unexpectedEvent(uint16_t eventType) { 49 uint32_t localEvent = eventType; 50 sendFatalFailureToHost("Test received unexpected event:", &localEvent); 51 } 52 53 const void *Test::getMessageDataFromHostEvent(uint32_t senderInstanceId, 54 uint16_t eventType, const void* eventData, 55 nanoapp_testing::MessageType expectedMessageType, 56 uint32_t expectedMessageSize) { 57 if (senderInstanceId != CHRE_INSTANCE_ID) { 58 sendFatalFailureToHost("Unexpected sender ID:", &senderInstanceId); 59 } 60 if (eventType != CHRE_EVENT_MESSAGE_FROM_HOST) { 61 unexpectedEvent(eventType); 62 } 63 if (eventData == nullptr) { 64 sendFatalFailureToHost("NULL eventData given"); 65 } 66 auto data = static_cast<const chreMessageFromHostData*>(eventData); 67 if (data->reservedMessageType != uint32_t(expectedMessageType)) { 68 sendFatalFailureToHost("Unexpected reservedMessageType:", 69 &(data->reservedMessageType)); 70 } 71 if (data->messageSize != expectedMessageSize) { 72 sendFatalFailureToHost("Unexpected messageSize:", &(data->messageSize)); 73 } 74 return data->message; 75 } 76 77 78 } // namespace general_test 79