/*
 * 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 {assertDefined} from 'common/assert_utils';
import {TimestampType} from 'common/time';
import {NO_TIMEZONE_OFFSET_FACTORY} from 'common/timestamp_factory';
import {UnitTestUtils} from 'test/unit/utils';
import {Parser} from 'trace/parser';
import {TraceType} from 'trace/trace_type';
import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';

describe('ParserInputMethodService', () => {
  describe('trace with elapsed + real timestamp', () => {
    let parser: Parser<HierarchyTreeNode>;

    beforeAll(async () => {
      jasmine.addCustomEqualityTester(UnitTestUtils.timestampEqualityTester);
      parser = (await UnitTestUtils.getParser(
        'traces/elapsed_and_real_timestamp/InputMethodService.pb',
      )) as Parser<HierarchyTreeNode>;
    });

    it('has expected trace type', () => {
      expect(parser.getTraceType()).toEqual(TraceType.INPUT_METHOD_SERVICE);
    });

    it('provides elapsed timestamps', () => {
      const expected = [
        NO_TIMEZONE_OFFSET_FACTORY.makeElapsedTimestamp(16578752896n),
      ];
      expect(parser.getTimestamps(TimestampType.ELAPSED)).toEqual(expected);
    });

    it('provides real timestamps', () => {
      const expected = [
        NO_TIMEZONE_OFFSET_FACTORY.makeRealTimestamp(1659107091180519857n),
      ];
      expect(parser.getTimestamps(TimestampType.REAL)).toEqual(expected);
    });

    it('retrieves trace entry', async () => {
      const entry = await parser.getEntry(0, TimestampType.REAL);
      expect(entry).toBeInstanceOf(HierarchyTreeNode);
      expect(entry.id).toEqual('InputMethodService entry');
    });

    it('applies timezone info to real timestamps only', async () => {
      const parserWithTimezoneInfo = (await UnitTestUtils.getParser(
        'traces/elapsed_and_real_timestamp/InputMethodService.pb',
        true,
      )) as Parser<HierarchyTreeNode>;
      expect(parserWithTimezoneInfo.getTraceType()).toEqual(
        TraceType.INPUT_METHOD_SERVICE,
      );

      expect(
        parserWithTimezoneInfo.getTimestamps(TimestampType.ELAPSED),
      ).toEqual([
        NO_TIMEZONE_OFFSET_FACTORY.makeElapsedTimestamp(16578752896n),
      ]);

      expect(parserWithTimezoneInfo.getTimestamps(TimestampType.REAL)).toEqual([
        NO_TIMEZONE_OFFSET_FACTORY.makeRealTimestamp(1659126891180519857n),
      ]);
    });
  });

  describe('trace with elapsed (only) timestamp', () => {
    let parser: Parser<HierarchyTreeNode>;

    beforeAll(async () => {
      parser = (await UnitTestUtils.getParser(
        'traces/elapsed_timestamp/InputMethodService.pb',
      )) as Parser<HierarchyTreeNode>;
    });

    it('has expected trace type', () => {
      expect(parser.getTraceType()).toEqual(TraceType.INPUT_METHOD_SERVICE);
    });

    it('provides elapsed timestamps', () => {
      expect(
        assertDefined(parser.getTimestamps(TimestampType.ELAPSED))[0],
      ).toEqual(
        NO_TIMEZONE_OFFSET_FACTORY.makeElapsedTimestamp(1149230019887n),
      );
    });

    it("doesn't provide real timestamps", () => {
      expect(parser.getTimestamps(TimestampType.REAL)).toEqual(undefined);
    });

    it('retrieves trace entry', async () => {
      const entry = await parser.getEntry(0, TimestampType.ELAPSED);
      expect(entry).toBeInstanceOf(HierarchyTreeNode);
      expect(entry.id).toEqual('InputMethodService entry');
    });

    it('does not apply timezone info to elapsed timestamps', async () => {
      const parserWithTimezoneInfo = (await UnitTestUtils.getParser(
        'traces/elapsed_timestamp/InputMethodService.pb',
        true,
      )) as Parser<HierarchyTreeNode>;
      expect(parserWithTimezoneInfo.getTraceType()).toEqual(
        TraceType.INPUT_METHOD_SERVICE,
      );

      expect(
        assertDefined(
          parserWithTimezoneInfo.getTimestamps(TimestampType.ELAPSED),
        )[0],
      ).toEqual(
        NO_TIMEZONE_OFFSET_FACTORY.makeElapsedTimestamp(1149230019887n),
      );
    });
  });
});
