1+ import { WebMap } from '../../../src/openlayers/mapping/WebMap.js' ;
2+ import { FetchRequest } from '../../../src/common/util/FetchRequest' ;
3+ import { Util as CommonUtil } from '../../../src/common/commontypes/Util' ;
4+ import '../../resources/WebMapV5.js' ;
5+ import { ArrayStatistic } from '../../../src/common/util/ArrayStatistic' ;
6+ import { Util } from '../../../src/openlayers/core/Util.js' ;
7+ import { StyleUtils } from '../../../src/openlayers/core/StyleUtils.js' ;
8+ import { DataFlowService } from '../../../src/openlayers/services/index.js' ;
9+ import { Server } from 'mock-socket' ;
10+ import { Object as obj } from 'ol' ;
11+ import Overlay from 'ol/Overlay' ;
12+ import * as olControl from 'ol/control' ;
13+ import Feature from 'ol/Feature' ;
14+ import * as olProj from 'ol/proj' ;
15+ import proj4 from 'proj4' ;
16+ import * as olLayer from 'ol/layer' ;
17+ import cloneDeep from 'lodash.clonedeep' ;
18+
19+ window . jsonsql = { query : ( ) => { } } ;
20+
21+ describe ( 'openlayers_WebMap' , ( ) => {
22+ var originalTimeout , testDiv , webMap ;
23+ var server = 'http://127.0.0.1:8090/iportal/' ;
24+ const originCookie = window . document . cookie ;
25+ const originalNavigator = window . navigator ;
26+ var id = 1788054202 ;
27+ let cookieValue ;
28+ beforeAll ( ( ) => {
29+ Object . defineProperty ( document , 'cookie' , {
30+ get ( ) {
31+ return cookieValue ;
32+ } ,
33+ set ( ) { }
34+ } ) ;
35+ // 重置navigator
36+ Object . defineProperty ( window , 'navigator' , {
37+ value : originalNavigator ,
38+ writable : true
39+ } ) ;
40+ } ) ;
41+
42+ afterAll ( ( ) => {
43+ window . document . cookie = originCookie ;
44+ window . navigator = originalNavigator ;
45+ } ) ;
46+ beforeEach ( ( ) => {
47+ testDiv = window . document . createElement ( 'div' ) ;
48+ testDiv . setAttribute ( 'id' , 'map' ) ;
49+ testDiv . style . styleFloat = 'left' ;
50+ testDiv . style . marginLeft = '8px' ;
51+ testDiv . style . marginTop = '50px' ;
52+ testDiv . style . width = '50px' ;
53+ testDiv . style . height = '50px' ;
54+ window . document . body . appendChild ( testDiv ) ;
55+ originalTimeout = jasmine . DEFAULT_TIMEOUT_INTERVAL ;
56+ jasmine . DEFAULT_TIMEOUT_INTERVAL = 50000 ;
57+
58+
59+
60+ } ) ;
61+ afterEach ( ( ) => {
62+ webMap = null ;
63+ window . document . body . removeChild ( testDiv ) ;
64+ jasmine . DEFAULT_TIMEOUT_INTERVAL = originalTimeout ;
65+ } ) ;
66+
67+ it ( 'getCookie' , ( ) => {
68+ cookieValue = 'testKey=testValue;language=zh-CN;another=value' ;
69+
70+ spyOn ( FetchRequest , 'get' ) . and . callFake ( ( url ) => {
71+ if ( url . indexOf ( 'map.json' ) > - 1 ) {
72+ var mapJson = datavizWebmap_ZXYTILE ;
73+ return Promise . resolve ( new Response ( mapJson ) ) ;
74+ }
75+ return Promise . resolve ( new Response ( JSON . stringify ( { } ) ) ) ;
76+ } ) ;
77+ const webmap = new WebMap ( id , { server : server } ) ;
78+ expect ( webmap . getCookie ( 'testKey' ) ) . toBe ( 'testValue' ) ;
79+ expect ( webmap . getCookie ( 'language' ) ) . toBe ( 'zh-CN' ) ;
80+ expect ( webmap . getCookie ( 'nonexistent' ) ) . toBeNull ( ) ;
81+ // 测试大小写不敏感
82+ expect ( webmap . getCookie ( 'TESTKEY' ) ) . toBe ( 'testValue' ) ;
83+ expect ( webmap . getCookie ( 'LANGUAGE' ) ) . toBe ( 'zh-CN' ) ;
84+ } ) ;
85+
86+ it ( 'formatCookieLang' , ( ) => {
87+ const webmap = new WebMap ( id , { server : server } ) ;
88+
89+ // 测试各种语言代码映射
90+ expect ( webmap . formatCookieLang ( 'zh' ) ) . toBe ( 'zh-CN' ) ;
91+ expect ( webmap . formatCookieLang ( 'ar' ) ) . toBe ( 'ar-EG' ) ;
92+ expect ( webmap . formatCookieLang ( 'bg' ) ) . toBe ( 'bg-BG' ) ;
93+ expect ( webmap . formatCookieLang ( 'ca' ) ) . toBe ( 'ca-ES' ) ;
94+ expect ( webmap . formatCookieLang ( 'cs' ) ) . toBe ( 'cs-CZ' ) ;
95+ expect ( webmap . formatCookieLang ( 'da' ) ) . toBe ( 'da-DK' ) ;
96+ expect ( webmap . formatCookieLang ( 'de' ) ) . toBe ( 'de-DE' ) ;
97+ expect ( webmap . formatCookieLang ( 'el' ) ) . toBe ( 'el-GR' ) ;
98+ expect ( webmap . formatCookieLang ( 'es' ) ) . toBe ( 'es-ES' ) ;
99+ expect ( webmap . formatCookieLang ( 'et' ) ) . toBe ( 'et-EE' ) ;
100+ expect ( webmap . formatCookieLang ( 'fa' ) ) . toBe ( 'fa-IR' ) ;
101+ expect ( webmap . formatCookieLang ( 'fl' ) ) . toBe ( 'fi-FI' ) ;
102+ expect ( webmap . formatCookieLang ( 'fr' ) ) . toBe ( 'fr-FR' ) ;
103+ expect ( webmap . formatCookieLang ( 'he' ) ) . toBe ( 'he-IL' ) ;
104+ expect ( webmap . formatCookieLang ( 'hu' ) ) . toBe ( 'hu-HU' ) ;
105+ expect ( webmap . formatCookieLang ( 'id' ) ) . toBe ( 'id-ID' ) ;
106+ expect ( webmap . formatCookieLang ( 'is' ) ) . toBe ( 'is-IS' ) ;
107+ expect ( webmap . formatCookieLang ( 'it' ) ) . toBe ( 'it-IT' ) ;
108+ expect ( webmap . formatCookieLang ( 'ja' ) ) . toBe ( 'ja-JP' ) ;
109+ expect ( webmap . formatCookieLang ( 'ko' ) ) . toBe ( 'ko-KR' ) ;
110+ expect ( webmap . formatCookieLang ( 'ku' ) ) . toBe ( 'ku-IQ' ) ;
111+ expect ( webmap . formatCookieLang ( 'mn' ) ) . toBe ( 'mn-MN' ) ;
112+ expect ( webmap . formatCookieLang ( 'nb' ) ) . toBe ( 'nb-NO' ) ;
113+ expect ( webmap . formatCookieLang ( 'ne' ) ) . toBe ( 'ne-NP' ) ;
114+ expect ( webmap . formatCookieLang ( 'nl' ) ) . toBe ( 'nl-NL' ) ;
115+ expect ( webmap . formatCookieLang ( 'pl' ) ) . toBe ( 'pl-PL' ) ;
116+ expect ( webmap . formatCookieLang ( 'pt' ) ) . toBe ( 'pt-PT' ) ;
117+ expect ( webmap . formatCookieLang ( 'ru' ) ) . toBe ( 'ru-RU' ) ;
118+ expect ( webmap . formatCookieLang ( 'sk' ) ) . toBe ( 'sk-SK' ) ;
119+ expect ( webmap . formatCookieLang ( 'sl' ) ) . toBe ( 'sl-SI' ) ;
120+ expect ( webmap . formatCookieLang ( 'sr' ) ) . toBe ( 'sr-RS' ) ;
121+ expect ( webmap . formatCookieLang ( 'sv' ) ) . toBe ( 'sv-SE' ) ;
122+ expect ( webmap . formatCookieLang ( 'th' ) ) . toBe ( 'th-TH' ) ;
123+ expect ( webmap . formatCookieLang ( 'tr' ) ) . toBe ( 'tr-TR' ) ;
124+ expect ( webmap . formatCookieLang ( 'uk' ) ) . toBe ( 'uk-UA' ) ;
125+ expect ( webmap . formatCookieLang ( 'vi' ) ) . toBe ( 'vi-VN' ) ;
126+ expect ( webmap . formatCookieLang ( 'en' ) ) . toBe ( 'en-US' ) ;
127+
128+ // 测试未知语言代码
129+ expect ( webmap . formatCookieLang ( 'unknown' ) ) . toBe ( 'en-US' ) ;
130+ } ) ;
131+
132+ it ( 'getLang_with_cookie' , ( ) => {
133+ cookieValue = 'language=zh' ;
134+ const webmap = new WebMap ( id , { server : server } ) ;
135+ spyOn ( webmap , 'formatCookieLang' ) . and . returnValue ( 'zh-CN' ) ;
136+ spyOn ( navigator , 'language' ) . and . returnValue ( 'en-US' ) ;
137+
138+ const lang = webmap . getLang ( ) ;
139+ expect ( lang ) . toBe ( 'zh-CN' ) ;
140+ expect ( webmap . formatCookieLang ) . toHaveBeenCalledWith ( 'zh' ) ;
141+ } ) ;
142+
143+ it ( 'getLang_without_cookie' , ( ) => {
144+ cookieValue = '' ;
145+ const webmap = new WebMap ( id , { server : server } ) ;
146+ // 模拟navigator.language属性
147+ Object . defineProperty ( window , 'navigator' , {
148+ value : {
149+ language : 'fr-FR' ,
150+ browserLanguage : undefined
151+ } ,
152+ writable : true
153+ } ) ;
154+
155+ const lang = webmap . getLang ( ) ;
156+ expect ( lang ) . toBe ( 'fr-FR' ) ;
157+ } ) ;
158+
159+ it ( 'getLang_without_cookie_IE_compatibility' , ( ) => {
160+ cookieValue = '' ;
161+ const webmap = new WebMap ( id , { server : server } ) ;
162+ // 模拟navigator没有language属性但有browserLanguage属性
163+ Object . defineProperty ( window , 'navigator' , {
164+ value : {
165+ language : undefined ,
166+ browserLanguage : 'de-DE'
167+ } ,
168+ writable : true
169+ } ) ;
170+
171+ const lang = webmap . getLang ( ) ;
172+ expect ( lang ) . toBe ( 'de-DE' ) ;
173+ } ) ;
174+
175+ it ( 'isSupportWebp' , ( done ) => {
176+ const webmap = new WebMap ( id , { server : server } ) ;
177+ spyOn ( webmap , 'isIE' ) . and . returnValue ( false ) ;
178+ spyOn ( webmap , 'isFirefox' ) . and . returnValue ( false ) ;
179+ spyOn ( webmap , 'isChrome' ) . and . returnValue ( false ) ;
180+ spyOn ( FetchRequest , 'get' ) . and . returnValue ( Promise . resolve ( new Response ( '' , { status : 200 } ) ) ) ;
181+
182+ const url = 'http://fakeurl' ;
183+ const token = null ;
184+ const proxy = false ;
185+
186+ const promise = webmap . isSupportWebp ( url , token , proxy ) ;
187+ expect ( Promise . resolve ( promise ) ) . toBe ( promise ) ; // 检查返回值是否为Promise
188+
189+ promise . then ( ( result ) => {
190+ expect ( result ) . toBe ( true ) ;
191+ expect ( FetchRequest . get ) . toHaveBeenCalled ( ) ;
192+ done ( ) ;
193+ } ) ;
194+ } ) ;
195+
196+ it ( 'isSupportWebp_IE_not_support' , ( ) => {
197+ const webmap = new WebMap ( id , { server : server } ) ;
198+ spyOn ( webmap , 'isIE' ) . and . returnValue ( true ) ;
199+
200+ const url = 'http://fakeurl' ;
201+ const token = null ;
202+ const proxy = false ;
203+
204+ const result = webmap . isSupportWebp ( url , token , proxy ) ;
205+ expect ( result ) . toBe ( false ) ;
206+ } ) ;
207+
208+ it ( 'isSupportWebp_Firefox_old_version_not_support' , ( ) => {
209+ const webmap = new WebMap ( id , { server : server } ) ;
210+ spyOn ( webmap , 'isIE' ) . and . returnValue ( false ) ;
211+ spyOn ( webmap , 'isFirefox' ) . and . returnValue ( true ) ;
212+ spyOn ( webmap , 'getFirefoxVersion' ) . and . returnValue ( 60 ) ;
213+
214+ const url = 'http://fakeurl' ;
215+ const token = null ;
216+ const proxy = false ;
217+
218+ const result = webmap . isSupportWebp ( url , token , proxy ) ;
219+ expect ( result ) . toBe ( false ) ;
220+ } ) ;
221+
222+ it ( 'isSupportWebp_Chrome_old_version_not_support' , ( ) => {
223+ const webmap = new WebMap ( id , { server : server } ) ;
224+ spyOn ( webmap , 'isIE' ) . and . returnValue ( false ) ;
225+ spyOn ( webmap , 'isFirefox' ) . and . returnValue ( false ) ;
226+ spyOn ( webmap , 'isChrome' ) . and . returnValue ( true ) ;
227+ spyOn ( webmap , 'getChromeVersion' ) . and . returnValue ( 25 ) ;
228+
229+ const url = 'http://fakeurl' ;
230+ const token = null ;
231+ const proxy = false ;
232+
233+ const result = webmap . isSupportWebp ( url , token , proxy ) ;
234+ expect ( result ) . toBe ( false ) ;
235+ } ) ;
236+
237+ it ( 'isSupportWebp_request_failed' , ( done ) => {
238+ const webmap = new WebMap ( id , { server : server } ) ;
239+ spyOn ( webmap , 'isIE' ) . and . returnValue ( false ) ;
240+ spyOn ( webmap , 'isFirefox' ) . and . returnValue ( false ) ;
241+ spyOn ( webmap , 'isChrome' ) . and . returnValue ( false ) ;
242+ spyOn ( FetchRequest , 'get' ) . and . returnValue ( Promise . reject ( new Error ( 'Network error' ) ) ) ;
243+
244+ const url = 'http://fakeurl' ;
245+ const token = null ;
246+ const proxy = false ;
247+
248+ const promise = webmap . isSupportWebp ( url , token , proxy ) ;
249+ expect ( Promise . resolve ( promise ) ) . toBe ( promise ) ; // 检查返回值是否为Promise
250+
251+ promise . then ( ( result ) => {
252+ expect ( result ) . toBe ( false ) ;
253+ done ( ) ;
254+ } ) ;
255+ } ) ;
256+
257+ it ( 'renameLayerId_no_duplicate' , ( ) => {
258+ const webmap = new WebMap ( id , { server : server } ) ;
259+
260+ const layers = [ { id : 'layer1' } , { id : 'layer2' } ] ;
261+ const curLayer = { id : 'layer3' } ;
262+
263+ webmap . renameLayerId ( layers , curLayer ) ;
264+
265+ // 没有重复应该保持不变
266+ expect ( curLayer . id ) . toBe ( 'layer3' ) ;
267+ } ) ;
268+
269+ it ( 'renameLayerId_with_duplicate' , ( ) => {
270+ const webmap = new WebMap ( id , { server : server } ) ;
271+
272+ const layers = [ { id : 'layer1' } , { id : 'layer2' } ] ;
273+ const curLayer = { id : 'layer1' } ;
274+
275+ webmap . renameLayerId ( layers , curLayer ) ;
276+
277+ // 有重复应该添加(1)后缀
278+ expect ( curLayer . id ) . toBe ( 'layer1(1)' ) ;
279+ } ) ;
280+
281+ it ( 'renameLayerId_with_duplicate_and_existing_suffix' , ( ) => {
282+ const webmap = new WebMap ( id , { server : server } ) ;
283+
284+ const layers = [ { id : 'layer1' } , { id : 'layer1(1)' } ] ;
285+ const curLayer = { id : 'layer1' } ;
286+
287+ webmap . renameLayerId ( layers , curLayer ) ;
288+
289+ // 如果已经存在带后缀的图层,应该递增
290+ expect ( curLayer . id ) . toBe ( 'layer1(2)' ) ;
291+ } ) ;
292+
293+ it ( 'renameLayerId_recursive_rename_if_still_duplicate' , ( ) => {
294+ const webmap = new WebMap ( id , { server : server } ) ;
295+
296+ const layers = [ { id : 'layer1' } , { id : 'layer1(1)' } ] ;
297+ const curLayer = { id : 'layer1' } ;
298+
299+ webmap . renameLayerId ( layers , curLayer ) ;
300+
301+ // 如果重命名后仍然重复,应该再次递增
302+ expect ( curLayer . id ) . toBe ( 'layer1(2)' ) ;
303+ } ) ;
304+ } ) ;
0 commit comments