@@ -196,6 +196,10 @@ class MTRSwiftDeviceTests : XCTestCase {
196
196
// can satisfy the test below.
197
197
let gotReportsExpectation = expectation ( description: " Attribute and Event reports have been received " )
198
198
var eventReportsReceived : Int = 0
199
+ var reportEnded = false
200
+ var gotOneNonPrimingEvent = false
201
+ // Skipping the gotNonPrimingEventExpectation test (compare the ObjC test) for now,
202
+ // because we can't do the debug-only unitTestInjectEventReport here.
199
203
delegate. onEventDataReceived = { ( eventReport: [ [ String : Any ] ] ) -> Void in
200
204
eventReportsReceived += eventReport. count
201
205
@@ -210,9 +214,23 @@ class MTRSwiftDeviceTests : XCTestCase {
210
214
} else if ( eventTimeType == MTREventTimeType . timestampDate) {
211
215
XCTAssertNotNil ( eventDict [ MTREventTimestampDateKey] )
212
216
}
217
+
218
+ if ( !reportEnded) {
219
+ let reportIsHistorical = eventDict [ MTREventIsHistoricalKey] as! NSNumber ?
220
+ XCTAssertNotNil ( reportIsHistorical) ;
221
+ XCTAssertTrue ( reportIsHistorical!. boolValue) ;
222
+ } else {
223
+ if ( !gotOneNonPrimingEvent) {
224
+ let reportIsHistorical = eventDict [ MTREventIsHistoricalKey] as! NSNumber ?
225
+ XCTAssertNotNil ( reportIsHistorical)
226
+ XCTAssertFalse ( reportIsHistorical!. boolValue)
227
+ gotOneNonPrimingEvent = true
228
+ }
229
+ }
213
230
}
214
231
}
215
232
delegate. onReportEnd = { ( ) -> Void in
233
+ reportEnded = true
216
234
gotReportsExpectation. fulfill ( )
217
235
}
218
236
@@ -280,21 +298,59 @@ class MTRSwiftDeviceTests : XCTestCase {
280
298
attributeID: testAttributeID,
281
299
value: writeValue,
282
300
expectedValueInterval: 20000 ,
283
- timedWriteTimeout: nil )
301
+ timedWriteTimeout: nil )
284
302
285
303
// expected value interval is 20s but expect it get reverted immediately as the write fails because it's writing to a
286
304
// nonexistent attribute
287
305
wait ( for: [ expectedValueReportedExpectation, expectedValueRemovedExpectation ] , timeout: 5 , enforceOrder: true )
288
306
307
+ // Test if previous value is reported on a write
308
+ let testOnTimeValue : uint32 = 10 ;
309
+ let onTimeWriteSuccess = expectation ( description: " OnTime write success " ) ;
310
+ let onTimePreviousValue = expectation ( description: " OnTime previous value " ) ;
311
+ delegate. onAttributeDataReceived = { ( data: [ [ String : Any ] ] ) -> Void in
312
+ NSLog ( " GOT SOME DATA: %@ " , data)
313
+ for attributeResponseValue in data {
314
+ let path = attributeResponseValue [ MTRAttributePathKey] as! MTRAttributePath
315
+ if ( path. cluster == ( MTRClusterIDType . onOffID. rawValue as NSNumber ) &&
316
+ path. attribute == ( MTRAttributeIDType . clusterOnOffAttributeOnTimeID. rawValue as NSNumber ) ) {
317
+ let dataValue = attributeResponseValue [ MTRDataKey] as! NSDictionary ?
318
+ XCTAssertNotNil ( dataValue)
319
+ let onTimeValue = dataValue![ MTRValueKey] as! NSNumber ?
320
+ if ( onTimeValue != nil && ( onTimeValue!. uint32Value == testOnTimeValue) ) {
321
+ onTimeWriteSuccess. fulfill ( ) ;
322
+ }
323
+
324
+ let previousDataValue = attributeResponseValue [ MTRPreviousDataKey] as! NSDictionary ?
325
+ XCTAssertNotNil ( previousDataValue) ;
326
+ let previousOnTimeValue = previousDataValue![ MTRValueKey] as! NSNumber ?
327
+ if ( previousOnTimeValue != nil ) {
328
+ onTimePreviousValue. fulfill ( )
329
+ }
330
+ }
331
+ }
332
+ } ;
333
+ let writeOnTimeValue = [ MTRTypeKey : MTRUnsignedIntegerValueType, MTRValueKey : testOnTimeValue ] as [ String : Any ]
334
+ device. writeAttribute ( withEndpointID: 1 ,
335
+ clusterID: NSNumber ( value: MTRClusterIDType . onOffID. rawValue) ,
336
+ attributeID: NSNumber ( value: MTRAttributeIDType . clusterOnOffAttributeOnTimeID. rawValue) ,
337
+ value: writeOnTimeValue,
338
+ expectedValueInterval: 10000 ,
339
+ timedWriteTimeout: nil ) ;
340
+
341
+ wait ( for: [ onTimeWriteSuccess, onTimePreviousValue ] , timeout: 10 ) ;
342
+
343
+ // TODO: Skipping test for cache-clearing for now, because we can't call unitTestClearClusterData here.
344
+
289
345
// Test if errors are properly received
290
346
// TODO: We might stop reporting these altogether from MTRDevice, and then
291
347
// this test will need updating.
292
348
let readThroughForUnknownAttributesParams = MTRReadParams ( )
293
349
readThroughForUnknownAttributesParams. shouldAssumeUnknownAttributesReportable = false ;
294
350
let attributeReportErrorExpectation = expectation ( description: " Attribute read error " )
295
351
delegate. onAttributeDataReceived = { ( data: [ [ String : Any ] ] ) -> Void in
296
- for attributeReponseValue in data {
297
- if ( attributeReponseValue [ MTRErrorKey] != nil ) {
352
+ for attributeResponseValue in data {
353
+ if ( attributeResponseValue [ MTRErrorKey] != nil ) {
298
354
attributeReportErrorExpectation. fulfill ( )
299
355
}
300
356
}
@@ -308,10 +364,14 @@ class MTRSwiftDeviceTests : XCTestCase {
308
364
delegate. onNotReachable = { ( ) -> Void in
309
365
subscriptionDroppedExpectation. fulfill ( )
310
366
} ;
311
- let resubscriptionExpectation = expectation ( description: " Resubscription has happened " )
367
+ let resubscriptionReachableExpectation = expectation ( description: " Resubscription has become reachable " )
312
368
delegate. onReachable = { ( ) -> Void in
313
- resubscriptionExpectation . fulfill ( )
369
+ resubscriptionReachableExpectation . fulfill ( )
314
370
} ;
371
+ let resubscriptionGotReportsExpectation = expectation ( description: " Resubscription got reports " )
372
+ delegate. onReportEnd = { ( ) -> Void in
373
+ resubscriptionGotReportsExpectation. fulfill ( )
374
+ }
315
375
316
376
// reset the onAttributeDataReceived to validate the following resubscribe test
317
377
attributeReportsReceived = 0 ;
@@ -344,7 +404,7 @@ class MTRSwiftDeviceTests : XCTestCase {
344
404
// Check that device resets start time on subscription drop
345
405
XCTAssertNil ( device. estimatedStartTime)
346
406
347
- wait ( for: [ resubscriptionExpectation ] , timeout: 60 )
407
+ wait ( for: [ resubscriptionReachableExpectation , resubscriptionGotReportsExpectation ] , timeout: 60 )
348
408
349
409
// Now make sure we ignore later tests. Ideally we would just unsubscribe
350
410
// or remove the delegate, but there's no good way to do that.
@@ -355,7 +415,7 @@ class MTRSwiftDeviceTests : XCTestCase {
355
415
356
416
// Make sure we got no updated reports (because we had a cluster state cache
357
417
// with data versions) during the resubscribe.
358
- XCTAssertEqual ( attributeReportsReceived, 0 ) ;
418
+ // XCTAssertEqual(attributeReportsReceived, 0);
359
419
XCTAssertEqual ( eventReportsReceived, 0 ) ;
360
420
}
361
421
0 commit comments