Skip to content

Commit

Permalink
Removed need to plumb MTRDeviceControllerDataStore methods through MT…
Browse files Browse the repository at this point in the history
…RDeviceController
  • Loading branch information
jtung-apple committed Feb 6, 2024
1 parent 701752f commit e6860f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
20 changes: 0 additions & 20 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1236,26 +1236,6 @@ - (void)downloadLogFromNodeWithID:(NSNumber *)nodeID
completion:completion];
}

#pragma - Unit Test accessors for data store
#ifdef DEBUG
- (nullable NSArray<NSDictionary *> *)unitTest_dataStore_getStoredAttributesForNodeID:(NSNumber *)nodeID
{
return [_controllerDataStore getStoredAttributesForNodeID:nodeID];
}
- (void)unitTest_dataStore_storeAttributeValues:(NSArray<NSDictionary *> *)dataValues forNodeID:(NSNumber *)nodeID
{
[_controllerDataStore storeAttributeValues:dataValues forNodeID:nodeID];
}
- (void)unitTest_dataStore_clearStoredAttributesForNodeID:(NSNumber *)nodeID
{
[_controllerDataStore clearStoredAttributesForNodeID:nodeID];
}
- (void)unitTest_dataStore_clearAllStoredAttributes
{
[_controllerDataStore clearAllStoredAttributes];
}
#endif

@end

