Skip to content

Commit 24c64b6

Browse files
committed
补充三个端高性能图层相关 UT。
1 parent 99b404b commit 24c64b6

File tree

11 files changed

+1000
-9
lines changed

11 files changed

+1000
-9
lines changed

src/openlayers/overlay/graphic/Graphic.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ export class Graphic extends ol.Object {
4848
/**
4949
* @function ol.Graphic.prototype.setId
5050
* @description 设置当前要素 ID。
51-
* @param {string} id - 要素 ID。
51+
* @param {string} id - 要素 ID。
5252
*/
53-
5453
setId(id) {
5554
this.id = id;
5655
}

test/leaflet/overlay/GraphicLayerSpec.js

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import '../../libs/deck.gl/5.1.3/deck.gl';
21
import {graphicLayer} from '../../../src/leaflet/overlay/GraphicLayer';
32
import {tiledMapLayer} from '../../../src/leaflet/mapping/TiledMapLayer';
43
import {circleStyle} from '../../../src/leaflet/overlay/graphic/CircleStyle';
@@ -80,5 +79,126 @@ describe('leaflet_GraphicLayer', () => {
8079
layer.remove();
8180
}, 1000)
8281
});
82+
83+
describe("GraphicLayer_graphic 相关", () => {
84+
let layer, graphics = [];
85+
const coors = [
86+
[-35.16, 38.05],
87+
[-36.16, 39.05],
88+
[-36.16, 40.05],
89+
[-37.16, 40.05],
90+
[-38.16, 39.05]
91+
];
92+
beforeAll(() => {
93+
for (let j = 0; j < coors.length; ++j) {
94+
graphics[j] = graphic({
95+
latLng: L.latLng(coors[j][0], coors[j][1])
96+
});
97+
graphics[j].setId(j);
98+
graphics[j].setAttributes({name: "graphic_" + j});
99+
}
100+
101+
});
102+
103+
it("getGraphicBy add getGraphicById", (done) => {
104+
layer = graphicLayer(graphics).addTo(map);
105+
setTimeout(() => {
106+
const graphic = layer.getGraphicBy("id", 1);
107+
expect(graphic).not.toBeNull();
108+
expect(graphic.getId()).toEqual(1);
109+
110+
const graphic1 = layer.getGraphicById(1);
111+
expect(graphic1.getId()).toEqual(1);
112+
113+
done();
114+
}, 1000);
115+
116+
});
117+
118+
it("getGraphicsByAttribute", (done) => {
119+
layer = graphicLayer(graphics).addTo(map);
120+
setTimeout(() => {
121+
const graphic = layer.getGraphicsByAttribute("name", "graphic_1");
122+
expect(graphic).not.toBeNull();
123+
expect(graphic[0].getAttributes().name).toBe("graphic_1");
124+
done();
125+
}, 1000);
126+
127+
});
128+
129+
it("removeGraphics", (done) => {
130+
layer = graphicLayer(graphics).addTo(map);
131+
setTimeout(() => {
132+
//删除单个
133+
let deleteGraphic = graphics[0];
134+
expect(layer.graphics.length).toEqual(5);
135+
layer.removeGraphics(deleteGraphic);
136+
expect(layer.graphics.length).toEqual(4);
137+
138+
//多个
139+
deleteGraphic = [graphics[1], graphics[2]];
140+
layer.removeGraphics(deleteGraphic);
141+
expect(layer.graphics.length).toEqual(2);
142+
143+
//默认
144+
layer.removeGraphics();
145+
expect(layer.graphics.length).toEqual(0);
146+
147+
done();
148+
}, 1000);
149+
});
150+
151+
it("getState", (done) => {
152+
layer = graphicLayer(graphics).addTo(map);
153+
setTimeout(() => {
154+
const state = layer.getState();
155+
expect(state).not.toBeNull();
156+
expect(state.color).toBe("#3388ff");
157+
done();
158+
}, 1000);
159+
});
160+
161+
it("setStyle", (done) => {
162+
layer = graphicLayer(graphics, {
163+
render: "canvas",
164+
color: [0, 0, 0, 255]
165+
}).addTo(map);
166+
setTimeout(() => {
167+
expect(layer.options.color).toEqual([0, 0, 0, 255]);
168+
layer.setStyle({color: "blue"});
169+
expect(layer.options.color).toEqual("blue");
170+
done();
171+
}, 4000);
172+
});
173+
174+
it("addGraphics", (done) => {
175+
layer = graphicLayer(graphics).addTo(map);
176+
setTimeout(() => {
177+
layer.addGraphics(graphics);
178+
expect(layer.graphics.length).toEqual(10);
179+
done();
180+
}, 4000);
181+
});
182+
183+
it("setGraphics", (done) => {
184+
layer = graphicLayer(graphics).addTo(map);
185+
setTimeout(() => {
186+
layer.clear();
187+
expect(layer.graphics.length).toEqual(0);
188+
let graphics = [];
189+
for (let j = 0; j < coors.length; ++j) {
190+
graphics[j] = graphic({
191+
latLng: L.latLng(coors[j][0], coors[j][1])
192+
});
193+
graphics[j].setId(j);
194+
graphics[j].setAttributes({name: "graphic_" + j});
195+
}
196+
layer.setGraphics(graphics);
197+
expect(layer.graphics.length).toEqual(5);
198+
done();
199+
}, 4000);
200+
});
201+
});
202+
83203
});
84204

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {graphic as graphicObj} from '../../../../src/leaflet/overlay/graphic/Graphic';
2+
3+
describe('leaflet_Graphic', () => {
4+
//todo
5+
var graphic, option = {
6+
latLng: L.latLng(-35.16, 38.05)
7+
};
8+
it("constructor", () => {
9+
graphic = graphicObj(option);
10+
11+
expect(graphic).not.toBeNull();
12+
expect(graphic._latLng).not.toBeNull();
13+
expect(graphic._latLng.lat).toEqual(-35.16);
14+
expect(graphic._latLng.lng).toEqual(38.05);
15+
16+
graphic = null;
17+
});
18+
it("setId add getId", () => {
19+
graphic = graphicObj(option);
20+
expect(graphic.getId()).toBeNull();
21+
graphic.setId("123");
22+
expect(graphic.getId()).toBe("123");
23+
graphic = null;
24+
});
25+
it("setAttributes add getAttributes", () => {
26+
graphic = graphicObj(option);
27+
expect(graphic.getAttributes()).toBeUndefined();
28+
29+
graphic.setAttributes({name: "graphic", type: "point"});
30+
expect(graphic.getAttributes().name).toBe("graphic");
31+
expect(graphic.getAttributes().type).toBe("point");
32+
graphic = null;
33+
});
34+
});

