From 2ab5953ea3785f8f8786cc35fb0cc76e8089018d Mon Sep 17 00:00:00 2001 From: Frederik Carlier Date: Thu, 25 Apr 2024 17:31:47 +0200 Subject: [PATCH] Add NSButtonCell, NSMenuItem keyEquivalentModifierMask encoding tests (#264) --- Tests/gui/NSButtonCell/TestInfo | 0 Tests/gui/NSButtonCell/encoding.m | 66 +++++++++++++++++++++++++++++++ Tests/gui/NSMenuItem/TestInfo | 0 Tests/gui/NSMenuItem/encoding.m | 49 +++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 Tests/gui/NSButtonCell/TestInfo create mode 100644 Tests/gui/NSButtonCell/encoding.m create mode 100644 Tests/gui/NSMenuItem/TestInfo create mode 100644 Tests/gui/NSMenuItem/encoding.m diff --git a/Tests/gui/NSButtonCell/TestInfo b/Tests/gui/NSButtonCell/TestInfo new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Tests/gui/NSButtonCell/encoding.m b/Tests/gui/NSButtonCell/encoding.m new file mode 100644 index 0000000000..a8ed7571b9 --- /dev/null +++ b/Tests/gui/NSButtonCell/encoding.m @@ -0,0 +1,66 @@ +#import "ObjectTesting.h" + +#import +#import +#import +#import +#import + +int main() +{ + START_SET("NSButtonCell encoding tests") + + NS_DURING + { + [NSApplication sharedApplication]; + } + NS_HANDLER + { + if ([[localException name] isEqualToString: NSInternalInconsistencyException ]) + SKIP("It looks like GNUstep backend is not yet installed") + } + NS_ENDHANDLER + + NSString* mask = @"NSButtonFlags2"; + NSButtonCell* item = [[NSButtonCell alloc] init]; + item.keyEquivalent = @"A"; + item.keyEquivalentModifierMask = NSShiftKeyMask; + + NSMutableData *data = [NSMutableData data]; + NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; + + [archiver encodeRootObject:item]; + [archiver finishEncoding]; + + NSError* error; + NSDictionary* archive = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:nil error:&error]; + + NSArray* topLevelObjects = [archive objectForKey:@"$objects"]; + + NSDictionary* dict; + + for (id element in topLevelObjects) + { + if ([element isKindOfClass:[NSDictionary class]]) + { + dict = (NSDictionary*)element; + + if ([[dict allKeys] containsObject:mask]) + { + break; + } + else + { + dict = nil; + } + } + } + + PASS(dict != nil, "Found a dict with a NSButtonFlags2 entry"); + + NSNumber* encodedKeyMask = [dict valueForKey:mask]; + PASS(encodedKeyMask != nil, "Retrieved the NSButtonFlags2 value"); + PASS([encodedKeyMask intValue] & NSEventModifierFlagDeviceIndependentFlagsMask << 8 == NSShiftKeyMask, "Encoded key mask 0x%x matches expected key mask 0x%x", [encodedKeyMask intValue] & NSEventModifierFlagDeviceIndependentFlagsMask << 8, NSShiftKeyMask << 8); + + END_SET("NSButtonCell encoding tests") +} \ No newline at end of file diff --git a/Tests/gui/NSMenuItem/TestInfo b/Tests/gui/NSMenuItem/TestInfo new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Tests/gui/NSMenuItem/encoding.m b/Tests/gui/NSMenuItem/encoding.m new file mode 100644 index 0000000000..40b2753626 --- /dev/null +++ b/Tests/gui/NSMenuItem/encoding.m @@ -0,0 +1,49 @@ +#import "ObjectTesting.h" + +#import +#import +#import +#import + +int main() +{ + NSString* mask = @"NSKeyEquivModMask"; + NSMenuItem* item = [[NSMenuItem alloc] init]; + item.keyEquivalentModifierMask = NSShiftKeyMask; + + NSMutableData *data = [NSMutableData data]; + NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; + + [archiver encodeRootObject:item]; + [archiver finishEncoding]; + + NSError* error; + NSDictionary* archive = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:nil error:&error]; + + NSArray* topLevelObjects = [archive objectForKey:@"$objects"]; + + NSDictionary* dict; + + for (id element in topLevelObjects) + { + if ([element isKindOfClass:[NSDictionary class]]) + { + dict = (NSDictionary*)element; + + if ([[dict allKeys] containsObject:mask]) + { + break; + } + else + { + dict = nil; + } + } + } + + PASS(dict != nil, "Found a dict with a NSKeyEquivModMask entry"); + + NSNumber* encodedKeyMask = [dict valueForKey:mask]; + PASS(encodedKeyMask != nil, "Retrieved the NSKeyEquivModMask value"); + PASS([encodedKeyMask intValue] == NSShiftKeyMask, "Encoded key mask 0x%x matches expected key mask 0x%x", [encodedKeyMask intValue], NSShiftKeyMask); +} \ No newline at end of file