/**
 * Copyright 2018 Google Inc. All rights reserved.
 * Modifications copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { test as it, expect } from './pageTest';
import { chromiumVersionLessThan } from '../config/utils';
it('should work @smoke', async ({ page, browserName, isMac }) => {
  await page.setContent(`
  
    Accessibility Test 
  
  
    Inputs 
    This is a description!
    Hello World
`);
  const snapshot = await page.accessibility.snapshot();
  expect(snapshot.children[0]).toEqual({
    role: browserName === 'firefox' ? 'text leaf' : 'text',
    name: 'Hello World',
  });
});
it('roledescription', async ({ page }) => {
  await page.setContent('Hi
');
  const snapshot = await page.accessibility.snapshot();
  expect(snapshot.children[0].roledescription).toEqual('foo');
});
it('orientation', async ({ page }) => {
  await page.setContent('11 ');
  const snapshot = await page.accessibility.snapshot();
  expect(snapshot.children[0].orientation).toEqual('vertical');
});
it('autocomplete', async ({ page }) => {
  await page.setContent('hi
');
  const snapshot = await page.accessibility.snapshot();
  expect(snapshot.children[0].autocomplete).toEqual('list');
  expect(snapshot.children[0].haspopup).toEqual('menu');
});
it('multiselectable', async ({ page }) => {
  await page.setContent('hey
');
  const snapshot = await page.accessibility.snapshot();
  expect(snapshot.children[0].multiselectable).toEqual(true);
});
it('keyshortcuts', async ({ page }) => {
  await page.setContent('hey
');
  const snapshot = await page.accessibility.snapshot();
  expect(snapshot.children[0].keyshortcuts).toEqual('foo');
});
it('should not report text nodes inside controls', async function({ page, browserName }) {
  await page.setContent(`
  `);
  const golden = {
    role: browserName === 'firefox' ? 'document' : 'WebArea',
    name: '',
    children: [{
      role: 'tab',
      name: 'Tab1',
      selected: true
    }, {
      role: 'tab',
      name: 'Tab2'
    }]
  };
  expect(await page.accessibility.snapshot()).toEqual(golden);
});
it('rich text editable fields should have children', async function({ page, browserName, browserVersion }) {
  it.skip(browserName === 'webkit', 'WebKit rich text accessibility is iffy');
  await page.setContent(`
  
    Edit this image: 
    Edit this image: 
    this is the inner content
    
    this is the inner content
    
    this is the inner content
    
My Button `);
  const button = await page.$('button');
  expect(await page.accessibility.snapshot({ root: button })).toEqual({
    role: 'button',
    name: 'My Button'
  });
});
it('should work an input', async ({ page }) => {
  await page.setContent(`
      First Item
      Second Item
      Third Item
     
  `);
  const menu = await page.$('div[role="menu"]');
  expect(await page.accessibility.snapshot({ root: menu })).toEqual({
    role: 'menu',
    name: 'My Menu',
    children:
    [{ role: 'menuitem', name: 'First Item' },
      { role: 'menuitem', name: 'Second Item' },
      { role: 'menuitem', name: 'Third Item' }],
    orientation: (browserName === 'webkit' || (browserName === 'chromium' && !chromiumVersionLessThan(browserVersion, '98.0.1089'))) ? 'vertical' : undefined
  });
});
it('should return null when the element is no longer in DOM', async ({ page }) => {
  await page.setContent(`My Button `);
  const button = await page.$('button');
  await page.$eval('button', button => button.remove());
  expect(await page.accessibility.snapshot({ root: button })).toEqual(null);
});
it('should show uninteresting nodes', async ({ page }) => {
  await page.setContent(`
    
  `);
  const root = await page.$('#root');
  const snapshot = await page.accessibility.snapshot({ root, interestingOnly: false });
  expect(snapshot.role).toBe('textbox');
  expect(snapshot.value).toContain('hello');
  expect(snapshot.value).toContain('world');
  expect(!!snapshot.children).toBe(true);
});
it('should work when there is a title ', async ({ page }) => {
  await page.setContent(`
    This is the title 
    This is the content
  `);
  const snapshot = await page.accessibility.snapshot();
  expect(snapshot.name).toBe('This is the title');
  expect(snapshot.children[0].name).toBe('This is the content');
});
it('should work with aria-invalid accessibility tree', async ({ page, browserName, server }) => {
  await page.goto(server.EMPTY_PAGE);
  await page.setContent(`WHO WE ARE `);
  expect(await page.accessibility.snapshot()).toEqual({
    'role': browserName === 'firefox' ? 'document' : 'WebArea',
    'name': '',
    'children': [
      {
        'role': 'link',
        'name': 'WHO WE ARE',
        'invalid': 'true',
        'value': browserName === 'firefox' ?  `${server.PREFIX}/hi` : undefined
      }
    ]
  });
});