From 31392005c5bd6a6db022214b47646e30cb552e5e Mon Sep 17 00:00:00 2001 From: Kean Mariotti Date: Tue, 2 Aug 2022 09:30:30 +0000 Subject: [PATCH] refactor test code - segregate unit and e2e test utils - add UnitTestUtils::getParser() Test: cd development/tools/winscope-ng && npm run build:all && npm run test:all Change-Id: I48212fe9bb1ef01b09301cd8a721f10ea18c9b77 --- .../src/parsers/parser_accessibility.spec.ts | 7 ++--- .../src/parsers/parser_common.spec.ts | 4 +-- .../parser_input_method_clients.spec.ts | 4 +-- ...arser_input_method_manager_service.spec.ts | 4 +-- .../parser_input_method_service.spec.ts | 4 +-- .../src/parsers/parser_protolog.spec.ts | 4 +-- .../parsers/parser_screen_recording.spec.ts | 4 +-- .../parser_screen_recording_legacy.spec.ts | 4 +-- .../parsers/parser_surface_flinger.spec.ts | 4 +-- .../parser_surface_flinger_dump.spec.ts | 4 +-- .../src/parsers/parser_transactions.spec.ts | 4 +-- .../src/parsers/parser_window_manager.spec.ts | 4 +-- .../parser_window_manager_dump.spec.ts | 4 +-- .../winscope-ng/src/test/{ => common}/blob.ts | 0 .../test/{test_utils.ts => common/utils.ts} | 20 ++++++------- tools/winscope-ng/src/test/e2e/utils.ts | 25 ++++++++++++++++ .../test/e2e/viewer_window_manager.spec.ts | 6 ++-- .../winscope-ng/src/test/e2e/winscope.spec.ts | 4 +-- tools/winscope-ng/src/test/unit/utils.ts | 29 +++++++++++++++++++ 19 files changed, 95 insertions(+), 44 deletions(-) rename tools/winscope-ng/src/test/{ => common}/blob.ts (100%) rename tools/winscope-ng/src/test/{test_utils.ts => common/utils.ts} (69%) create mode 100644 tools/winscope-ng/src/test/e2e/utils.ts create mode 100644 tools/winscope-ng/src/test/unit/utils.ts diff --git a/tools/winscope-ng/src/parsers/parser_accessibility.spec.ts b/tools/winscope-ng/src/parsers/parser_accessibility.spec.ts index a648dbf27..5b486bb23 100644 --- a/tools/winscope-ng/src/parsers/parser_accessibility.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_accessibility.spec.ts @@ -17,16 +17,13 @@ import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; import {Parser} from "./parser"; import {ParserFactory} from "./parser_factory"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; describe("ParserAccessibility", () => { let parser: Parser; beforeAll(async () => { - const trace = TestUtils.getFixtureBlob("trace_Accessibility.pb"); - const parsers = await new ParserFactory().createParsers([trace]); - expect(parsers.length).toEqual(1); - parser = parsers[0]; + parser = await UnitTestUtils.getParser("traces/Accessibility.pb"); }); it("has expected trace type", () => { diff --git a/tools/winscope-ng/src/parsers/parser_common.spec.ts b/tools/winscope-ng/src/parsers/parser_common.spec.ts index 391bc78c8..e0a8605d5 100644 --- a/tools/winscope-ng/src/parsers/parser_common.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_common.spec.ts @@ -16,13 +16,13 @@ import {Timestamp, TimestampType} from "common/trace/timestamp"; import {ParserFactory} from "./parser_factory"; import {Parser} from "./parser"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; describe("Parser", () => { let parser: Parser; beforeAll(async () => { - const buffer = TestUtils.getFixtureBlob("trace_WindowManager.pb"); + const buffer = UnitTestUtils.getFixtureBlob("trace_WindowManager.pb"); const parsers = await new ParserFactory().createParsers([buffer]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/parsers/parser_input_method_clients.spec.ts b/tools/winscope-ng/src/parsers/parser_input_method_clients.spec.ts index 8ad3f785b..6f391cbe1 100644 --- a/tools/winscope-ng/src/parsers/parser_input_method_clients.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_input_method_clients.spec.ts @@ -17,13 +17,13 @@ import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; import {ParserFactory} from "./parser_factory"; import {Parser} from "./parser"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; describe("ParserInputMethodlClients", () => { let parser: Parser; beforeAll(async () => { - const buffer = TestUtils.getFixtureBlob("trace_InputMethodClients.pb"); + const buffer = UnitTestUtils.getFixtureBlob("trace_InputMethodClients.pb"); const parsers = await new ParserFactory().createParsers([buffer]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/parsers/parser_input_method_manager_service.spec.ts b/tools/winscope-ng/src/parsers/parser_input_method_manager_service.spec.ts index 293af4604..455db1e2d 100644 --- a/tools/winscope-ng/src/parsers/parser_input_method_manager_service.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_input_method_manager_service.spec.ts @@ -17,13 +17,13 @@ import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; import {ParserFactory} from "./parser_factory"; import {Parser} from "./parser"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; describe("ParserInputMethodManagerService", () => { let parser: Parser; beforeAll(async () => { - const buffer = TestUtils.getFixtureBlob("trace_InputMethodManagerService.pb"); + const buffer = UnitTestUtils.getFixtureBlob("trace_InputMethodManagerService.pb"); const parsers = await new ParserFactory().createParsers([buffer]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/parsers/parser_input_method_service.spec.ts b/tools/winscope-ng/src/parsers/parser_input_method_service.spec.ts index f3c2f1995..1da9481b5 100644 --- a/tools/winscope-ng/src/parsers/parser_input_method_service.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_input_method_service.spec.ts @@ -17,13 +17,13 @@ import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; import {ParserFactory} from "./parser_factory"; import {Parser} from "./parser"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; describe("ParserInputMethodService", () => { let parser: Parser; beforeAll(async () => { - const buffer = TestUtils.getFixtureBlob("trace_InputMethodService.pb"); + const buffer = UnitTestUtils.getFixtureBlob("trace_InputMethodService.pb"); const parsers = await new ParserFactory().createParsers([buffer]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/parsers/parser_protolog.spec.ts b/tools/winscope-ng/src/parsers/parser_protolog.spec.ts index eb5f40d4d..ab73af269 100644 --- a/tools/winscope-ng/src/parsers/parser_protolog.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_protolog.spec.ts @@ -17,7 +17,7 @@ import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; import {ParserFactory} from "./parser_factory"; import {Parser} from "./parser"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; import {LogMessage} from "../common/trace/protolog"; describe("ParserProtoLog", () => { @@ -33,7 +33,7 @@ describe("ParserProtoLog", () => { }; beforeAll(async () => { - const buffer = TestUtils.getFixtureBlob("trace_ProtoLog.pb"); + const buffer = UnitTestUtils.getFixtureBlob("trace_ProtoLog.pb"); const parsers = await new ParserFactory().createParsers([buffer]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/parsers/parser_screen_recording.spec.ts b/tools/winscope-ng/src/parsers/parser_screen_recording.spec.ts index 95620bdea..211fa5371 100644 --- a/tools/winscope-ng/src/parsers/parser_screen_recording.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_screen_recording.spec.ts @@ -16,7 +16,7 @@ import {ScreenRecordingTraceEntry} from "common/trace/screen_recording"; import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; import {Parser} from "./parser"; import {ParserFactory} from "./parser_factory"; @@ -24,7 +24,7 @@ describe("ParserScreenRecording", () => { let parser: Parser; beforeAll(async () => { - const trace = TestUtils.getFixtureBlob("screen_recording.mp4"); + const trace = UnitTestUtils.getFixtureBlob("screen_recording.mp4"); const parsers = await new ParserFactory().createParsers([trace]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/parsers/parser_screen_recording_legacy.spec.ts b/tools/winscope-ng/src/parsers/parser_screen_recording_legacy.spec.ts index 648ce05de..a5175a16f 100644 --- a/tools/winscope-ng/src/parsers/parser_screen_recording_legacy.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_screen_recording_legacy.spec.ts @@ -16,7 +16,7 @@ import {ScreenRecordingTraceEntry} from "common/trace/screen_recording"; import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; import {Parser} from "./parser"; import {ParserFactory} from "./parser_factory"; @@ -24,7 +24,7 @@ describe("ParserScreenRecordingLegacy", () => { let parser: Parser; beforeAll(async () => { - const trace = TestUtils.getFixtureBlob("screen_recording_legacy.mp4"); + const trace = UnitTestUtils.getFixtureBlob("screen_recording_legacy.mp4"); const parsers = await new ParserFactory().createParsers([trace]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/parsers/parser_surface_flinger.spec.ts b/tools/winscope-ng/src/parsers/parser_surface_flinger.spec.ts index 17a2894d1..467018e96 100644 --- a/tools/winscope-ng/src/parsers/parser_surface_flinger.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_surface_flinger.spec.ts @@ -16,7 +16,7 @@ import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; import {LayerTraceEntry} from "common/trace/flickerlib/layers/LayerTraceEntry"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; import {Parser} from "./parser"; import {ParserFactory} from "./parser_factory"; @@ -24,7 +24,7 @@ describe("ParserSurfaceFlinger", () => { let parser: Parser; beforeAll(async () => { - const buffer = TestUtils.getFixtureBlob("trace_SurfaceFlinger.pb"); + const buffer = UnitTestUtils.getFixtureBlob("trace_SurfaceFlinger.pb"); const parsers = await new ParserFactory().createParsers([buffer]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/parsers/parser_surface_flinger_dump.spec.ts b/tools/winscope-ng/src/parsers/parser_surface_flinger_dump.spec.ts index f6b79faff..508ae9c17 100644 --- a/tools/winscope-ng/src/parsers/parser_surface_flinger_dump.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_surface_flinger_dump.spec.ts @@ -16,7 +16,7 @@ import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; import {LayerTraceEntry} from "common/trace/flickerlib/layers/LayerTraceEntry"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; import {Parser} from "./parser"; import {ParserFactory} from "./parser_factory"; @@ -24,7 +24,7 @@ describe("ParserSurfaceFlingerDump", () => { let parser: Parser; beforeAll(async () => { - const buffer = TestUtils.getFixtureBlob("dump_SurfaceFlinger.pb"); + const buffer = UnitTestUtils.getFixtureBlob("dump_SurfaceFlinger.pb"); const parsers = await new ParserFactory().createParsers([buffer]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/parsers/parser_transactions.spec.ts b/tools/winscope-ng/src/parsers/parser_transactions.spec.ts index cd6f4cadb..dce33ad7c 100644 --- a/tools/winscope-ng/src/parsers/parser_transactions.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_transactions.spec.ts @@ -17,13 +17,13 @@ import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; import {Parser} from "./parser"; import {ParserFactory} from "./parser_factory"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; describe("ParserTransactions", () => { let parser: Parser; beforeAll(async () => { - const buffer = TestUtils.getFixtureBlob("trace_Transactions.pb"); + const buffer = UnitTestUtils.getFixtureBlob("trace_Transactions.pb"); const parsers = await new ParserFactory().createParsers([buffer]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/parsers/parser_window_manager.spec.ts b/tools/winscope-ng/src/parsers/parser_window_manager.spec.ts index 5409f3a56..ca1a816b0 100644 --- a/tools/winscope-ng/src/parsers/parser_window_manager.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_window_manager.spec.ts @@ -18,13 +18,13 @@ import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; import {ParserFactory} from "./parser_factory"; import {Parser} from "./parser"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; describe("ParserWindowManager", () => { let parser: Parser; beforeAll(async () => { - const buffer = TestUtils.getFixtureBlob("trace_WindowManager.pb"); + const buffer = UnitTestUtils.getFixtureBlob("trace_WindowManager.pb"); const parsers = await new ParserFactory().createParsers([buffer]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/parsers/parser_window_manager_dump.spec.ts b/tools/winscope-ng/src/parsers/parser_window_manager_dump.spec.ts index 9e05202b8..5c8cd6fcf 100644 --- a/tools/winscope-ng/src/parsers/parser_window_manager_dump.spec.ts +++ b/tools/winscope-ng/src/parsers/parser_window_manager_dump.spec.ts @@ -18,13 +18,13 @@ import {Timestamp, TimestampType} from "common/trace/timestamp"; import {TraceType} from "common/trace/trace_type"; import {ParserFactory} from "./parser_factory"; import {Parser} from "./parser"; -import {TestUtils} from "test/test_utils"; +import {UnitTestUtils} from "test/unit/utils"; describe("ParserWindowManagerDump", () => { let parser: Parser; beforeAll(async () => { - const buffer = TestUtils.getFixtureBlob("dump_WindowManager.pb"); + const buffer = UnitTestUtils.getFixtureBlob("dump_WindowManager.pb"); const parsers = await new ParserFactory().createParsers([buffer]); expect(parsers.length).toEqual(1); parser = parsers[0]; diff --git a/tools/winscope-ng/src/test/blob.ts b/tools/winscope-ng/src/test/common/blob.ts similarity index 100% rename from tools/winscope-ng/src/test/blob.ts rename to tools/winscope-ng/src/test/common/blob.ts diff --git a/tools/winscope-ng/src/test/test_utils.ts b/tools/winscope-ng/src/test/common/utils.ts similarity index 69% rename from tools/winscope-ng/src/test/test_utils.ts rename to tools/winscope-ng/src/test/common/utils.ts index dcb05487e..92d06e4cf 100644 --- a/tools/winscope-ng/src/test/test_utils.ts +++ b/tools/winscope-ng/src/test/common/utils.ts @@ -17,27 +17,27 @@ import * as fs from "fs"; import * as path from "path"; import {Blob} from "./blob"; -class TestUtils { +class CommonTestUtils { static getFixtureBlob(filename: string): Blob { - const buffer = TestUtils.loadFixture(filename); + const buffer = CommonTestUtils.loadFixture(filename); return new Blob(buffer); } static loadFixture(filename: string): ArrayBuffer { - return fs.readFileSync(TestUtils.getFixturePath(filename)); + return fs.readFileSync(CommonTestUtils.getFixturePath(filename)); } static getFixturePath(filename: string): string { - return path.join(TestUtils.getProjectRootPath(), "src/test/fixtures", filename); - } - - static getProductionIndexHtmlPath(): string { - return path.join(TestUtils.getProjectRootPath(), "dist/prod/index.html"); + return path.join(CommonTestUtils.getProjectRootPath(), "src/test/fixtures", filename); } static getProjectRootPath(): string { - return path.join(__dirname, "../.."); + let root = __dirname; + while (path.basename(root) !== "winscope-ng") { + root = path.dirname(root); + } + return root; } } -export {TestUtils}; +export {CommonTestUtils}; diff --git a/tools/winscope-ng/src/test/e2e/utils.ts b/tools/winscope-ng/src/test/e2e/utils.ts new file mode 100644 index 000000000..a286b7db6 --- /dev/null +++ b/tools/winscope-ng/src/test/e2e/utils.ts @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * 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 * as path from "path"; +import {CommonTestUtils} from "../common/utils"; + +class E2eTestUtils extends CommonTestUtils { + static getProductionIndexHtmlPath(): string { + return path.join(CommonTestUtils.getProjectRootPath(), "dist/prod/index.html"); + } +} + +export {E2eTestUtils}; diff --git a/tools/winscope-ng/src/test/e2e/viewer_window_manager.spec.ts b/tools/winscope-ng/src/test/e2e/viewer_window_manager.spec.ts index 0b8da4979..f3f4384d8 100644 --- a/tools/winscope-ng/src/test/e2e/viewer_window_manager.spec.ts +++ b/tools/winscope-ng/src/test/e2e/viewer_window_manager.spec.ts @@ -14,17 +14,17 @@ * limitations under the License. */ import {browser, element, by} from "protractor"; -import {TestUtils} from "../test_utils"; +import {E2eTestUtils} from "./utils"; describe("Viewer WindowManager", () => { beforeAll(async () => { browser.manage().timeouts().implicitlyWait(1000); - browser.get("file://" + TestUtils.getProductionIndexHtmlPath()); + browser.get("file://" + E2eTestUtils.getProductionIndexHtmlPath()); }), it("processes trace and renders view", () => { const inputFile = element(by.css("input[type=\"file\"]")); - inputFile.sendKeys(TestUtils.getFixturePath("trace_WindowManager.pb")); + inputFile.sendKeys(E2eTestUtils.getFixturePath("trace_WindowManager.pb")); const windowManagerViewerTitle = element(by.css(".viewer-window-manager .title")); expect(windowManagerViewerTitle.getText()).toContain("Window Manager"); diff --git a/tools/winscope-ng/src/test/e2e/winscope.spec.ts b/tools/winscope-ng/src/test/e2e/winscope.spec.ts index 5cb0d605d..ac7032ad1 100644 --- a/tools/winscope-ng/src/test/e2e/winscope.spec.ts +++ b/tools/winscope-ng/src/test/e2e/winscope.spec.ts @@ -14,11 +14,11 @@ * limitations under the License. */ import {browser, element, by} from "protractor"; -import {TestUtils} from "../test_utils"; +import {E2eTestUtils} from "./utils"; describe("winscope", () => { beforeAll(() => { - browser.get("file://" + TestUtils.getProductionIndexHtmlPath()); + browser.get("file://" + E2eTestUtils.getProductionIndexHtmlPath()); }), it("has title", () => { diff --git a/tools/winscope-ng/src/test/unit/utils.ts b/tools/winscope-ng/src/test/unit/utils.ts new file mode 100644 index 000000000..7d42d0d67 --- /dev/null +++ b/tools/winscope-ng/src/test/unit/utils.ts @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * 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 {Parser} from "parsers/parser"; +import {ParserFactory} from "parsers/parser_factory"; +import {CommonTestUtils} from "test/common/utils"; + +class UnitTestUtils extends CommonTestUtils { + static async getParser(filename: string): Promise { + const trace = CommonTestUtils.getFixtureBlob(filename); + const parsers = await new ParserFactory().createParsers([trace]); + expect(parsers.length).toEqual(1); + return parsers[0]; + } +} + +export {UnitTestUtils};