Skip to content

Commit ba53172

Browse files
committed
Merge remote-tracking branch 'origin/main' into feat/vite-improvements
2 parents 5f9d619 + 80f3120 commit ba53172

File tree

23 files changed

+815
-584
lines changed

23 files changed

+815
-584
lines changed

apps/automated/src/http/http-tests.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { ImageSource } from '@nativescript/core';
22
import * as TKUnit from '../tk-unit';
33
import * as http from '@nativescript/core/http';
44
import * as fs from '@nativescript/core/file-system';
5-
import { addHeader } from '@nativescript/core/http/http-request';
5+
import { requestInternal, addHeader, BaseHttpContent } from '@nativescript/core/http/http-request-internal';
66

77
export var test_getString_isDefined = function () {
88
TKUnit.assert(typeof http.getString !== 'undefined', 'Method http.getString() should be defined!');
@@ -329,6 +329,89 @@ export var test_request_requestShouldTimeout = function (done) {
329329
});
330330
};
331331

332+
export var test_requestInternal_responseStatusCodeShouldBeDefined = function (done) {
333+
requestInternal({ url: 'https://http-echo.nativescript.org/get', method: 'GET' }).then(
334+
function (response) {
335+
//// Argument (response) is HttpResponse!
336+
var statusCode = response.statusCode;
337+
try {
338+
TKUnit.assert(typeof statusCode !== 'undefined', 'response.statusCode should be defined!');
339+
done(null);
340+
} catch (err) {
341+
done(err);
342+
}
343+
},
344+
function (e) {
345+
//// Argument (e) is Error!
346+
done(e);
347+
},
348+
);
349+
};
350+
351+
export var test_requestInternal_responseContentShouldExposeNativeContentFunctions = function (done) {
352+
requestInternal({ url: 'https://http-echo.nativescript.org/get', method: 'GET' }).then(
353+
function (response) {
354+
try {
355+
TKUnit.assert(typeof response.content.toNativeImage === 'function' && typeof response.content.toNativeString === 'function', `response.content should expose native content functions!`);
356+
done(null);
357+
} catch (err) {
358+
done(err);
359+
}
360+
},
361+
function (e) {
362+
//// Argument (e) is Error!
363+
done(e);
364+
},
365+
);
366+
};
367+
368+
export var test_requestInternal_responseContentShouldExposeHandlerFunctions = function (done) {
369+
const responseHandler = {
370+
toDummy1: () => 'dummy1',
371+
toDummy2: () => 'dummy2',
372+
};
373+
374+
requestInternal({ url: 'https://http-echo.nativescript.org/get', method: 'GET' }, responseHandler).then(
375+
function (response) {
376+
try {
377+
TKUnit.assert(typeof response.content.toDummy1 === 'function' && typeof response.content.toDummy2 === 'function', `response.content should expose content handler functions!`);
378+
done(null);
379+
} catch (err) {
380+
done(err);
381+
}
382+
},
383+
function (e) {
384+
//// Argument (e) is Error!
385+
done(e);
386+
},
387+
);
388+
};
389+
390+
export var test_requestInternal_responseHandlerShouldBeAvailable = function (done) {
391+
const suffix = '-nsformatted';
392+
const responseHandler = {
393+
toFormattedString: function (this: BaseHttpContent) {
394+
return this.toNativeString() + suffix;
395+
},
396+
};
397+
398+
requestInternal({ url: 'https://http-echo.nativescript.org/get', method: 'GET' }, responseHandler).then(
399+
function (response) {
400+
const value = response.content.toFormattedString();
401+
try {
402+
TKUnit.assert(typeof value === 'string' && value.endsWith(suffix), `response.content.toFormattedString should return the response string appended with ${suffix} at the end!`);
403+
done(null);
404+
} catch (err) {
405+
done(err);
406+
}
407+
},
408+
function (e) {
409+
//// Argument (e) is Error!
410+
done(e);
411+
},
412+
);
413+
};
414+
332415
export var test_request_responseStatusCodeShouldBeDefined = function (done) {
333416
var result: http.HttpResponse;
334417

apps/automated/src/ui/image/image-tests.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Image } from '@nativescript/core/ui/image';
22
import { StackLayout } from '@nativescript/core/ui/layouts/stack-layout';
33
import { GridLayout } from '@nativescript/core/ui/layouts/grid-layout';
4-
import { PropertyChangeData, Utils } from '@nativescript/core';
4+
import { PropertyChangeData } from '@nativescript/core';
55
import * as utils from '@nativescript/core/utils';
66
import * as TKUnit from '../../tk-unit';
77
import { getColor } from '../../ui-helper';
@@ -27,8 +27,6 @@ if (__ANDROID__) {
2727
appHelpers.initImageCache(Application.android.startActivity, appHelpers.CacheMode.memory); // use memory cache only.
2828
}
2929

