aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/hsvapalette_test.html
blob: a9c7845186faf089a1462ed088abe32cc4689795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>HsvaPalette Unit Tests</title>
  <script src="../base.js"></script>
  <script src="../deps.js"></script>
  <script>
    goog.require('goog.color.alpha');
    goog.require('goog.events');
    goog.require('goog.math.Rect');
    goog.require('goog.style');
    goog.require('goog.testing.PropertyReplacer');
    goog.require('goog.testing.jsunit');
    goog.require('goog.ui.HsvaPalette');
    goog.require('goog.userAgent');
  </script>
</head>
<body>
  <div id="sandbox"></div>
  <script>
    var samplePalette;
    var eventWasFired = false;
    var stubs = new goog.testing.PropertyReplacer();

    function setUp() {
      samplePalette = new goog.ui.HsvaPalette();
    }

    function tearDown() {
      samplePalette.dispose();
      stubs.reset();
    }

    function testZeroAlpha() {
      var palette = new goog.ui.HsvaPalette(null, undefined, 0);
      assertEquals(0, palette.getAlpha());
    }

    function testOptionalInitialColor() {
      var alpha = 0.5;
      var color = '#0000ff';
      var palette = new goog.ui.HsvaPalette(null, color, alpha);
      assertEquals(color, palette.getColor());
      assertEquals(alpha, palette.getAlpha());
    }

    function testCustomClassName() {
      var customClassName = 'custom-plouf';
      var customClassPalette =
          new goog.ui.HsvaPalette(null, null, null, customClassName);
      customClassPalette.createDom();
      assertTrue(goog.dom.classes.has(customClassPalette.getElement(),
                                      customClassName));
    }

    function testSetColor() {
      var color = '#abcdef01';
      samplePalette.setColorRgbaHex(color);
      assertEquals(color,
          goog.color.alpha.parse(samplePalette.getColorRgbaHex()).hex);
    }

    function testRender() {
      samplePalette.render(document.getElementById('sandbox'));

      assertTrue(samplePalette.isInDocument());

      var elem = samplePalette.getElement();
      assertNotNull(elem);
      assertEquals(goog.dom.TagName.DIV, elem.tagName);

      if (goog.userAgent.IE && !goog.userAgent.isVersion('7')) {
        assertSameElements('On IE6, the noalpha class must be present',
            ['goog-hsva-palette', 'goog-hsva-palette-noalpha'],
            goog.dom.classes.get(elem));
      } else {
        assertEquals('The noalpha class must not be present',
           'goog-hsva-palette', elem.className);
      }
    }

    function testNoLeftOverListenersAfterDispose() {
      var initialListenerCount = goog.events.getTotalListenerCount();
      samplePalette.render(document.getElementById('sandbox'));
      samplePalette.dispose();
      assertEquals(initialListenerCount, goog.events.getTotalListenerCount());
    }

    function testInputColor() {
      samplePalette.render(document.getElementById('sandbox'));
      var color = '#00112233';
      samplePalette.inputEl_.value = color;
      samplePalette.handleInput(null);
      assertEquals(color,
          goog.color.alpha.parse(samplePalette.getColorRgbaHex()).hex);
    }

    function testHandleMouseMoveAlpha() {
      samplePalette.render(document.getElementById('sandbox'));
      stubs.set(goog.dom, 'getPageScroll', function() {
        return new goog.math.Coordinate(0, 0);
      });

      // Lowering the opacity of a dark, opaque red should yield a
      // more transparent red.
      samplePalette.setColorRgbaHex('#630c0000');
      goog.style.setPageOffset(samplePalette.aImageEl_, 0, 0);
      goog.style.setSize(samplePalette.aImageEl_, 10, 100);
      var boundaries = goog.style.getBounds(samplePalette.aImageEl_);

      var event = new goog.events.Event();
      event.clientY = boundaries.top;
      samplePalette.handleMouseMoveA_(boundaries, event);

      assertEquals('#630c00ff', samplePalette.getColorRgbaHex());
    }

    function testSwatchOpacity() {
      samplePalette.render(document.getElementById('sandbox'));

      samplePalette.setAlpha(1);
      assertEquals(1, goog.style.getOpacity(samplePalette.swatchEl_));

      samplePalette.setAlpha(0x99 / 0xff);
      assertEquals(0.6, goog.style.getOpacity(samplePalette.swatchEl_));

      samplePalette.setAlpha(0);
      assertEquals(0, goog.style.getOpacity(samplePalette.swatchEl_));
    }

    function testNoTransparencyBehavior() {
      samplePalette.render(document.getElementById('sandbox'));

      samplePalette.inputEl_.value = '#abcdef22';
      samplePalette.handleInput(null);
      samplePalette.inputEl_.value = '#abcdef';
      samplePalette.handleInput(null);
      assertEquals(1, goog.style.getOpacity(samplePalette.swatchEl_));
    }

  </script>
</body>
</html>