<!doctype html> <!– @license Copyright © 2015 The Polymer Project Authors. All rights reserved. This code may only be used under the BSD style license found at polymer.github.io/LICENSE.txt The complete set of authors may be found at polymer.github.io/AUTHORS.txt The complete set of contributors may be found at 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 polymer.github.io/PATENTS.txt –> <html> <head>

<title>paper-input tests</title>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">

<script src="../../webcomponentsjs/webcomponents-lite.js"></script>

<script src="../../web-component-tester/browser.js"></script>
<script src="../../iron-test-helpers/test-helpers.js"></script>
<script src="../../iron-test-helpers/mock-interactions.js"></script>

<link rel="import" href="../paper-input.html">
<link rel="import" href="letters-only.html">

</head> <body>

<test-fixture id="basic">
  <template>
    <paper-input></paper-input>
  </template>
</test-fixture>

<test-fixture id="has-tabindex">
  <template>
    <paper-input tabindex="0"></paper-input>
  </template>
</test-fixture>

<test-fixture id="label">
  <template>
    <paper-input label="foo"></paper-input>
  </template>
</test-fixture>

<test-fixture id="label-has-value">
  <template>
    <paper-input label="foo" value="bar"></paper-input>
  </template>
</test-fixture>

<test-fixture id="error">
  <template>
    <paper-input auto-validate pattern="[0-9]*" value="foobar" error-message="error"></paper-input>
  </template>
</test-fixture>

<test-fixture id="required">
  <template>
    <paper-input auto-validate required error-message="error"></paper-input>
  </template>
</test-fixture>

<test-fixture id="required-no-auto-validate">
  <template>
    <paper-input required error-message="error"></paper-input>
  </template>
</test-fixture>

<test-fixture id="required-char-counter">
  <template>
    <paper-input auto-validate char-counter required error-message="error"></paper-input>
  </template>
</test-fixture>

<test-fixture id="char-counter">
  <template>
    <paper-input char-counter value="foobar"></paper-input>
  </template>
</test-fixture>

<test-fixture id="type-number-char-counter">
  <template>
    <paper-input type="number" char-counter value="1138"></paper-input>
  </template>
</test-fixture>

<test-fixture id="always-float-label">
  <template>
    <paper-input always-float-label label="foo"></paper-input>
  </template>
</test-fixture>

<test-fixture id="placeholder">
  <template>
    <paper-input label="foo" placeholder="bar"></paper-input>
  </template>
</test-fixture>

<test-fixture id="date">
  <template>
    <paper-input label="foo" type="date"></paper-input>
  </template>
</test-fixture>

<letters-only></letters-only>

<test-fixture id="validator">
  <template>
    <paper-input value="123123" validator="letters-only" auto-validate></paper-input>
  </template>
</test-fixture>

<test-fixture id="multiple-inputs">
  <template>
    <paper-input label="one"></paper-input>
    <paper-input label="two"></paper-input>
    <paper-input label="three"></paper-input>
    <paper-input label="four"></paper-input>
  </template>
</test-fixture>