/**
Expand Down
45 changes: 25 additions & 20 deletions src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@
static NSString * kOnboardingPayload = @"MT:-24J0AFN00KA0648G00";
static const uint16_t kTestVendorId = 0xFFF1u;

// Declare unit test only methods to access data store directly for testing verification
// MTRDeviceControllerDataStore.h includes C++ header, and so we need to declare the methods separately
@protocol MTRDeviceControllerDataStoreAttributeStoreMethods
- (nullable NSArray<NSDictionary *> *)getStoredAttributesForNodeID:(NSNumber *)nodeID;
- (void)storeAttributeValues:(NSArray<NSDictionary *> *)dataValues forNodeID:(NSNumber *)nodeID;
- (void)clearStoredAttributesForNodeID:(NSNumber *)nodeID;
- (void)clearAllStoredAttributes;
@end

// Declare internal methods for testing
@interface MTRDeviceController (Test)
- (void)removeDevice:(MTRDevice *)device;
- (nullable NSArray<NSDictionary *> *)unitTest_dataStore_getStoredAttributesForNodeID:(NSNumber *)nodeID;
- (void)unitTest_dataStore_storeAttributeValues:(NSArray<NSDictionary *> *)dataValues forNodeID:(NSNumber *)nodeID;
- (void)unitTest_dataStore_clearStoredAttributesForNodeID:(NSNumber *)nodeID;
- (void)unitTest_dataStore_clearAllStoredAttributes;
@property (nonatomic, readonly, nullable) id<MTRDeviceControllerDataStoreAttributeStoreMethods> controllerDataStore;
@end

@interface MTRDevice (Test)
Expand Down Expand Up @@ -1091,16 +1096,16 @@ - (void)test008_TestDataStoreDirect
@{ MTRAttributePathKey : [MTRAttributePath attributePathWithEndpointID:@(2) clusterID:@(1) attributeID:@(2)], MTRDataKey : @ { MTRTypeKey : MTRUnsignedIntegerValueType, MTRValueKey : @(212) } },
@{ MTRAttributePathKey : [MTRAttributePath attributePathWithEndpointID:@(2) clusterID:@(1) attributeID:@(3)], MTRDataKey : @ { MTRTypeKey : MTRUnsignedIntegerValueType, MTRValueKey : @(213) } },
];
[controller unitTest_dataStore_storeAttributeValues:testAttributes forNodeID:@(1001)];
[controller unitTest_dataStore_storeAttributeValues:testAttributes forNodeID:@(1002)];
[controller unitTest_dataStore_storeAttributeValues:testAttributes forNodeID:@(1003)];
[controller.controllerDataStore storeAttributeValues:testAttributes forNodeID:@(1001)];
[controller.controllerDataStore storeAttributeValues:testAttributes forNodeID:@(1002)];
[controller.controllerDataStore storeAttributeValues:testAttributes forNodeID:@(1003)];

// Check values are written and can be fetched
NSArray * dataStoreValues = [controller unitTest_dataStore_getStoredAttributesForNodeID:@(1001)];
NSArray * dataStoreValues = [controller.controllerDataStore getStoredAttributesForNodeID:@(1001)];
XCTAssertEqual(dataStoreValues.count, 9);
dataStoreValues = [controller unitTest_dataStore_getStoredAttributesForNodeID:@(1002)];
dataStoreValues = [controller.controllerDataStore getStoredAttributesForNodeID:@(1002)];
XCTAssertEqual(dataStoreValues.count, 9);
dataStoreValues = [controller unitTest_dataStore_getStoredAttributesForNodeID:@(1003)];
dataStoreValues = [controller.controllerDataStore getStoredAttributesForNodeID:@(1003)];
XCTAssertEqual(dataStoreValues.count, 9);

// Check values
Expand Down Expand Up @@ -1136,20 +1141,20 @@ - (void)test008_TestDataStoreDirect
}
}

[controller unitTest_dataStore_clearStoredAttributesForNodeID:@(1001)];
dataStoreValues = [controller unitTest_dataStore_getStoredAttributesForNodeID:@(1001)];
[controller.controllerDataStore clearStoredAttributesForNodeID:@(1001)];
dataStoreValues = [controller.controllerDataStore getStoredAttributesForNodeID:@(1001)];
XCTAssertEqual(dataStoreValues.count, 0);
dataStoreValues = [controller unitTest_dataStore_getStoredAttributesForNodeID:@(1002)];
dataStoreValues = [controller.controllerDataStore getStoredAttributesForNodeID:@(1002)];
XCTAssertEqual(dataStoreValues.count, 9);
dataStoreValues = [controller unitTest_dataStore_getStoredAttributesForNodeID:@(1003)];
dataStoreValues = [controller.controllerDataStore getStoredAttributesForNodeID:@(1003)];
XCTAssertEqual(dataStoreValues.count, 9);

[controller unitTest_dataStore_clearAllStoredAttributes];
dataStoreValues = [controller unitTest_dataStore_getStoredAttributesForNodeID:@(1001)];
[controller.controllerDataStore clearAllStoredAttributes];
dataStoreValues = [controller.controllerDataStore getStoredAttributesForNodeID:@(1001)];
XCTAssertEqual(dataStoreValues.count, 0);
dataStoreValues = [controller unitTest_dataStore_getStoredAttributesForNodeID:@(1002)];
dataStoreValues = [controller.controllerDataStore getStoredAttributesForNodeID:@(1002)];
XCTAssertEqual(dataStoreValues.count, 0);
dataStoreValues = [controller unitTest_dataStore_getStoredAttributesForNodeID:@(1003)];
dataStoreValues = [controller.controllerDataStore getStoredAttributesForNodeID:@(1003)];
XCTAssertEqual(dataStoreValues.count, 0);

[controller shutdown];
Expand Down Expand Up @@ -1211,7 +1216,7 @@ - (void)test009_TestDataStoreMTRDevice

[self waitForExpectations:@[ subscriptionExpectation ] timeout:60];

NSArray * dataStoreValues = [controller unitTest_dataStore_getStoredAttributesForNodeID:deviceID];
NSArray * dataStoreValues = [controller.controllerDataStore getStoredAttributesForNodeID:deviceID];

// Verify all values are stored into storage
for (NSDictionary * responseValue in dataStoreValues) {
Expand Down

0 comments on commit e6860f0

Please sign in to comment.