|
| 1 | +/* Coverage for the NSFormCell title API: the -init defaults, the title, |
| 2 | + title alignment, title font and title writing-direction accessors, the |
| 3 | + manual and automatic title-width behaviour of -titleWidth and |
| 4 | + -titleWidth:, the mnemonic title, the attributed title and the string / |
| 5 | + attributed placeholder pair. The title cell touches the font backend, |
| 6 | + so the set is skipped when the backend is unavailable. |
| 7 | +*/ |
| 8 | +#include "Testing.h" |
| 9 | + |
| 10 | +#include <Foundation/NSAttributedString.h> |
| 11 | +#include <Foundation/NSAutoreleasePool.h> |
| 12 | +#include <Foundation/NSGeometry.h> |
| 13 | + |
| 14 | +#include <AppKit/NSApplication.h> |
| 15 | +#include <AppKit/NSFont.h> |
| 16 | +#include <AppKit/NSFormCell.h> |
| 17 | + |
| 18 | +int main() |
| 19 | +{ |
| 20 | + CREATE_AUTORELEASE_POOL(arp); |
| 21 | + NSFormCell *cell; |
| 22 | + |
| 23 | + START_SET("NSFormCell title") |
| 24 | + |
| 25 | + NS_DURING |
| 26 | + { |
| 27 | + [NSApplication sharedApplication]; |
| 28 | + } |
| 29 | + NS_HANDLER |
| 30 | + { |
| 31 | + if ([[localException name] isEqualToString: NSInternalInconsistencyException]) |
| 32 | + SKIP("It looks like GNUstep backend is not yet installed") |
| 33 | + } |
| 34 | + NS_ENDHANDLER |
| 35 | + |
| 36 | + /* Defaults from -init. */ |
| 37 | + cell = AUTORELEASE([[NSFormCell alloc] init]); |
| 38 | + pass([[cell title] isEqualToString: @"Field:"], "default title is Field:"); |
| 39 | + pass([cell titleAlignment] == NSRightTextAlignment, "title is right aligned by default"); |
| 40 | + pass([cell alignment] == NSLeftTextAlignment, "the entry itself is left aligned by default"); |
| 41 | + pass([cell isEditable], "a form cell is editable by default"); |
| 42 | + pass([cell isBezeled], "a form cell is bezeled by default"); |
| 43 | + pass([cell titleBaseWritingDirection] == NSWritingDirectionNatural, |
| 44 | + "title writing direction is natural by default"); |
| 45 | + |
| 46 | + /* Title, alignment, font and writing-direction accessors. */ |
| 47 | + [cell setTitle: @"Name:"]; |
| 48 | + pass([[cell title] isEqualToString: @"Name:"], "setTitle: updates the title"); |
| 49 | + pass([[[cell attributedTitle] string] isEqualToString: @"Name:"], |
| 50 | + "attributedTitle tracks the title string"); |
| 51 | + [cell setTitleAlignment: NSLeftTextAlignment]; |
| 52 | + pass([cell titleAlignment] == NSLeftTextAlignment, "setTitleAlignment: updates the alignment"); |
| 53 | + [cell setTitleFont: [NSFont systemFontOfSize: 24.0]]; |
| 54 | + pass([[cell titleFont] pointSize] == 24.0, "setTitleFont: updates the title font"); |
| 55 | + [cell setTitleBaseWritingDirection: NSWritingDirectionRightToLeft]; |
| 56 | + pass([cell titleBaseWritingDirection] == NSWritingDirectionRightToLeft, |
| 57 | + "setTitleBaseWritingDirection: updates the writing direction"); |
| 58 | + |
| 59 | + /* A manually set title width is reported verbatim and ignores the |
| 60 | + available size passed to -titleWidth:. */ |
| 61 | + [cell setTitleWidth: 123.0]; |
| 62 | + pass([cell titleWidth] == 123.0, "a manual title width is reported verbatim"); |
| 63 | + pass([cell titleWidth: NSMakeSize(500.0, 20.0)] == 123.0, |
| 64 | + "a manual title width ignores the offered size"); |
| 65 | + pass([cell titleWidth: NSMakeSize(10.0, 20.0)] == 123.0, |
| 66 | + "a manual title width ignores a small offered size too"); |
| 67 | + |
| 68 | + /* A negative width restores automatic sizing: -titleWidth is then the |
| 69 | + natural width, and -titleWidth: clamps to the offered size. */ |
| 70 | + [cell setTitleWidth: -1.0]; |
| 71 | + { |
| 72 | + CGFloat natural = [cell titleWidth]; |
| 73 | + pass(natural > 0.0, "the automatic title width is positive"); |
| 74 | + pass([cell titleWidth: NSMakeSize(1.0, 20.0)] == 1.0, |
| 75 | + "an offered width below the natural width is used as is"); |
| 76 | + pass([cell titleWidth: NSMakeSize(natural + 1000.0, 20.0)] == natural, |
| 77 | + "an offered width above the natural width falls back to the natural width"); |
| 78 | + } |
| 79 | + |
| 80 | + /* The mnemonic title strips the ampersand. */ |
| 81 | + cell = AUTORELEASE([[NSFormCell alloc] init]); |
| 82 | + [cell setTitleWithMnemonic: @"&File"]; |
| 83 | + pass([[cell title] isEqualToString: @"File"], "setTitleWithMnemonic: drops the ampersand"); |
| 84 | + |
| 85 | + /* The placeholder is either a plain string or an attributed string; the |
| 86 | + accessor for the other kind returns nil. */ |
| 87 | + cell = AUTORELEASE([[NSFormCell alloc] init]); |
| 88 | + [cell setPlaceholderString: @"type here"]; |
| 89 | + pass([[cell placeholderString] isEqualToString: @"type here"], |
| 90 | + "placeholderString returns the plain placeholder"); |
| 91 | + pass([cell placeholderAttributedString] == nil, |
| 92 | + "placeholderAttributedString is nil for a plain placeholder"); |
| 93 | + { |
| 94 | + NSAttributedString *attr = AUTORELEASE([[NSAttributedString alloc] initWithString: @"attr ph"]); |
| 95 | + [cell setPlaceholderAttributedString: attr]; |
| 96 | + pass([[[cell placeholderAttributedString] string] isEqualToString: @"attr ph"], |
| 97 | + "placeholderAttributedString returns the attributed placeholder"); |
| 98 | + pass([cell placeholderString] == nil, |
| 99 | + "placeholderString is nil for an attributed placeholder"); |
| 100 | + RELEASE(attr); |
| 101 | + } |
| 102 | + |
| 103 | + END_SET("NSFormCell title") |
| 104 | + |
| 105 | + DESTROY(arp); |
| 106 | + return 0; |
| 107 | +} |
0 commit comments