2016-10-02 21:22:49 +00:00
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
2017-05-19 00:35:45 +00:00
let { session , contextGroup , Protocol } = InspectorTest . start ( "Test that profiling can only be started when Profiler was enabled and that Profiler.disable command will stop recording all profiles." ) ;
2016-10-02 21:22:49 +00:00
2016-10-03 23:32:52 +00:00
Protocol . Profiler . start ( ) . then ( didFailToStartWhenDisabled ) ;
2016-10-02 21:22:49 +00:00
disallowConsoleProfiles ( ) ;
function disallowConsoleProfiles ( )
{
2016-10-03 23:32:52 +00:00
Protocol . Profiler . onConsoleProfileStarted ( function ( messageObject )
2016-10-02 21:22:49 +00:00
{
InspectorTest . log ( "FAIL: console profile started " + JSON . stringify ( messageObject , null , 4 ) ) ;
2016-10-03 23:32:52 +00:00
} ) ;
Protocol . Profiler . onConsoleProfileFinished ( function ( messageObject )
2016-10-02 21:22:49 +00:00
{
InspectorTest . log ( "FAIL: unexpected profile received " + JSON . stringify ( messageObject , null , 4 ) ) ;
2016-10-03 23:32:52 +00:00
} ) ;
2016-10-02 21:22:49 +00:00
}
function allowConsoleProfiles ( )
{
2016-10-03 23:32:52 +00:00
Protocol . Profiler . onConsoleProfileStarted ( function ( messageObject )
2016-10-02 21:22:49 +00:00
{
InspectorTest . log ( "PASS: console initiated profile started" ) ;
2016-10-03 23:32:52 +00:00
} ) ;
Protocol . Profiler . onConsoleProfileFinished ( function ( messageObject )
2016-10-02 21:22:49 +00:00
{
InspectorTest . log ( "PASS: console initiated profile received" ) ;
2016-10-03 23:32:52 +00:00
} ) ;
2016-10-02 21:22:49 +00:00
}
function didFailToStartWhenDisabled ( messageObject )
{
2017-05-19 00:35:45 +00:00
if ( ! expectedError ( "didFailToStartWhenDisabled" , messageObject ) )
2016-10-02 21:22:49 +00:00
return ;
allowConsoleProfiles ( ) ;
2016-10-03 23:32:52 +00:00
Protocol . Profiler . enable ( ) ;
Protocol . Profiler . start ( ) . then ( didStartFrontendProfile ) ;
2016-10-02 21:22:49 +00:00
}
function didStartFrontendProfile ( messageObject )
{
2017-05-19 00:35:45 +00:00
if ( ! expectedSuccess ( "didStartFrontendProfile" , messageObject ) )
2016-10-02 21:22:49 +00:00
return ;
2016-10-03 23:32:52 +00:00
Protocol . Runtime . evaluate ( { expression : "console.profile('p1');" } ) . then ( didStartConsoleProfile ) ;
2016-10-02 21:22:49 +00:00
}
function didStartConsoleProfile ( messageObject )
{
2017-05-19 00:35:45 +00:00
if ( ! expectedSuccess ( "didStartConsoleProfile" , messageObject ) )
2016-10-02 21:22:49 +00:00
return ;
2016-10-03 23:32:52 +00:00
Protocol . Profiler . disable ( ) . then ( didDisableProfiler ) ;
2016-10-02 21:22:49 +00:00
}
function didDisableProfiler ( messageObject )
{
2017-05-19 00:35:45 +00:00
if ( ! expectedSuccess ( "didDisableProfiler" , messageObject ) )
2016-10-02 21:22:49 +00:00
return ;
2016-10-03 23:32:52 +00:00
Protocol . Profiler . enable ( ) ;
Protocol . Profiler . stop ( ) . then ( didStopFrontendProfile ) ;
2016-10-02 21:22:49 +00:00
}
function didStopFrontendProfile ( messageObject )
{
2017-05-19 00:35:45 +00:00
if ( ! expectedError ( "no front-end initiated profiles found" , messageObject ) )
2016-10-02 21:22:49 +00:00
return ;
disallowConsoleProfiles ( ) ;
2016-10-03 23:32:52 +00:00
Protocol . Runtime . evaluate ( { expression : "console.profileEnd();" } ) . then ( didStopConsoleProfile ) ;
2016-10-02 21:22:49 +00:00
}
function didStopConsoleProfile ( messageObject )
{
2017-05-19 00:35:45 +00:00
if ( ! expectedSuccess ( "didStopConsoleProfile" , messageObject ) )
2016-10-02 21:22:49 +00:00
return ;
InspectorTest . completeTest ( ) ;
}
2017-05-19 00:35:45 +00:00
function checkExpectation ( fail , name , messageObject )
{
if ( fail === ! ! messageObject . error ) {
InspectorTest . log ( "PASS: " + name ) ;
return true ;
}
InspectorTest . log ( "FAIL: " + name + ": " + JSON . stringify ( messageObject ) ) ;
InspectorTest . completeTest ( ) ;
return false ;
}
var expectedSuccess = checkExpectation . bind ( null , false ) ;
var expectedError = checkExpectation . bind ( null , true ) ;