|
| 1 | +/* Coverage for the NSPopUpButtonCell menu model: the defaults, adding / |
| 2 | + inserting / removing titled items, the automatic selection of the first |
| 3 | + item, the way a duplicate title moves the existing item, the item and |
| 4 | + index queries, selection by index and title, and the pull-down / |
| 5 | + autoenable / edge / arrow-position accessors. The cell builds a menu in |
| 6 | + -init, so the set is skipped when the backend is unavailable. |
| 7 | +*/ |
| 8 | +#include "Testing.h" |
| 9 | + |
| 10 | +#include <Foundation/NSArray.h> |
| 11 | +#include <Foundation/NSAutoreleasePool.h> |
| 12 | + |
| 13 | +#include <AppKit/NSApplication.h> |
| 14 | +#include <AppKit/NSMenuItem.h> |
| 15 | +#include <AppKit/NSPopUpButtonCell.h> |
| 16 | + |
| 17 | +static NSPopUpButtonCell * |
| 18 | +popup(void) |
| 19 | +{ |
| 20 | + return AUTORELEASE([[NSPopUpButtonCell alloc] initTextCell: @"" pullsDown: NO]); |
| 21 | +} |
| 22 | + |
| 23 | +int |
| 24 | +main(int argc, char **argv) |
| 25 | +{ |
| 26 | + CREATE_AUTORELEASE_POOL(arp); |
| 27 | + NSPopUpButtonCell *cell; |
| 28 | + |
| 29 | + START_SET("NSPopUpButtonCell menu") |
| 30 | + |
| 31 | + NS_DURING |
| 32 | + { |
| 33 | + [NSApplication sharedApplication]; |
| 34 | + } |
| 35 | + NS_HANDLER |
| 36 | + { |
| 37 | + if ([[localException name] isEqualToString: NSInternalInconsistencyException]) |
| 38 | + SKIP("It looks like GNUstep backend is not yet installed") |
| 39 | + } |
| 40 | + NS_ENDHANDLER |
| 41 | + |
| 42 | + /* Defaults. */ |
| 43 | + cell = popup(); |
| 44 | + pass([cell pullsDown] == NO, "a popup cell does not pull down by default"); |
| 45 | + pass([cell autoenablesItems] == YES, "items are auto-enabled by default"); |
| 46 | + pass([cell usesItemFromMenu] == YES, "it uses the item from the menu by default"); |
| 47 | + pass([cell altersStateOfSelectedItem] == YES, "it alters the selected item state by default"); |
| 48 | + pass([cell numberOfItems] == 0, "a new popup cell has no items"); |
| 49 | + pass([cell indexOfSelectedItem] == -1, "a new popup cell has no selection"); |
| 50 | + |
| 51 | + /* Adding the first item selects it automatically. */ |
| 52 | + cell = popup(); |
| 53 | + [cell addItemWithTitle: @"alpha"]; |
| 54 | + pass([cell numberOfItems] == 1 && [cell indexOfSelectedItem] == 0 |
| 55 | + && [[cell titleOfSelectedItem] isEqualToString: @"alpha"], |
| 56 | + "adding the first item selects it"); |
| 57 | + [cell addItemsWithTitles: [NSArray arrayWithObjects: @"beta", @"gamma", nil]]; |
| 58 | + pass([cell numberOfItems] == 3 && [cell indexOfSelectedItem] == 0, |
| 59 | + "adding more items leaves the first one selected"); |
| 60 | + pass([[cell itemTitles] isEqualToArray: |
| 61 | + ([NSArray arrayWithObjects: @"alpha", @"beta", @"gamma", nil])], |
| 62 | + "itemTitles returns the titles in order"); |
| 63 | + |
| 64 | + /* A duplicate title moves the existing item rather than adding another. */ |
| 65 | + [cell addItemWithTitle: @"beta"]; |
| 66 | + pass([cell numberOfItems] == 3 && [cell indexOfItemWithTitle: @"beta"] == 2, |
| 67 | + "re-adding a title moves the item to the end without duplicating it"); |
| 68 | + |
| 69 | + /* Inserting at a position. */ |
| 70 | + cell = popup(); |
| 71 | + [cell addItemsWithTitles: [NSArray arrayWithObjects: @"a", @"b", @"c", nil]]; |
| 72 | + [cell insertItemWithTitle: @"inserted" atIndex: 1]; |
| 73 | + pass([[cell itemTitleAtIndex: 1] isEqualToString: @"inserted"] |
| 74 | + && [cell numberOfItems] == 4, |
| 75 | + "insertItemWithTitle:atIndex: inserts at the index"); |
| 76 | + |
| 77 | + /* Item and index queries. */ |
| 78 | + cell = popup(); |
| 79 | + [cell addItemsWithTitles: [NSArray arrayWithObjects: @"one", @"two", @"three", nil]]; |
| 80 | + pass([cell indexOfItemWithTitle: @"two"] == 1, "indexOfItemWithTitle: finds the item"); |
| 81 | + pass([cell indexOfItemWithTitle: @"missing"] == -1, |
| 82 | + "indexOfItemWithTitle: returns -1 for an absent title"); |
| 83 | + pass([cell itemWithTitle: @"three"] != nil, "itemWithTitle: returns the item"); |
| 84 | + pass([[[cell lastItem] title] isEqualToString: @"three"], "lastItem returns the last item"); |
| 85 | + pass([[cell itemAtIndex: 0] title] != nil, "itemAtIndex: returns the item at the index"); |
| 86 | + |
| 87 | + /* Tag lookup. */ |
| 88 | + [[cell itemAtIndex: 2] setTag: 42]; |
| 89 | + pass([cell indexOfItemWithTag: 42] == 2, "indexOfItemWithTag: finds the tagged item"); |
| 90 | + pass([cell indexOfItemWithTag: 99] == -1, "indexOfItemWithTag: returns -1 for an absent tag"); |
| 91 | + |
| 92 | + /* Selection by index and title. */ |
| 93 | + [cell selectItemAtIndex: 2]; |
| 94 | + pass([cell indexOfSelectedItem] == 2 |
| 95 | + && [[cell titleOfSelectedItem] isEqualToString: @"three"], |
| 96 | + "selectItemAtIndex: selects the item at the index"); |
| 97 | + [cell selectItemWithTitle: @"one"]; |
| 98 | + pass([cell indexOfSelectedItem] == 0, "selectItemWithTitle: selects the matching item"); |
| 99 | + |
| 100 | + /* Removing by title, and clearing the whole list. */ |
| 101 | + [cell removeItemWithTitle: @"two"]; |
| 102 | + pass([cell numberOfItems] == 2 && [cell indexOfItemWithTitle: @"two"] == -1, |
| 103 | + "removeItemWithTitle: removes the item"); |
| 104 | + [cell removeAllItems]; |
| 105 | + pass([cell numberOfItems] == 0 && [cell indexOfSelectedItem] == -1, |
| 106 | + "removeAllItems empties the list and clears the selection"); |
| 107 | + |
| 108 | + /* Property accessors round-trip. */ |
| 109 | + cell = popup(); |
| 110 | + [cell setPullsDown: YES]; |
| 111 | + pass([cell pullsDown] == YES, "setPullsDown: round trips"); |
| 112 | + [cell setAutoenablesItems: NO]; |
| 113 | + pass([cell autoenablesItems] == NO, "setAutoenablesItems: round trips"); |
| 114 | + [cell setPreferredEdge: NSMinXEdge]; |
| 115 | + pass([cell preferredEdge] == NSMinXEdge, "setPreferredEdge: round trips"); |
| 116 | + [cell setArrowPosition: NSPopUpArrowAtBottom]; |
| 117 | + pass([cell arrowPosition] == NSPopUpArrowAtBottom, "setArrowPosition: round trips"); |
| 118 | + |
| 119 | + END_SET("NSPopUpButtonCell menu") |
| 120 | + |
| 121 | + DESTROY(arp); |
| 122 | + return 0; |
| 123 | +} |
0 commit comments