• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2012 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/**
6 * @fileoverview Helper functions for use in tracing tests.
7 */
8base.exportTo('test_utils', function() {
9  function getAsync(url, cb) {
10    var req = new XMLHttpRequest();
11    req.open('GET', url, true);
12    req.onreadystatechange = function(aEvt) {
13      if (req.readyState == 4) {
14        window.setTimeout(function() {
15          if (req.status == 200) {
16            cb(req.responseText);
17          } else {
18            console.log('Failed to load ' + url);
19          }
20        }, 0);
21      }
22    };
23    req.send(null);
24  }
25
26  function newAsyncSlice(start, duration, startThread, endThread) {
27    return newAsyncSliceNamed('a', start, duration, startThread, endThread);
28  }
29
30  function newAsyncSliceNamed(name, start, duration, startThread, endThread) {
31    var s = new tracing.TimelineAsyncSlice('', name, 0, start);
32    s.duration = duration;
33    s.startThread = startThread;
34    s.endThread = endThread;
35    var subSlice = new tracing.TimelineAsyncSlice('', name, 0, start);
36    subSlice.duration = duration;
37    subSlice.startThread = startThread;
38    subSlice.endThread = endThread;
39    s.subSlices = [subSlice];
40    return s;
41  }
42
43  function newSlice(start, duration) {
44    return newSliceNamed('a', start, duration);
45  }
46
47  function newSliceNamed(name, start, duration) {
48    var s = new tracing.TimelineSlice('', name, 0, start, {}, duration);
49    return s;
50  }
51
52  function newSliceCategory(category, name, start, duration) {
53    var s = new tracing.TimelineSlice(category, name, 0, start, {}, duration);
54    return s;
55  }
56
57  function findSliceNamed(slices, name) {
58    for (var i = 0; i < slices.length; i++)
59      if (slices[i].title == name)
60        return slices[i];
61      return undefined;
62  }
63
64  return {
65    getAsync: getAsync,
66    newAsyncSlice: newAsyncSlice,
67    newAsyncSliceNamed: newAsyncSliceNamed,
68    newSlice: newSlice,
69    newSliceNamed: newSliceNamed,
70    newSliceCategory: newSliceCategory,
71    findSliceNamed: findSliceNamed
72  };
73});
74