/**
 * Copyright 2017 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 { it, expect } from './fixtures';
const expectedOutput = '
hello
';
it('should work', async ({page, server}) => {
  await page.setContent('hello
');
  const result = await page.content();
  expect(result).toBe(expectedOutput);
});
it('should work with domcontentloaded', async ({page, server}) => {
  await page.setContent('hello
', { waitUntil: 'domcontentloaded' });
  const result = await page.content();
  expect(result).toBe(expectedOutput);
});
it('should work with doctype', async ({page, server}) => {
  const doctype = '';
  await page.setContent(`${doctype}hello
`);
  const result = await page.content();
  expect(result).toBe(`${doctype}${expectedOutput}`);
});
it('should work with HTML 4 doctype', async ({page, server}) => {
  const doctype = '';
  await page.setContent(`${doctype}hello
`);
  const result = await page.content();
  expect(result).toBe(`${doctype}${expectedOutput}`);
});
it('should respect timeout', async ({page, server, playwright}) => {
  const imgPath = '/img.png';
  // stall for image
  server.setRoute(imgPath, (req, res) => {});
  let error = null;
  await page.setContent(` `, {timeout: 1}).catch(e => error = e);
  expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
});
it('should respect default navigation timeout', async ({page, server, playwright}) => {
  page.setDefaultNavigationTimeout(1);
  const imgPath = '/img.png';
  // stall for image
  server.setRoute(imgPath, (req, res) => {});
  const error = await page.setContent(`
`, {timeout: 1}).catch(e => error = e);
  expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
});
it('should respect default navigation timeout', async ({page, server, playwright}) => {
  page.setDefaultNavigationTimeout(1);
  const imgPath = '/img.png';
  // stall for image
  server.setRoute(imgPath, (req, res) => {});
  const error = await page.setContent(` `).catch(e => e);
  expect(error.message).toContain('page.setContent: Timeout 1ms exceeded.');
  expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
});
it('should await resources to load', async ({page, server}) => {
  const imgPath = '/img.png';
  let imgResponse = null;
  server.setRoute(imgPath, (req, res) => imgResponse = res);
  let loaded = false;
  const contentPromise = page.setContent(`
`).catch(e => e);
  expect(error.message).toContain('page.setContent: Timeout 1ms exceeded.');
  expect(error).toBeInstanceOf(playwright.errors.TimeoutError);
});
it('should await resources to load', async ({page, server}) => {
  const imgPath = '/img.png';
  let imgResponse = null;
  server.setRoute(imgPath, (req, res) => imgResponse = res);
  let loaded = false;
  const contentPromise = page.setContent(` `).then(() => loaded = true);
  await server.waitForRequest(imgPath);
  expect(loaded).toBe(false);
  imgResponse.end();
  await contentPromise;
});
it('should work fast enough', async ({page, server}) => {
  for (let i = 0; i < 20; ++i)
    await page.setContent('
`).then(() => loaded = true);
  await server.waitForRequest(imgPath);
  expect(loaded).toBe(false);
  imgResponse.end();
  await contentPromise;
});
it('should work fast enough', async ({page, server}) => {
  for (let i = 0; i < 20; ++i)
    await page.setContent('yo
');
});
it('should work with tricky content', async ({page, server}) => {
  await page.setContent('hello world
' + '\x7F');
  expect(await page.$eval('div', div => div.textContent)).toBe('hello world');
});
it('should work with accents', async ({page, server}) => {
  await page.setContent('aberración
');
  expect(await page.$eval('div', div => div.textContent)).toBe('aberración');
});
it('should work with emojis', async ({page, server}) => {
  await page.setContent('🐥
');
  expect(await page.$eval('div', div => div.textContent)).toBe('🐥');
});
it('should work with newline', async ({page, server}) => {
  await page.setContent('\n
');
  expect(await page.$eval('div', div => div.textContent)).toBe('\n');
});