@@ -737,6 +737,62 @@ void main() {
737737 }
738738 });
739739
740+ testWidgets ('setContexts and removedContexts sync to native' , (tester) async {
741+ await restoreFlutterOnErrorAfter (() async {
742+ await setupSentryAndApp (tester);
743+ });
744+
745+ await Sentry .configureScope ((scope) async {
746+ scope.setContexts ('key1' , 'randomValue' );
747+ scope.setContexts ('key2' ,
748+ {'String' : 'Value' , 'Bool' : true , 'Int' : 123 , 'Double' : 12.3 });
749+ scope.setContexts ('key3' , true );
750+ scope.setContexts ('key4' , 12 );
751+ scope.setContexts ('key5' , 12.3 );
752+ });
753+
754+ var contexts = await SentryFlutter .native ? .loadContexts ();
755+ final values = contexts! ['contexts' ];
756+ expect (values, isNotNull, reason: 'Contexts are null' );
757+
758+ if (Platform .isIOS) {
759+ expect (values['key1' ], {'value' : 'randomValue' }, reason: 'key1 mismatch' );
760+ expect (values['key2' ],
761+ {'String' : 'Value' , 'Bool' : 1 , 'Int' : 123 , 'Double' : 12.3 },
762+ reason: 'key2 mismatch' );
763+ // bool values are mapped to num values of 1 or 0 during objc conversion
764+ expect (values['key3' ], {'value' : 1 }, reason: 'key3 mismatch' );
765+ expect (values['key4' ], {'value' : 12 }, reason: 'key4 mismatch' );
766+ expect (values['key5' ], {'value' : 12.3 }, reason: 'key5 mismatch' );
767+ } else if (Platform .isAndroid) {
768+ expect (values['key1' ], 'randomValue' , reason: 'key1 mismatch' );
769+ expect (values['key2' ],
770+ {'String' : 'Value' , 'Bool' : true , 'Int' : 123 , 'Double' : 12.3 },
771+ reason: 'key2 mismatch' );
772+ expect (values['key3' ], true , reason: 'key3 mismatch' );
773+ expect (values['key4' ], 12 , reason: 'key4 mismatch' );
774+ expect (values['key5' ], 12.3 , reason: 'key5 mismatch' );
775+ }
776+
777+ await Sentry .configureScope ((scope) async {
778+ scope.removeContexts ('key1' );
779+ scope.removeContexts ('key2' );
780+ scope.removeContexts ('key3' );
781+ scope.removeContexts ('key4' );
782+ scope.removeContexts ('key5' );
783+ });
784+
785+ contexts = await SentryFlutter .native ? .loadContexts ();
786+ final removedValues = contexts! ['contexts' ];
787+ expect (removedValues, isNotNull, reason: 'Contexts are null' );
788+
789+ expect (removedValues['key1' ], isNull, reason: 'key1 should be removed' );
790+ expect (removedValues['key2' ], isNull, reason: 'key2 should be removed' );
791+ expect (removedValues['key3' ], isNull, reason: 'key3 should be removed' );
792+ expect (removedValues['key4' ], isNull, reason: 'key4 should be removed' );
793+ expect (removedValues['key5' ], isNull, reason: 'key5 should be removed' );
794+ });
795+
740796 group ('e2e' , () {
741797 var output = find.byKey (const Key ('output' ));
742798 late Fixture fixture;
0 commit comments