Primiano Tucci | 8bcf608 | 2021-06-11 17:15:22 +0100 | [diff] [blame^] | 1 | // Copyright (C) 2021 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | import * as fs from 'fs'; |
| 16 | import * as path from 'path'; |
| 17 | import * as puppeteer from 'puppeteer'; |
| 18 | import {assertExists} from '../base/logging'; |
| 19 | |
| 20 | import { |
| 21 | compareScreenshots, |
| 22 | getTestTracePath, |
| 23 | waitForPerfettoIdle |
| 24 | } from './perfetto_ui_test_helper'; |
| 25 | |
| 26 | declare var global: {__BROWSER__: puppeteer.Browser;}; |
| 27 | const browser = assertExists(global.__BROWSER__); |
| 28 | const expectedScreenshotPath = path.join('test', 'data', 'ui-screenshots'); |
| 29 | |
| 30 | |
| 31 | async function getPage(): Promise<puppeteer.Page> { |
| 32 | const pages = (await browser.pages()); |
| 33 | expect(pages.length).toBe(1); |
| 34 | return pages[pages.length - 1]; |
| 35 | } |
| 36 | |
| 37 | // Executed once at the beginning of the test. Navigates to the UI. |
| 38 | beforeAll(async () => { |
| 39 | jest.setTimeout(60000); |
| 40 | const page = await getPage(); |
| 41 | await page.setViewport({width: 1920, height: 1080}); |
| 42 | await page.goto('http://localhost:10000/?testing=1'); |
| 43 | }); |
| 44 | |
| 45 | // After each test (regardless of nesting) capture a screenshot named after the |
| 46 | // test('') name and compare the screenshot with the expected one in |
| 47 | // /test/data/ui-screenshots. |
| 48 | afterEach(async () => { |
| 49 | let testName = expect.getState().currentTestName; |
| 50 | testName = testName.replace(/[^a-z0-9-]/gmi, '_').toLowerCase(); |
| 51 | const page = await getPage(); |
| 52 | |
| 53 | // cwd() is set to //out/ui when running tests, just create a subdir in there. |
| 54 | // The CI picks up this directory and uploads to GCS after every failed run. |
| 55 | const tmpDir = path.resolve('./ui-test-artifacts'); |
| 56 | if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir); |
| 57 | const screenshotName = `ui-${testName}.png`; |
| 58 | const actualFilename = path.join(tmpDir, screenshotName); |
| 59 | const expectedFilename = path.join(expectedScreenshotPath, screenshotName); |
| 60 | await page.screenshot({path: actualFilename}); |
| 61 | const rebaseline = process.env['PERFETTO_UI_TESTS_REBASELINE'] === '1'; |
| 62 | if (rebaseline) { |
| 63 | console.log('Saving reference screenshot into', expectedFilename); |
| 64 | fs.copyFileSync(actualFilename, expectedFilename); |
| 65 | } else { |
| 66 | await compareScreenshots(actualFilename, expectedFilename); |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | describe('android_trace_30s', () => { |
| 71 | test('load', async () => { |
| 72 | const page = await getPage(); |
| 73 | const file = await page.waitForSelector('input.trace_file'); |
| 74 | const tracePath = getTestTracePath('example_android_trace_30s.pb'); |
| 75 | assertExists(file).uploadFile(tracePath); |
| 76 | await waitForPerfettoIdle(page); |
| 77 | }); |
| 78 | |
| 79 | test('expand_camera', async () => { |
| 80 | const page = await getPage(); |
| 81 | await page.click('.main-canvas'); |
| 82 | await page.click('h1[title="com.google.android.GoogleCamera 5506"]'); |
| 83 | await page.evaluate(() => { |
| 84 | document.querySelector('.scrolling-panel-container')!.scrollTo(0, 400); |
| 85 | }); |
| 86 | await waitForPerfettoIdle(page); |
| 87 | }); |
| 88 | |
| 89 | test('search', async () => { |
| 90 | const page = await getPage(); |
| 91 | const searchInput = '.omnibox input'; |
| 92 | await page.focus(searchInput); |
| 93 | await page.keyboard.type('TrimMaps'); |
| 94 | await waitForPerfettoIdle(page); |
| 95 | for (let i = 0; i < 10; i++) { |
| 96 | await page.keyboard.type('\n'); |
| 97 | } |
| 98 | await waitForPerfettoIdle(page); |
| 99 | }); |
| 100 | }); |