element_test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. // Use of this source code is governed by the Apache License, Version 2.0.
  3. goog.provide('goog.graphics.ext.ElementTest');
  4. goog.setTestOnly('goog.graphics.ext.ElementTest');
  5. goog.require('goog.graphics');
  6. goog.require('goog.graphics.ext');
  7. goog.require('goog.testing.StrictMock');
  8. goog.require('goog.testing.jsunit');
  9. var el, graphics, mockWrapper;
  10. function setUp() {
  11. var div = document.getElementById('root');
  12. graphics = new goog.graphics.ext.Graphics(100, 100, 200, 200);
  13. div.innerHTML = '';
  14. graphics.render(div);
  15. mockWrapper = new goog.testing.StrictMock(goog.graphics.Element);
  16. }
  17. function tearDown() {
  18. mockWrapper.$verify();
  19. }
  20. function assertPosition(fn, left, top, opt_width, opt_height) {
  21. mockWrapper.setTransformation(0, 0, 0, 5, 5);
  22. mockWrapper.setTransformation(
  23. left, top, 0, (opt_width || 10) / 2, (opt_height || 10) / 2);
  24. mockWrapper.$replay();
  25. el = new goog.graphics.ext.Element(graphics, mockWrapper);
  26. el.setSize(10, 10);
  27. fn();
  28. }
  29. function testLeft() {
  30. assertPosition(function() {
  31. el.setLeft(10);
  32. }, 10, 0);
  33. assertFalse(el.isParentDependent());
  34. }
  35. function testLeftPercent() {
  36. assertPosition(function() {
  37. el.setLeft('10%');
  38. }, 20, 0);
  39. }
  40. function testCenter() {
  41. assertPosition(function() {
  42. el.setCenter(0);
  43. }, 95, 0);
  44. assertTrue(el.isParentDependent());
  45. }
  46. function testCenterPercent() {
  47. assertPosition(function() {
  48. el.setCenter('10%');
  49. }, 115, 0);
  50. }
  51. function testRight() {
  52. assertPosition(function() {
  53. el.setRight(10);
  54. }, 180, 0);
  55. assertTrue(el.isParentDependent());
  56. }
  57. function testRightPercent() {
  58. assertPosition(function() {
  59. el.setRight('10%');
  60. }, 170, 0);
  61. assertTrue(el.isParentDependent());
  62. }
  63. function testTop() {
  64. assertPosition(function() {
  65. el.setTop(10);
  66. }, 0, 10);
  67. assertFalse(el.isParentDependent());
  68. }
  69. function testTopPercent() {
  70. assertPosition(function() {
  71. el.setTop('10%');
  72. }, 0, 20);
  73. }
  74. function testMiddle() {
  75. assertPosition(function() {
  76. el.setMiddle(0);
  77. }, 0, 95);
  78. assertTrue(el.isParentDependent());
  79. }
  80. function testMiddlePercent() {
  81. assertPosition(function() {
  82. el.setMiddle('10%');
  83. }, 0, 115);
  84. }
  85. function testBottom() {
  86. assertPosition(function() {
  87. el.setBottom(10);
  88. }, 0, 180);
  89. assertTrue(el.isParentDependent());
  90. }
  91. function testBottomPercent() {
  92. assertPosition(function() {
  93. el.setBottom('10%');
  94. }, 0, 170);
  95. assertTrue(el.isParentDependent());
  96. }
  97. function testSize() {
  98. assertPosition(function() {
  99. el.setSize(100, 100);
  100. }, 0, 0, 100, 100);
  101. assertFalse(el.isParentDependent());
  102. }
  103. function testSizePercent() {
  104. assertPosition(function() {
  105. el.setSize('10%', '20%');
  106. }, 0, 0, 20, 40);
  107. assertTrue(el.isParentDependent());
  108. }