30-
const expectLayoutRequest = __APPLE__ && Utils.SDK_VERSION >= 18;
31-
3230
export const test_Image_Members = function () {
3331
const image = new ImageModule.Image();
3432
TKUnit.assert(types.isUndefined(image.src), 'Image.src is defined');
@@ -269,17 +267,17 @@ export const test_SettingImageSourceWhenSizedToParentDoesNotRequestLayout = ios(
269267

270268
let mainPage = helper.getCurrentPage();
271269
mainPage.content = host;
272-
TKUnit.waitUntilReady(() => host.isLoaded);
270+
271+
const nativeHostView = host.nativeViewProtected as UIView;
272+
273+
// Check if native view layer is still marked as dirty before proceeding
274+
TKUnit.waitUntilReady(() => host.isLoaded && nativeHostView?.layer && !nativeHostView.layer.needsLayout());
273275

274276
let called = false;
275277
image.requestLayout = () => (called = true);
276278
image.src = '~/assets/logo.png';
277279

278-
if (expectLayoutRequest) {
279-
TKUnit.assertTrue(called, 'image.requestLayout should be called.');
280-
} else {
281-
TKUnit.assertFalse(called, 'image.requestLayout should not be called.');
282-
}
280+
TKUnit.assertFalse(called, 'image.requestLayout should not be called.');
283281
});
284282

285283
export const test_SettingImageSourceWhenFixedWidthAndHeightDoesNotRequestLayout = ios(() => {
@@ -291,17 +289,17 @@ export const test_SettingImageSourceWhenFixedWidthAndHeightDoesNotRequestLayout
291289

292290
let mainPage = helper.getCurrentPage();
293291
mainPage.content = host;
294-
TKUnit.waitUntilReady(() => host.isLoaded);
292+
293+
const nativeHostView = host.nativeViewProtected as UIView;
294+
295+
// Check if native view layer is still marked as dirty before proceeding
296+
TKUnit.waitUntilReady(() => host.isLoaded && nativeHostView?.layer && !nativeHostView.layer.needsLayout());
295297

296298
let called = false;
297299
image.requestLayout = () => (called = true);
298300
image.src = '~/assets/logo.png';
299301

300-
if (expectLayoutRequest) {
301-
TKUnit.assertTrue(called, 'image.requestLayout should be called.');
302-
} else {
303-
TKUnit.assertFalse(called, 'image.requestLayout should not be called.');
304-
}
302+
TKUnit.assertFalse(called, 'image.requestLayout should not be called.');
305303
});
306304

