PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/Tests/AppKit/CPFontTest.j

http://github.com/cacaodev/cappuccino
Unknown | 160 lines | 124 code | 36 blank | 0 comment | 0 complexity | f0d174575a7b70211f566738b16ac92a MD5 | raw file
  1@import <AppKit/AppKit.j>
  2
  3@implementation CPFontTest : OJTestCase
  4{
  5    CPFont _systemFont;
  6    CPFont _boldSystemFont;
  7
  8    CPFont _customFont;
  9    CPFont _boldCustomFont;
 10}
 11
 12- (void)setUp
 13{
 14    _systemFont = [CPFont systemFontOfSize:15];
 15    _boldSystemFont = [CPFont boldSystemFontOfSize:15];
 16
 17    _customFont = [CPFont fontWithName:@"Marker Felt, Lucida Grande, Helvetica" size:30];
 18    _boldCustomFont = [CPFont boldFontWithName:@"Helvetica" size:30];
 19}
 20
 21- (void)testSystemFontCSSString
 22{
 23    var font = _CPFontConcatNameWithFallback([CPFont systemFontFace]);
 24
 25    [self assert:[_systemFont cssString] equals:@"15px " + font];
 26}
 27
 28- (void)testBoldSystemFontCSSString
 29{
 30    var font = _CPFontConcatNameWithFallback([CPFont systemFontFace]);
 31
 32    [self assert:[_boldSystemFont cssString] equals:@"bold 15px " + font];
 33}
 34
 35- (void)testCustomFontCSSString
 36{
 37    [self assert:[_customFont cssString] equals:@"30px \"Marker Felt\", \"Lucida Grande\", Helvetica, Arial, sans-serif"];
 38}
 39
 40- (void)testBoldCustomFontCSSString
 41{
 42    [self assert:[_boldCustomFont cssString] equals:@"bold 30px Helvetica, Arial, sans-serif"];
 43}
 44
 45- (void)testIsEqual
 46{
 47    [self assert:_customFont equals:_customFont];
 48    [self assert:_systemFont equals:_systemFont];
 49    [self assert:_systemFont notEqual:_customFont];
 50    [self assert:_customFont notEqual:"a string"];
 51    [self assert:_customFont notEqual:nil];
 52}
 53
 54- (void)testConstructorsIsBoldIsItalic
 55{
 56    [self testConstructor:@selector(fontWithName:size:) args:["Arial", 12] isBold:NO isItalic:NO];
 57    [self testConstructor:@selector(boldFontWithName:size:) args:["Arial", 13] isBold:YES isItalic:NO];
 58    [self testConstructor:@selector(fontWithName:size:italic:) args:["Arial", 14, YES] isBold:NO isItalic:YES];
 59    [self testConstructor:@selector(boldFontWithName:size:italic:) args:["Arial", 15, NO] isBold:YES isItalic:NO];
 60}
 61
 62- (void)testConstructor:(SEL)aSelector args:(CPArray)args isBold:(BOOL)bold isItalic:(BOOL)italic
 63{
 64    var inv = [CPInvocation invocationWithMethodSignature:nil];
 65    [inv setTarget:CPFont];
 66    [inv setSelector:aSelector];
 67    [args enumerateObjectsUsingBlock:function(arg, idx)
 68    {
 69        [inv setArgument:arg atIndex:idx + 2];
 70    }];
 71
 72    [inv invoke];
 73    var font = [inv returnValue];
 74
 75    [self assertTrue:([font isBold] == bold) message: [font description] + " should be bold: " + bold];
 76    [self assertTrue:([font isItalic] == italic) message: [font description] + " should be italic: " + italic];
 77
 78    // get from cache
 79    [inv invoke];
 80    var cachedFont = [inv returnValue];
 81    [self assert:cachedFont equals:font];
 82
 83    [self assertTrue:([cachedFont isBold] == bold) message:" cached " + [cachedFont description] + " should be bold: " + bold];
 84    [self assertTrue:([cachedFont isItalic] == italic) message:" cached " + [cachedFont description] + " should be italic: " + italic];
 85}
 86
 87- (void)testSizes
 88{
 89    var font = [CPFont fontWithName:@"Arial" size:0],
 90        systemSize = [CPFont systemFontSize];
 91
 92    [self assertTrue:([font size] === systemSize) message:" font size should be system size (" + systemSize + ")"];
 93
 94    var sysfont1 = [CPFont systemFontOfSize:12];
 95    [self assertTrue:([sysfont1 size] === 12) message:" font size should be 12"];
 96
 97    var sysfont2 = [CPFont systemFontOfSize:0];
 98    [self assertTrue:([sysfont2 size] === systemSize) message:" font size should be system size (" + systemSize + ")"];
 99
100    var sysfont3 = [CPFont systemFontOfSize:-1];
101    [self assertTrue:([sysfont3 size] === systemSize) message:" font size should be system size (" + systemSize + ")"];
102
103    var newSystemSize = 13;
104    [CPFont setSystemFontSize:newSystemSize];
105    [self assertTrue:([sysfont2 size] === systemSize) message:" font size should be old system size (" + systemSize + ")"];
106    [self assertTrue:([sysfont3 size] === newSystemSize) message:" font size should be new system size (" + newSystemSize + ")"];
107}
108
109- (void)testFaces
110{
111    var font = [CPFont fontWithName:@"Georgia" size:0],
112        systemFace = [CPFont systemFontFace];
113
114    [self assertTrue:([font familyName] === @"Georgia") message:" font face should be Georgia"];
115
116    var sysfont = [CPFont systemFontOfSize:12];
117    [self assertTrue:([sysfont familyName] === systemFace) message:" font face should be system face (" + systemFace + ")"];
118
119    var newSystemFace = @"Georgia";
120    [CPFont setSystemFontFace:newSystemFace];
121    [self assertTrue:([sysfont familyName] === newSystemFace) message:" font face should be new system face (" + newSystemFace + ")"];
122}
123
124@end
125
126var _CPFontStripRegExp = new RegExp("(^\\s*[\"']?|[\"']?\\s*$)", "g");
127
128var _CPFontConcatNameWithFallback = function(aName)
129{
130    var names = _CPFontNormalizedNameArray(aName),
131        fallbackFaces = ["Arial", "sans-serif"];
132
133    // Remove the fallback names used in the names passed in
134    for (var i = 0; i < names.length; ++i)
135    {
136        for (var j = 0; j < fallbackFaces.length; ++j)
137        {
138            if (names[i].toLowerCase() === fallbackFaces[j].toLowerCase())
139            {
140                fallbackFaces.splice(j, 1);
141                break;
142            }
143        }
144
145        if (names[i].indexOf(" ") > 0)
146            names[i] = '"' + names[i] + '"';
147    }
148
149    return names.concat(fallbackFaces).join(", ");
150};
151
152var _CPFontNormalizedNameArray = function(aName)
153{
154    var names = aName.split(",");
155
156    for (var i = 0; i < names.length; ++i)
157        names[i] = names[i].replace(_CPFontStripRegExp, "");
158
159    return names;
160};