<script>

  suite('basic', function() {

    test('setting value sets the input value', function() {
      var input = fixture('basic');
      input.value = 'foobar';
      assert.equal(input.inputElement.value, input.value, 'inputElement.value equals input.value');
    });

    test('placeholder does not overlap label', function() {
      var input = fixture('placeholder');
      assert.equal(input.inputElement.placeholder, input.placeholder, 'inputElement.placeholder equals input.placeholder');
      assert.equal(input.noLabelFloat, false);
      var floatingLabel = Polymer.dom(Polymer.dom(input.root).querySelector('paper-input-container').root).querySelector('.label-is-floating');
      assert.ok(floatingLabel);
    });

    test('special types autofloat the label', function() {
      var input = fixture('date');
      // Browsers that don't support special <input> types like `date` fallback
      // to `text`, so make sure to only test if type is still preserved after
      // the element is attached.
      if (input.inputElement.type === "date") {
        assert.equal(input.alwaysFloatLabel, true);
        var floatingLabel = Polymer.dom(Polymer.dom(input.root).querySelector('paper-input-container').root).querySelector('.label-is-floating');
        assert.ok(floatingLabel);
      }
    });

    test('always-float-label attribute works without placeholder', function() {
      var input = fixture('always-float-label');
      var container = Polymer.dom(input.root).querySelector('paper-input-container');
      var inputContent = Polymer.dom(container.root).querySelector('.input-content');
      assert.isTrue(inputContent.classList.contains('label-is-floating'), 'label is floating');
    });

    test('label does not receive pointer events', function() {
      var input = fixture('always-float-label');
      var label = Polymer.dom(input.root).querySelector('label');
      assert.equal(getComputedStyle(label).pointerEvents, 'none');
    });

    test('error message is displayed', function() {
      var input = fixture('error');
      forceXIfStamp(input);
      var error = Polymer.dom(input.root).querySelector('paper-input-error');
      assert.ok(error, 'paper-input-error exists');
      assert.notEqual(getComputedStyle(error).display, 'none', 'error is not display:none');
    });

    test('empty required input shows error', function() {
      var input = fixture('required');
      forceXIfStamp(input);
      var error = Polymer.dom(input.root).querySelector('paper-input-error');
      assert.ok(error, 'paper-input-error exists');
      assert.notEqual(getComputedStyle(error).display, 'none', 'error is not display:none');
    });

    test('character counter is displayed', function() {
      var input = fixture('char-counter');
      forceXIfStamp(input);
      var counter = Polymer.dom(input.root).querySelector('paper-input-char-counter')
      assert.ok(counter, 'paper-input-char-counter exists');
      assert.equal(counter._charCounterStr, input.value.length, 'character counter shows the value length');
    });

    test('character counter is correct for type=number', function() {
      var input = fixture('type-number-char-counter');
      forceXIfStamp(input);
      var counter = Polymer.dom(input.root).querySelector('paper-input-char-counter')
      assert.ok(counter, 'paper-input-char-counter exists');
      assert.equal(counter._charCounterStr, input.value.toString().length, 'character counter shows the value length');
    });

    test('validator is used', function() {
      var input = fixture('validator');
      assert.ok(input.inputElement.invalid, 'input is invalid');
    });

    test('caret position is preserved', function() {
      var input = fixture('basic');
      var ironInput = Polymer.dom(input.root).querySelector('input[is="iron-input"]');
      input.value = 'nananana';
      ironInput.selectionStart = 2;
      ironInput.selectionEnd = 2;

      input.updateValueAndPreserveCaret('nanananabatman');

      assert.equal(ironInput.selectionStart, 2, 'selectionStart is preserved');
      assert.equal(ironInput.selectionEnd, 2, 'selectionEnd is preserved');
    });

    test('setting autofocus to true implictly acquires focus', function(done) {
      var input = fixture('basic');
      var inputFocusSpy = sinon.spy(input.inputElement, 'focus');
      window.setTimeout(function() {
        assert(inputFocusSpy.called);
        done();
      }, 50);
      input.autofocus = true;
    });

    test('autofocus doesn\'t grab focus if another element already has it', function(done) {
      var inputs = fixture('multiple-inputs');
      var inputFocusSpies = inputs.map(function(input) {
        return sinon.spy(input.inputElement, 'focus');
      });
      window.setTimeout(function() {
        assert(inputFocusSpies[0].called, 'first autofocus input with grabbed focus');
        assert(!inputFocusSpies[1].called, 'second autofocus input let first input keep focus');
        done();
      }, 50);
      inputs[0].autofocus = true;
      inputs[1].autofocus = true; // Shouldn't cause focus to change
    });

  });

  suite('focus/blur events', function() {
    var input;

    setup(function() {
      input = fixture('basic');
    });

    // At the moment, it is very hard to correctly fire exactly
    // one focus/blur events on a paper-input. This is because
    // when a paper-input is focused, it needs to focus
    // its underlying native input, which will also fire a `blur`
    // event.
    test('focus events fired on host element', function() {
      input.addEventListener('focus', function(event) {
        assert(input.focused, 'input is focused');
      });
      MockInteractions.focus(input);
    });

    test('focus events fired on host element if nested element is focused', function() {
      input.addEventListener('focus', function(event) {
        assert(input.focused, 'input is focused');
      });
      MockInteractions.focus(input.inputElement);
    });

    test('blur events fired on host element', function() {
      MockInteractions.focus(input);
      input.addEventListener('blur', function(event) {
        assert(!input.focused, 'input is blurred');
      });
      MockInteractions.blur(input);
    });

    test('blur events fired on host element nested element is blurred', function() {
      MockInteractions.focus(input);
      input.addEventListener('blur', function(event) {
        assert(!input.focused, 'input is blurred');
      });
      MockInteractions.blur(input.inputElement);
    });

    test('focusing then bluring sets the focused attribute correctly', function() {
      MockInteractions.focus(input);
      assert(input.focused, 'input is focused');
      MockInteractions.blur(input);
      assert(!input.focused, 'input is blurred');
      MockInteractions.focus(input.inputElement);
      assert(input.focused, 'input is focused');
      MockInteractions.blur(input.inputElement);
      assert(!input.focused, 'input is blurred');
    });

    test('focusing then bluring with shift-tab removes the focused attribute correctly', function() {
      MockInteractions.focus(input);
      assert(input.focused, 'input is focused');

      // Fake a shift-tab induced blur by forcing the flag.
      input._shiftTabPressed = true;
      MockInteractions.blur(input.inputElement);
      assert(!input.focused, 'input is blurred');
    });
  });

  suite('focused styling (integration test)', function() {

    test('underline is colored when input is focused', function(done) {
      var input = fixture('basic');
      var container = Polymer.dom(input.root).querySelector('paper-input-container');
      var line = Polymer.dom(container.root).querySelector('.underline');
      assert.isFalse(line.classList.contains('is-highlighted'), 'line is not highlighted when input is not focused');
      MockInteractions.focus(input.inputElement);
      requestAnimationFrame(function() {
        assert.isTrue(line.classList.contains('is-highlighted'), 'line is highlighted when input is focused');
        done();
      });
    });

  });

  suite('validation', function() {

    test('invalid attribute updated after calling validate()', function() {
      var input = fixture('required-no-auto-validate');
      forceXIfStamp(input);
      input.validate();
      var error = Polymer.dom(input.root).querySelector('paper-input-error');
      assert.ok(error, 'paper-input-error exists');
      assert.notEqual(getComputedStyle(error).display, 'none', 'error is not display:none');
      assert.isTrue(input.invalid, 'invalid is true');
    });

  });

  suite('a11y', function() {
    test('has aria-labelledby, which is monotonically increasing', function() {
      var inputs = fixture('multiple-inputs');

      // Find the first index of the input in this fixture. Since the label
      // ids monotonically increase every time a new input is created, and
      // this fixture isn't the first one in the document, we're going to start
      // at an ID > 1.
      var firstLabel = Polymer.dom(inputs[0].root).querySelector('label').id;
      var index = parseInt(firstLabel.substr(firstLabel.lastIndexOf('-') + 1));

      for (var i = 0; i < inputs.length; i++ ) {
        var input = inputs[i].inputElement;
        var label = Polymer.dom(inputs[i].root).querySelector('label').id;

        assert.isTrue(input.hasAttribute('aria-labelledby'));
        assert.equal(label, 'paper-input-label-' + (index++));
        assert.equal(input.getAttribute('aria-labelledby'), label);
      }
    });

    test('has aria-describedby for error message', function() {
      var input = fixture('required');
      forceXIfStamp(input);
      assert.isTrue(input.inputElement.hasAttribute('aria-describedby'));
      assert.equal(input.inputElement.getAttribute('aria-describedby'), Polymer.dom(input.root).querySelector('paper-input-error').id, 'aria-describedby points to the error message');
    });

    test('has aria-describedby for character counter', function() {
      var input = fixture('char-counter');
      forceXIfStamp(input);
      assert.isTrue(input.inputElement.hasAttribute('aria-describedby'));
      assert.equal(input.inputElement.getAttribute('aria-describedby'), Polymer.dom(input.root).querySelector('paper-input-char-counter').id, 'aria-describedby points to the character counter');
    });

    test('has aria-describedby for character counter and error', function() {
      var input = fixture('required-char-counter');
      forceXIfStamp(input);
      assert.isTrue(input.inputElement.hasAttribute('aria-describedby'));
      assert.equal(input.inputElement.getAttribute('aria-describedby'), Polymer.dom(input.root).querySelector('paper-input-error').id + ' ' + Polymer.dom(input.root).querySelector('paper-input-char-counter').id, 'aria-describedby points to the error message and character counter');
    });

    test('focus an input with tabindex', function(done) {
      var input = fixture('has-tabindex');
      flush(function() {
        MockInteractions.focus(input);
        flush(function() {
          assert.equal(input.shadowRoot ? input.shadowRoot.activeElement :
              document.activeElement, input._focusableElement);
          done();
        });
      });
    });
  });

</script>

</body> </html>