Skip to content

Commit b08fe4b

Browse files
[fix]UT
AI-GEN: 70% Cursor
1 parent 075c7b1 commit b08fe4b

10 files changed

Lines changed: 977 additions & 3 deletions

‎src/mapboxgl/mapping/layers/VideoMapLayer.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export default class VideoMapLayer extends mapboxgl.Evented {
105105
}
106106

107107
_bindTimeUpdates() {
108-
const videoElement = this.video.tech && this.video.tech().el();
108+
const videoElement = this.video && this.video.tech && this.video.tech().el();
109109
if (!videoElement) {
110110
return;
111111
}
@@ -181,7 +181,7 @@ export default class VideoMapLayer extends mapboxgl.Evented {
181181
* @description 播放视频。
182182
*/
183183
play() {
184-
if (this.video) {
184+
if (this.video && typeof this.video.play === 'function') {
185185
this.video.play();
186186
}
187187
const source = this._getVideoSource();
@@ -194,7 +194,7 @@ export default class VideoMapLayer extends mapboxgl.Evented {
194194
* @description 暂停视频。
195195
*/
196196
pause() {
197-
if (this.video) {
197+
if (this.video && typeof this.video.pause === 'function') {
198198
this.video.pause();
199199
}
200200
const source = this._getVideoSource();

‎test/mapboxgl/control/VideoMapDrawControlSpec.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,5 @@ describe('mapboxgl_VideoMapDrawControl', () => {
189189
expect(container.querySelectorAll('button').length).toBe(2);
190190
control.onRemove();
191191
});
192+
192193
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { VideoMapDrawControl } from '../../../src/mapboxgl/control/VideoMapDrawControl';
2+
3+
function createDrawStub() {
4+
return {
5+
activate: jasmine.createSpy('activate'),
6+
changeMode: jasmine.createSpy('changeMode'),
7+
setStyles: jasmine.createSpy('setStyles'),
8+
getStyles: jasmine.createSpy('getStyles').and.returnValue({ point: {} }),
9+
getUiMode: jasmine.createSpy('getUiMode').and.returnValue('simple_select'),
10+
getSelectedIds: jasmine.createSpy('getSelectedIds').and.returnValue([]),
11+
getMode: jasmine.createSpy('getMode').and.returnValue(null),
12+
delete: jasmine.createSpy('delete'),
13+
cancel: jasmine.createSpy('cancel')
14+
};
15+
}
16+
17+
function createVideoMapStub(draw) {
18+
return {
19+
on: jasmine.createSpy('on'),
20+
off: jasmine.createSpy('off'),
21+
_getDraw: jasmine.createSpy('_getDraw').and.returnValue(draw)
22+
};
23+
}
24+
25+
describe('VideoMapDrawControl branches', () => {
26+
it('binds, switches and unbinds VideoMap event listeners', () => {
27+
const firstDraw = createDrawStub();
28+
const nextDraw = createDrawStub();
29+
const firstVideoMap = createVideoMapStub(firstDraw);
30+
const nextVideoMap = createVideoMapStub(nextDraw);
31+
const control = new VideoMapDrawControl();
32+
control.onAdd({});
33+
34+
control.setVideoMap(firstVideoMap);
35+
expect(firstVideoMap.on.calls.count()).toBe(5);
36+
expect(firstDraw.activate).toHaveBeenCalled();
37+
control.setVideoMap(nextVideoMap);
38+
expect(firstVideoMap.off.calls.count()).toBe(5);
39+
expect(nextVideoMap.on.calls.count()).toBe(5);
40+
expect(nextDraw.activate).toHaveBeenCalled();
41+
42+
control.onRemove();
43+
expect(nextVideoMap.off.calls.count()).toBe(5);
44+
expect(nextDraw.changeMode).toHaveBeenCalledWith('simple_select');
45+
});
46+
47+
it('forwards styles and returns null styles without a VideoMap', () => {
48+
const control = new VideoMapDrawControl();
49+
control.setStyles({ point: { 'circle-color': '#f00' } });
50+
expect(control.getStyles()).toBeNull();
51+
52+
const draw = createDrawStub();
53+
const videoMap = createVideoMapStub(draw);
54+
control.setVideoMap(videoMap);
55+
const styles = { line: { 'line-width': 5 } };
56+
control.setStyles(styles);
57+
expect(videoMap._getDraw).toHaveBeenCalledWith({ styles: control.options.styles });
58+
expect(draw.setStyles).toHaveBeenCalledWith(styles);
59+
expect(control.getStyles()).toEqual({ point: {} });
60+
});
61+
62+
it('handles missing targets and each draw/trash button action', () => {
63+
const control = new VideoMapDrawControl();
64+
control._onButtonClick({ mode: 'draw_point' });
65+
66+
const draw = createDrawStub();
67+
const videoMap = createVideoMapStub(draw);
68+
control.setVideoMap(videoMap);
69+
control._onButtonClick({ mode: 'draw_point' });
70+
expect(draw.changeMode).toHaveBeenCalledWith('draw_point');
71+
72+
draw.getUiMode.and.returnValue('draw_point');
73+
control._onButtonClick({ mode: 'draw_point' });
74+
expect(draw.changeMode).toHaveBeenCalledWith('simple_select');
75+
76+
draw.getSelectedIds.and.returnValue(['feature-1']);
77+
control._onButtonClick({ mode: 'trash' });
78+
expect(draw.delete).toHaveBeenCalled();
79+
80+
draw.getSelectedIds.and.returnValue([]);
81+
draw.getMode.and.returnValue('LineString');
82+
control._onButtonClick({ mode: 'trash' });
83+
expect(draw.cancel).toHaveBeenCalled();
84+
});
85+
});
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import CoordTransfer from '../../../src/mapboxgl/mapping/CoordTransfer';
2+
3+
function createCv() {
4+
function Mat() {
5+
this.data64F = [1, 0, 0, 0, 1, 0, 0, 0, 1];
6+
this.delete = jasmine.createSpy('delete');
7+
this.inv = () => this;
8+
}
9+
10+
return {
11+
CV_64FC1: 'CV_64FC1',
12+
Mat,
13+
matFromArray: (rows, cols, type, values) => {
14+
const mat = new Mat(rows, cols, type);
15+
mat.data64F = values;
16+
return mat;
17+
},
18+
Rodrigues: jasmine.createSpy('Rodrigues'),
19+
gemm: jasmine.createSpy('gemm'),
20+
multiply: jasmine.createSpy('multiply'),
21+
subtract: jasmine.createSpy('subtract')
22+
};
23+
}
24+
25+
describe('CoordTransfer', () => {
26+
it('should return null for invalid camera input and points behind the camera', () => {
27+
const transfer = new CoordTransfer(null, {});
28+
29+
expect(transfer.toRotationMatrix(0, 0, 0)).toBeUndefined();
30+
expect(transfer.toCameraCoordinate([1])).toBeNull();
31+
expect(transfer.projectCameraToVideo([1, 2, 3])).toBeNull();
32+
33+
transfer._fx = 100;
34+
transfer._fy = 100;
35+
transfer._cx = 10;
36+
transfer._cy = 20;
37+
expect(transfer.projectCameraToVideo([1, 2, 0])).toBeNull();
38+
expect(transfer.projectCameraToVideo([1, 2, 2])).toEqual([60, 120]);
39+
});
40+
41+
it('should convert camera coordinates and video coordinates', () => {
42+
const transfer = new CoordTransfer(null, {});
43+
transfer._R = [1, 0, 0, 0, 1, 0, 0, 0, 1];
44+
transfer._t = [10, 20, 30];
45+
transfer._fx = 100;
46+
transfer._fy = 200;
47+
transfer._cx = 5;
48+
transfer._cy = 6;
49+
50+
expect(transfer.toCameraCoordinate([1, 2])).toEqual([11, 22, 30]);
51+
const videoCoordinate = transfer.toVideoCoordinate([1, 2]);
52+
expect(videoCoordinate.data64F[0]).toBeCloseTo(41.6667, 4);
53+
expect(videoCoordinate.data64F[1]).toBeCloseTo(152.6667, 4);
54+
expect(videoCoordinate.depth).toBe(30);
55+
expect(transfer.toVideoCoordinate([1])).toEqual({ data64F: [], depth: null });
56+
});
57+
58+
it('should build camera matrices and release replaced matrices', () => {
59+
const cv = createCv();
60+
const transfer = new CoordTransfer(cv, {});
61+
transfer.rotationMatrix = new cv.Mat();
62+
transfer.rotationMat3 = new cv.Mat();
63+
transfer.translationMatrix = new cv.Mat();
64+
transfer.k = new cv.Mat();
65+
transfer.setCameraLocation({
66+
pitch: 0,
67+
roll: 0,
68+
yaw: 0,
69+
x: 1,
70+
y: 2,
71+
z: 3,
72+
fx: 100,
73+
fy: 200,
74+
centerX: 10,
75+
centerY: 20
76+
});
77+
78+
expect(cv.Rodrigues).toHaveBeenCalled();
79+
expect(cv.gemm).toHaveBeenCalled();
80+
expect(transfer.toCameraMatrix(100, 200, 10, 20).data64F).toEqual([
81+
100, 0, 10, 0, 200, 20, 0, 0, 1
82+
]);
83+
});
84+
});

0 commit comments

Comments
 (0)