@@ -116,24 +116,37 @@ - (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSEr
116
116
117
117
@end
118
118
119
+ typedef void (^MTRDeviceTestDelegateDataHandler)(NSArray <NSDictionary <NSString *, id > *> *);
120
+
119
121
@interface MTRDeviceTestDelegate : NSObject <MTRDeviceDelegate>
120
122
@property (nonatomic ) dispatch_block_t onSubscriptionEstablished;
123
+ @property (nonatomic , nullable ) MTRDeviceTestDelegateDataHandler onAttributeDataReceived;
124
+ @property (nonatomic , nullable ) MTRDeviceTestDelegateDataHandler onEventDataReceived;
125
+ @property (nonatomic , nullable ) dispatch_block_t onSubscriptionDropped;
121
126
@end
122
127
123
128
@implementation MTRDeviceTestDelegate
124
129
- (void )device : (MTRDevice *)device stateChanged : (MTRDeviceState)state
125
130
{
126
131
if (state == MTRDeviceStateReachable) {
127
132
self.onSubscriptionEstablished ();
133
+ } else if (state == MTRDeviceStateUnknown && self.onSubscriptionDropped != nil ) {
134
+ self.onSubscriptionDropped ();
128
135
}
129
136
}
130
137
131
138
- (void )device : (MTRDevice *)device receivedAttributeReport : (NSArray <NSDictionary<NSString *, id> *> *)attributeReport
132
139
{
140
+ if (self.onAttributeDataReceived != nil ) {
141
+ self.onAttributeDataReceived (attributeReport);
142
+ }
133
143
}
134
144
135
145
- (void )device : (MTRDevice *)device receivedEventReport : (NSArray <NSDictionary<NSString *, id> *> *)eventReport
136
146
{
147
+ if (self.onEventDataReceived != nil ) {
148
+ self.onEventDataReceived (eventReport);
149
+ }
137
150
}
138
151
139
152
@end
@@ -1369,9 +1382,68 @@ - (void)test017_TestMTRDeviceBasics
1369
1382
[subscriptionExpectation fulfill ];
1370
1383
};
1371
1384
1385
+ __block unsigned attributeReportsReceived = 0 ;
1386
+ delegate.onAttributeDataReceived = ^(NSArray <NSDictionary <NSString *, id > *> * data) {
1387
+ attributeReportsReceived += data.count ;
1388
+ };
1389
+
1390
+ __block unsigned eventReportsReceived = 0 ;
1391
+ delegate.onEventDataReceived = ^(NSArray <NSDictionary <NSString *, id > *> * data) {
1392
+ eventReportsReceived += data.count ;
1393
+ };
1394
+
1372
1395
[device setDelegate: delegate queue: queue];
1373
1396
1374
1397
[self waitForExpectations: @[ subscriptionExpectation ] timeout: 60 ];
1398
+
1399
+ XCTAssertNotEqual (attributeReportsReceived, 0 );
1400
+
1401
+ attributeReportsReceived = 0 ;
1402
+ eventReportsReceived = 0 ;
1403
+
1404
+ XCTestExpectation * resubscriptionExpectation = [self expectationWithDescription: @" Resubscription has happened" ];
1405
+ delegate.onSubscriptionEstablished = ^() {
1406
+ [resubscriptionExpectation fulfill ];
1407
+ };
1408
+
1409
+ XCTestExpectation * subscriptionDroppedExpectation = [self expectationWithDescription: @" Subscription has dropped" ];
1410
+ delegate.onSubscriptionDropped = ^() {
1411
+ [subscriptionDroppedExpectation fulfill ];
1412
+ };
1413
+
1414
+ // Now trigger another subscription which will cause ours to drop; we should re-subscribe after that.
1415
+ MTRBaseDevice * baseDevice = GetConnectedDevice ();
1416
+ __auto_type params = [[MTRSubscribeParams alloc ] initWithMinInterval: @(1 ) maxInterval: @(10 )];
1417
+ params.resubscribeIfLost = NO ;
1418
+ params.replaceExistingSubscriptions = YES ;
1419
+ // Create second subscription which will cancel the first subscription. We
1420
+ // can use a non-existent path here to cut down on the work that gets done.
1421
+ [baseDevice subscribeAttributeWithEndpointId: @10000
1422
+ clusterId: @6
1423
+ attributeId: @0
1424
+ minInterval: @(1 )
1425
+ maxInterval: @(2 )
1426
+ params: params
1427
+ clientQueue: queue
1428
+ reportHandler: ^(id _Nullable values, NSError * _Nullable error) {
1429
+ }
1430
+ subscriptionEstablished: ^() {
1431
+ }];
1432
+
1433
+ [self waitForExpectations: @[ subscriptionDroppedExpectation, resubscriptionExpectation ] timeout: 60 enforceOrder: YES ];
1434
+
1435
+ // Now make sure we ignore later tests. Ideally we would just unsubscribe
1436
+ // or remove the delegate, but there's no good way to do that.
1437
+ delegate.onSubscriptionEstablished = ^() {
1438
+ };
1439
+ delegate.onSubscriptionDropped = nil ;
1440
+ delegate.onAttributeDataReceived = nil ;
1441
+ delegate.onEventDataReceived = nil ;
1442
+
1443
+ // Make sure we got no updated reports (because we had a cluster state cache
1444
+ // with data versions) during the resubscribe.
1445
+ XCTAssertEqual (attributeReportsReceived, 0 );
1446
+ XCTAssertEqual (eventReportsReceived, 0 );
1375
1447
}
1376
1448
1377
1449
- (void )test018_SubscriptionErrorWhenNotResubscribing
0 commit comments