@@ -40,12 +40,14 @@ class TestDelegate : public Delegate
40
40
{
41
41
public:
42
42
TestDelegate () {}
43
- DataModel::Nullable<chip:: Percent> HandleOpenValve (DataModel::Nullable<chip:: Percent> level) override
43
+ DataModel::Nullable<Percent> HandleOpenValve (DataModel::Nullable<Percent> level) override
44
44
{
45
+ lastRequestedLevel = level;
45
46
return DataModel::NullNullable;
46
47
}
47
48
CHIP_ERROR HandleCloseValve () override { return CHIP_NO_ERROR; }
48
49
void HandleRemainingDurationTick (uint32_t duration) override {}
50
+ DataModel::Nullable<Percent> lastRequestedLevel;
49
51
};
50
52
51
53
TEST_F (TestValveConfigurationAndControlClusterLogic, TestConformanceValid)
@@ -504,6 +506,156 @@ TEST_F(TestValveConfigurationAndControlClusterLogic, TestSetDefaultOpenLevel)
504
506
EXPECT_EQ (logic_no_level.SetDefaultOpenLevel (testVal), CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE);
505
507
}
506
508
509
+ TEST_F (TestValveConfigurationAndControlClusterLogic, TestHandleOpenDuration)
510
+ {
511
+ TestDelegate delegate;
512
+ TestPersistentStorageDelegate storageDelegate;
513
+ EndpointId endpoint = 0 ;
514
+ MatterContext context = MatterContext (endpoint, storageDelegate);
515
+ ClusterLogic logic = ClusterLogic (delegate, context);
516
+
517
+ ClusterConformance conformance = { .featureMap = to_underlying (Feature::kLevel ) | to_underlying (Feature::kTimeSync ),
518
+ .supportsDefaultOpenLevel = true ,
519
+ .supportsValveFault = true ,
520
+ .supportsLevelStep = true };
521
+ EXPECT_EQ (logic.Init (conformance), CHIP_NO_ERROR);
522
+
523
+ DataModel::Nullable<ElapsedS> valElapsedSNullable;
524
+
525
+ EXPECT_EQ (logic.GetOpenDuration (valElapsedSNullable), CHIP_NO_ERROR);
526
+ EXPECT_EQ (valElapsedSNullable, DataModel::NullNullable);
527
+
528
+ EXPECT_EQ (logic.GetDefaultOpenDuration (valElapsedSNullable), CHIP_NO_ERROR);
529
+ EXPECT_EQ (valElapsedSNullable, DataModel::NullNullable);
530
+
531
+ // Fall back to default
532
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::nullopt), CHIP_NO_ERROR);
533
+ EXPECT_EQ (logic.GetOpenDuration (valElapsedSNullable), CHIP_NO_ERROR);
534
+ EXPECT_EQ (valElapsedSNullable, DataModel::NullNullable);
535
+
536
+ DataModel::Nullable<ElapsedS> defaultOpenDuration;
537
+ defaultOpenDuration.SetNonNull (12u );
538
+ EXPECT_EQ (logic.SetDefaultOpenDuration (defaultOpenDuration), CHIP_NO_ERROR);
539
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::nullopt), CHIP_NO_ERROR);
540
+ EXPECT_EQ (logic.GetOpenDuration (valElapsedSNullable), CHIP_NO_ERROR);
541
+ EXPECT_EQ (valElapsedSNullable, defaultOpenDuration);
542
+
543
+ // Set from command parameters
544
+ DataModel::Nullable<ElapsedS> openDuration;
545
+ openDuration.SetNull ();
546
+ EXPECT_EQ (logic.HandleOpenCommand (std::make_optional (openDuration), std::nullopt), CHIP_NO_ERROR);
547
+ EXPECT_EQ (logic.GetOpenDuration (valElapsedSNullable), CHIP_NO_ERROR);
548
+ EXPECT_EQ (valElapsedSNullable, DataModel::NullNullable);
549
+
550
+ openDuration.SetNonNull (12u );
551
+ EXPECT_EQ (logic.HandleOpenCommand (std::make_optional (openDuration), std::nullopt), CHIP_NO_ERROR);
552
+ EXPECT_EQ (logic.GetOpenDuration (valElapsedSNullable), CHIP_NO_ERROR);
553
+ EXPECT_EQ (valElapsedSNullable.ValueOr (0 ), 12u );
554
+ }
555
+
556
+ TEST_F (TestValveConfigurationAndControlClusterLogic, TestHandleOpenTargetLevelFeatureUnsupported)
557
+ {
558
+ TestDelegate delegate;
559
+ TestPersistentStorageDelegate storageDelegate;
560
+ EndpointId endpoint = 0 ;
561
+ MatterContext context = MatterContext (endpoint, storageDelegate);
562
+ ClusterLogic logic = ClusterLogic (delegate, context);
563
+
564
+ ClusterConformance conformance = {
565
+ .featureMap = 0 , .supportsDefaultOpenLevel = false , .supportsValveFault = false , .supportsLevelStep = false
566
+ };
567
+ EXPECT_EQ (logic.Init (conformance), CHIP_NO_ERROR);
568
+
569
+ // Set the last value to something non-null first so we can ensure the delegate was called correctly.
570
+ delegate.lastRequestedLevel .SetNonNull (0 );
571
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::nullopt), CHIP_NO_ERROR);
572
+ // This configuration doesn't support level, so the delegate should be called with a NullNullable
573
+ EXPECT_EQ (delegate.lastRequestedLevel , DataModel::NullNullable);
574
+
575
+ // Should get an error when this is called with target level set since the feature is unsupported.
576
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::make_optional (50u )), CHIP_ERROR_INVALID_ARGUMENT);
577
+ }
578
+
579
+ TEST_F (TestValveConfigurationAndControlClusterLogic, TestHandleOpenTargetLevelNotSuppliedNoDefaultSupported)
580
+ {
581
+ TestDelegate delegate;
582
+ TestPersistentStorageDelegate storageDelegate;
583
+ EndpointId endpoint = 0 ;
584
+ MatterContext context = MatterContext (endpoint, storageDelegate);
585
+ ClusterLogic logic = ClusterLogic (delegate, context);
586
+
587
+ ClusterConformance conformance = { .featureMap = to_underlying (Feature::kLevel ),
588
+ .supportsDefaultOpenLevel = false ,
589
+ .supportsValveFault = false ,
590
+ .supportsLevelStep = false };
591
+ EXPECT_EQ (logic.Init (conformance), CHIP_NO_ERROR);
592
+ // Set the last value to something non-null first so we can ensure the delegate was called correctly.
593
+ delegate.lastRequestedLevel .SetNonNull (0 );
594
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::nullopt), CHIP_NO_ERROR);
595
+ EXPECT_EQ (delegate.lastRequestedLevel .ValueOr (0 ), 100u );
596
+ }
597
+
598
+ TEST_F (TestValveConfigurationAndControlClusterLogic, TestHandleOpenTargetLevelNotSuppliedDefaultSupported)
599
+ {
600
+ TestDelegate delegate;
601
+ TestPersistentStorageDelegate storageDelegate;
602
+ EndpointId endpoint = 0 ;
603
+ MatterContext context = MatterContext (endpoint, storageDelegate);
604
+ ClusterLogic logic = ClusterLogic (delegate, context);
605
+
606
+ ClusterConformance conformance = { .featureMap = to_underlying (Feature::kLevel ),
607
+ .supportsDefaultOpenLevel = true ,
608
+ .supportsValveFault = false ,
609
+ .supportsLevelStep = false };
610
+ EXPECT_EQ (logic.Init (conformance), CHIP_NO_ERROR);
611
+ // Set the last value to something non-null first so we can ensure the delegate was called correctly.
612
+ delegate.lastRequestedLevel .SetNonNull (0 );
613
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::nullopt), CHIP_NO_ERROR);
614
+ EXPECT_EQ (delegate.lastRequestedLevel .ValueOr (0 ), 100u );
615
+
616
+ EXPECT_EQ (logic.SetDefaultOpenLevel (50u ), CHIP_NO_ERROR);
617
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::nullopt), CHIP_NO_ERROR);
618
+ EXPECT_EQ (delegate.lastRequestedLevel .ValueOr (0 ), 50u );
619
+ }
620
+
621
+ TEST_F (TestValveConfigurationAndControlClusterLogic, TestHandleOpenTargetLevelSupplied)
622
+ {
623
+ TestDelegate delegate;
624
+ TestPersistentStorageDelegate storageDelegate;
625
+ EndpointId endpoint = 0 ;
626
+ MatterContext context = MatterContext (endpoint, storageDelegate);
627
+ ClusterLogic logic = ClusterLogic (delegate, context);
628
+
629
+ ClusterConformance conformance = { .featureMap = to_underlying (Feature::kLevel ),
630
+ .supportsDefaultOpenLevel = true ,
631
+ .supportsValveFault = false ,
632
+ .supportsLevelStep = true };
633
+ ClusterState state = ClusterState ();
634
+ state.levelStep = 33 ;
635
+ EXPECT_EQ (logic.Init (conformance, state), CHIP_NO_ERROR);
636
+ // Set the last value to something non-null first so we can ensure the delegate was called correctly.
637
+ delegate.lastRequestedLevel .SetNonNull (0 );
638
+
639
+ // 33, 66, 99 and 100 should all work, nothing else should
640
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::make_optional (33u )), CHIP_NO_ERROR);
641
+ EXPECT_EQ (delegate.lastRequestedLevel .ValueOr (0 ), 33u );
642
+
643
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::make_optional (66u )), CHIP_NO_ERROR);
644
+ EXPECT_EQ (delegate.lastRequestedLevel .ValueOr (0 ), 66u );
645
+
646
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::make_optional (99u )), CHIP_NO_ERROR);
647
+ EXPECT_EQ (delegate.lastRequestedLevel .ValueOr (0 ), 99u );
648
+
649
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::make_optional (100u )), CHIP_NO_ERROR);
650
+ EXPECT_EQ (delegate.lastRequestedLevel .ValueOr (0 ), 100u );
651
+
652
+ EXPECT_EQ (logic.HandleOpenCommand (std::nullopt, std::make_optional (32u )), CHIP_ERROR_INVALID_ARGUMENT);
653
+ // Ensure this wasn't called again.
654
+ EXPECT_EQ (delegate.lastRequestedLevel .ValueOr (0 ), 100u );
655
+ }
656
+
657
+ // TODO: Add tests (and support) for checking default open level against the level step
658
+
507
659
} // namespace ValveConfigurationAndControl
508
660
} // namespace Clusters
509
661
} // namespace app
0 commit comments