<commit_before>/*global defineSuite*/
defineSuite([
        'Scene/ImageryLayer',
        'Core/EllipsoidTerrainProvider',
        'Core/loadImage',
        'Core/loadJsonp',
        'Core/loadWithXhr',
        'Core/Rectangle',
        'Renderer/ComputeEngine',
        'Scene/ArcGisMapServerImageryProvider',
        'Scene/BingMapsImageryProvider',
        'Scene/createTileMapServiceImageryProvider',
        'Scene/Globe',
        'Scene/GlobeSurfaceTile',
        'Scene/Imagery',
        'Scene/ImageryLayerCollection',
        'Scene/ImageryState',
        'Scene/NeverTileDiscardPolicy',
        'Scene/QuadtreeTile',
        'Scene/SingleTileImageryProvider',
        'Scene/WebMapServiceImageryProvider',
        'Specs/createScene',
        'Specs/pollToPromise'
    ], function(
        ImageryLayer,
        EllipsoidTerrainProvider,
        loadImage,
        loadJsonp,
        loadWithXhr,
        Rectangle,
        ComputeEngine,
        ArcGisMapServerImageryProvider,
        BingMapsImageryProvider,
        createTileMapServiceImageryProvider,
        Globe,
        GlobeSurfaceTile,
        Imagery,
        ImageryLayerCollection,
        ImageryState,
        NeverTileDiscardPolicy,
        QuadtreeTile,
        SingleTileImageryProvider,
        WebMapServiceImageryProvider,
        createScene,
        pollToPromise) {
    'use strict';

    var scene;
    var computeEngine;

    beforeAll(function() {
        scene = createScene();
        computeEngine = new ComputeEngine(scene.context);
    });

    afterAll(function() {
        scene.destroyForSpecs();
        computeEngine.destroy();
    });

    afterEach(function() {
        loadJsonp.loadAndExecuteScript = loadJsonp.defaultLoadAndExecuteScript;
        loadImage.createImage = loadImage.defaultCreateImage;
        loadWithXhr.load = loadWithXhr.defaultLoad;

        scene.frameState.commandList.length = 0;
    });

    function CustomDiscardPolicy() {
        this.shouldDiscard = false;
    }

    CustomDiscardPolicy.prototype.isReady = function() {
        return true;
    };

    CustomDiscardPolicy.prototype.shouldDiscardImage = function(image) {
        return this.shouldDiscard;
    };

    it('discards tiles when the ImageryProviders discard policy says to do so', function() {
        loadImage.createImage = function(url, crossOrigin, deferred) {
            loadImage.defaultCreateImage('Data/Images/Red16x16.png', crossOrigin, deferred);
        };

        loadWithXhr.load = function(url, responseType, method, data, headers, deferred, overrideMimeType) {
            loadWithXhr.defaultLoad('Data/Images/Red16x16.png', responseType, method, data, headers, deferred);
        };

        var discardPolicy = new CustomDiscardPolicy();

        var provider = new WebMapServiceImageryProvider({
            url : 'made/up/url',
            layers : 'foo',
            tileDiscardPolicy : discardPolicy
        });

        var layer = new ImageryLayer(provider);

        return pollToPromise(function() {
            return provider.ready;
        }).then(function() {
            discardPolicy.shouldDiscard = true;
            var imagery = new Imagery(layer, 0, 0, 0);
            imagery.addReference();
            layer._requestImagery(imagery);

            return pollToPromise(function() {
                return imagery.state === ImageryState.RECEIVED;
            }).then(function() {
                layer._createTexture(scene.context, imagery);
                expect(imagery.state).toEqual(ImageryState.INVALID);
                imagery.releaseReference();
            });
        });
    });

    function createWebMercatorProvider() {
        loadJsonp.loadAndExecuteScript = function(url, functionName) {
            window[functionName]({
                "authenticationResultCode" : "ValidCredentials",
                "brandLogoUri" : "http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
                "copyright" : "Copyright © 2012 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
                "resourceSets" : [{
                    "estimatedTotal" : 1,
                    "resources" : [{
                        "__type" : "ImageryMetadata:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
                        "imageHeight" : 256,
                        "imageUrl" : "http:\/\/invalid.{subdomain}.invalid\/tiles\/r{quadkey}?g=1062&lbl=l1&productSet=mmCB",
                        "imageUrlSubdomains" : ["t0"],
                        "imageWidth" : 256,
                        "imageryProviders" : null,
                        "vintageEnd" : null,
                        "vintageStart" : null,
                        "zoomMax" : 21,
                        "zoomMin" : 1
                    }]
                }],
                "statusCode" : 200,
                "statusDescription" : "OK",
                "traceId" : "c9cf8c74a8b24644974288c92e448972|EWRM003311|02.00.171.2600|"
            });
        };

        loadImage.createImage = function(url, crossOrigin, deferred) {
            loadImage.defaultCreateImage('Data/Images/Red16x16.png', crossOrigin, deferred);
        };

        loadWithXhr.load = function(url, responseType, method, data, headers, deferred, overrideMimeType) {
            loadWithXhr.defaultLoad('Data/Images/Red16x16.png', responseType, method, data, headers, deferred);
        };

        return new BingMapsImageryProvider({
            url : 'http://host.invalid',
            tileDiscardPolicy : new NeverTileDiscardPolicy()
        });
    }

    it('reprojects web mercator images when necessary', function() {
        var provider = createWebMercatorProvider();
        var layer = new ImageryLayer(provider);

        return pollToPromise(function() {
            return provider.ready;
        }).then(function() {
            var imagery = new Imagery(layer, 0, 0, 0);
            imagery.addReference();
            layer._requestImagery(imagery);

            return pollToPromise(function() {
                return imagery.state === ImageryState.RECEIVED;
            }).then(function() {
                layer._createTexture(scene.context, imagery);

                return pollToPromise(function() {
                    return imagery.state === ImageryState.TEXTURE_LOADED;
                }).then(function() {
                    var textureBeforeReprojection = imagery.textureWebMercator;
                    layer._reprojectTexture(scene.frameState, imagery);
                    layer.queueReprojectionCommands(scene.frameState);
                    scene.frameState.commandList[0].execute(computeEngine);

                    return pollToPromise(function() {
                        return imagery.state === ImageryState.READY;
                    }).then(function() {
                        expect(imagery.texture).toBeDefined();
                        expect(textureBeforeReprojection).not.toEqual(imagery.texture);
                        imagery.releaseReference();
                    });
                });
            });
        });
    });

    it('cancels reprojection', function() {
        var provider = createWebMercatorProvider();
        var layer = new ImageryLayer(provider);

        return pollToPromise(function() {
            return provider.ready;
        }).then(function() {
            var imagery = new Imagery(layer, 0, 0, 0);
            imagery.addReference();
            layer._requestImagery(imagery);

            return pollToPromise(function() {
                return imagery.state === ImageryState.RECEIVED;
            }).then(function() {
                layer._createTexture(scene.context, imagery);

                return pollToPromise(function() {
                    return imagery.state === ImageryState.TEXTURE_LOADED;
                }).then(function() {
                    layer._reprojectTexture(scene.frameState, imagery);
                    layer.cancelReprojections();
                    layer.queueReprojectionCommands(scene.frameState);
                    expect(scene.frameState.commandList.length).toEqual(0);
                });
            });<commit_msg>Add some specs.<commit_after>/*global defineSuite*/
defineSuite([
        'Scene/ImageryLayer',
        'Core/EllipsoidTerrainProvider',
        'Core/loadImage',
        'Core/loadJsonp',
        'Core/loadWithXhr',
        'Core/Rectangle',
        'Renderer/ComputeEngine',
        'Scene/ArcGisMapServerImageryProvider',
        'Scene/BingMapsImageryProvider',
        'Scene/createTileMapServiceImageryProvider',
        'Scene/Globe',
        'Scene/GlobeSurfaceTile',
        'Scene/Imagery',
        'Scene/ImageryLayerCollection',
        'Scene/ImageryState',
        'Scene/NeverTileDiscardPolicy',
        'Scene/QuadtreeTile',
        'Scene/SingleTileImageryProvider',
        'Scene/WebMapServiceImageryProvider',
        'Specs/createScene',
        'Specs/pollToPromise'
    ], function(
        ImageryLayer,
        EllipsoidTerrainProvider,
        loadImage,
        loadJsonp,
        loadWithXhr,
        Rectangle,
        ComputeEngine,
        ArcGisMapServerImageryProvider,
        BingMapsImageryProvider,
        createTileMapServiceImageryProvider,
        Globe,
        GlobeSurfaceTile,
        Imagery,
        ImageryLayerCollection,
        ImageryState,
        NeverTileDiscardPolicy,
        QuadtreeTile,
        SingleTileImageryProvider,
        WebMapServiceImageryProvider,
        createScene,
        pollToPromise) {
    'use strict';

    var scene;
    var computeEngine;

    beforeAll(function() {
        scene = createScene();
        computeEngine = new ComputeEngine(scene.context);
    });

    afterAll(function() {
        scene.destroyForSpecs();
        computeEngine.destroy();
    });

    afterEach(function() {
        loadJsonp.loadAndExecuteScript = loadJsonp.defaultLoadAndExecuteScript;
        loadImage.createImage = loadImage.defaultCreateImage;
        loadWithXhr.load = loadWithXhr.defaultLoad;

        scene.frameState.commandList.length = 0;
    });

    function CustomDiscardPolicy() {
        this.shouldDiscard = false;
    }

    CustomDiscardPolicy.prototype.isReady = function() {
        return true;
    };

    CustomDiscardPolicy.prototype.shouldDiscardImage = function(image) {
        return this.shouldDiscard;
    };

    it('discards tiles when the ImageryProviders discard policy says to do so', function() {
        loadImage.createImage = function(url, crossOrigin, deferred) {
            loadImage.defaultCreateImage('Data/Images/Red16x16.png', crossOrigin, deferred);
        };

        loadWithXhr.load = function(url, responseType, method, data, headers, deferred, overrideMimeType) {
            loadWithXhr.defaultLoad('Data/Images/Red16x16.png', responseType, method, data, headers, deferred);
        };

        var discardPolicy = new CustomDiscardPolicy();

        var provider = new WebMapServiceImageryProvider({
            url : 'made/up/url',
            layers : 'foo',
            tileDiscardPolicy : discardPolicy
        });

        var layer = new ImageryLayer(provider);

        return pollToPromise(function() {
            return provider.ready;
        }).then(function() {
            discardPolicy.shouldDiscard = true;
            var imagery = new Imagery(layer, 0, 0, 0);
            imagery.addReference();
            layer._requestImagery(imagery);

            return pollToPromise(function() {
                return imagery.state === ImageryState.RECEIVED;
            }).then(function() {
                layer._createTexture(scene.context, imagery);
                expect(imagery.state).toEqual(ImageryState.INVALID);
                imagery.releaseReference();
            });
        });
    });

    function createWebMercatorProvider() {
        loadJsonp.loadAndExecuteScript = function(url, functionName) {
            window[functionName]({
                "authenticationResultCode" : "ValidCredentials",
                "brandLogoUri" : "http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
                "copyright" : "Copyright © 2012 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
                "resourceSets" : [{
                    "estimatedTotal" : 1,
                    "resources" : [{
                        "__type" : "ImageryMetadata:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
                        "imageHeight" : 256,
                        "imageUrl" : "http:\/\/invalid.{subdomain}.invalid\/tiles\/r{quadkey}?g=1062&lbl=l1&productSet=mmCB",
                        "imageUrlSubdomains" : ["t0"],
                        "imageWidth" : 256,
                        "imageryProviders" : null,
                        "vintageEnd" : null,
                        "vintageStart" : null,
                        "zoomMax" : 21,
                        "zoomMin" : 1
                    }]
                }],
                "statusCode" : 200,
                "statusDescription" : "OK",
                "traceId" : "c9cf8c74a8b24644974288c92e448972|EWRM003311|02.00.171.2600|"
            });
        };

        loadImage.createImage = function(url, crossOrigin, deferred) {
            loadImage.defaultCreateImage('Data/Images/Red16x16.png', crossOrigin, deferred);
        };

        loadWithXhr.load = function(url, responseType, method, data, headers, deferred, overrideMimeType) {
            loadWithXhr.defaultLoad('Data/Images/Red16x16.png', responseType, method, data, headers, deferred);
        };

        return new BingMapsImageryProvider({
            url : 'http://host.invalid',
            tileDiscardPolicy : new NeverTileDiscardPolicy()
        });
    }

    it('reprojects web mercator images when necessary', function() {
        var provider = createWebMercatorProvider();
        var layer = new ImageryLayer(provider);

        return pollToPromise(function() {
            return provider.ready;
        }).then(function() {
            var imagery = new Imagery(layer, 0, 0, 0);
            imagery.addReference();
            layer._requestImagery(imagery);

            return pollToPromise(function() {
                return imagery.state === ImageryState.RECEIVED;
            }).then(function() {
                layer._createTexture(scene.context, imagery);

                return pollToPromise(function() {
                    return imagery.state === ImageryState.TEXTURE_LOADED;
                }).then(function() {
                    var textureBeforeReprojection = imagery.textureWebMercator;
                    layer._reprojectTexture(scene.frameState, imagery, true);
                    layer.queueReprojectionCommands(scene.frameState);
                    scene.frameState.commandList[0].execute(computeEngine);

                    return pollToPromise(function() {
                        return imagery.state === ImageryState.READY;
                    }).then(function() {
                        expect(imagery.texture).toBeDefined();
                        expect(textureBeforeReprojection).not.toEqual(imagery.texture);
                        imagery.releaseReference();
                    });
                });
            });
        });
    });

    it('does not reproject web mercator images when not necessary', function() {
        var provider = createWebMercatorProvider();
        var layer = new ImageryLayer(provider);

        return pollToPromise(function() {
            return provider.ready;
        }).then(function() {
            var imagery = new Imagery(layer, 0, 1, 3);
            imagery.addReference();
            layer._requestImagery(imagery);

            return pollToPromise(function() {
                return imagery.state === ImageryState.RECEIVED;
            }).then(function() {
                layer._createTexture(scene.context, imagery);

                return pollToPromise(function() {
                    return imagery.state === ImageryState.TEXTURE_LOADED;
                }).then(function() {
                    var textureBeforeReprojection = imagery.textureWebMercator;
                    layer._reprojectTexture(scene.frameState, imagery, false);
                    layer.queueReprojectionCommands(scene.frameState);
                    expect(scene.frameState.commandList.length).toBe(0);

                    return pollToPromise(function() {
                        return imagery.state === ImageryState.READY;
                    }).then(function() {
                        expect(imagery.texture).not.toBeDefined();
                        imagery.releaseReference();
                    });
                });
            });
        });
    });

    it('reprojects web mercator images later if it becomes necessary later', function() {
        var provider = createWebMercatorProvider();
        var layer = new ImageryLayer(provider);

        return pollToPromise(function() {
            return provider.ready;
        }).then(function() {
            var imagery = new Imagery(layer, 0, 1, 3);
            imagery.addReference();
            layer._requestImagery(imagery);

            return pollToPromise(function() {
                return imagery.state === ImageryState.RECEIVED;
            }).then(function() {
                layer._createTexture(scene.context, imagery);

                return pollToPromise(function() {
                    return imagery.state === ImageryState.TEXTURE_LOADED;
                }).then(function() {
                    var textureBeforeReprojection = imagery.textureWebMercator;
                    layer._reprojectTexture(scene.frameState, imagery, false);
                    layer.queueReprojectionCommands(scene.frameState);
                    expect(scene.frameState.commandList.length).toBe(0);

                    return pollToPromise(function() {
                        return imagery.state === ImageryState.READY;
                    }).then(function() {
                        expect(imagery.texture).not.toBeDefined();

                        layer._reprojectTexture(scene.frameState, imagery, true);
                        layer.queueReprojectionCommands(scene.frameState);
                        scene.frameState.commandList[0].execute(computeEngine);

                        return pollToPromise(function() {
                            return imagery.state === ImageryState.READY;
                        }).then(function() {
                            expect(imagery.texture).toBeDefined();
                            expect(textureBeforeReprojection).not.toEqual(imagery.texture);
                            imagery.releaseReference();
                        });
                    });
                });
            });
        });
    });

    it('cancels reprojection', function() {
        var provider = createWebMercatorProvider();
        var layer = new ImageryLayer(provider);

        return pollToPromise(function() {
            return provider.ready;
        }).then(function() {
            var imagery = new Imagery(layer, 0, 0, 0);
            imagery.addReference();
            layer._requestImagery(imagery);

            return pollToPromise(function() {
                return imagery.state === ImageryState.RECEIVED;
            }).then(function() {
                layer._createTexture(scene.context, imagery);

                return pollToPromise(function() {
                    return imagery.state === ImageryState.TEXTURE_LOADED;
                }).then(function() {
                    layer._reprojectTexture(scene.frameState, imagery);
                    layer.cancelReprojections();
                    layer.queueReprojectionCommands(scene.frameState);
                    expect(scene.frameState.commandList.length).toEqual(0);
                });
            });
{'fraction_non_alphanumeric': 0.10361183852957162, 'fraction_numerical': 0.00963486338257819, 'mean_word_length': 1.8684807256235827, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3927664', 'n_tokens_mistral': 5473, 'n_tokens_neox': 5052, 'n_words': 877}