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 */ 8 base.require('model.counter'); 9 base.exportTo('test_utils', function() { 10 function getAsync(url, cb) { 11 var req = new XMLHttpRequest(); 12 req.open('GET', url, true); 13 req.onreadystatechange = function(aEvt) { 14 if (req.readyState == 4) { 15 window.setTimeout(function() { 16 if (req.status == 200) { 17 cb(req.responseText); 18 } else { 19 console.log('Failed to load ' + url); 20 } 21 }, 0); 22 } 23 }; 24 req.send(null); 25 } 26 27 function newAsyncSlice(start, duration, startThread, endThread) { 28 return newAsyncSliceNamed('a', start, duration, startThread, endThread); 29 } 30 31 function newAsyncSliceNamed(name, start, duration, startThread, endThread) { 32 var s = new tracing.model.AsyncSlice('', name, 0, start); 33 s.duration = duration; 34 s.startThread = startThread; 35 s.endThread = endThread; 36 var subSlice = new tracing.model.AsyncSlice('', name, 0, start); 37 subSlice.duration = duration; 38 subSlice.startThread = startThread; 39 subSlice.endThread = endThread; 40 s.subSlices = [subSlice]; 41 return s; 42 } 43 44 function newCounter(parent) { 45 return newCounterNamed(parent, 'a'); 46 } 47 48 function newCounterNamed(parent, name) { 49 var s = new tracing.model.Counter(parent, name, null, name); 50 return s; 51 } 52 53 function newCounterCategory(parent, category, name) { 54 var s = new tracing.model.Counter(parent, name, category, name); 55 return s; 56 } 57 58 function newSlice(start, duration) { 59 return newSliceNamed('a', start, duration); 60 } 61 62 function newSliceNamed(name, start, duration) { 63 var s = new tracing.model.Slice('', name, 0, start, {}, duration); 64 return s; 65 } 66 67 function newSliceCategory(category, name, start, duration) { 68 var s = new tracing.model.Slice(category, name, 0, start, {}, duration); 69 return s; 70 } 71 72 function findSliceNamed(slices, name) { 73 for (var i = 0; i < slices.length; i++) 74 if (slices[i].title == name) 75 return slices[i]; 76 return undefined; 77 } 78 79 return { 80 getAsync: getAsync, 81 newAsyncSlice: newAsyncSlice, 82 newAsyncSliceNamed: newAsyncSliceNamed, 83 newCounter: newCounter, 84 newCounterNamed: newCounterNamed, 85 newCounterCategory: newCounterCategory, 86 newSlice: newSlice, 87 newSliceNamed: newSliceNamed, 88 newSliceCategory: newSliceCategory, 89 findSliceNamed: findSliceNamed 90 }; 91 }); 92