1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 Copyright (c) 2012 The Chromium Authors. All rights reserved. 5 Use of this source code is governed by a BSD-style license that can be 6 found in the LICENSE file. 7 --> 8 <head> 9 <title>ProfilingView tests</title> 10 <script src="base.js"></script> 11 <style> 12 .profiling-view { 13 border: 1px solid black; 14 } 15 </style> 16 </head> 17 <body> 18 <script> 19 base.require('unittest'); 20 base.require('test_utils'); 21 base.require('profiling_view'); 22 base.require('tracing_controller'); 23 </script> 24 <script> 25 'use strict'; 26 27 var testData = [ 28 {name: 'a', args: {}, pid: 52, ts: 15000, cat: 'foo', tid: 53, ph: 'B'}, 29 {name: 'a', args: {}, pid: 52, ts: 19000, cat: 'foo', tid: 53, ph: 'E'}, 30 {name: 'b', args: {}, pid: 52, ts: 32000, cat: 'foo', tid: 53, ph: 'B'}, 31 {name: 'b', args: {}, pid: 52, ts: 54000, cat: 'foo', tid: 53, ph: 'E'} 32 ]; 33 var systemTraceTestData = [ 34 'systrace.sh-8170 [000] 0.013: sched_switch: ' + 35 'prev_comm=systrace.sh prev_pid=8170 prev_prio=120 ' + 36 'prev_state=x ==> next_comm=kworker/1:0 next_pid=7873 ' + 37 'next_prio=120', 38 ' kworker/1:0-7873 [000] 0.036: sched_switch: ' + 39 'prev_comm=kworker/1:0 prev_pid=7873 prev_prio=120 ' + 40 'prev_state=S ==> next_comm=debugd next_pid=4404 next_prio=120', 41 ' debugd-4404 [000] 0.070: sched_switch: prev_comm=debugd ' + 42 'prev_pid=4404 prev_prio=120 prev_state=S ==> ' + 43 'next_comm=dbus-daemon next_pid=510 next_prio=120', 44 'systrace.sh-8182 [000] 0.000: tracing_mark_write: ' + 45 'trace_event_clock_sync: parent_ts=0.0' 46 ].join('\n'); 47 48 /* This test just instantiates a ProflingView and adds it to the DOM 49 * to help with non-unittest UI work. 50 */ 51 function testInstantiate() { 52 var view = new tracing.ProfilingView(); 53 var tracingController; 54 55 // This code emulates Chrome's responses to sendFn enough that the real 56 // tracing controller can be used to interactively test the UI. 57 var systemTraceRequested = false; 58 function send(message, opt_args) { 59 var args = opt_args || []; 60 if (message == 'beginTracing') { 61 systemTraceRequested = opt_args[0]; 62 } else if (message == 'beginRequestBufferPercentFull') { 63 setTimeout(function() { 64 view.tracingController.onRequestBufferPercentFullComplete(0.5); 65 }, 1); 66 } else if (message == 'endTracingAsync') { 67 setTimeout(function() { 68 if (systemTraceRequested) { 69 view.tracingController.onSystemTraceDataCollected( 70 systemTraceTestData); 71 } 72 view.tracingController.onTraceDataCollected(testData); 73 view.tracingController.onEndTracingComplete(); 74 }, 1); 75 } else if (message == 'loadTraceFile') { 76 setTimeout(function() { 77 view.tracingController.onLoadTraceFileComplete( 78 JSON.stringify(testData)); 79 }, 150); 80 } else if (message == 'saveTraceFile') { 81 setTimeout(function() { 82 view.tracingController.onSaveTraceFileComplete(); 83 }, 1); 84 } 85 } 86 87 tracingController = new tracing.TracingController(send); 88 tracingController.supportsSystemTracing_ = true; 89 90 view.tracingController = tracingController; 91 view.focusElement = view; 92 this.addHTMLOutput(undefined, view); 93 } 94 95 /* 96 * Just enough of the TracingController to support the tests below. 97 */ 98 function FakeTracingController() { 99 } 100 101 FakeTracingController.prototype = { 102 __proto__: base.EventTarget.prototype, 103 104 get supportsSystemTracing() { 105 return base.isChromeOS; 106 }, 107 108 beginTracing: function(opt_systemTracingEnabled) { 109 this.wasBeginTracingCalled = true; 110 this.wasBeginTracingCalledWithSystemTracingEnabled = 111 opt_systemTracingEnabled; 112 }, 113 114 get traceEvents() { 115 if (!this.wasBeginTracingCalled) 116 return undefined; 117 return testData; 118 }, 119 120 get systemTraceEvents() { 121 if (!this.wasBeginTracingCalled) 122 return []; 123 if (!this.wasBeginTracingCalledWithSystemTracingEnabled) 124 return []; 125 return systemTraceTestData; 126 } 127 128 }; 129 130 function recordTestCommon() { 131 var view = new tracing.ProfilingView(); 132 var tracingController = new FakeTracingController(); 133 view.tracingController = tracingController; 134 view.querySelector('button.record').click(); 135 assertTrue(tracingController.wasBeginTracingCalled); 136 assertEquals(base.isChromeOS, 137 tracingController.wasBeginTracingCalledWithSystemTracingEnabled); 138 139 var e = new base.Event('traceEnded'); 140 var didRefresh = false; 141 e.events = tracingController.traceEvents; 142 tracingController.dispatchEvent(e); 143 assertTrue(!!view.timelineView.model); 144 } 145 146 function testRecordNonCros() { 147 var old = base.isChromeOS; 148 base.isChromeOS = false; 149 try { 150 recordTestCommon(); 151 } finally { 152 base.isChromeOS = old; 153 } 154 } 155 156 function testRecordCros() { 157 var old = base.isChromeOS; 158 base.isChromeOS = true; 159 try { 160 recordTestCommon(); 161 } finally { 162 base.isChromeOS = old; 163 } 164 } 165 </script> 166 </body> 167 </html> 168