it('should start and stop video recording', function() {
    var serial = 'GAGA123';
    var pid = 7;

    var fakeCaptureProc;
    var killCount = 0;
    var processSpawnStub = test_utils.stubOutProcessSpawn(sandbox);
    processSpawnStub.callback = function(proc, command, args) {
      if (videoCommand === command) {
        proc.pid = pid;
        fakeCaptureProc = proc;
        return true; // keep alive
      }
      var stdout;
      if ('ps' === command) {
        stdout = [
          '1 ' + pid + ' ' + videoCommand + ' -f x -s ' + serial + ' y',
          '1 ' + (pid + 100) + ' ignoreMe',
          pid + ' ' + (pid + 10) + ' raw_capture'
        ].join('\n');
      } else if ('kill' === command) {
        if (pid === parseInt(args[args.length - 1], 10) && fakeCaptureProc) {
          fakeCaptureProc.emit('exit', undefined, 'SIGAGA');
          fakeCaptureProc = undefined;
        }
        killCount += 1;
        stdout = '';
      } else {
        should.fail('Unexpected command: ' + command);
      }
      if (undefined !== stdout) {
        proc.stdout.emit('data', stdout);
      }
    };
    // Check for existence of the video record script
    var fsExistsStub = sandbox.stub(fs, 'exists', function(path, cb) {
      global.setTimeout(function() {
        cb(videoCommand === path);
      }, 1);
    });

    should.equal('[]', app.getSchedule());
    var videoFile = 'test.avi';
    var deviceType = 'shmantra';
    var videoCard = '2';

    var video = new video_hdmi.VideoHdmi(app, videoCommand);
    video.scheduleStartVideoRecording(videoFile, serial, deviceType, videoCard);
    test_utils.tickUntilIdle(app, sandbox);
    should.equal(2, killCount);
    processSpawnStub.assertCalls(
        {0: 'ps', 1: '-u', 2: /^\d+$/}, // Find our leftover capture pid
        {0: 'ps', 1: '-u', 2: /^\d+$/}, // Get the pid tree
        ['kill', '-9', ('' + pid)],
        ['kill', '-9', ('' + (pid + 10))],
        [videoCommand, '-f', videoFile, '-s', serial, '-t', deviceType, '-d',
            videoCard, '-w']);
    should.ok(fsExistsStub.calledOnce);

    // Watch for IDLE -- make sure the wait for recording exit has finished.
    should.equal('[]', app.getSchedule());
    video.scheduleStopVideoRecording();
    test_utils.tickUntilIdle(app, sandbox);
    processSpawnStub.assertCalls(
        {0: 'ps'},
        ['kill', '-9', ('' + pid)],
        ['kill', '-9', ('' + (pid + 10))]);
    should.equal(4, killCount);
    should.equal(undefined, fakeCaptureProc);
    processSpawnStub.assertCall();
  });
  it('should start and stop video recording', function() {
    var serial = 'GAGA123';
    var pid = 7;

    var fakeCaptureProc;
    var processSpawnStub = test_utils.stubOutProcessSpawn(sandbox);
    processSpawnStub.callback = function(proc, command) {
      if (videoCommand === command) {
        proc.pid = pid;
        fakeCaptureProc = proc;
        return true; // keep alive
      }
    };
    // Check for existence of the video record script
    var fsExistsStub = sandbox.stub(fs, 'exists', function(path, cb) {
      global.setTimeout(function() {
        cb(videoCommand === path);
      }, 1);
    });

    var killCount = 0;
    sandbox.stub(child_process, 'exec', function(command, callback) {
      if (/^ps\s/.test(command)) {
        var want_all = (/^ps(\s+-o\s+\S+)*$/.test(command));
        var want_pid = (want_all ||
            (new RegExp('^ps\\s+-p\\s' + pid + '\\s')).test(command));
        var want_ppid = (want_all || (new RegExp(
            '^ps\\s[^|]*\\|\\s+grep\\s+"\\^\\s+\\*' + pid + '\\s')).test(
            command));
        var lines = [];
        if (want_pid) {
          lines.push('1 ' + pid + ' ' + videoCommand + ' -f foo -s ' + serial +
              ' x');
        }
        if (want_all) {
          lines.push('1 ' + (pid + 100) + ' ignoreMe');
        }
        if (want_ppid) {
          lines.push(pid + ' ' + (pid + 10) + ' raw_capture');
        }
        callback(undefined, lines.join('\n'), '');
      } else if (/^kill\s/.test(command)) {
        if ((new RegExp('^kill\\s+(-\\d+\\s+)?' + pid + '$')).test(command) &&
            fakeCaptureProc) {
          fakeCaptureProc.emit('exit', undefined, 'SIGAGA');
          fakeCaptureProc = undefined;
        }
        killCount += 1;
        callback(undefined, '', '');
      } else {
        should.fail('Unexpected command: ' + command);
      }
    });

    should.equal('[]', app.getSchedule());
    var videoFile = 'test.avi';
    var deviceType = 'shmantra';
    var videoCard = '2';

    var video = new video_hdmi.VideoHdmi(app, videoCommand);
    should.equal('[]', app.getSchedule());
    video.scheduleStartVideoRecording(videoFile, serial, deviceType, videoCard);
    sandbox.clock.tick(webdriver.promise.Application.EVENT_LOOP_FREQUENCY * 10);
    should.equal('[]', app.getSchedule());
    should.equal(1, killCount);
    processSpawnStub.assertCall(videoCommand, '-f', videoFile, '-s', serial,
         '-t', deviceType, '-d', videoCard, '-w');
    should.ok(fsExistsStub.calledOnce);

    // Watch for IDLE -- make sure the wait for recording exit has finished.
    var idleSpy = sandbox.spy();
    app.on(webdriver.promise.Application.EventType.IDLE, idleSpy);
    video.scheduleStopVideoRecording();
    sandbox.clock.tick(webdriver.promise.Application.EVENT_LOOP_FREQUENCY * 10);
    should.equal('[]', app.getSchedule());
    should.equal(3, killCount);
    should.equal(undefined, fakeCaptureProc);
    processSpawnStub.assertCall();
    should.ok(idleSpy.calledOnce);
  });