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