<!DOCTYPE HTML>
<html>
<!--
Copyright (c) 2012 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<head>
<title>TimelineSliceGroup tests</title>
<script src="base.js"></script>
<script>
  base.require('unittest');
  base.require('test_utils');
  base.require('timeline_slice_group');
</script>
</head>
<body>
<script>
  'use strict';

  var TimelineSlice = tracing.TimelineSlice;
  var TimelineSliceGroup = tracing.TimelineSliceGroup;
  var newSlice = test_utils.newSlice;
  var newSliceNamed = test_utils.newSliceNamed;

  function testBasicBeginEnd() {
    var group = new TimelineSliceGroup();
    assertEquals(group.openSliceCount, 0);
    var sliceA = group.beginSlice('', 'a', 1, {a: 1});
    assertEquals(1, group.openSliceCount);
    assertEquals('a', sliceA.title);
    assertEquals(1, sliceA.start);
    assertEquals(1, sliceA.args.a);

    var sliceB = group.endSlice(3);
    assertEquals(sliceA, sliceB);
    assertEquals(2, sliceB.duration);
  }

  function testNestedBeginEnd() {
    var group = new TimelineSliceGroup();
    assertEquals(group.openSliceCount, 0);
    group.beginSlice('', 'a', 1);
    group.beginSlice('', 'b', 2);
    group.endSlice(2.5);
    group.endSlice(3);

    assertEquals(2, group.slices.length);
    assertEquals('b', group.slices[0].title);
    assertEquals(0.5, group.slices[0].duration);

    assertEquals('a', group.slices[1].title);
    assertEquals(2, group.slices[1].duration);
  }

  function testBounds() {
    var group = new TimelineSliceGroup();
    group.updateBounds();
    assertEquals(group.minTimestamp, undefined);
    assertEquals(group.maxTimestamp, undefined);

    group.pushSlice(newSlice(1, 3));
    group.pushSlice(newSlice(7, 2));
    group.updateBounds();
    assertEquals(1, group.minTimestamp);
    assertEquals(9, group.maxTimestamp);
  }

  function testBoundsWithPartial() {
    var group = new TimelineSliceGroup();
    group.beginSlice('', 'a', 7);
    group.updateBounds();
    assertEquals(7, group.minTimestamp);
    assertEquals(7, group.maxTimestamp);
  }

  function testBoundsWithTwoPartials() {
    var group = new TimelineSliceGroup();
    group.beginSlice('', 'a', 0);
    group.beginSlice('', 'a', 1);
    group.updateBounds();
    assertEquals(0, group.minTimestamp);
    assertEquals(1, group.maxTimestamp);
  }

  function testBoundsWithBothPartialAndRegular() {
    var group = new TimelineSliceGroup();
    group.updateBounds();
    assertEquals(undefined, group.minTimestamp);
    assertEquals(undefined, group.maxTimestamp);

    group.pushSlice(newSlice(1, 3));
    group.beginSlice('', 'a', 7);
    group.updateBounds();
    assertEquals(1, group.minTimestamp);
    assertEquals(7, group.maxTimestamp);
  }

  function testAutocloserBasic() {
    var group = new TimelineSliceGroup();
    assertEquals(group.openSliceCount, 0);

    group.pushSlice(newSliceNamed('a', 1, 0.5));

    group.beginSlice('', 'b', 2);
    group.beginSlice('', 'c', 2.5);
    group.endSlice(3);

    group.autoCloseOpenSlices();
    group.updateBounds();

    assertEquals(1, group.minTimestamp);
    assertEquals(3, group.maxTimestamp);
    assertEquals(3, group.slices.length);
    assertEquals('a', group.slices[0].title);
    assertEquals('c', group.slices[1].title);
    assertEquals('b', group.slices[2].title);
    assertTrue(group.slices[2].didNotFinish);
    assertEquals(1, group.slices[2].duration);
  }

  function testAutocloserWithSubTasks() {
    var group = new TimelineSliceGroup();
    assertEquals(group.openSliceCount, 0);

    group.beginSlice('', 'a', 1);
    group.beginSlice('', 'b1', 2);
    group.endSlice(3);
    group.beginSlice('', 'b2', 3);

    group.autoCloseOpenSlices();
    assertEquals(3, group.slices.length);
    assertEquals('b1', group.slices[0].title);

    assertEquals('b2', group.slices[1].title);
    assertTrue(group.slices[1].didNotFinish);
    assertEquals(0, group.slices[1].duration);

    assertEquals('a', group.slices[2].title);
    assertTrue(group.slices[2].didNotFinish);
    assertEquals(2, group.slices[2].duration);
  }

  // TODO: test cretion of subSlices
</script>
</body>
</html>