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