1import { decodeAndTransformProto, FILE_TYPES, FILE_DECODERS } from '../src/decode';
2import Tag from '../src/flickerlib/tags/Tag';
3import Error from '../src/flickerlib/errors/Error';
4import { TaggingEngine } from '../src/flickerlib/common.js';
5import fs from 'fs';
6import path from 'path';
7
8const tagTrace = '../spec/traces/tag_trace.winscope';
9const errorTrace = '../spec/traces/error_trace.winscope';
10
11describe("Tag Transformation", () => {
12  it("can transform tag traces", () => {
13    const buffer = new Uint8Array(fs.readFileSync(path.resolve(__dirname, tagTrace)));
14
15    const data = decodeAndTransformProto(buffer, FILE_DECODERS[FILE_TYPES.TAG_TRACE].decoderParams, true);
16
17    expect(data.entries[0].timestamp.toString()).toEqual('159979677861');
18    expect(data.entries[1].timestamp.toString()).toEqual('161268519083');
19    expect(data.entries[2].timestamp.toString()).toEqual('161126718496');
20    expect(data.entries[3].timestamp.toString()).toEqual('161613497398');
21    expect(data.entries[4].timestamp.toString()).toEqual('161227062777');
22    expect(data.entries[5].timestamp.toString()).toEqual('161268519083');
23    expect(data.entries[6].timestamp.toString()).toEqual('161825945076');
24    expect(data.entries[7].timestamp.toString()).toEqual('162261072567');
25
26    expect(data.entries[0].tags).toEqual([new Tag(12345,"PIP_ENTER",true,1,"",0)]);
27    expect(data.entries[1].tags).toEqual([new Tag(12345,"PIP_ENTER",false,2,"",2)]);
28    expect(data.entries[2].tags).toEqual([new Tag(67890,"ROTATION",true,3,"",3)]);
29    expect(data.entries[3].tags).toEqual([new Tag(67890,"ROTATION",false,4,"",4)]);
30    expect(data.entries[4].tags).toEqual([new Tag(9876,"PIP_EXIT",true,5,"",5)]);
31    expect(data.entries[5].tags).toEqual([new Tag(9876,"PIP_EXIT",false,6,"",6)]);
32    expect(data.entries[6].tags).toEqual([new Tag(54321,"IME_APPEAR",true,7,"",7)]);
33    expect(data.entries[7].tags).toEqual([new Tag(54321,"IME_APPEAR",false,8,"",8)]);
34  })
35});
36
37describe("Detect Tag", () => {
38  it("can detect tags", () => {
39    const wmFile = '../spec/traces/regular_rotation_in_last_state_wm_trace.winscope'
40    const layersFile = '../spec/traces/regular_rotation_in_last_state_layers_trace.winscope'
41    const wmBuffer = new Uint8Array(fs.readFileSync(path.resolve(__dirname, wmFile)));
42    const layersBuffer = new Uint8Array(fs.readFileSync(path.resolve(__dirname, layersFile)));
43
44    const wmTrace = decodeAndTransformProto(wmBuffer, FILE_DECODERS[FILE_TYPES.WINDOW_MANAGER_TRACE].decoderParams, true);
45    const layersTrace = decodeAndTransformProto(layersBuffer, FILE_DECODERS[FILE_TYPES.SURFACE_FLINGER_TRACE].decoderParams, true);
46
47    const engine = new TaggingEngine(wmTrace, layersTrace, (text) => { console.log(text) });
48    const tagTrace = engine.run();
49    expect(tagTrace.size).toEqual(4);
50    expect(tagTrace.entries[0].timestamp.toString()).toEqual('280186737540384');
51    expect(tagTrace.entries[1].timestamp.toString()).toEqual('280187243649340');
52    expect(tagTrace.entries[2].timestamp.toString()).toEqual('280188522078113');
53    expect(tagTrace.entries[3].timestamp.toString()).toEqual('280189020672174');
54  })
55});
56
57describe("Error Transformation", () => {
58  it("can transform error traces", () => {
59    const buffer = new Uint8Array(fs.readFileSync(path.resolve(__dirname, errorTrace)));
60
61    const data = decodeAndTransformProto(buffer, FILE_DECODERS[FILE_TYPES.ERROR_TRACE].decoderParams, true);
62
63    expect(data.entries[0].timestamp.toString()).toEqual('161401263106');
64    expect(data.entries[1].timestamp.toString()).toEqual('161126718496');
65    expect(data.entries[2].timestamp.toString()).toEqual('162261072567');
66
67    expect(data.entries[0].errors).toEqual([new Error("","",33,"",33)]);
68    expect(data.entries[1].errors).toEqual([new Error("","",66,"",66)]);
69    expect(data.entries[2].errors).toEqual([new Error("","",99,"",99)]);
70  })
71});
72