-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some cluster tests to iOS ChipTool (#6402)
- Loading branch information
1 parent
e78324e
commit 6df63b4
Showing
7 changed files
with
3,611 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
src/darwin/Framework/CHIP/templates/clusters-tests.zapt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
// | ||
// CHIPClustersTests.m | ||
// CHIPClustersTests | ||
/** | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// module headers | ||
#import <CHIP/CHIP.h> | ||
|
||
// additional includes | ||
#import "CHIPError.h" | ||
|
||
// system dependencies | ||
#import <XCTest/XCTest.h> | ||
|
||
const uint16_t kTimeoutInMs = 10; | ||
const uint64_t kDeviceId = 1; | ||
const uint16_t kDiscriminator = 3840; | ||
const uint32_t kSetupPINCode = 20202021; | ||
const uint16_t kRemotePort = 11097; | ||
const uint16_t kLocalPort = 11098; | ||
NSString * kAddress = @"::1"; | ||
|
||
CHIPDevice * GetPairedDevice(uint64_t deviceId) | ||
{ | ||
CHIPDeviceController * controller = [CHIPDeviceController sharedController]; | ||
XCTAssertNotNil(controller); | ||
|
||
NSError * pairingError; | ||
CHIPDevice * device = [controller getPairedDevice:deviceId error:&pairingError]; | ||
XCTAssertEqual(pairingError.code, 0); | ||
XCTAssertNotNil(device); | ||
|
||
return device; | ||
} | ||
|
||
@interface CHIPToolPairingDelegate : NSObject <CHIPDevicePairingDelegate> | ||
@property (nonatomic, strong) XCTestExpectation * expectation; | ||
@end | ||
|
||
@implementation CHIPToolPairingDelegate | ||
- (id)initWithExpectation:(XCTestExpectation *)expectation | ||
{ | ||
self = [super init]; | ||
if (self) { | ||
_expectation = expectation; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)onPairingComplete:(NSError *)error | ||
{ | ||
XCTAssertEqual(error.code, 0); | ||
[_expectation fulfill]; | ||
_expectation = nil; | ||
} | ||
@end | ||
|
||
@interface CHIPClustersTests : XCTestCase | ||
@end | ||
|
||
@implementation CHIPClustersTests | ||
|
||
- (void)testInitStack | ||
{ | ||
XCTestExpectation * expectation = [self expectationWithDescription:@"Pairing Complete"]; | ||
|
||
CHIPDeviceController * controller = [CHIPDeviceController sharedController]; | ||
XCTAssertNotNil(controller); | ||
|
||
CHIPToolPairingDelegate * pairing = [[CHIPToolPairingDelegate alloc] initWithExpectation:expectation]; | ||
dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.pairing", DISPATCH_QUEUE_SERIAL); | ||
|
||
[controller setListenPort:kLocalPort]; | ||
[controller setPairingDelegate:pairing queue:callbackQueue]; | ||
|
||
BOOL started = [controller startup:nil queue:nil]; | ||
XCTAssertTrue(started); | ||
|
||
NSError * error; | ||
[controller pairDevice:kDeviceId | ||
address:kAddress | ||
port:kRemotePort | ||
discriminator:kDiscriminator | ||
setupPINCode:kSetupPINCode | ||
error:&error]; | ||
XCTAssertEqual(error.code, 0); | ||
|
||
[self waitForExpectationsWithTimeout:kTimeoutInMs handler:nil]; | ||
} | ||
|
||
- (void)testShutdownStack | ||
{ | ||
CHIPDeviceController * controller = [CHIPDeviceController sharedController]; | ||
XCTAssertNotNil(controller); | ||
|
||
NSError * error; | ||
[controller unpairDevice:kDeviceId error:&error]; | ||
XCTAssertEqual(error.code, 0); | ||
|
||
BOOL stopped = [controller shutdown]; | ||
XCTAssertTrue(stopped); | ||
} | ||
|
||
{{#chip_client_clusters}} | ||
{{#chip_server_cluster_commands}} | ||
{{#unless (zcl_command_arguments_count this.id)}} | ||
- (void)testSendCluster{{asCamelCased parent.name false}}{{asCamelCased name false}}Command | ||
{ | ||
XCTestExpectation * expectation = [self expectationWithDescription:@"{{asCamelCased parent.name false}}{{asCamelCased name false}}"]; | ||
|
||
CHIPDevice * device = GetPairedDevice(kDeviceId); | ||
dispatch_queue_t queue = dispatch_get_main_queue(); | ||
CHIP{{asCamelCased parent.name false}} * cluster = [[CHIP{{asCamelCased parent.name false}} alloc] initWithDevice:device endpoint:{{asExpectedEndpointForCluster (asCamelCased parent.name false)}} queue:queue]; | ||
XCTAssertNotNil(cluster); | ||
|
||
[cluster {{asCamelCased name}}:^(NSError * err, NSDictionary * values) { | ||
NSLog(@"{{asCamelCased parent.name false}} {{asCamelCased name false}} Error: %@", err); | ||
XCTAssertEqual(err.code, {{asExpectedReturnValueForCommand (asCamelCased parent.name false) (asCamelCased name false)}}); | ||
[expectation fulfill]; | ||
}]; | ||
|
||
[self waitForExpectationsWithTimeout:kTimeoutInMs handler:nil]; | ||
} | ||
{{/unless}} | ||
{{/chip_server_cluster_commands}} | ||
{{/chip_client_clusters}} | ||
|
||
{{#chip_client_clusters}} | ||
{{#chip_server_cluster_attributes}} | ||
- (void)testSendCluster{{asCamelCased parent.name false}}ReadAttribute{{asCamelCased name false}} | ||
{ | ||
XCTestExpectation * expectation = [self expectationWithDescription:@"{{asCamelCased parent.name false}}ReadAttribute{{asCamelCased name false}}"]; | ||
|
||
CHIPDevice * device = GetPairedDevice(kDeviceId); | ||
dispatch_queue_t queue = dispatch_get_main_queue(); | ||
CHIP{{asCamelCased parent.name false}} * cluster = [[CHIP{{asCamelCased parent.name false}} alloc] initWithDevice:device endpoint:{{asExpectedEndpointForCluster (asCamelCased parent.name false)}} queue:queue]; | ||
XCTAssertNotNil(cluster); | ||
|
||
[cluster readAttribute{{asCamelCased name false}}:^(NSError * err, NSDictionary * values) { | ||
NSLog(@"{{asCamelCased parent.name false}} {{asCamelCased name false}} Error: %@", err); | ||
XCTAssertEqual(err.code, 0); | ||
[expectation fulfill]; | ||
}]; | ||
|
||
[self waitForExpectationsWithTimeout:kTimeoutInMs handler:nil]; | ||
} | ||
|
||
{{#if (isWritableAttribute)}} | ||
- (void)testSendCluster{{asCamelCased parent.name false}}WriteAttribute{{asCamelCased name false}} | ||
{ | ||
XCTestExpectation * expectation = [self expectationWithDescription:@"{{asCamelCased parent.name false}}WriteAttribute{{asCamelCased name false}}"]; | ||
|
||
CHIPDevice * device = GetPairedDevice(kDeviceId); | ||
dispatch_queue_t queue = dispatch_get_main_queue(); | ||
CHIP{{asCamelCased parent.name false}} * cluster = [[CHIP{{asCamelCased parent.name false}} alloc] initWithDevice:device endpoint:{{asExpectedEndpointForCluster (asCamelCased parent.name false)}} queue:queue]; | ||
XCTAssertNotNil(cluster); | ||
|
||
{{asObjectiveCBasicType type}} value = {{asTestValue}}; | ||
[cluster writeAttribute{{asCamelCased name false}}:value completionHandler:^(NSError * err, NSDictionary * values) { | ||
NSLog(@"{{asCamelCased parent.name false}} {{asCamelCased name false}} Error: %@", err); | ||
XCTAssertEqual(err.code, 0); | ||
[expectation fulfill]; | ||
}]; | ||
|
||
[self waitForExpectationsWithTimeout:kTimeoutInMs handler:nil]; | ||
} | ||
{{/if}} | ||
{{/chip_server_cluster_attributes}} | ||
{{/chip_client_clusters}} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Import helpers from zap core | ||
const zapPath = '../../../../../third_party/zap/repo/src-electron/'; | ||
const templateUtil = require(zapPath + 'generator/template-util.js') | ||
const zclHelper = require(zapPath + 'generator/helper-zcl.js') | ||
|
||
const ChipTypesHelper = require('../../../../../src/app/zap-templates/common/ChipTypesHelper.js'); | ||
const StringHelper = require('../../../../../src/app/zap-templates/common/StringHelper.js'); | ||
|
||
// Ideally those clusters clusters endpoints should be retrieved from the | ||
// descriptor cluster. | ||
function asExpectedEndpointForCluster(clusterName) | ||
{ | ||
switch (clusterName) { | ||
case 'Basic': | ||
case 'Descriptor': | ||
case 'GeneralCommissioning': | ||
case 'GroupKeyManagement': | ||
case 'NetworkCommissioning': | ||
case 'OperationalCredentials': | ||
return 0; | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
function asExpectedReturnValueForCommand(clusterName, commandName) | ||
{ | ||
switch (clusterName) { | ||
case 'GeneralCommissioning': | ||
switch (commandName) { | ||
// All methods are not implemented and returns `EMBER_ZCL_STATUS_FAILURE` | ||
default: | ||
return 1; | ||
} | ||
break; | ||
|
||
case 'OperationalCredentials': | ||
switch (commandName) { | ||
// All methods are not implemented and returns `EMBER_ZCL_STATUS_FAILURE` | ||
default: | ||
return 1; | ||
} | ||
break; | ||
|
||
case 'Thermostat': | ||
switch (commandName) { | ||
// All those methods are not implemented and directly returns false. So the stack returns | ||
// `EMBER_ZCL_STATUS_UNSUP_CLUSTER_COMMAND` | ||
case 'GetRelayStatusLog': | ||
case 'ClearWeeklySchedule': | ||
case 'SetWeeklySchedule': | ||
case 'GetWeeklySchedule': | ||
return 1; | ||
} | ||
break; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
function asTestValue() | ||
{ | ||
if (StringHelper.isOctetString(this.type)) { | ||
return '[@"Test" dataUsingEncoding:NSUTF8StringEncoding]'; | ||
} else if (StringHelper.isCharString(this.type)) { | ||
return '@"Test"'; | ||
} else { | ||
return this.min || this.max || 0; | ||
} | ||
} | ||
|
||
// | ||
// Module exports | ||
// | ||
exports.asExpectedEndpointForCluster = asExpectedEndpointForCluster; | ||
exports.asExpectedReturnValueForCommand = asExpectedReturnValueForCommand; | ||
exports.asTestValue = asTestValue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "Framework templates", | ||
"version": "chip-v1", | ||
"helpers": [ | ||
"../../../../../src/app/zap-templates/partials/helper.js", | ||
"../../../../../src/app/zap-templates/common/StringHelper.js", | ||
"../../../../../src/app/zap-templates/templates/app/helper.js", | ||
"../../../../../src/app/zap-templates/templates/chip/helper.js", | ||
"helper.js" | ||
], | ||
"override": "../../../../../src/app/zap-templates/common/override.js", | ||
"partials": [], | ||
"templates": [ | ||
{ | ||
"path": "clusters-tests.zapt", | ||
"name": "Cluster Tests", | ||
"output": "src/darwin/Framework/CHIPTests/CHIPClustersTests.m" | ||
} | ||
] | ||
} |
Oops, something went wrong.