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 base.require('tracing.test_utils'); 8 base.require('about_tracing.tracing_controller'); 9 10 base.unittest.testSuite('about_tracing.tracing_controller', function() { 11 test('loadTraceFileCompleteWithJSONfiles', function() { 12 var callbackFired = false; 13 14 var traceEventData = '[{"a":1, "b":2}]'; 15 16 var tc = new about_tracing.TracingController(undefined); 17 tc.addEventListener('loadTraceFileComplete', function(event) { 18 callbackFired = true; 19 }); 20 tc.onLoadTraceFileComplete(traceEventData); 21 22 assertTrue(callbackFired); 23 assertEquals(traceEventData, tc.traceEventData); 24 }); 25 26 test('loadTraceFileCompleteWithNonJSONfiles', function() { 27 var callbackFired = false; 28 29 var tc = new about_tracing.TracingController(undefined); 30 tc.addEventListener('loadTraceFileComplete', function(event) { 31 callbackFired = true; 32 }); 33 tc.onLoadTraceFileComplete('<DOCTYPE>'); 34 35 assertEquals('<DOCTYPE>', tc.traceEventData); 36 assertTrue(callbackFired); 37 }); 38 39 function SendStub() { 40 this.sends = []; 41 } 42 SendStub.prototype = { 43 reset: function() { 44 this.sends = []; 45 }, 46 47 send: function(msg, args) { 48 this.sends.push({ 49 msg: msg, 50 args: args 51 }); 52 }, 53 54 get numSends() { 55 return this.sends.length; 56 }, 57 58 getMessage: function(i) { 59 return this.sends[i].msg; 60 }, 61 62 getArgs: function(i) { 63 return this.sends[i].args; 64 } 65 }; 66 67 test('saveTraceFile', function() { 68 var sendStub = new SendStub(); 69 var tc = new about_tracing.TracingController(sendStub.send.bind(sendStub)); 70 tc.traceEventData_ = JSON.stringify([1, 2, 3]); 71 assertEquals(1, sendStub.numSends); 72 assertEquals('tracingControllerInitialized', sendStub.getMessage(0)); 73 sendStub.reset(); 74 75 tc.beginSaveTraceFile(); 76 77 assertEquals(1, sendStub.numSends); 78 assertEquals('saveTraceFile', sendStub.getMessage(0)); 79 var savedDataString = sendStub.getArgs(0)[0]; 80 var savedData = JSON.parse(savedDataString); 81 assertArrayEquals([1, 2, 3], savedData.traceEvents); 82 }); 83 }); 84