ParserWindowManagerDump more robust to invalid input

ParserWindowManagerDump is prone to accepting invalid inputs because it lacks a magic number check.
This commit reduces changes of accepting invalid inputs by making sure that the parser is actually
able to create a trace entry from the decoded proto.

Test: npm run build:unit && npm run test:unit
Change-Id: I1aad12387ce064cde6e27e09cd6d329bb21f5066
diff --git a/tools/winscope-ng/src/parsers/parser_common.spec.ts b/tools/winscope-ng/src/parsers/parser_common.spec.ts
index 60d45f5..1ed8efb 100644
--- a/tools/winscope-ng/src/parsers/parser_common.spec.ts
+++ b/tools/winscope-ng/src/parsers/parser_common.spec.ts
@@ -15,9 +15,17 @@
  */
 import {Timestamp, TimestampType} from "common/trace/timestamp";
 import {Parser} from "./parser";
+import {CommonTestUtils} from "test/common/utils";
 import {UnitTestUtils} from "test/unit/utils";
+import {ParserFactory} from "./parser_factory";
 
 describe("Parser", () => {
+  it("is robust to empty trace file", async () => {
+    const trace = await CommonTestUtils.getFixtureFile("traces/empty.pb");
+    const [parsers, errors] = await new ParserFactory().createParsers([trace]);
+    expect(parsers.length).toEqual(0);
+  });
+
   describe("real timestamp", () => {
     let parser: Parser;
 
diff --git a/tools/winscope-ng/src/parsers/parser_window_manager_dump.ts b/tools/winscope-ng/src/parsers/parser_window_manager_dump.ts
index 0fc7d81..f929de2 100644
--- a/tools/winscope-ng/src/parsers/parser_window_manager_dump.ts
+++ b/tools/winscope-ng/src/parsers/parser_window_manager_dump.ts
@@ -33,7 +33,16 @@
   }
 
   override decodeTrace(buffer: Uint8Array): any[] {
-    return [WindowManagerServiceDumpProto.decode(buffer)];
+    const entryProto = WindowManagerServiceDumpProto.decode(buffer);
+
+    // This parser is prone to accepting invalid inputs because it lacks a magic
+    // number. Let's reduce the chances of accepting invalid inputs by making
+    // sure that a trace entry can actually be created from the decoded proto.
+    // If the trace entry creation fails, an exception is thrown and the parser
+    // will be considered unsuited for this input data.
+    this.processDecodedEntry(0, entryProto);
+
+    return [entryProto];
   }
 
   override getTimestamp(type: TimestampType, entryProto: any): undefined|Timestamp {
diff --git a/tools/winscope-ng/src/test/fixtures/traces/empty.pb b/tools/winscope-ng/src/test/fixtures/traces/empty.pb
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tools/winscope-ng/src/test/fixtures/traces/empty.pb