<!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>TimelineFindControl tests</title>
<style>
  .timeline-view {
    border: 1px solid black;
    margin: 10px;
    height: 350px;
  }
  .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_find_control');
</script>
</head>
<body>
<script>
    'use strict';

    var newSliceNamed = test_utils.newSliceNamed;

    /*
     * Just enough of the Timeline to support the tests below.
     */
    var FakeTimeline = base.ui.define('div');

    FakeTimeline.prototype = {
      __proto__: HTMLDivElement.prototype,

      decorate: function() {
        this.addAllObjectsMatchingFilterToSelectionReturnValue = [];

        this.selection = new tracing.TimelineSelection();
        this.keyHelp = '<keyHelp>';

        // Put some simple UI in for testing purposes.
        var noteEl = document.createElement('div');
        noteEl.textContent = 'FakeTimeline:';
        this.appendChild(noteEl);

        this.statusEl_ = document.createElement('div');
        this.appendChild(this.statusEl_);
        this.refresh_();
      },

      refresh_: function() {
        var status;
        if (this.model)
          status = 'model=set';
        else
          status = 'model=undefined';
        this.statusEl_.textContent = status;
      },

      setSelectionAndMakeVisible: function(selection, zoomAllowed) {
        this.selection = selection;
      },

      addAllObjectsMatchingFilterToSelection: function(filter, selection) {
        var n = this.addAllObjectsMatchingFilterToSelectionReturnValue.length;
        for (var i = 0; i < n; i++)
          selection.push_(
              this.addAllObjectsMatchingFilterToSelectionReturnValue[i]);
      }
    };

    /*
     * This test just instantiates a FindDialog and adds it to the DOM
     * to help with non-unittest UI work.
     */
    function testInstantiateTimelineFindControl() {
      var ctl = new tracing.TimelineFindControl();
      var didFindPrevious = false;
      var didFindNext = false;
      ctl.controller = {
        findNext: function() {
          didFindNext = true;
        },

        findPrevious: function() {
          didFindPrevious = true;
        },

        filterHits: [],

        currentHitIndex: 0
      };
      this.addHTMLOutput(undefined, ctl);
      ctl.querySelector('input').focus();
      ctl.querySelector('input').blur();

      ctl.querySelector('.find-previous').click();
      assertTrue(didFindPrevious);
      ctl.querySelector('.find-next').click();
      assertTrue(didFindNext);
    }

    function testFindControllerNoTimeline() {
      var controller = new tracing.TimelineFindController();
      controller.findNext();
      controller.findPrevious();
    }

    function testFindControllerEmptyHit() {
      var timeline = new FakeTimeline();
      var controller = new tracing.TimelineFindController();
      controller.timeline = timeline;

      timeline.selection = new tracing.TimelineSelection();
      controller.findNext();
      assertArrayShallowEquals([], timeline.selection);
      controller.findPrevious();
      assertArrayShallowEquals([], timeline.selection);
    }

    function testFindControllerOneHit() {
      var timeline = new FakeTimeline();
      var controller = new tracing.TimelineFindController();
      controller.timeline = timeline;

      timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1];
      controller.findNext();
      assertArrayShallowEquals([1], timeline.selection);
      controller.findNext();
      assertArrayShallowEquals([1], timeline.selection);
      controller.findPrevious();
      assertArrayShallowEquals([1], timeline.selection);
    }

    function testFindControllerMultipleHits() {
      var timeline = new FakeTimeline();
      var controller = new tracing.TimelineFindController();
      controller.timeline = timeline;

      timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];

      // Loop through hits then when we wrap, try moving backward.
      controller.findNext();
      assertArrayShallowEquals([1], timeline.selection);
      controller.findNext();
      assertArrayShallowEquals([2], timeline.selection);
      controller.findNext();
      assertArrayShallowEquals([3], timeline.selection);
      controller.findNext();
      assertArrayShallowEquals([1], timeline.selection);
      controller.findPrevious();
      assertArrayShallowEquals([3], timeline.selection);
      controller.findPrevious();
      assertArrayShallowEquals([2], timeline.selection);
    }

    function testFindControllerChangeFilterAfterNext() {
      var timeline = new FakeTimeline();
      var controller = new tracing.TimelineFindController();
      controller.timeline = timeline;

      timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];

      // Loop through hits then when we wrap, try moving backward.
      controller.findNext();
      timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [4];
      controller.filterText = 'asdfsf';
      controller.findNext();
      assertArrayShallowEquals([4], timeline.selection);
    }

    function testFindControllerSelectsFirstItemImmediately() {
      var timeline = new FakeTimeline();
      var controller = new tracing.TimelineFindController();
      controller.timeline = timeline;
      timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];
      controller.filterText = 'asdfsf';
      assertArrayShallowEquals([1], timeline.selection);
      controller.findNext();
      assertArrayShallowEquals([2], timeline.selection);
    }

    function testFindControllerWithRealTimeline() {
      var model = new tracing.TimelineModel();
      var p1 = model.getOrCreateProcess(1);
      var t1 = p1.getOrCreateThread(1);
      t1.pushSlice(new tracing.TimelineThreadSlice('', 'a', 0, 1, {}, 3));

      var timeline = new tracing.Timeline();
      timeline.model = model;

      var controller = new tracing.TimelineFindController();
      controller.timeline = timeline;

      // Test find with no filterText.
      controller.findNext();

      // Test find with filter txt.
      controller.filterText = 'a';
      controller.findNext();
      assertEquals(1, timeline.selection.length);
      assertEquals(t1.slices[0], timeline.selection[0].slice);

      controller.filterText = 'xxx';
      controller.findNext();
      assertEquals(0, timeline.selection.length);
      controller.findNext();
      assertEquals(0, timeline.selection.length);
    }
</script>
</body>
</html>