aboutsummaryrefslogtreecommitdiff
path: root/catapult/third_party/polymer/components/iron-behaviors/test/focused-state.html
blob: 3b75c704e153558068d77adff7012463d8d6c148 (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
153
154
155
156
157
158
159
160
161
<!doctype html>
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
  <title>focused-state</title>

  <script src="../../webcomponentsjs/webcomponents-lite.js"></script>
  <script src="../../web-component-tester/browser.js"></script>
  <script src="../../iron-test-helpers/mock-interactions.js"></script>
  <link rel="import" href="test-elements.html">
</head>
<body>

  <test-fixture id="TrivialFocusedState">
    <template>
      <test-control tabindex="-1"></test-control>
    </template>
  </test-fixture>

  <test-fixture id="NestedFocusedState">
    <template>
      <nested-focusable></nested-focusable>
    </template>
  </test-fixture>

  <test-fixture id="LightDOM">
    <template>
      <test-light-dom>
        <input id="input">
        <nested-focusable></nested-focusable>
      </test-light-dom>
    </template>
  </test-fixture>

  <script>
    suite('focused-state', function() {
      var focusTarget;

      setup(function() {
        focusTarget = fixture('TrivialFocusedState');
      });

      suite('when is focused', function() {
        test('receives a focused attribute', function() {
          expect(focusTarget.hasAttribute('focused')).to.be.eql(false);
          MockInteractions.focus(focusTarget);
          expect(focusTarget.hasAttribute('focused')).to.be.eql(true);
        });

        test('focused property is true', function() {
          expect(focusTarget.focused).to.not.be.eql(true);
          MockInteractions.focus(focusTarget);
          expect(focusTarget.focused).to.be.eql(true);
        });
      });

      suite('when is blurred', function() {
        test('loses the focused attribute', function() {
          MockInteractions.focus(focusTarget);
          expect(focusTarget.hasAttribute('focused')).to.be.eql(true);
          MockInteractions.blur(focusTarget);
          expect(focusTarget.hasAttribute('focused')).to.be.eql(false);
        });

        test('focused property is false', function() {
          MockInteractions.focus(focusTarget);
          expect(focusTarget.focused).to.be.eql(true);
          MockInteractions.blur(focusTarget);
          expect(focusTarget.focused).to.be.eql(false);
        });
      });

      suite('when the focused state is disabled', function() {
        test('will not be focusable', function() {
          var blurSpy = sinon.spy(focusTarget, 'blur');
          MockInteractions.focus(focusTarget);
          focusTarget.disabled = true;
          expect(focusTarget.getAttribute('tabindex')).to.be.eql('-1');
          expect(blurSpy.called).to.be.eql(true);
        });
      });
    });

    suite('nested focusable', function() {
      var focusable;

      setup(function() {
        focusable = fixture('NestedFocusedState');
      });

      test('focus/blur events fired on host element', function() {
        var nFocusEvents = 0;
        var nBlurEvents = 0;

        focusable.addEventListener('focus', function() {
          nFocusEvents += 1;
          expect(focusable.focused).to.be.equal(true);
          MockInteractions.blur(focusable.$.input);
        });
        focusable.addEventListener('blur', function() {
          expect(focusable.focused).to.be.equal(false);
          nBlurEvents += 1;
        });

        MockInteractions.focus(focusable.$.input);

        expect(nBlurEvents).to.be.greaterThan(0);
        expect(nFocusEvents).to.be.greaterThan(0);
      });

    });


    suite('elements in the light dom', function() {
      var lightDOM, input, lightDescendantShadowInput;

      setup(function() {
        lightDOM = fixture('LightDOM');
        input = document.querySelector('#input');
        lightDescendantShadowInput = Polymer.dom(lightDOM)
            .querySelector('nested-focusable').$.input;
      });

      test('should not fire the focus event', function() {
        var nFocusEvents = 0;

        lightDOM.addEventListener('focus', function() {
          nFocusEvents += 1;
        });

        MockInteractions.focus(input);

        expect(nFocusEvents).to.be.equal(0);
      });

      test('should not fire the focus event from shadow descendants', function() {
        var nFocusEvents = 0;

        lightDOM.addEventListener('focus', function() {
          nFocusEvents += 1;
        });

        MockInteractions.focus(lightDescendantShadowInput);

        expect(nFocusEvents).to.be.equal(0);
      });

    });

  </script>

</body>
</html>