<!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>TimelineModel tests</title>
<style>
  .timeline-view {
    border: 1px solid black;
    margin: 10px;
  }
  .timeline-find-dialog {
    border: 1px solid black;
    margin: 10px;
  }
</style>
<script src="base.js"></script>
<script>
  base.require('unittest');
  base.require('test_utils');
  base.require('timeline_model');
</script>
</head>
<body>
<script>
'use strict';

var TimelineSlice = tracing.TimelineSlice;
var TimelineThreadSlice = tracing.TimelineThreadSlice;
var TimelineThread = tracing.TimelineThread;
var TimelineModel = tracing.TimelineModel;
var TimelineTitleFilter = tracing.TimelineTitleFilter;

function createModelWithOneOfEverything() {
  var m = new TimelineModel();
  var cpu = m.getOrCreateCpu(1);
  cpu.slices.push(test_utils.newSlice(1, 3));

  var p = m.getOrCreateProcess(1);
  var t = p.getOrCreateThread(1);
  t.pushSlice(new TimelineThreadSlice('', 'a', 0, 1, {}, 4));
  t.asyncSlices.push(test_utils.newAsyncSlice(0, 1, t, t));

  var c = p.getOrCreateCounter('', 'ProcessCounter');
  c.seriesNames.push('a', 'b');
  c.seriesColors.push(0, 1);
  c.timestamps.push(0, 1, 2, 3);
  c.samples.push(5, 10, 6, 15, 5, 12, 7, 16);

  var c1 = cpu.getOrCreateCounter('', 'CpuCounter');
  c1.seriesNames.push('a', 'b');
  c1.seriesColors.push(0, 1);
  c1.timestamps.push(0, 1, 2, 3);
  c1.samples.push(5, 10, 6, 15, 5, 12, 7, 16);

  m.updateBounds();

  return m;
}

function testModelBounds_EmptyModel() {
  var m = new TimelineModel();
  m.updateBounds();
  assertEquals(undefined, m.minTimestamp);
  assertEquals(undefined, m.maxTimestamp);
}

function testModelBounds_OneEmptyThread() {
  var m = new TimelineModel();
  var t = m.getOrCreateProcess(1).getOrCreateThread(1);
  m.updateBounds();
  assertEquals(undefined, m.minTimestamp);
  assertEquals(undefined, m.maxTimestamp);
}

function testModelBounds_OneThread() {
  var m = new TimelineModel();
  var t = m.getOrCreateProcess(1).getOrCreateThread(1);
  t.pushSlice(new TimelineThreadSlice('', 'a', 0, 1, {}, 3));
  m.updateBounds();
  assertEquals(1, m.minTimestamp);
  assertEquals(4, m.maxTimestamp);
}

function testModelBounds_OneThreadAndOneEmptyThread() {
  var m = new TimelineModel();
  var t1 = m.getOrCreateProcess(1).getOrCreateThread(1);
  t1.pushSlice(new TimelineThreadSlice('', 'a', 0, 1, {}, 3));
  var t2 = m.getOrCreateProcess(1).getOrCreateThread(1);
  m.updateBounds();
  assertEquals(1, m.minTimestamp);
  assertEquals(4, m.maxTimestamp);
}

function testModelBounds_OneCpu() {
  var m = new TimelineModel();
  var cpu = m.getOrCreateCpu(1);
  cpu.slices.push(test_utils.newSlice(1, 3));
  m.updateBounds();
  assertEquals(1, m.minTimestamp);
  assertEquals(4, m.maxTimestamp);
}


function testModelBounds_OneCpuOneThread() {
  var m = new TimelineModel();
  var cpu = m.getOrCreateCpu(1);
  cpu.slices.push(test_utils.newSlice(1, 3));

  var t = m.getOrCreateProcess(1).getOrCreateThread(1);
  t.pushSlice(new TimelineThreadSlice('', 'a', 0, 1, {}, 4));

  m.updateBounds();
  assertEquals(1, m.minTimestamp);
  assertEquals(5, m.maxTimestamp);
}

function testModelCanImportEmpty() {
  var m;
  m = new TimelineModel([]);
  m = new TimelineModel('');
}

function testTimelineTitleFilter() {
  var s0 = test_utils.newSlice(1, 3);
  assertFalse(new TimelineTitleFilter('').matchSlice(s0));

  assertTrue(new TimelineTitleFilter('a').matchSlice(s0));
  assertFalse(new TimelineTitleFilter('x').matchSlice(s0));

  var s1 = test_utils.newSliceNamed('ba', 1, 3);
  assertTrue(new TimelineTitleFilter('a').matchSlice(s1));
  assertTrue(new TimelineTitleFilter('ba').matchSlice(s1));
  assertFalse(new TimelineTitleFilter('x').matchSlice(s1));
}

function testTimelineModel_toJSON() {
  var m = createModelWithOneOfEverything();
  assertNotNull(JSON.stringify(m));
}

function testTimelineModel_findAllThreadsNamed() {
  var m = new TimelineModel();
  var t = m.getOrCreateProcess(1).getOrCreateThread(1);
  t.name = 'CrBrowserMain';

  m.updateBounds();
  var f = m.findAllThreadsNamed('CrBrowserMain');
  assertArrayEquals([t], f);
  f = m.findAllThreadsNamed('NoSuchThread');
  assertEquals(0, f.length);
}

function testTimelineModel_updateCategories() {
  var m = new TimelineModel();
  var t = m.getOrCreateProcess(1).getOrCreateThread(1);
  t.pushSlice(new TimelineThreadSlice('categoryA', 'a', 0, 1, {}, 3));
  t.pushSlice(new TimelineThreadSlice('categoryA', 'a', 0, 1, {}, 3));
  t.pushSlice(new TimelineThreadSlice('categoryB', 'a', 0, 1, {}, 3));
  t.pushSlice(new TimelineThreadSlice('categoryA', 'a', 0, 1, {}, 3));
  t.pushSlice(new TimelineThreadSlice('', 'a', 0, 1, {}, 3));
  m.updateCategories_();
  assertArrayEquals(['categoryA', 'categoryB'], m.categories);
}
</script>
</body>
</html>