diff --git a/src/darwin/Framework/CHIPTests/CHIPClustersTests.m b/src/darwin/Framework/CHIPTests/CHIPClustersTests.m index e249b7fedc88d6..923c9563d32117 100644 --- a/src/darwin/Framework/CHIPTests/CHIPClustersTests.m +++ b/src/darwin/Framework/CHIPTests/CHIPClustersTests.m @@ -2410,7 +2410,7 @@ - (void)testSendClusterTestSubscribe_OnOff_000002_On [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; } -- (void)testSendClusterTestSubscribe_OnOff_000003_WaitForAttributeReport +- (void)testSendClusterTestSubscribe_OnOff_000003_WaitForReport { XCTestExpectation * expectation = [self expectationWithDescription:@"Check for attribute report"]; CHIPDevice * device = GetPairedDevice(kDeviceId); @@ -2419,12 +2419,48 @@ - (void)testSendClusterTestSubscribe_OnOff_000003_WaitForAttributeReport XCTAssertNotNil(cluster); [cluster reportAttributeOnOffWithResponseHandler:^(NSError * err, NSDictionary * values) { - NSLog(@"Reporting Test Report: %@", err); - XCTAssertEqual(err.code, 0); + NSLog(@"Check for attribute report Error: %@", err); + XCTAssertEqual(err.code, 0); XCTAssertEqual([values[@"value"] boolValue], true); [expectation fulfill]; }]; + + [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; +} +- (void)testSendClusterTestSubscribe_OnOff_000004_Off +{ + XCTestExpectation * expectation = [self expectationWithDescription:@"Turn Off the light to see attribute change"]; + CHIPDevice * device = GetPairedDevice(kDeviceId); + dispatch_queue_t queue = dispatch_get_main_queue(); + CHIPOnOff * cluster = [[CHIPOnOff alloc] initWithDevice:device endpoint:1 queue:queue]; + XCTAssertNotNil(cluster); + + [cluster off:^(NSError * err, NSDictionary * values) { + NSLog(@"Turn Off the light to see attribute change Error: %@", err); + + XCTAssertEqual(err.code, 0); + [expectation fulfill]; + }]; + + [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; +} +- (void)testSendClusterTestSubscribe_OnOff_000005_WaitForReport +{ + XCTestExpectation * expectation = [self expectationWithDescription:@"Check for attribute report"]; + CHIPDevice * device = GetPairedDevice(kDeviceId); + dispatch_queue_t queue = dispatch_get_main_queue(); + CHIPOnOff * cluster = [[CHIPOnOff alloc] initWithDevice:device endpoint:1 queue:queue]; + XCTAssertNotNil(cluster); + + [cluster reportAttributeOnOffWithResponseHandler:^(NSError * err, NSDictionary * values) { + NSLog(@"Check for attribute report Error: %@", err); + + XCTAssertEqual(err.code, 0); + XCTAssertEqual([values[@"value"] boolValue], false); + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kTimeoutInSeconds handler:nil]; } diff --git a/zzz_generated/chip-tool/zap-generated/test/Commands.h b/zzz_generated/chip-tool/zap-generated/test/Commands.h index 616105d61766f7..22b6fa8c2846b5 100644 --- a/zzz_generated/chip-tool/zap-generated/test/Commands.h +++ b/zzz_generated/chip-tool/zap-generated/test/Commands.h @@ -10580,7 +10580,7 @@ class TestDelayCommands : public TestCommand switch (mTestIndex++) { case 0: - err = TestSendClusterTestSuiteCommandWaitForMs_0(); + err = TestSendClusterDelayCommandsCommandWaitForMs_0(); break; } @@ -10599,9 +10599,9 @@ class TestDelayCommands : public TestCommand // Tests methods // - CHIP_ERROR TestSendClusterTestSuiteCommandWaitForMs_0() + CHIP_ERROR TestSendClusterDelayCommandsCommandWaitForMs_0() { - ChipLogProgress(chipTool, "TestSuite - WaitForMs - Wait 100ms"); + ChipLogProgress(chipTool, "DelayCommands - Wait 100ms"); return WaitForMs(100); } @@ -10639,7 +10639,13 @@ class TestSubscribe_OnOff : public TestCommand err = TestSendClusterOnOffCommandOn_2(); break; case 3: - err = TestSendClusterTestSuiteCommandWaitForAttributeReport_3(); + err = TestSendClusterOnOffCommandWaitForReport_3(); + break; + case 4: + err = TestSendClusterOnOffCommandOff_4(); + break; + case 5: + err = TestSendClusterOnOffCommandWaitForReport_5(); break; } @@ -10652,7 +10658,7 @@ class TestSubscribe_OnOff : public TestCommand private: std::atomic_uint16_t mTestIndex; - const uint16_t mTestCount = 4; + const uint16_t mTestCount = 6; // // Tests methods @@ -10848,30 +10854,49 @@ class TestSubscribe_OnOff : public TestCommand runner->NextTest(); } - // The callback should be called atleast once - using OnReportCallback_3 = void (*)(void * context, bool onOff); - chip::Callback::Callback mOnReportCallback_3{ SubscribeAttribute_3_OnReportCallback, this }; + // Test Check for attribute report + using SuccessCallback_3 = void (*)(void * context, bool onOff); + chip::Callback::Callback mOnSuccessCallback_3{ OnTestSendClusterOnOffCommandWaitForReport_3_SuccessResponse, + this }; + + bool mIsFailureExpected_3 = 0; bool mReceivedReport_3 = false; - CHIP_ERROR TestSendClusterTestSuiteCommandWaitForAttributeReport_3() + CHIP_ERROR TestSendClusterOnOffCommandWaitForReport_3() { - ChipLogProgress(chipTool, "TestSuite - WaitForAttributeReport - Check for attribute report"); + ChipLogProgress(chipTool, "On/Off - Check for attribute report: Sending command..."); + chip::Controller::OnOffCluster cluster; cluster.Associate(mDevice, 1); - return cluster.ReportAttributeOnOff(mOnReportCallback_3.Cancel()); + + CHIP_ERROR err = CHIP_NO_ERROR; + + err = cluster.ReportAttributeOnOff(mOnSuccessCallback_3.Cancel()); + + return err; } - static void SubscribeAttribute_3_OnReportCallback(void * context, bool onOff) + static void OnTestSendClusterOnOffCommandWaitForReport_3_SuccessResponse(void * context, bool onOff) { - ChipLogProgress(chipTool, "On/Off - Subscribe OnOff Attribute: Report Data"); + ChipLogProgress(chipTool, "On/Off - Check for attribute report: Success Response"); + TestSubscribe_OnOff * runner = reinterpret_cast(context); + if (runner->mIsFailureExpected_3 == true) + { + ChipLogError(chipTool, "Error: The test was expecting a failure callback. Got success callback"); + runner->SetCommandExitStatus(CHIP_ERROR_INTERNAL); + return; + } + if (runner->mReceivedReport_3) { - // Receiving attribute more than once is not an issue, since the following handler will override previous handlers. + // Receiving report more than once is not an error since the subscription may be alive for a long time. + ChipLogProgress(chipTool, "Note: on report called more than once."); return; } + runner->mReceivedReport_3 = true; if (onOff != true) { @@ -10880,8 +10905,114 @@ class TestSubscribe_OnOff : public TestCommand return; } - runner->mReceivedReport_3 = true; - ChipLogProgress(chipTool, "On/Off - report received."); + runner->NextTest(); + } + + // Test Turn Off the light to see attribute change + using SuccessCallback_4 = void (*)(void * context); + chip::Callback::Callback mOnSuccessCallback_4{ OnTestSendClusterOnOffCommandOff_4_SuccessResponse, this }; + chip::Callback::Callback mOnFailureCallback_4{ OnTestSendClusterOnOffCommandOff_4_FailureResponse, + this }; + + bool mIsFailureExpected_4 = 0; + + CHIP_ERROR TestSendClusterOnOffCommandOff_4() + { + ChipLogProgress(chipTool, "On/Off - Turn Off the light to see attribute change: Sending command..."); + + chip::Controller::OnOffCluster cluster; + cluster.Associate(mDevice, 1); + + CHIP_ERROR err = CHIP_NO_ERROR; + + err = cluster.Off(mOnSuccessCallback_4.Cancel(), mOnFailureCallback_4.Cancel()); + + return err; + } + + static void OnTestSendClusterOnOffCommandOff_4_FailureResponse(void * context, uint8_t status) + { + ChipLogProgress(chipTool, "On/Off - Turn Off the light to see attribute change: Failure Response"); + + TestSubscribe_OnOff * runner = reinterpret_cast(context); + + if (runner->mIsFailureExpected_4 == false) + { + ChipLogError(chipTool, "Error: The test was expecting a success callback. Got failure callback"); + runner->SetCommandExitStatus(CHIP_ERROR_INTERNAL); + return; + } + + runner->NextTest(); + } + + static void OnTestSendClusterOnOffCommandOff_4_SuccessResponse(void * context) + { + ChipLogProgress(chipTool, "On/Off - Turn Off the light to see attribute change: Success Response"); + + TestSubscribe_OnOff * runner = reinterpret_cast(context); + + if (runner->mIsFailureExpected_4 == true) + { + ChipLogError(chipTool, "Error: The test was expecting a failure callback. Got success callback"); + runner->SetCommandExitStatus(CHIP_ERROR_INTERNAL); + return; + } + + runner->NextTest(); + } + + // Test Check for attribute report + using SuccessCallback_5 = void (*)(void * context, bool onOff); + chip::Callback::Callback mOnSuccessCallback_5{ OnTestSendClusterOnOffCommandWaitForReport_5_SuccessResponse, + this }; + + bool mIsFailureExpected_5 = 0; + + bool mReceivedReport_5 = false; + + CHIP_ERROR TestSendClusterOnOffCommandWaitForReport_5() + { + ChipLogProgress(chipTool, "On/Off - Check for attribute report: Sending command..."); + + chip::Controller::OnOffCluster cluster; + cluster.Associate(mDevice, 1); + + CHIP_ERROR err = CHIP_NO_ERROR; + + err = cluster.ReportAttributeOnOff(mOnSuccessCallback_5.Cancel()); + + return err; + } + + static void OnTestSendClusterOnOffCommandWaitForReport_5_SuccessResponse(void * context, bool onOff) + { + ChipLogProgress(chipTool, "On/Off - Check for attribute report: Success Response"); + + TestSubscribe_OnOff * runner = reinterpret_cast(context); + + if (runner->mIsFailureExpected_5 == true) + { + ChipLogError(chipTool, "Error: The test was expecting a failure callback. Got success callback"); + runner->SetCommandExitStatus(CHIP_ERROR_INTERNAL); + return; + } + + if (runner->mReceivedReport_5) + { + // Receiving report more than once is not an error since the subscription may be alive for a long time. + ChipLogProgress(chipTool, "Note: on report called more than once."); + return; + } + runner->mReceivedReport_5 = true; + + if (onOff != false) + { + ChipLogError(chipTool, "Error: Value mismatch. Expected: '%s'", "false"); + runner->SetCommandExitStatus(CHIP_ERROR_INTERNAL); + return; + } + runner->NextTest(); } };