• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "gtest/gtest.h"
18 
19 #include "chre/core/ble_request.h"
20 
21 using chre::BleRequest;
22 
TEST(BleRequest,DefaultMinimalRequest)23 TEST(BleRequest, DefaultMinimalRequest) {
24   BleRequest request;
25   EXPECT_FALSE(request.isEnabled());
26   EXPECT_EQ(CHRE_BLE_SCAN_MODE_BACKGROUND, request.getMode());
27   EXPECT_EQ(0, request.getReportDelayMs());
28   EXPECT_TRUE(request.getGenericFilters().empty());
29   EXPECT_EQ(CHRE_BLE_RSSI_THRESHOLD_NONE, request.getRssiThreshold());
30 }
31 
TEST(BleRequest,AggressiveModeIsHigherThanBackground)32 TEST(BleRequest, AggressiveModeIsHigherThanBackground) {
33   BleRequest backgroundMode(
34       0 /* instanceId */, true /* enable */, CHRE_BLE_SCAN_MODE_BACKGROUND,
35       0 /* reportDelayMs */, nullptr /* filter */, nullptr /* cookie */);
36   BleRequest aggressiveMode(
37       0 /* instanceId */, true /* enable */, CHRE_BLE_SCAN_MODE_AGGRESSIVE,
38       0 /* reportDelayMs */, nullptr /* filter */, nullptr /* cookie */);
39 
40   BleRequest mergedRequest;
41   EXPECT_TRUE(mergedRequest.mergeWith(aggressiveMode));
42   EXPECT_FALSE(mergedRequest.mergeWith(backgroundMode));
43 
44   EXPECT_TRUE(mergedRequest.isEnabled());
45   EXPECT_EQ(CHRE_BLE_SCAN_MODE_AGGRESSIVE, mergedRequest.getMode());
46   EXPECT_TRUE(mergedRequest.getGenericFilters().empty());
47   EXPECT_EQ(CHRE_BLE_RSSI_THRESHOLD_NONE, mergedRequest.getRssiThreshold());
48 }
49 
TEST(BleRequest,MergeWithReplacesParametersOfDisabledRequest)50 TEST(BleRequest, MergeWithReplacesParametersOfDisabledRequest) {
51   chreBleScanFilterV1_9 filter;
52   filter.rssiThreshold = -5;
53   filter.genericFilterCount = 1;
54   auto scanFilters = std::make_unique<chreBleGenericFilter>();
55   scanFilters->type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE;
56   scanFilters->len = 2;
57   filter.genericFilters = scanFilters.get();
58   BleRequest enabled(0, true, CHRE_BLE_SCAN_MODE_AGGRESSIVE, 20, &filter,
59                      nullptr /* cookie */);
60 
61   BleRequest mergedRequest;
62   EXPECT_FALSE(mergedRequest.isEnabled());
63   EXPECT_TRUE(mergedRequest.mergeWith(enabled));
64   EXPECT_TRUE(mergedRequest.isEnabled());
65   EXPECT_EQ(CHRE_BLE_SCAN_MODE_AGGRESSIVE, mergedRequest.getMode());
66   EXPECT_EQ(20, mergedRequest.getReportDelayMs());
67   EXPECT_EQ(-5, mergedRequest.getRssiThreshold());
68   EXPECT_EQ(1, mergedRequest.getGenericFilters().size());
69   EXPECT_EQ(CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE,
70             mergedRequest.getGenericFilters()[0].type);
71   EXPECT_EQ(2, mergedRequest.getGenericFilters()[0].len);
72 }
73 
TEST(BleRequest,IsEquivalentToBasic)74 TEST(BleRequest, IsEquivalentToBasic) {
75   BleRequest backgroundMode(
76       0 /* instanceId */, true /* enable */, CHRE_BLE_SCAN_MODE_BACKGROUND,
77       0 /* reportDelayMs */, nullptr /* filter */, nullptr /* cookie */);
78   EXPECT_TRUE(backgroundMode.isEquivalentTo(backgroundMode));
79 }
80 
TEST(BleRequest,IsNotEquivalentToBasic)81 TEST(BleRequest, IsNotEquivalentToBasic) {
82   BleRequest backgroundMode(
83       0 /* instanceId */, true /* enable */, CHRE_BLE_SCAN_MODE_BACKGROUND,
84       0 /* reportDelayMs */, nullptr /* filter */, nullptr /* cookie */);
85   BleRequest aggressiveMode(
86       0 /* instanceId */, true /* enable */, CHRE_BLE_SCAN_MODE_AGGRESSIVE,
87       0 /* reportDelayMs */, nullptr /* filter */, nullptr /* cookie */);
88   EXPECT_FALSE(backgroundMode.isEquivalentTo(aggressiveMode));
89 }
90 
TEST(BleRequest,IsNotEquivalentWithCookie)91 TEST(BleRequest, IsNotEquivalentWithCookie) {
92   constexpr uint32_t kCookieOne = 123;
93   constexpr uint32_t kCookieTwo = 234;
94   BleRequest requestOne(0 /* instanceId */, true /* enable */,
95                         CHRE_BLE_SCAN_MODE_BACKGROUND, 0 /* reportDelayMs */,
96                         nullptr /* filter */, &kCookieOne);
97   BleRequest requestTwo(0 /* instanceId */, true /* enable */,
98                         CHRE_BLE_SCAN_MODE_BACKGROUND, 0 /* reportDelayMs */,
99                         nullptr /* filter */, &kCookieTwo);
100   EXPECT_TRUE(requestOne.isEquivalentTo(requestTwo));
101 }
102 
TEST(BleRequest,IsEquivalentToAdvanced)103 TEST(BleRequest, IsEquivalentToAdvanced) {
104   chreBleScanFilterV1_9 filter;
105   filter.rssiThreshold = -5;
106   filter.genericFilterCount = 1;
107   auto scanFilters = std::make_unique<chreBleGenericFilter>();
108   scanFilters->type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE;
109   scanFilters->len = 4;
110   filter.genericFilters = scanFilters.get();
111 
112   BleRequest backgroundMode(
113       100 /* instanceId */, true /* enable */, CHRE_BLE_SCAN_MODE_BACKGROUND,
114       100 /* reportDelayMs */, &filter, nullptr /* cookie */);
115   EXPECT_TRUE(backgroundMode.isEquivalentTo(backgroundMode));
116 }
117 
TEST(BleRequest,IsNotEquivalentToAdvanced)118 TEST(BleRequest, IsNotEquivalentToAdvanced) {
119   chreBleScanFilterV1_9 filter;
120   filter.rssiThreshold = -5;
121   filter.genericFilterCount = 1;
122   auto scanFilters = std::make_unique<chreBleGenericFilter>();
123   scanFilters->type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE;
124   scanFilters->len = 4;
125   filter.genericFilters = scanFilters.get();
126 
127   BleRequest backgroundMode(
128       100 /* instanceId */, true /* enable */, CHRE_BLE_SCAN_MODE_BACKGROUND,
129       100 /* reportDelayMs */, &filter /* filter */, nullptr /* cookie */);
130   BleRequest aggressiveMode(
131       0 /* instanceId */, true /* enable */, CHRE_BLE_SCAN_MODE_AGGRESSIVE,
132       0 /* reportDelayMs */, nullptr /* filter */, nullptr /* cookie */);
133 
134   EXPECT_FALSE(backgroundMode.isEquivalentTo(aggressiveMode));
135 }
136 
TEST(BleRequest,GetScanFilter)137 TEST(BleRequest, GetScanFilter) {
138   chreBleScanFilterV1_9 filter;
139   filter.rssiThreshold = -5;
140   filter.genericFilterCount = 1;
141   auto scanFilters = std::make_unique<chreBleGenericFilter>();
142   scanFilters->type = CHRE_BLE_AD_TYPE_SERVICE_DATA_WITH_UUID_16_LE;
143   scanFilters->len = 4;
144   filter.genericFilters = scanFilters.get();
145 
146   BleRequest backgroundMode(
147       100 /* instanceId */, true /* enable */, CHRE_BLE_SCAN_MODE_BACKGROUND,
148       100 /* reportDelayMs */, &filter /* filter */, nullptr /* cookie */);
149 
150   chreBleScanFilterV1_9 retFilter = backgroundMode.getScanFilter();
151   EXPECT_EQ(filter.rssiThreshold, retFilter.rssiThreshold);
152   EXPECT_EQ(filter.genericFilterCount, retFilter.genericFilterCount);
153   EXPECT_EQ(0, memcmp(scanFilters.get(), retFilter.genericFilters,
154                       sizeof(chreBleGenericFilter)));
155 }
156