text: // Copyright 2014 The Chromium Authors. All rights reserved.
  // Use of this source code is governed by a BSD-style license that can be
  // found in the LICENSE file.
  
  import {getFilenameFromURL, PDFViewerElement, shouldIgnoreKeyEvents, ViewerToolbarElement} from 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/pdf_viewer_wrapper.js';
  
  const tests = [
    /**
     * Test that some key elements exist and that they have a shadowRoot. This
     * verifies that Polymer is working correctly.
     */
    function testHasElements() {
      const viewer = /** @type {!PDFViewerElement} */ (
          document.body.querySelector('pdf-viewer'));
      const elementNames = ['viewer-pdf-sidenav', 'viewer-toolbar'];
  
      for (let i = 0; i < elementNames.length; i++) {
        const elements = viewer.shadowRoot.querySelectorAll(elementNames[i]);
        chrome.test.assertEq(1, elements.length);
        chrome.test.assertTrue(elements[0].shadowRoot !== null);
      }
      chrome.test.succeed();
    },
  
    /**
     * Test that the plugin element exists and is navigated to the correct URL.
     */
    function testPluginElement() {
      const viewer = /** @type {!PDFViewerElement} */ (
          document.body.querySelector('pdf-viewer'));
      const plugin = viewer.shadowRoot.querySelector('#plugin');
      chrome.test.assertEq('embed', plugin.localName);
  
      chrome.test.assertTrue(
          plugin.getAttribute('original-url').indexOf('/pdf/test.pdf') !== -1);
      chrome.test.succeed();
    },
  
    function testShouldIgnoreKeyEvents() {
      const viewer = /** @type {!PDFViewerElement} */ (
          document.body.querySelector('pdf-viewer'));
      const toolbar = /** @type {!ViewerToolbarElement} */ (
          viewer.shadowRoot.querySelector('#toolbar'));
  
      // Test case where an <input> field is focused.
      toolbar.shadowRoot.querySelector('viewer-page-selector')
          .pageSelector.focus();
      chrome.test.assertTrue(shouldIgnoreKeyEvents());
  
      // Test case where another field is focused.
      const rotateButton = toolbar.shadowRoot.querySelector(
          'cr-icon-button[iron-icon=\'pdf:rotate-left\']');
      rotateButton.focus();
      chrome.test.assertFalse(shouldIgnoreKeyEvents());
  
      // Test case where the plugin itself is focused.
      viewer.shadowRoot.querySelector('#plugin').focus();
      chrome.test.assertFalse(shouldIgnoreKeyEvents());
  
      chrome.test.succeed();
    },
  
    /**
     * Test that the PDF filename is correctly extracted from URLs with query
     * parameters and fragments.
     */
    function testGetFilenameFromURL(url) {
      chrome.test.assertEq(
          'path.pdf',
          getFilenameFromURL(
              'http://example/com/path/with/multiple/sections/path.pdf'));
  
      chrome.test.assertEq(
          'fragment.pdf',
          getFilenameFromURL('http://example.com/fragment.pdf#zoom=100/Title'));
  
      chrome.test.assertEq(
          'query.pdf', getFilenameFromURL('http://example.com/query.pdf?p=a/b'));
  
      chrome.test.assertEq(
          'both.pdf',
          getFilenameFromURL('http://example.com/both.pdf?p=a/b#zoom=100/Title'));
  
      chrome.test.assertEq(
          'name with spaces.pdf',
          getFilenameFromURL('http://example.com/name%20with%20spaces.pdf'));
  
      chrome.test.assertEq(
          'invalid%EDname.pdf',
          getFilenameFromURL('http://example.com/invalid%EDname.pdf'));
  
      chrome.test.succeed();
    }
  ];
  
  chrome.test.runTests(tests);
  