-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathSensingKitTests.m
More file actions
197 lines (155 loc) · 9.89 KB
/
Copy pathSensingKitTests.m
File metadata and controls
197 lines (155 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// SensingKitTests.m
// SensingKitTests
//
// Copyright (c) 2014. Kleomenis Katevas
// Kleomenis Katevas, [email protected]
//
// This file is part of SensingKit-iOS library.
// For more information, please visit https://www.sensingkit.org
//
// SensingKit-iOS is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SensingKit-iOS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with SensingKit-iOS. If not, see <http://www.gnu.org/licenses/>.
//
#import <Foundation/Foundation.h>
#import <XCTest/XCTest.h>
#import <SensingKit/SensingKit.h>
@import CoreBluetooth;
@interface SensingKitTests : XCTestCase
@property (nonatomic, strong) SensingKitLib *sensingKit;
@end
@implementation SensingKitTests
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
// Init SensingKitLib
self.sensingKit = [SensingKitLib sharedSensingKitLib];
}
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testSensorStrings
{
XCTAssertTrue([[NSString nonspacedStringWithSensorType:Accelerometer] isEqualToString:@"Accelerometer"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:Gyroscope] isEqualToString:@"Gyroscope"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:Magnetometer] isEqualToString:@"Magnetometer"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:DeviceMotion] isEqualToString:@"DeviceMotion"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:MotionActivity] isEqualToString:@"MotionActivity"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:Pedometer] isEqualToString:@"Pedometer"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:Altimeter] isEqualToString:@"Altimeter"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:BatteryStatus] isEqualToString:@"BatteryStatus"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:Location] isEqualToString:@"Location"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:Heading] isEqualToString:@"Heading"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:iBeaconProximity] isEqualToString:@"iBeaconProximity"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:EddystoneProximity] isEqualToString:@"EddystoneProximity"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:Microphone] isEqualToString:@"Microphone"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:ScreenBrightness] isEqualToString:@"ScreenBrightness"], @"Sensor name is wrong.");
XCTAssertTrue([[NSString nonspacedStringWithSensorType:NetworkConnection] isEqualToString:@"NetworkConnection"], @"Sensor name is wrong.");
}
- (void)testSensingKitLib
{
XCTAssertNotNil(self.sensingKit, @"SensingKitLib cannot be nil.");
SensingKitLib *sensingKit2 = [SensingKitLib sharedSensingKitLib];
XCTAssertNotNil(sensingKit2, @"SensingKitLib cannot be nil.");
XCTAssertEqual(self.sensingKit, sensingKit2, @"Instances should be identical.");
XCTAssertTrue([self.sensingKit registerSensor:BatteryStatus error:NULL], @"Sensor registration should be allowed.");
XCTAssertTrue([self.sensingKit isSensorRegistered:BatteryStatus], @"Sensor should be registered.");
XCTAssertFalse([self.sensingKit isSensorSensing:BatteryStatus], @"Sensor should not be sensing.");
// Subscribe a sensor data listener
XCTAssertTrue([self.sensingKit subscribeToSensor:BatteryStatus
withHandler:^(SKSensorType sensorType, SKSensorData *sensorData, NSError *error) {
// Do nothing
} error:NULL], @"Subscribe to sensor should be allowed.");
XCTAssertFalse([self.sensingKit subscribeToSensor:Accelerometer
withHandler:^(SKSensorType sensorType, SKSensorData *sensorData, NSError *error) {
// Do nothing
} error:NULL], @"Subscribe to sensor should not be allowed as sensor is not registered.");
XCTAssertTrue([self.sensingKit startContinuousSensingWithSensor:BatteryStatus error:NULL], @"Start continuous sensing with a registered sensor with subscribed handler should be allowed.");
XCTAssertTrue([self.sensingKit isSensorSensing:BatteryStatus], @"Sensor should be sensing.");
XCTAssertTrue([self.sensingKit stopContinuousSensingWithSensor:BatteryStatus error:NULL], @"Stop continuous sensing with a sensor that is currently sensing should be allowed.");
XCTAssertFalse([self.sensingKit isSensorSensing:BatteryStatus], @"Sensor should not be sensing.");
// Do not unsubscribe here.. (should be automated by deregisterSensor)
XCTAssertTrue([self.sensingKit deregisterSensor:BatteryStatus error:NULL], @"Sensor should be succesfully deregistered.");
XCTAssertFalse([self.sensingKit isSensorRegistered:BatteryStatus], @"Sensor should not be registered.");
}
- (void)testSensorAvailability
{
for (SKSensorType i = 0; i < TOTAL_SENSORS; i++) {
// No need for XCTAssert here, it just needs to pass without crashing (abort())
[self.sensingKit isSensorAvailable:i];
}
}
- (void)testSensorRegistrationDeregistration
{
// test registration
for (SKSensorType i = 0; i < TOTAL_SENSORS; i++) {
// if sensor is available
if ([self.sensingKit isSensorAvailable:(SKSensorType)i]) {
// registration should be permitted
XCTAssertTrue([self.sensingKit registerSensor:i error:NULL], @"Registration should be permitted.");
}
else {
// error
XCTAssertFalse([self.sensingKit registerSensor:i error:NULL], @"Registration should not be permitted.");
}
}
// test isRegistered
for (SKSensorType i = 0; i < TOTAL_SENSORS; i++) {
// if sensor is available
if ([self.sensingKit isSensorAvailable:(SKSensorType)i]) {
XCTAssertTrue([self.sensingKit isSensorRegistered:i], @"Sensor should be registered.");
}
else {
XCTAssertFalse([self.sensingKit isSensorRegistered:i], @"Sensor should not be registered as registration was not permitted previously.");
}
}
// test deregistration
for (SKSensorType i = 0; i < TOTAL_SENSORS; i++) {
// if sensor is available
if ([self.sensingKit isSensorAvailable:(SKSensorType)i]) {
XCTAssertTrue([self.sensingKit deregisterSensor:i error:NULL], @"Deregistration should be permitted.");
}
else {
XCTAssertFalse([self.sensingKit deregisterSensor:i error:NULL], @"Sensor should not be deregistered as registration was never allowed.");
}
}
// test isRegistered again
for (SKSensorType i = 0; i < TOTAL_SENSORS; i++) {
// All sensors should now now be registered
XCTAssertFalse([self.sensingKit isSensorRegistered:i], @"Sensor shoud not be registered.");
}
}
- (void)testSensorConfiguration
{
// test set configuration without being registered
SKBatteryStatusConfiguration *batteryConfiguration = [[SKBatteryStatusConfiguration alloc] init];
XCTAssertFalse([self.sensingKit setConfiguration:batteryConfiguration toSensor:BatteryStatus error:NULL], @"Configuration should not be set as sensor is not registered.");
// test set configuration
XCTAssertTrue([self.sensingKit registerSensor:BatteryStatus error:NULL], @"Sensor should be registered succesfully.");
XCTAssertTrue([self.sensingKit setConfiguration:batteryConfiguration toSensor:BatteryStatus error:NULL], @"Configuration should be set.");
// test with wrong type of configuration
SKAccelerometerConfiguration *accelerometerConfiguration = [[SKAccelerometerConfiguration alloc] init];
XCTAssertFalse([self.sensingKit setConfiguration:accelerometerConfiguration toSensor:BatteryStatus error:NULL], @"Accelerometer configuration should not be set into a Battery sensor.");
// deregister sensor
XCTAssertTrue([self.sensingKit deregisterSensor:BatteryStatus error:NULL], @"Sensor should be deregistered sucessfully.");
// Test SKEddystoneProximityConfiguration
SKEddystoneProximityConfiguration *eddystoneConfiguration = [[SKEddystoneProximityConfiguration alloc] init];
XCTAssertTrue([eddystoneConfiguration setNamespaceFilter:@"123456" error:NULL], @"Valid namespace should be allowed.");
XCTAssertTrue([eddystoneConfiguration setNamespaceFilter:nil error:NULL], @"Nil namespace should be allowed.");
XCTAssertTrue([eddystoneConfiguration setNamespaceFilter:@"" error:NULL], @"Empty namespace should be allowed.");
XCTAssertFalse([eddystoneConfiguration setNamespaceFilter:@"zzzzzzz" error:NULL], @"Invalid namespace should not be allowed.");
}
@end