15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import {
  Resource,
  TextureManager,
  TextureUniform,
  when,
} from "../../../Source/Cesium.js";
import createScene from "../../createScene.js";
import pollToPromise from "../../pollToPromise.js";
 
describe(
  "Scene/ModelExperimental/TextureManager",
  function () {
    var scene;
 
    beforeAll(function () {
      scene = createScene();
    });
 
    afterAll(function () {
      scene.destroyForSpecs();
    });
 
    var textureManagers = [];
    afterEach(function () {
      for (var i = 0; i < textureManagers.length; i++) {
        var textureManager = textureManagers[i];
        if (!textureManager.isDestroyed()) {
          textureManager.destroy();
        }
      }
      textureManagers.length = 0;
    });
 
    function waitForTextureLoad(textureManager, textureId) {
      var oldValue = textureManager.getTexture(textureId);
      return pollToPromise(function () {
        scene.renderForSpecs();
        textureManager.update(scene.frameState);
 
        // Checking that the texture changed allows the waitForTextureLoad()
        // to be called multiple times in one promise chain.
        return textureManager.getTexture(textureId) !== oldValue;
      }).then(function () {
        return textureManager.getTexture(textureId);
      });
    }
    var blueUrl = "Data/Images/Blue2x2.png";
    var greenUrl = "Data/Images/Green1x4.png";
 
    it("constructs", function () {
      var textureManager = new TextureManager();
      textureManagers.push(textureManager);
      expect(textureManager._textures).toEqual({});
      expect(textureManager._loadedImages).toEqual([]);
    });
 
    it("loads texture from a URL", function () {
      var textureManager = new TextureManager();
      textureManagers.push(textureManager);
      var id = "testTexture";
 
      textureManager.loadTexture2D(
        id,
        new TextureUniform({
          url: blueUrl,
        })
      );
 
      return waitForTextureLoad(textureManager, id).then(function (texture) {
        expect(texture.width).toBe(2);
        expect(texture.height).toBe(2);
      });
    });
 
    it("loads texture from a typed array", function () {
      var textureManager = new TextureManager();
      textureManagers.push(textureManager);
      var id = "testTexture";
 
      textureManager.loadTexture2D(
        id,
        new TextureUniform({
          typedArray: new Uint8Array([255, 0, 0, 255, 0, 255, 0, 255]),
          width: 1,
          height: 2,
        })
      );
 
      return waitForTextureLoad(textureManager, id).then(function (texture) {
        expect(texture.width).toBe(1);
        expect(texture.height).toBe(2);
      });
    });
 
    it("destroys old texture before adding a new one", function () {
      var textureManager = new TextureManager();
      textureManagers.push(textureManager);
      var id = "testTexture";
 
      textureManager.loadTexture2D(
        id,
        new TextureUniform({
          url: blueUrl,
        })
      );
 
      return waitForTextureLoad(textureManager, id).then(function (
        blueTexture
      ) {
        expect(blueTexture.width).toBe(2);
        expect(blueTexture.height).toBe(2);
        expect(blueTexture.isDestroyed()).toBe(false);
 
        textureManager.loadTexture2D(
          id,
          new TextureUniform({
            url: greenUrl,
          })
        );
        return waitForTextureLoad(textureManager, id).then(function (
          greenTexture
        ) {
          expect(blueTexture.isDestroyed()).toBe(true);
          expect(greenTexture.width).toBe(1);
          expect(greenTexture.height).toBe(4);
          expect(greenTexture.isDestroyed()).toBe(false);
        });
      });
    });
 
    it("getTexture returns undefined for unknown texture", function () {
      var textureManager = new TextureManager();
      textureManagers.push(textureManager);
      var texture = textureManager.getTexture("notATexture");
      expect(texture).not.toBeDefined();
    });
 
    it("sets a defaultTexture on error", function () {
      spyOn(Resource.prototype, "fetchImage").and.returnValue(when.reject());
      var textureManager = new TextureManager();
      textureManagers.push(textureManager);
      var id = "testTexture";
 
      // Call update first to ensure the default texture is available
      // when the fetchImage() call rejects
      textureManager.update(scene.frameState);
      textureManager.loadTexture2D(
        id,
        new TextureUniform({
          url: "https://example.com/not-a-texture.jpg",
        })
      );
      return waitForTextureLoad(textureManager, id)
        .then(function (texture) {
          var defaultTexture = scene.frameState.context.defaultTexture;
          expect(texture).toBe(defaultTexture);
        })
        .otherwise(console.error);
    });
 
    it("destroys", function () {
      var textureManager = new TextureManager();
      textureManagers.push(textureManager);
      var id = "testTexture";
 
      textureManager.loadTexture2D(
        id,
        new TextureUniform({
          url: blueUrl,
        })
      );
 
      return waitForTextureLoad(textureManager, id).then(function (texture) {
        expect(textureManager.isDestroyed()).toBe(false);
        expect(texture.isDestroyed()).toBe(false);
 
        textureManager.destroy();
        expect(textureManager.isDestroyed()).toBe(true);
        expect(texture.isDestroyed()).toBe(true);
      });
    });
  },
  "WebGL"
);