PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test/com/google/maps/extras/gradients/test/GradientControlTest.as

http://gmaps-utility-library-flash.googlecode.com/
ActionScript | 304 lines | 228 code | 50 blank | 26 comment | 1 complexity | cc2e300f775761aeebc6801222e79b79 MD5 | raw file
  1. /**
  2. * Copyright 2009 Google Inc.
  3. * Licensed under the Apache License, Version 2.0:
  4. * http://www.apache.org/licenses/LICENSE-2.0
  5. */
  6. package com.google.maps.extras.gradients.test {
  7. import flash.errors.IllegalOperationError;
  8. import flexunit.framework.TestCase;
  9. import com.google.maps.Color;
  10. import com.google.maps.extras.gradients.GradientControl;
  11. import com.google.maps.extras.gradients.GradientControl;
  12. import com.google.maps.extras.gradients.GradientRule;
  13. import com.google.maps.extras.gradients.GradientRuleList;
  14. import com.google.maps.extras.gradients.GradientUtil;
  15. import com.google.maps.extras.gradients.ValueColorGradient;
  16. /**
  17. * Test collection for GradientControl and related classes
  18. *
  19. * @author Simon Ilyushchenko
  20. */
  21. public class GradientControlTest extends flexunit.framework.TestCase {
  22. public function testLinearScale(): void {
  23. var src:Object = { 'min': 0, 'max': 10 };
  24. var dst:Object = { 'min': 100, 'max': 200 };
  25. assertEquals(140, GradientUtil.linearScale(src, dst, 4));
  26. assertEquals(145, GradientUtil.linearScale(src, dst, 4.5));
  27. assertEquals(150, GradientUtil.linearScale(src, dst, 4, 2));
  28. // Dst limits are never exceeded.
  29. assertEquals(200, GradientUtil.linearScale(src, dst, 1000));
  30. assertEquals(100, GradientUtil.linearScale(src, dst, -50));
  31. // Zero src or dst interval lengts are ok.
  32. src = { 'min': 0, 'max': 10 };
  33. dst = { 'min': -100, 'max': -200 };
  34. assertEquals(-120, GradientUtil.linearScale(src, dst, 2));
  35. src = { 'min': 0, 'max': 0 };
  36. dst = { 'min': 0, 'max': 0 };
  37. assertEquals(0, GradientUtil.linearScale(src, dst, 0));
  38. src = { 'min': 5, 'max': 5 };
  39. dst = { 'min': -10, 'max': 10 };
  40. assertEquals(-10, GradientUtil.linearScale(src, dst, 20));
  41. src = { 'min': 5, 'max': 15 };
  42. dst = { 'min': 10, 'max': 10 };
  43. assertEquals(10, GradientUtil.linearScale(src, dst, 11));
  44. }
  45. // TODO(simonf): what, no assertRaises?
  46. // Add checks for absent min/max keys in src and dst.
  47. public function testLinearScaleError(): void {
  48. var src:Object = { 'min': 0, 'max': 10 };
  49. var dst:Object = { 'min': 100, 'max': 200 };
  50. try
  51. {
  52. GradientUtil.linearScale(src, dst, 4, -2);
  53. }
  54. catch ( e : IllegalOperationError )
  55. {
  56. return;
  57. }
  58. assertFalse ( "Should have thrown an IllegalOperationError" );
  59. }
  60. public function testGradientColorForValue():void {
  61. var g:ValueColorGradient = new ValueColorGradient();
  62. // 0 and 100 percent are defaults
  63. g.minColor = Color.RED;
  64. g.maxColor = Color.YELLOW;
  65. g.minValue = 10;
  66. g.maxValue = 20;
  67. assertEquals(
  68. 0xff8000,
  69. g.colorForValue(15));
  70. }
  71. private function verifyGradients(
  72. gc:GradientControl, valuesAndColors:Array):void {
  73. for each(var dict:Object in valuesAndColors) {
  74. assertEquals(dict['c'], gc.colorForValue(dict['v']));
  75. }
  76. }
  77. public function testTwoColorGradient():void {
  78. var values:Array = [20, 10, 13];
  79. var gradientControl:GradientControl = (
  80. GradientRuleList.twoColorGradientControl(
  81. Color.RED, Color.YELLOW, values)
  82. )
  83. var expected:Array = [
  84. {'v': 15, 'c': 0xff8000}]
  85. verifyGradients(gradientControl, expected);
  86. var xml:XML =
  87. <gradientRuleList>
  88. <gradientRule>
  89. <gradient>
  90. <minColor>0xff0000</minColor>
  91. <maxColor>0xffff00</maxColor>
  92. </gradient>
  93. </gradientRule>
  94. </gradientRuleList>;
  95. var ruleList:GradientRuleList = GradientRuleList.fromXML(xml);
  96. var gc2:GradientControl = ruleList.applyGradientToValueList(values);
  97. verifyGradients(gc2, expected);
  98. }
  99. public function testTwoColorGradientOneValue():void {
  100. var values:Array = [10];
  101. var gradientControl:GradientControl = (
  102. GradientRuleList.twoColorGradientControl(
  103. Color.RED, Color.YELLOW, values)
  104. )
  105. var expected:Array = [
  106. {'v': 15, 'c': Color.RED}]
  107. verifyGradients(gradientControl, expected);
  108. }
  109. public function testThreeColorGradient():void {
  110. var values:Array = [131.7, 170, 110];
  111. var gradientControl:GradientControl = (
  112. GradientRuleList.threeColorGradientControl(
  113. Color.RED, Color.YELLOW, Color.GREEN, values)
  114. )
  115. // Test values at the middle of each half (at 25% and 75% of the total).
  116. var expected:Array = [
  117. {'v': 125, 'c': 0xff8000},
  118. {'v': 155, 'c': 0x80ff00},
  119. ]
  120. verifyGradients(gradientControl, expected);
  121. var xml:XML =
  122. <gradientRuleList>
  123. <gradientRule>
  124. <gradient>
  125. <minColor>0xff0000</minColor>
  126. <maxColor>0xffff00</maxColor>
  127. <maxPercent>50</maxPercent>
  128. </gradient>
  129. </gradientRule>
  130. <gradientRule>
  131. <gradient>
  132. <minColor>0xffff00</minColor>
  133. <maxColor>0x00ff00</maxColor>
  134. </gradient>
  135. </gradientRule>
  136. </gradientRuleList>;
  137. var ruleList:GradientRuleList = GradientRuleList.fromXML(xml);
  138. var gc2:GradientControl = ruleList.applyGradientToValueList(values);
  139. verifyGradients(gc2, expected);
  140. }
  141. // First a simple rule, then a recursive one.
  142. public function testRecursiveGradient():void {
  143. var values:Array = [300, 400];
  144. var g1:GradientRule = new GradientRule();
  145. g1.maxValue = 330;
  146. g1.minColor = Color.RED;
  147. g1.maxColor = 0xff8000;
  148. var g2:GradientRule = new GradientRule();
  149. g2.maxPercent = 10;
  150. g2.minColor = Color.YELLOW;
  151. g2.maxColor = Color.GREEN;
  152. var g3:GradientRule = new GradientRule();
  153. g3.maxColor = Color.BLUE;
  154. var gcr2:GradientRuleList = new GradientRuleList([g2, g3]);
  155. var gcr:GradientRuleList = new GradientRuleList([g1, gcr2]);
  156. var gc:GradientControl = gcr.applyGradientToValueList(values);
  157. var expected:Array = [
  158. // Values < 330 are handled by g1
  159. {'v': 315, 'c': 0xff4000},
  160. // Values below 10% of the range (330, 400) are handled by g2 in gcr2
  161. {'v': 335, 'c': 0x49ff00},
  162. // Values above 10% of the range (330, 400) are handled by g3 in gcr2
  163. {'v': 355, 'c': 0x00b649},
  164. ]
  165. verifyGradients(gc, expected);
  166. var xml:XML =
  167. <gradientRuleList>
  168. <gradientRule>
  169. <gradient>
  170. <minColor>0xff0000</minColor>
  171. <maxColor>0xff8000</maxColor>
  172. <maxValue>330</maxValue>
  173. </gradient>
  174. </gradientRule>
  175. <gradientRule>
  176. <gradientRuleList>
  177. <gradientRule>
  178. <gradient>
  179. <minColor>0xffff00</minColor>
  180. <maxColor>0x00ff00</maxColor>
  181. <maxPercent>10</maxPercent>
  182. </gradient>
  183. </gradientRule>
  184. <gradientRule>
  185. <gradient>
  186. <maxColor>0x0000ff</maxColor>
  187. </gradient>
  188. </gradientRule>
  189. </gradientRuleList>
  190. </gradientRule>
  191. </gradientRuleList>;
  192. var ruleList:GradientRuleList = GradientRuleList.fromXML(xml);
  193. var gc2:GradientControl = ruleList.applyGradientToValueList(values);
  194. verifyGradients(gc2, expected);
  195. }
  196. // First a recursirve rule, than a simple one.
  197. public function testRecursiveGradient2():void {
  198. var values:Array = [300, 400];
  199. var g1:GradientRule = new GradientRule();
  200. g1.maxPercent = 20;
  201. g1.minColor = Color.RED;
  202. g1.maxColor = 0xff8000;
  203. var g2:GradientRule = new GradientRule();
  204. g2.minColor = Color.YELLOW;
  205. g2.maxColor = Color.GREEN;
  206. //g2.maxValue = 350 is derived from g3.
  207. var g3:GradientRule = new GradientRule();
  208. // g3.minColor = Color.GREEN is derived from g2.
  209. g3.minValue = 350;
  210. g3.maxColor = Color.BLUE;
  211. var gcr:GradientRuleList = new GradientRuleList([g1, g2]);
  212. var gcr2:GradientRuleList = new GradientRuleList([gcr, g3]);
  213. var gc:GradientControl = gcr2.applyGradientToValueList(values);
  214. var expected:Array = [
  215. // Values < 320 (20% of the range [300, 350]) are handled by g1 in gcr.
  216. {'v': 305, 'c': 0xff4000},
  217. // Values > 320 (20% of the range [300, 350]) are handled by g2 in gcr.
  218. {'v': 335, 'c': 0x60ff00},
  219. // Values above 350 are handled by g3
  220. {'v': 355, 'c': 0x00e61a},
  221. ]
  222. verifyGradients(gc, expected);
  223. var xml:XML =
  224. <gradientRuleList>
  225. <gradientRule>
  226. <gradientRuleList>
  227. <gradientRule>
  228. <gradient>
  229. <minColor>0xff0000</minColor>
  230. <maxColor>0xff8000</maxColor>
  231. <maxPercent>20</maxPercent>
  232. </gradient>
  233. </gradientRule>
  234. <gradientRule>
  235. <gradient>
  236. <minColor>0xffff00</minColor>
  237. <maxColor>0x00ff00</maxColor>
  238. </gradient>
  239. </gradientRule>
  240. </gradientRuleList>
  241. </gradientRule>
  242. <gradientRule>
  243. <gradient>
  244. <maxColor>0x0000ff</maxColor>
  245. <minValue>350</minValue>
  246. </gradient>
  247. </gradientRule>
  248. </gradientRuleList>;
  249. var ruleList:GradientRuleList = GradientRuleList.fromXML(xml);
  250. var gc2:GradientControl = ruleList.applyGradientToValueList(values);
  251. verifyGradients(gc2, expected);
  252. }
  253. }
  254. }