307305
export const test_SettingImageSourceWhenSizedToContentShouldInvalidate = ios(() => {

apps/automated/src/ui/label/label-tests.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import * as helper from '../../ui-helper';
66

77
const testDir = 'ui/label';
88

9-
const expectLayoutRequest = __APPLE__ && Utils.SDK_VERSION >= 18;
10-
119
export class LabelTest extends testModule.UITest<Label> {
1210
public create(): Label {
1311
const label = new Label();
@@ -603,7 +601,11 @@ export class LabelTest extends testModule.UITest<Label> {
603601

604602
let mainPage = helper.getCurrentPage();
605603
mainPage.content = host;
606-
TKUnit.waitUntilReady(() => host.isLoaded);
604+
605+
const nativeHostView = host.nativeViewProtected as UIView;
606+
607+
// Check if native view layer is still marked as dirty before proceeding
608+
TKUnit.waitUntilReady(() => host.isLoaded && nativeHostView?.layer && !nativeHostView.layer.needsLayout());
607609

608610
let called = false;
609611
label.requestLayout = () => (called = true);
@@ -618,7 +620,7 @@ export class LabelTest extends testModule.UITest<Label> {
618620
}
619621

620622
public test_SettingTextWhenInFixedSizeGridShouldNotRequestLayout() {
621-
this.requestLayoutFixture(expectLayoutRequest, '', (label) => {
623+
this.requestLayoutFixture(false, '', (label) => {
622624
label.textWrap = false;
623625
let host = new GridLayout();
624626
host.width = 100;
@@ -629,7 +631,7 @@ export class LabelTest extends testModule.UITest<Label> {
629631
}
630632

631633
public test_ChangingTextWhenInFixedSizeGridShouldNotRequestLayout() {
632-
this.requestLayoutFixture(expectLayoutRequest, 'Hello World', (label) => {
634+
this.requestLayoutFixture(false, 'Hello World', (label) => {
633635
label.textWrap = false;
634636
let host = new GridLayout();
635637
host.width = 100;
@@ -640,7 +642,7 @@ export class LabelTest extends testModule.UITest<Label> {
640642
}
641643

642644
public test_SettingTextWhenFixedWidthAndHeightDoesNotRequestLayout() {
643-
this.requestLayoutFixture(expectLayoutRequest, '', (label) => {
645+
this.requestLayoutFixture(false, '', (label) => {
644646
label.textWrap = false;
645647
let host = new StackLayout();
646648
label.width = 100;
@@ -651,7 +653,7 @@ export class LabelTest extends testModule.UITest<Label> {
651653
}
652654

653655
public test_ChangingTextWhenFixedWidthAndHeightDoesNotRequestLayout() {
654-
this.requestLayoutFixture(expectLayoutRequest, 'Hello World', (label) => {
656+
this.requestLayoutFixture(false, 'Hello World', (label) => {
655657
label.textWrap = false;
656658
let host = new StackLayout();
657659
label.width = 100;
@@ -692,7 +694,7 @@ export class LabelTest extends testModule.UITest<Label> {
692694
}
693695

694696
public test_ChangingTextOnSingleLineTextWhenWidthIsSizedToParentAndHeightIsSizedToContentShouldNotRequestLayout() {
695-
this.requestLayoutFixture(expectLayoutRequest, 'Hello World', (label) => {
697+
this.requestLayoutFixture(false, 'Hello World', (label) => {
696698
label.textWrap = false;
697699
let host = new StackLayout();
698700
host.width = 100;

package-lock.json

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/http/http-interfaces.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ImageSource } from '../image-source';
22
import type { File } from '../file-system';
3+
import type { BaseHttpContent } from './http-request-internal';
34

45
/**
56
* Provides options for the http requests.
@@ -39,7 +40,7 @@ export interface HttpRequestOptions {
3940
/**
4041
* Encapsulates HTTP-response information from an HTTP-request.
4142
*/
42-
export interface HttpResponse {
43+
export interface HttpResponse<T = HttpContent> {
4344
/**
4445
* Gets the response status code.
4546
*/
@@ -53,7 +54,7 @@ export interface HttpResponse {
5354
/**
5455
* Gets the response content.
5556
*/
56-
content?: HttpContent;
57+
content?: T;
5758
}
5859

5960
export type Headers = { [key: string]: string | string[] };
@@ -62,15 +63,8 @@ export enum HttpResponseEncoding {
6263
UTF8,
6364
GBK,
6465
}
65-
/**
66-
* Encapsulates the content of an HttpResponse.
67-
*/
68-
export interface HttpContent {
69-
/**
70-
* Gets the response body as raw data.
71-
*/
72-
raw: any;
7366

67+
export interface HttpContentHandler {
7468
/**
7569
* Gets the response body as ArrayBuffer
7670
*/
@@ -96,3 +90,8 @@ export interface HttpContent {
9690
*/
9791
toFile: (destinationFilePath?: string) => File;
9892
}
93+
94+
/**
95+
* Encapsulates the content of an HttpResponse.
96+
*/
97+
export interface HttpContent extends HttpContentHandler, BaseHttpContent {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Headers } from '../http-interfaces';
2+
3+
export function addHeader(headers: Headers, key: string, value: string): void {
4+
if (!headers[key]) {
5+
headers[key] = value;
6+
} else if (Array.isArray(headers[key])) {
7+
headers[key].push(value);
8+
} else {
9+
headers[key] = [headers[key], value];
10+
}
11+
}

0 commit comments

Comments
 (0)