blob: ab8383e90803a84062bc2343d347dcb0dfa4e810 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
static NSString *const SampleDefaultsKey = @"sampleShortcut";
@interface MASShortcutBinderTests : XCTestCase
@property(strong) MASShortcutBinder *binder;
@property(strong) MASShortcutMonitor *monitor;
@property(strong) NSUserDefaults *defaults;
@end
@implementation MASShortcutBinderTests
- (void) setUp
{
[super setUp];
[self setBinder:[[MASShortcutBinder alloc] init]];
[self setMonitor:[_binder shortcutMonitor]];
[self setDefaults:[[NSUserDefaults alloc] init]];
[_defaults removeObjectForKey:SampleDefaultsKey];
}
- (void) tearDown
{
[_monitor unregisterAllShortcuts];
[self setMonitor:nil];
[self setDefaults:nil];
[self setBinder:nil];
[super tearDown];
}
- (void) testInitialValueReading
{
MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:1 modifierFlags:1];
[_defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:shortcut] forKey:SampleDefaultsKey];
[_binder bindShortcutWithDefaultsKey:SampleDefaultsKey toAction:^{}];
XCTAssertTrue([_monitor isShortcutRegistered:shortcut],
@"Pass the initial shortcut from defaults to shortcut monitor.");
}
- (void) testValueChangeReading
{
MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:1 modifierFlags:1];
[_binder bindShortcutWithDefaultsKey:SampleDefaultsKey toAction:^{}];
[_defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:shortcut] forKey:SampleDefaultsKey];
XCTAssertTrue([_monitor isShortcutRegistered:shortcut],
@"Pass the shortcut from defaults to shortcut monitor after defaults change.");
}
- (void) testValueClearing
{
MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:1 modifierFlags:1];
[_binder bindShortcutWithDefaultsKey:SampleDefaultsKey toAction:^{}];
[_defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:shortcut] forKey:SampleDefaultsKey];
[_defaults removeObjectForKey:SampleDefaultsKey];
XCTAssertFalse([_monitor isShortcutRegistered:shortcut],
@"Unregister shortcut from monitor after value is cleared from defaults.");
}
- (void) testBindingRemoval
{
MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:1 modifierFlags:1];
[_binder bindShortcutWithDefaultsKey:SampleDefaultsKey toAction:^{}];
[_defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:shortcut] forKey:SampleDefaultsKey];
[_binder breakBindingWithDefaultsKey:SampleDefaultsKey];
XCTAssertFalse([_monitor isShortcutRegistered:shortcut],
@"Unregister shortcut from monitor after binding was removed.");
}
- (void) testRebinding
{
MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:1 modifierFlags:1];
[_defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:shortcut] forKey:SampleDefaultsKey];
[_binder bindShortcutWithDefaultsKey:SampleDefaultsKey toAction:^{}];
[_binder breakBindingWithDefaultsKey:SampleDefaultsKey];
[_binder bindShortcutWithDefaultsKey:SampleDefaultsKey toAction:^{}];
XCTAssertTrue([_monitor isShortcutRegistered:shortcut],
@"Bind after unbinding.");
}
- (void) testTransformerDeserialization
{
MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:5 modifierFlags:1048576];
NSDictionary *storedShortcut = @{@"keyCode": @5, @"modifierFlags": @1048576};
[_defaults setObject:storedShortcut forKey:SampleDefaultsKey];
[_binder setBindingOptions:@{NSValueTransformerBindingOption:[MASDictionaryTransformer new]}];
[_binder bindShortcutWithDefaultsKey:SampleDefaultsKey toAction:^{}];
XCTAssertTrue([_monitor isShortcutRegistered:shortcut],
@"Deserialize shortcut from user defaults using a custom transformer.");
}
- (void) testDefaultShortcuts
{
MASShortcut *shortcut = [MASShortcut shortcutWithKeyCode:5 modifierFlags:1048576];
[_binder registerDefaultShortcuts:@{SampleDefaultsKey: shortcut}];
[_binder bindShortcutWithDefaultsKey:SampleDefaultsKey toAction:^{}];
XCTAssertTrue([_monitor isShortcutRegistered:shortcut],
@"Bind shortcut using a default value.");
}
// See issue #64 <http://git.io/x5YS> for rationale and discussion.
- (void) testIllegalSymbolsInBindingNames
{
XCTAssertThrows([_binder bindShortcutWithDefaultsKey:@"foo.bar" toAction:^{}],
@"Throw for illegal binding symbols: a dot (“.”).");
XCTAssertThrows([_binder bindShortcutWithDefaultsKey:@"foo bar" toAction:^{}],
@"Throw for illegal binding symbols: a space (“ ”).");
}
@end
|