1 /*
2  * Copyright (C) 2020 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 GNSSPAL_IMPL_TEST_H_
18 #define GNSSPAL_IMPL_TEST_H_
19 
20 #include "chre/pal/gnss.h"
21 #include "chre/platform/condition_variable.h"
22 #include "chre/platform/mutex.h"
23 #include "chre/util/fixed_size_vector.h"
24 #include "chre/util/time.h"
25 #include "gtest/gtest.h"
26 
27 namespace gnss_pal_impl_test {
28 
29 class PalGnssTest : public ::testing::TestWithParam<uint64_t> {
30  public:
31   /**
32    * Implements CHRE PAL API callbacks
33    */
34   void requestStateResync();
35   void locationStatusChangeCallback(bool enabled, uint8_t errorCode);
36   void locationEventCallback(struct chreGnssLocationEvent *event);
37   void measurementStatusChangeCallback(bool enabled, uint8_t errorCode);
38   void measurementEventCallback(struct chreGnssDataEvent *event);
39 
40  protected:
41   void SetUp() override;
42 
43   void TearDown() override;
44 
45   /**
46    * Prepares for a subsequent PAL API call that expects an async response.
47    */
prepareForAsyncResponse()48   void prepareForAsyncResponse() {
49     errorCode_ = CHRE_ERROR_LAST;
50   }
51 
52   /**
53    * Waits for an async response by the PAL implementation (e.g. via location
54    * or measurement status change callback), and asserts that a success
55    * error code was received.
56    */
57   void waitForAsyncResponseAssertSuccess(chre::Nanoseconds timeoutNs);
58 
59   //! The pointer to the CHRE PAL implementation API
60   const struct chrePalGnssApi *api_;
61 
62   //! The error code of the most recent callback
63   uint8_t errorCode_ = CHRE_ERROR_LAST;
64 
65   //! True if location session is currently enabled
66   bool locationSessionEnabled_ = false;
67 
68   //! True if location session is currently enabled
69   bool measurementSessionEnabled_ = false;
70 
71   //! A list to store the location events
72   static constexpr size_t kEventArraySize = 5;
73   chre::FixedSizeVector<chreGnssLocationEvent *, kEventArraySize>
74       locationEventVector_;
75   chre::FixedSizeVector<chreGnssDataEvent *, kEventArraySize>
76       measurementEventVector_;
77 
78   //! Mutex to protect class variables
79   chre::Mutex mutex_;
80   chre::ConditionVariable condVar_;
81 };
82 
83 }  // namespace gnss_pal_impl_test
84 
85 #endif  // GNSSPAL_IMPL_TEST_H_
86