Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / find_control_test.js
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 'use strict';
6
7 tvcm.require('tracing.test_utils');
8 tvcm.require('tracing.find_control');
9
10 tvcm.unittest.testSuite('tracing.find_control_test', function() {
11   /*
12    * Just enough of the Timeline to support the tests below.
13    */
14   var FakeTimeline = tvcm.ui.define('div');
15
16   FakeTimeline.prototype = {
17     __proto__: HTMLDivElement.prototype,
18
19     decorate: function() {
20       this.addAllObjectsMatchingFilterToSelectionReturnValue = [];
21
22       this.selection = new tracing.Selection();
23       this.highlight = new tracing.Selection();
24       this.keyHelp = '<keyHelp>';
25
26       // Put some simple UI in for testing purposes.
27       var noteEl = document.createElement('div');
28       noteEl.textContent = 'FakeTimeline:';
29       this.appendChild(noteEl);
30
31       this.statusEl_ = document.createElement('div');
32       this.appendChild(this.statusEl_);
33       this.refresh_();
34     },
35
36     refresh_: function() {
37       var status;
38       if (this.model)
39         status = 'model=set';
40       else
41         status = 'model=undefined';
42       this.statusEl_.textContent = status;
43     },
44
45     addAllObjectsMatchingFilterToSelection: function(filter, selection) {
46       var n = this.addAllObjectsMatchingFilterToSelectionReturnValue.length;
47       for (var i = 0; i < n; i++) {
48         selection.push(
49             this.addAllObjectsMatchingFilterToSelectionReturnValue[i]);
50       }
51     },
52
53     setHighlightAndClearSelection: function(highlight) {
54       this.highlight = highlight;
55     }
56   };
57
58   test('instantiate', function() {
59     var ctl = new tracing.FindControl();
60     ctl.controller = {
61       findNext: function() {
62       },
63
64       findPrevious: function() {
65       },
66
67       reset: function() {},
68
69       filterHits: ['a', 'b'],
70
71       currentHitIndex: 0
72     };
73
74     this.addHTMLOutput(ctl);
75   });
76
77   test('findControllerNoTimeline', function() {
78     var controller = new tracing.FindController();
79     controller.findNext();
80     controller.findPrevious();
81   });
82
83   test('findControllerEmptyHit', function() {
84     var timeline = new FakeTimeline();
85     var controller = new tracing.FindController();
86     controller.timeline = timeline;
87
88     timeline.selection = new tracing.Selection();
89     timeline.highlight = new tracing.Selection();
90     controller.findNext();
91     assertArrayShallowEquals([], timeline.selection);
92     assertArrayShallowEquals([], timeline.highlight);
93     controller.findPrevious();
94     assertArrayShallowEquals([], timeline.selection);
95     assertArrayShallowEquals([], timeline.highlight);
96   });
97
98   test('findControllerOneHit', function() {
99     var timeline = new FakeTimeline();
100     var controller = new tracing.FindController();
101     controller.timeline = timeline;
102
103     timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1];
104     controller.filterText = 'asdf';
105
106     assertArrayShallowEquals([], timeline.selection);
107     assertArrayShallowEquals([1], timeline.highlight);
108     controller.findNext();
109     assertArrayShallowEquals([1], timeline.selection);
110     assertArrayShallowEquals([1], timeline.highlight);
111     controller.findNext();
112     assertArrayShallowEquals([1], timeline.selection);
113     assertArrayShallowEquals([1], timeline.highlight);
114     controller.findPrevious();
115     assertArrayShallowEquals([1], timeline.selection);
116     assertArrayShallowEquals([1], timeline.highlight);
117   });
118
119   test('findControllerMultipleHits', function() {
120     var timeline = new FakeTimeline();
121     var controller = new tracing.FindController();
122     controller.timeline = timeline;
123
124     timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];
125     controller.filterText = 'asdf';
126
127     // Loop through hits then when we wrap, try moving backward.
128     assertArrayShallowEquals([], timeline.selection);
129     assertArrayShallowEquals([1, 2, 3], timeline.highlight);
130     controller.findNext();
131     assertArrayShallowEquals([1], timeline.selection);
132     controller.findNext();
133     assertArrayShallowEquals([2], timeline.selection);
134     controller.findNext();
135     assertArrayShallowEquals([3], timeline.selection);
136     controller.findNext();
137     assertArrayShallowEquals([1], timeline.selection);
138     controller.findPrevious();
139     assertArrayShallowEquals([3], timeline.selection);
140     controller.findPrevious();
141     assertArrayShallowEquals([2], timeline.selection);
142     assertArrayShallowEquals([1, 2, 3], timeline.highlight);
143   });
144
145   test('findControllerChangeFilterAfterNext', function() {
146     var timeline = new FakeTimeline();
147     var controller = new tracing.FindController();
148     controller.timeline = timeline;
149
150     timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];
151     controller.filterText = 'asdf';
152
153     // Loop through hits then when we wrap, try moving backward.
154     controller.findNext();
155     timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [4];
156     controller.filterText = 'asdfsf';
157     controller.findNext();
158     assertArrayShallowEquals([4], timeline.selection);
159   });
160
161   test('findControllerSelectsAllItemsFirst', function() {
162     var timeline = new FakeTimeline();
163     var controller = new tracing.FindController();
164     controller.timeline = timeline;
165
166     timeline.addAllObjectsMatchingFilterToSelectionReturnValue = [1, 2, 3];
167     controller.filterText = 'asdfsf';
168     assertArrayShallowEquals([], timeline.selection);
169     assertArrayShallowEquals([1, 2, 3], timeline.highlight);
170     controller.findNext();
171     assertArrayShallowEquals([1], timeline.selection);
172     controller.findNext();
173     assertArrayShallowEquals([2], timeline.selection);
174     assertArrayShallowEquals([1, 2, 3], timeline.highlight);
175   });
176
177   test('findControllerWithRealTimeline', function() {
178     var model = new tracing.TraceModel();
179     var p1 = model.getOrCreateProcess(1);
180     var t1 = p1.getOrCreateThread(1);
181     t1.sliceGroup.pushSlice(new tracing.trace_model.ThreadSlice(
182         '', 'a', 0, 1, {}, 3));
183
184     var timeline = new tracing.TimelineTrackView();
185     timeline.model = model;
186
187     var controller = new tracing.FindController();
188     controller.timeline = timeline;
189
190     // Test find with no filterText.
191     controller.findNext();
192
193     // Test find with filter txt.
194     controller.filterText = 'a';
195     assertArrayEquals([], timeline.selection);
196     assertArrayEquals(t1.sliceGroup.slices, timeline.highlight);
197
198     controller.findNext();
199     assertEquals(1, timeline.selection.length);
200     assertEquals(t1.sliceGroup.slices[0], timeline.selection[0]);
201
202     controller.filterText = 'xxx';
203     assertEquals(0, timeline.highlight.length);
204     assertEquals(0, timeline.selection.length);
205     controller.findNext();
206     assertEquals(0, timeline.selection.length);
207     controller.findNext();
208     assertEquals(0, timeline.selection.length);
209   });
210 });