screenshot.test.js 5.38 KB
/*
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2016 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

/* globals Promise, sinon, describe, it, beforeEach, afterEach, expect */
/* eslint max-nested-callbacks: 0 */
define([
    'screens/player/shared/serviceadmin',
    'screens/player/shared/test-helper/stub-service-interface',
    'screens/player/shared/test-helper/service',
    'screens/player/shared/test-helper/command-handler',
    'screens/player/store/store',
    'screens/player/firmware/preferences/preferences',
    'screens/player/firmware/screenshot/screenshot',
    'screens/player/firmware/network/network',
    'screens/player/firmware/log/logger',
    'screens/player/command/command',
    'screens/player/command/screenshot'
], function(ServiceAdmin, stubServiceInterface, serviceTestHelper, commandHandlerTestHelper, Store, Preferences, Screenshot, Network, Logger, PlayerCommand, screenshotCmd) {
    'use strict';

    describe('screens/player/command/screenshot', function() {

        var stubStoreService, stubPreferencesService, stubScreenshotService, stubNetworkService, stubLoggerService;
        var deviceId;

        beforeEach(function() {
            stubStoreService = stubServiceInterface(ServiceAdmin, Store);
            stubPreferencesService = stubServiceInterface(ServiceAdmin, Preferences);
            stubScreenshotService = stubServiceInterface(ServiceAdmin, Screenshot);
            stubNetworkService = stubServiceInterface(ServiceAdmin, Network);
            stubLoggerService = stubServiceInterface(ServiceAdmin, Logger);
            stubLoggerService.api.getWriter.returns({
                log: sinon.stub(),
                error: sinon.stub()
            });

            deviceId = 'foo';
            stubPreferencesService.api.getPreferences.returns({ device: deviceId });
        });

        afterEach(function() {
            stubStoreService.restore();
            stubPreferencesService.restore();
            stubScreenshotService.restore();
            stubNetworkService.restore();
            stubLoggerService.restore();
        });

        serviceTestHelper.runDefaultTests(screenshotCmd, PlayerCommand.serviceName);
        commandHandlerTestHelper.runDefaultTests(screenshotCmd, 'screenshot');

        it('sends the screenshot if the Promise on the screenshot service resolves', function() {
            var contents = 'data:image/svg+xml;utf8,';
            var devicePostUrl = '/foo/bar/baz';

            stubScreenshotService.api.captureScreenshotAsURI.returns(Promise.resolve(contents));
            stubStoreService.api.getState.returns({ statusmodel: { devicePostUrl: devicePostUrl } });

            var settings = {
                method: 'POST',
                data: { data: contents }
            };

            return screenshotCmd.handleCommand('screenshot').then(function() {
                expect(stubNetworkService.api.ajax).to.have.been.calledWithExactly(devicePostUrl + '.screenshot.json', settings);
            });
        });

        it('logs an error if the Promise on the screenshot service rejects', function(done) {
            var err = new Error('error');

            stubScreenshotService.api.captureScreenshotAsURI.returns(Promise.reject(err));
            stubStoreService.api.getState.returns({ statusmodel: { devicePostUrl: '/foo/bar/baz' } });

            screenshotCmd.handleCommand('screenshot').catch(function() {
                expect(stubLoggerService.api.getWriter().error).to.have.been.calledWithExactly(err);
                done();
            });
        });

        it('logs the device ID when sending the screenshot', function() {
            stubScreenshotService.api.captureScreenshotAsURI.returns(Promise.resolve());
            stubStoreService.api.getState.returns({ statusmodel: { devicePostUrl: '/foo/bar/baz' } });

            return screenshotCmd.handleCommand('screenshot').then(function() {
                expect(stubLoggerService.api.getWriter().log).to.have.been.calledWithExactly('Sending screenshot for ' + deviceId);
            });
        });

        it('logs an error and rejects if the devicePostUrl is missing in the state', function(done) {
            stubStoreService.api.getState.returns({ statusmodel: {} });

            screenshotCmd.handleCommand('screenshot').catch(function() {
                expect(stubLoggerService.api.getWriter().error).to.have.been.called;
                done();
            });
        });

        it('throws an error and does nothing on an missing command', function() {
            expect(function() { screenshotCmd.handleCommand(); }).to.throw();
            expect(stubNetworkService.api.ajax).to.not.have.been.called;
        });

        it('throws an error and does nothing on an invalid command', function() {
            expect(function() { screenshotCmd.handleCommand('invalid'); }).to.throw();
            expect(stubNetworkService.api.ajax).to.not.have.been.called;
        });

    });
});