/
CPPopUpButtonTest.j
205 lines (137 loc) · 6.56 KB
/
CPPopUpButtonTest.j
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
@import <AppKit/CPPopUpButton.j>
@import <AppKit/CPApplication.j>
@implementation CPPopUpButtonTest : OJTestCase
{
CPPopUpButton button @accessors;
}
- (void)setUp
{
button = [CPPopUpButton new];
}
- (void)testMenuSynchronization
{
var popUpButton = [[CPPopUpButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0) pullsDown:NO];
[self assert:CPNotFound equals:[popUpButton indexOfSelectedItem]];
[popUpButton addItemWithTitle:@"one"];
[self assert:0 equals:[popUpButton indexOfSelectedItem]];
[popUpButton addItemWithTitle:@"two"];
[popUpButton addItemWithTitle:@"three"];
[popUpButton addItemWithTitle:@"four"];
[popUpButton addItemWithTitle:@"five"];
[popUpButton addItemWithTitle:@"six"];
[self assert:0 equals:[popUpButton indexOfSelectedItem]];
[popUpButton insertItemWithTitle:@"negative one" atIndex:0];
[self assert:1 equals:[popUpButton indexOfSelectedItem]];
var items = [
[[CPMenuItem alloc] initWithTitle:@"negative five" action:nil keyEquivalent:@""],
[[CPMenuItem alloc] initWithTitle:@"negative four" action:nil keyEquivalent:@""],
[[CPMenuItem alloc] initWithTitle:@"negative three" action:nil keyEquivalent:@""],
[[CPMenuItem alloc] initWithTitle:@"negative two" action:nil keyEquivalent:@""]
],
indexes = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 4)],
mutableItemsArray = [[popUpButton menu] mutableArrayValueForKey:@"items"];
[mutableItemsArray insertObjects:items atIndexes:indexes];
[self assert:5 equals:[popUpButton indexOfSelectedItem]];
[[popUpButton menu] removeItemAtIndex:5];
[self assert:4 equals:[popUpButton indexOfSelectedItem]];
[[popUpButton menu] removeItemAtIndex:0];
[self assert:3 equals:[popUpButton indexOfSelectedItem]];
[popUpButton selectItemAtIndex:1];
indexes = [CPIndexSet indexSetWithIndexesInRange:CPMakeRange(1, 3)];
[mutableItemsArray removeObjectsAtIndexes:indexes];
[self assert:0 equals:[popUpButton indexOfSelectedItem]];
var pullDownButton = [[CPPopUpButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0) pullsDown:YES];
[pullDownButton addItemWithTitle:@"First Item"];
[self assert:YES equals:[[pullDownButton itemAtIndex:0] isHidden]];
[pullDownButton addItemWithTitle:@"Second Item"];
[self assert:YES equals:[[pullDownButton itemAtIndex:0] isHidden]];
[self assert:NO equals:[[pullDownButton itemAtIndex:1] isHidden]];
[pullDownButton removeItemAtIndex:0];
[self assert:YES equals:[[pullDownButton itemAtIndex:0] isHidden]];
[pullDownButton insertItemWithTitle:@"A Title" atIndex:0];
[self assert:YES equals:[[pullDownButton itemAtIndex:0] isHidden]];
[self assert:NO equals:[[pullDownButton itemAtIndex:1] isHidden]];
}
- (void)testMenuItemSynchronization
{
var popUpButton = [[CPPopUpButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0) pullsDown:NO];
[popUpButton addItemWithTitle:@"zero"];
[popUpButton addItemWithTitle:@"one"];
[popUpButton addItemWithTitle:@"two"];
[popUpButton addItemWithTitle:@"three"];
[popUpButton addItemWithTitle:@"four"];
[popUpButton addItemWithTitle:@"five"];
[popUpButton addItemWithTitle:@"six"];
[self assert:@"zero" same:[popUpButton title]];
[[popUpButton itemAtIndex:0] setTitle:@"new title"];
[self assert:@"new title" same:[popUpButton title]];
[popUpButton selectItemAtIndex:3];
[self assert:@"three" same:[popUpButton title]];
[[popUpButton itemAtIndex:3] setTitle:@"something else"];
[self assert:@"something else" same:[popUpButton title]];
[popUpButton selectItemAtIndex:6];
[self assert:@"six" same:[popUpButton title]];
[[popUpButton itemAtIndex:6] setTitle:@"another title"];
[self assert:@"another title" same:[popUpButton title]];
var popUpButton = [[CPPopUpButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0) pullsDown:YES];
[popUpButton addItemWithTitle:@"zero"];
[popUpButton addItemWithTitle:@"one"];
[popUpButton addItemWithTitle:@"two"];
[popUpButton addItemWithTitle:@"three"];
[popUpButton addItemWithTitle:@"four"];
[popUpButton addItemWithTitle:@"five"];
[popUpButton addItemWithTitle:@"six"];
[self assert:@"zero" same:[popUpButton title]];
[[popUpButton itemAtIndex:0] setTitle:@"new title"];
[self assert:@"new title" same:[popUpButton title]];
[popUpButton selectItemAtIndex:3];
[self assert:@"new title" same:[popUpButton title]];
[[popUpButton itemAtIndex:3] setTitle:@"something else"];
[self assert:@"new title" same:[popUpButton title]];
[popUpButton selectItemAtIndex:6];
[self assert:@"new title" same:[popUpButton title]];
[[popUpButton itemAtIndex:6] setTitle:@"another title"];
[self assert:@"new title" same:[popUpButton title]];
}
- (void)testItemTitles
{
[self assert:[] equals:[button itemTitles]];
[button addItem:[[CPMenuItem alloc] initWithTitle:"Option A" action:nil keyEquivalent:nil]];
[button addItem:[[CPMenuItem alloc] initWithTitle:"Option B" action:nil keyEquivalent:nil]];
[self assert:["Option A", "Option B"] equals:[button itemTitles]];
}
- (void)testBindingSelectedTag
{
var dict = [CPDictionary dictionary],
item = nil;
item = [[CPMenuItem alloc] initWithTitle:@"Mouse" action:nil keyEquivalent:nil];
[item setTag:20];
[button addItem:item];
item = [[CPMenuItem alloc] initWithTitle:@"Elephant" action:nil keyEquivalent:nil];
[item setTag:4990000];
[button addItem:item];
[dict setObject:20 forKey:@"weight"];
[button bind:@"selectedTag"
toObject:dict
withKeyPath:@"weight"
options:nil];
[self assert:20 equals:[button selectedTag]];
[button selectItemAtIndex:1];
// simulate selection by the user (this fails because we don't reverse set the value)
// see the discussion for pull request 1018 for more details about the problem.
// https://github.com/280north/cappuccino/pull/1018
// [button sendAction:[button action] to:[button target]];
//
// [self assert:4990000 equals:[dict objectForKey:@"weight"]];
}
- (void)testItemWithTitle
{
[self assert:nil equals:[[self button] itemWithTitle:@"I dont exist"]];
[[self button] addItemWithTitle:@"one"];
[[self button] addItemWithTitle:@"two"];
[[self button] addItemWithTitle:@"three"];
[self assert:0 equals:[[self button] indexOfItemWithTitle:@"one"]];
[self assert:1 equals:[[self button] indexOfItemWithTitle:@"two"]];
[self assert:2 equals:[[self button] indexOfItemWithTitle:@"three"]];
}
@end