test/mapboxgl/overlay/DeckglLayerSpec.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,118 @@ describe('mapboxgl_DeckglLayer', () => {
248248
done();
249249
}, 3000)
250250
});
251+
252+
it("getGraphicBy add getGraphicById", (done) => {
253+
let graphics = [];
254+
map = new ol.Map({
255+
target: 'map',
256+
view: new ol.View({
257+
center: [0, 0],
258+
zoom: 2,
259+
projection: 'EPSG:4326'
260+
}),
261+
renderer: ['canvas']
262+
});
263+
for (let j = 0; j < coors.length; ++j) {
264+
graphics[j] = new ol.Graphic(new ol.geom.Point(coors[j]));
265+
graphics[j].setId(j);
266+
graphics[j].setAttributes({name: "graphic_" + j});
267+
}
268+
const graphicLayer = new ol.layer.Image({
269+
source: new Graphic({
270+
graphics: graphics,
271+
map: map
272+
})
273+
});
274+
map.addLayer(graphicLayer);
275+
276+
setTimeout(() => {
277+
const graphic = graphicLayer.getSource().getGraphicBy("id", 1);
278+
expect(graphic).not.toBeNull();
279+
expect(graphic.getId()).toEqual(1);
280+
281+
const graphic1 = graphicLayer.getSource().getGraphicById(1);
282+
expect(graphic1.getId()).toEqual(1);
283+
284+
map.removeLayer(graphicLayer);
285+
done();
286+
}, 4000)
287+
288+
289+
});
290+
it("getGraphicsByAttribute", (done) => {
291+
let graphics = [];
292+
map = new ol.Map({
293+
target: 'map',
294+
view: new ol.View({
295+
center: [0, 0],
296+
zoom: 2,
297+
projection: 'EPSG:4326'
298+
}),
299+
renderer: ['canvas']
300+
});
301+
for (let j = 0; j < coors.length; ++j) {
302+
graphics[j] = new ol.Graphic(new ol.geom.Point(coors[j]));
303+
graphics[j].setId(j);
304+
graphics[j].setAttributes({name: "graphic_" + j});
305+
}
306+
const graphicLayer = new ol.layer.Image({
307+
source: new Graphic({
308+
graphics: graphics,
309+
map: map
310+
})
311+
});
312+
map.addLayer(graphicLayer);
313+
314+
setTimeout(() => {
315+
const graphic = graphicLayer.getSource().getGraphicsByAttribute("name", "graphic_1");
316+
expect(graphic).not.toBeNull();
317+
expect(graphic[0].getAttributes().name).toBe("graphic_1");
318+
map.removeLayer(graphicLayer);
319+
done();
320+
}, 4000);
321+
});
322+
it("removeGraphics", () => {
323+
let graphics = [];
324+
map = new ol.Map({
325+
target: 'map',
326+
view: new ol.View({
327+
center: [0, 0],
328+
zoom: 2,
329+
projection: 'EPSG:4326'
330+
}),
331+
renderer: ['canvas']
332+
});
333+
for (let j = 0; j < coors.length; ++j) {
334+
graphics[j] = new ol.Graphic(new ol.geom.Point(coors[j]));
335+
graphics[j].setId(j);
336+
graphics[j].setAttributes({name: "graphic_" + j});
337+
}
338+
const graphicLayer = new ol.layer.Image({
339+
source: new Graphic({
340+
graphics: graphics,
341+
map: map
342+
})
343+
});
344+
map.addLayer(graphicLayer);
345+
346+
setTimeout(() => {
347+
const graphicSource = graphicLayer.getSource();
348+
//删除单个
349+
let deleteGraphic = graphic[0];
350+
expect(graphicSource.graphics.length).toEqual(5);
351+
graphicSource.removeGraphics(deleteGraphic);
352+
expect(graphicSource.graphics.length).toEqual(4);
353+
354+
//多个
355+
deleteGraphic = [graphic[1], graphic[2]];
356+
graphicSource.removeGraphics(deleteGraphic);
357+
expect(graphicSource.graphics.length).toEqual(2);
358+
359+
//默认
360+
graphicSource.removeGraphics();
361+
expect(graphicSource.graphics.length).toEqual(0);
362+
}, 4000);
363+
364+
});
251365
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy