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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import { Cartesian3 } from "../../Source/Cesium.js";
import { Color } from "../../Source/Cesium.js";
import { JulianDate } from "../../Source/Cesium.js";
import { ReferenceFrame } from "../../Source/Cesium.js";
import { BillboardGraphics } from "../../Source/Cesium.js";
import { ColorMaterialProperty } from "../../Source/Cesium.js";
import { ConstantPositionProperty } from "../../Source/Cesium.js";
import { ConstantProperty } from "../../Source/Cesium.js";
import { Entity } from "../../Source/Cesium.js";
import { EntityCollection } from "../../Source/Cesium.js";
import { ReferenceProperty } from "../../Source/Cesium.js";
 
describe("DataSources/ReferenceProperty", function () {
  var time = JulianDate.now();
 
  it("constructor sets expected values", function () {
    var collection = new EntityCollection();
    var objectId = "testId";
    var propertyNames = ["foo", "bar", "baz"];
    var property = new ReferenceProperty(collection, objectId, propertyNames);
 
    expect(property.targetCollection).toBe(collection);
    expect(property.targetId).toEqual(objectId);
    expect(property.targetPropertyNames).toEqual(propertyNames);
  });
 
  it("fromString sets expected values", function () {
    var collection = new EntityCollection();
    var objectId = "testId";
    var propertyNames = ["foo", "bar", "baz"];
 
    var property = ReferenceProperty.fromString(
      collection,
      "testId#foo.bar.baz"
    );
 
    expect(property.targetCollection).toBe(collection);
    expect(property.targetId).toEqual(objectId);
    expect(property.targetPropertyNames).toEqual(propertyNames);
  });
 
  it("fromString works with escaped values", function () {
    var collection = new EntityCollection();
    var objectId = "#identif\\#ier.";
    var propertyNames = ["propertyName", ".abc\\", "def"];
    var property = ReferenceProperty.fromString(
      collection,
      "\\#identif\\\\\\#ier\\.#propertyName.\\.abc\\\\.def"
    );
 
    expect(property.targetCollection).toBe(collection);
    expect(property.targetId).toEqual(objectId);
    expect(property.targetPropertyNames).toEqual(propertyNames);
  });
 
  it("properly tracks resolved property", function () {
    var testObject = new Entity({
      id: "testId",
    });
    testObject.billboard = new BillboardGraphics();
    testObject.billboard.scale = new ConstantProperty(5);
 
    var collection = new EntityCollection();
    collection.add(testObject);
 
    // Basic property resolution
    var property = ReferenceProperty.fromString(
      collection,
      "testId#billboard.scale"
    );
    expect(property.referenceFrame).toBeUndefined();
    expect(property.isConstant).toEqual(true);
    expect(property.resolvedProperty).toBe(testObject.billboard.scale);
    expect(property.getValue(time)).toEqual(5);
 
    var listener = jasmine.createSpy("listener");
    property.definitionChanged.addEventListener(listener);
 
    // A change to exist target property is reflected in reference.
    testObject.billboard.scale.setValue(6);
    expect(listener).toHaveBeenCalledWith(property);
    expect(property.isConstant).toEqual(true);
    expect(property.getValue(time)).toEqual(6);
    listener.calls.reset();
 
    // Assignment of new leaf property to existing target is reflected in reference.
    testObject.billboard.scale = new ConstantProperty(7);
    expect(listener).toHaveBeenCalledWith(property);
    expect(property.isConstant).toEqual(true);
    expect(property.getValue(time)).toEqual(7);
    listener.calls.reset();
 
    // Assignment of non-leaf property to existing target is reflected in reference.
    testObject.billboard = new BillboardGraphics();
    testObject.billboard.scale = new ConstantProperty(8);
    expect(listener).toHaveBeenCalledWith(property);
    expect(property.isConstant).toEqual(true);
    expect(property.getValue(time)).toEqual(8);
    listener.calls.reset();
 
    // Removing an object should cause the reference to be severed.
    collection.remove(testObject);
 
    expect(listener).not.toHaveBeenCalled();
    expect(property.isConstant).toEqual(true);
    expect(property.getValue(time)).toBeUndefined();
    listener.calls.reset();
 
    // Adding a new object should re-wire the reference.
    var testObject2 = new Entity({
      id: "testId",
    });
    testObject2.billboard = new BillboardGraphics();
    testObject2.billboard.scale = new ConstantProperty(9);
    collection.add(testObject2);
    expect(listener).toHaveBeenCalledWith(property);
    expect(property.isConstant).toEqual(true);
    expect(property.getValue(time)).toEqual(9);
 
    // setting the target property to undefined should cause the reference to be severed.
    testObject2.billboard.scale = undefined;
    expect(listener).toHaveBeenCalledWith(property);
    expect(property.isConstant).toEqual(true);
    expect(property.getValue(time)).toBeUndefined();
 
    // Assigning a valid property should re-connect the reference.
    testObject2.billboard.scale = new ConstantProperty(10);
    expect(listener).toHaveBeenCalledWith(property);
    expect(property.isConstant).toEqual(true);
    expect(property.getValue(time)).toEqual(10);
  });
 
  it("works with position properties", function () {
    var testObject = new Entity({
      id: "testId",
    });
    testObject.position = new ConstantPositionProperty(
      new Cartesian3(1, 2, 3),
      ReferenceFrame.FIXED
    );
 
    var collection = new EntityCollection();
    collection.add(testObject);
 
    // Basic property resolution
    var property = ReferenceProperty.fromString(collection, "testId#position");
    expect(property.isConstant).toEqual(true);
    expect(property.referenceFrame).toEqual(ReferenceFrame.FIXED);
    expect(property.getValue(time)).toEqual(testObject.position.getValue(time));
    expect(
      property.getValueInReferenceFrame(time, ReferenceFrame.INERTIAL)
    ).toEqual(
      testObject.position.getValueInReferenceFrame(
        time,
        ReferenceFrame.INERTIAL
      )
    );
 
    property = ReferenceProperty.fromString(collection, "nonExistent#position");
    expect(property.isConstant).toEqual(true);
    expect(property.referenceFrame).toBeUndefined();
    expect(property.getValue(time)).toBeUndefined();
    expect(
      property.getValueInReferenceFrame(time, ReferenceFrame.INERTIAL)
    ).toBeUndefined();
  });
 
  it("works with material properties", function () {
    var testObject = new Entity({
      id: "testId",
    });
    testObject.addProperty("testMaterial");
    testObject.testMaterial = new ColorMaterialProperty(Color.WHITE);
 
    var collection = new EntityCollection();
    collection.add(testObject);
 
    // Basic property resolution
    var property = ReferenceProperty.fromString(
      collection,
      "testId#testMaterial"
    );
    expect(property.isConstant).toEqual(true);
    expect(property.getType(time)).toEqual(
      testObject.testMaterial.getType(time)
    );
    expect(property.getValue(time)).toEqual(
      testObject.testMaterial.getValue(time)
    );
 
    property = ReferenceProperty.fromString(
      collection,
      "nonExistent#testMaterial"
    );
    expect(property.isConstant).toEqual(true);
    expect(property.referenceFrame).toBeUndefined();
    expect(property.getType(time)).toBeUndefined();
    expect(property.getValue(time)).toBeUndefined();
  });
 
  it("equals works", function () {
    var entityCollection = new EntityCollection();
 
    var left = ReferenceProperty.fromString(
      entityCollection,
      "objectId#foo.bar"
    );
    var right = ReferenceProperty.fromString(
      entityCollection,
      "objectId#foo.bar"
    );
    expect(left.equals(right)).toEqual(true);
 
    // collection differs
    right = ReferenceProperty.fromString(
      new EntityCollection(),
      "objectId#foo.bar"
    );
    expect(left.equals(right)).toEqual(false);
 
    // target id differs
    right = ReferenceProperty.fromString(
      entityCollection,
      "otherObjectId#foo.bar"
    );
    expect(left.equals(right)).toEqual(false);
 
    // number of sub-properties differ
    right = ReferenceProperty.fromString(entityCollection, "objectId#foo");
    expect(left.equals(right)).toEqual(false);
 
    // sub-properties of same length differ
    right = ReferenceProperty.fromString(entityCollection, "objectId#foo.baz");
    expect(left.equals(right)).toEqual(false);
  });
 
  it("does not raise definition changed when target entity has not changed.", function () {
    var testObject = new Entity({
      id: "testId",
    });
    testObject.billboard = new BillboardGraphics();
    testObject.billboard.scale = new ConstantProperty(5);
 
    var otherObject = new Entity({
      id: "other",
    });
 
    var collection = new EntityCollection();
    collection.add(testObject);
    collection.add(otherObject);
 
    var property = ReferenceProperty.fromString(
      collection,
      "testId#billboard.scale"
    );
    expect(property.resolvedProperty).toBe(testObject.billboard.scale);
 
    var listener = jasmine.createSpy("listener");
    property.definitionChanged.addEventListener(listener);
 
    collection.remove(otherObject);
    expect(listener).not.toHaveBeenCalled();
    collection.remove(testObject);
    expect(listener).not.toHaveBeenCalled();
  });
 
  it("attaches to a target entity created later", function () {
    var collection = new EntityCollection();
 
    var property = ReferenceProperty.fromString(
      collection,
      "testId#billboard.scale"
    );
    expect(property.resolvedProperty).toBeUndefined();
 
    var listener = jasmine.createSpy("listener");
    property.definitionChanged.addEventListener(listener);
 
    var otherObject = new Entity({
      id: "other",
    });
    collection.add(otherObject);
 
    expect(listener).not.toHaveBeenCalled();
    expect(property.resolvedProperty).toBeUndefined();
 
    var testObject = new Entity({
      id: "testId",
    });
    testObject.billboard = new BillboardGraphics();
    testObject.billboard.scale = new ConstantProperty(5);
    collection.add(testObject);
 
    expect(listener).toHaveBeenCalledWith(property);
    expect(property.resolvedProperty).toBe(testObject.billboard.scale);
  });
 
  it("constructor throws with undefined targetCollection", function () {
    expect(function () {
      return new ReferenceProperty(undefined, "objectid", ["property"]);
    }).toThrowDeveloperError();
  });
 
  it("constructor throws with undefined targetId", function () {
    expect(function () {
      return new ReferenceProperty(new EntityCollection(), undefined, [
        "property",
      ]);
    }).toThrowDeveloperError();
  });
 
  it("constructor throws with undefined targetPropertyNames", function () {
    expect(function () {
      return new ReferenceProperty(
        new EntityCollection(),
        "objectId",
        undefined
      );
    }).toThrowDeveloperError();
  });
 
  it("constructor throws with empty targetPropertyNames", function () {
    expect(function () {
      return new ReferenceProperty(new EntityCollection(), "objectId", []);
    }).toThrowDeveloperError();
  });
 
  it("constructor throws with empty targetId", function () {
    expect(function () {
      return new ReferenceProperty(new EntityCollection(), "", ["property"]);
    }).toThrowDeveloperError();
  });
 
  it("fromString throws with undefined targetCollection", function () {
    expect(function () {
      return ReferenceProperty.fromString(undefined, "objectid#property");
    }).toThrowDeveloperError();
  });
 
  it("fromString throws with undefined referenceString", function () {
    expect(function () {
      return ReferenceProperty.fromString(new EntityCollection(), undefined);
    }).toThrowDeveloperError();
  });
 
  it("fromString throws with invalid referenceString", function () {
    expect(function () {
      return ReferenceProperty.fromString(new EntityCollection(), "foo");
    }).toThrowDeveloperError();
 
    expect(function () {
      return ReferenceProperty.fromString(new EntityCollection(), "foo#");
    }).toThrowDeveloperError();
 
    expect(function () {
      return ReferenceProperty.fromString(new EntityCollection(), "#bar");
    }).toThrowDeveloperError();
  });
 
  it("getValue returns undefined if target entity can not be resolved", function () {
    var collection = new EntityCollection();
    var property = ReferenceProperty.fromString(collection, "testId#foo.bar");
    expect(property.getValue(time)).toBeUndefined();
  });
 
  it("getValue returns undefined if target property can not be resolved", function () {
    var collection = new EntityCollection();
 
    var testObject = new Entity({
      id: "testId",
    });
    collection.add(testObject);
 
    var property = ReferenceProperty.fromString(collection, "testId#billboard");
    expect(property.getValue(time)).toBeUndefined();
  });
 
  it("getValue returns undefined if sub-property of target property can not be resolved", function () {
    var collection = new EntityCollection();
 
    var testObject = new Entity({
      id: "testId",
    });
    testObject.billboard = new BillboardGraphics();
    collection.add(testObject);
 
    var property = ReferenceProperty.fromString(
      collection,
      "testId#billboard.foo"
    );
    expect(property.getValue(time)).toBeUndefined();
  });
});