• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef _GTS_NANOAPPS_GENERAL_TEST_TEST_H_
18 #define _GTS_NANOAPPS_GENERAL_TEST_TEST_H_
19 
20 #include <shared/macros.h>
21 #include <shared/send_message.h>
22 
23 #include "chre_api/chre.h"
24 
25 namespace general_test {
26 
27 /**
28  * Abstract base for all test cases.
29  */
30 class Test {
31  public:
32   Test(uint32_t minSupportedVersion);
~Test()33   virtual ~Test() {}
34 
35   void testHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
36                        const void *eventData);
37 
38   void testSetUp(uint32_t messageSize, const void *message);
39 
40  protected:
41   /**
42    * Report a test-ending error due to an unexpectedEvent.
43    * This method halts execution on an unexpected event,
44    * and never returns.
45    *
46    * @param eventType  The event type
47    */
48   static void unexpectedEvent(uint16_t eventType);
49 
50   /**
51    * Wrapper structure to store the current async request.
52    */
53   struct chreAsyncRequest {
54     //! An opaque value that will be included in the chreAsyncResult
55     //! sent in relation to a chre async request.
56     const void *cookie;
57 
58     //! A type of request. Same field as that in {@link #chreAsyncResult}
59     uint8_t requestType;
60 
61     //! Timestamp when mading the request.
62     uint64_t requestTimeNs;
63 
64     //! Timeout to receive the chre async result.
65     uint64_t timeoutNs;
66   };
67 
68   /**
69    * Reports a test-ending error due to failure in chreAsyncResult.
70    *
71    * 1. chre async result is not success.
72    * 2. chre async result success, but errorCode is not CHRE_ERROR_NONE.
73    * 3. request cookie mismatch.
74    * 4. requestType mismatch.
75    * 5. result timeout.
76    *
77    * @param result chreAsyncResult of an async request.
78    * @param request lastest chre async request.
79    */
80   static void validateChreAsyncResult(const chreAsyncResult *result,
81                                       const chreAsyncRequest &request);
82 
83   /**
84    * Get the message data sent from the host, after performing consistency
85    * checks.
86    *
87    * The method centralizes a number of common consistency checks that tests
88    * will perform in taking the given CHRE event data and extracting out
89    * the raw data payload sent by the host.  This method is still useful
90    * when no message data is expected from the host, as we'll still
91    * perform the checks.
92    *
93    * This method will end the test in failure if any of the following happen:
94    * o 'senderInstanceId' != CHRE_INSTANCE_ID
95    * o 'eventType' != CHRE_EVENT_MESSAGE_FROM_HOST
96    * o 'eventData'->reservedMessageType != expectedMessageType
97    * o 'eventData'->messageSize != expectedMessageSize
98    *
99    * On passing all consistency checks, the message data can be expected
100    * to be in 'eventData->message'
101    *
102    * @param senderInstanceId  From handleEvent()
103    * @param eventType  From handleEvent()
104    * @param eventData  From handleEvent()
105    * @param expectedMessageType  The expected 'reservedMessageType' field
106    *     when 'eventData' is seen as a chreMessageFromHostData.
107    * @param expectedMessageSize  The expected 'messageSize' field
108    *     when 'eventData' is seen as a chreMessageFromHostData.
109    */
110   static const void *getMessageDataFromHostEvent(
111       uint32_t senderInstanceId, uint16_t eventType, const void *eventData,
112       nanoapp_testing::MessageType expectedMessageType,
113       uint32_t expectedMessageSize);
114 
115   virtual void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
116                            const void *eventData) = 0;
117   virtual void setUp(uint32_t messageSize, const void *message) = 0;
118 
119   /**
120    * The platform reported CHRE API version.
121    *
122    * Nanoapps may use this to determine what version they are running
123    * on and perform any version specific behaviours.
124    */
125   const uint32_t mApiVersion;
126 
127  private:
128   /**
129    * Is the nanoapp supported by the platform reported CHRE API version.
130    *
131    * Nanoapps specify the minimum CHRE API version required during
132    * construction. If it is at least the version that is being reported
133    * by the platform then mIsSupported will be true. Else, the nanoapp
134    * will skip the test.
135    */
136   const bool mIsSupported;
137 };
138 
139 }  // namespace general_test
140 
141 #endif  // _GTS_NANOAPPS_GENERAL_TEST_TEST_H_
142