2015-04-21 17:48:54 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-11-02 13:14:11 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2017-11-02 13:14:11 +00:00
|
|
|
* @emails oncall+react_native
|
2015-04-21 17:48:54 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-04-21 17:48:54 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const AssetRegistry = require('AssetRegistry');
|
|
|
|
const Platform = require('Platform');
|
|
|
|
const NativeModules = require('NativeModules');
|
|
|
|
const resolveAssetSource = require('../resolveAssetSource');
|
2015-04-21 17:48:54 +00:00
|
|
|
|
2015-04-22 20:11:30 +00:00
|
|
|
function expectResolvesAsset(input, expectedSource) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const assetId = AssetRegistry.registerAsset(input);
|
2015-05-08 00:30:41 +00:00
|
|
|
expect(resolveAssetSource(assetId)).toEqual(expectedSource);
|
2015-04-22 20:11:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 17:48:54 +00:00
|
|
|
describe('resolveAssetSource', () => {
|
|
|
|
beforeEach(() => {
|
2016-12-19 01:02:57 +00:00
|
|
|
jest.resetModules();
|
2015-04-21 17:48:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns same source for simple static and network images', () => {
|
2018-05-10 22:44:52 +00:00
|
|
|
const source1 = {uri: 'https://www.facebook.com/logo'};
|
2015-04-21 17:48:54 +00:00
|
|
|
expect(resolveAssetSource(source1)).toBe(source1);
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const source2 = {uri: 'logo'};
|
2015-04-21 17:48:54 +00:00
|
|
|
expect(resolveAssetSource(source2)).toBe(source2);
|
|
|
|
});
|
|
|
|
|
2015-05-08 00:30:41 +00:00
|
|
|
it('does not change deprecated assets', () => {
|
2018-05-11 02:06:46 +00:00
|
|
|
expect(
|
|
|
|
resolveAssetSource({
|
|
|
|
deprecated: true,
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
uri: 'logo',
|
|
|
|
}),
|
|
|
|
).toEqual({
|
2015-05-08 00:30:41 +00:00
|
|
|
deprecated: true,
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
uri: 'logo',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-24 17:56:58 +00:00
|
|
|
it('ignores any weird data', () => {
|
|
|
|
expect(resolveAssetSource(null)).toBe(null);
|
|
|
|
expect(resolveAssetSource(42)).toBe(null);
|
|
|
|
expect(resolveAssetSource('nonsense')).toBe(null);
|
|
|
|
});
|
|
|
|
|
2015-05-12 16:50:41 +00:00
|
|
|
describe('bundle was loaded from network (DEV)', () => {
|
2015-04-21 17:48:54 +00:00
|
|
|
beforeEach(() => {
|
2018-05-11 02:06:46 +00:00
|
|
|
NativeModules.SourceCode.scriptURL = 'http://10.0.0.1:8081/main.bundle';
|
2015-09-09 15:49:45 +00:00
|
|
|
Platform.OS = 'ios';
|
2015-04-21 17:48:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('uses network image', () => {
|
2018-05-11 02:06:46 +00:00
|
|
|
expectResolvesAsset(
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
fileSystemLocation: '/root/app/module/a',
|
|
|
|
httpServerLocation: '/assets/module/a',
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
scales: [1],
|
|
|
|
hash: '5b6f00f',
|
|
|
|
name: 'logo',
|
|
|
|
type: 'png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
uri:
|
|
|
|
'http://10.0.0.1:8081/assets/module/a/logo.png?platform=ios&hash=5b6f00f',
|
|
|
|
scale: 1,
|
|
|
|
},
|
|
|
|
);
|
2015-04-21 17:48:54 +00:00
|
|
|
});
|
|
|
|
|
2015-04-22 23:31:13 +00:00
|
|
|
it('picks matching scale', () => {
|
2018-05-11 02:06:46 +00:00
|
|
|
expectResolvesAsset(
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
fileSystemLocation: '/root/app/module/a',
|
|
|
|
httpServerLocation: '/assets/module/a',
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
scales: [1, 2, 3],
|
|
|
|
hash: '5b6f00f',
|
|
|
|
name: 'logo',
|
|
|
|
type: 'png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
uri:
|
|
|
|
'http://10.0.0.1:8081/assets/module/a/logo@2x.png?platform=ios&hash=5b6f00f',
|
|
|
|
scale: 2,
|
|
|
|
},
|
|
|
|
);
|
2015-04-22 23:31:13 +00:00
|
|
|
});
|
2015-04-21 17:48:54 +00:00
|
|
|
});
|
|
|
|
|
2015-10-12 18:34:27 +00:00
|
|
|
describe('bundle was loaded from file on iOS', () => {
|
2015-04-22 20:11:30 +00:00
|
|
|
beforeEach(() => {
|
2015-09-18 01:34:14 +00:00
|
|
|
NativeModules.SourceCode.scriptURL =
|
2015-11-05 20:46:38 +00:00
|
|
|
'file:///Path/To/Sample.app/main.bundle';
|
2015-05-12 16:50:41 +00:00
|
|
|
Platform.OS = 'ios';
|
|
|
|
});
|
|
|
|
|
2015-04-22 20:11:30 +00:00
|
|
|
it('uses pre-packed image', () => {
|
2018-05-11 02:06:46 +00:00
|
|
|
expectResolvesAsset(
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
fileSystemLocation: '/root/app/module/a',
|
|
|
|
httpServerLocation: '/assets/module/a',
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
scales: [1],
|
|
|
|
hash: '5b6f00f',
|
|
|
|
name: 'logo',
|
|
|
|
type: 'png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
uri: 'file:///Path/To/Sample.app/assets/module/a/logo.png',
|
|
|
|
scale: 1,
|
|
|
|
},
|
|
|
|
);
|
2015-04-21 17:48:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-06 16:45:34 +00:00
|
|
|
describe('bundle was loaded from assets on Android', () => {
|
2015-05-12 16:50:41 +00:00
|
|
|
beforeEach(() => {
|
2015-09-18 01:34:14 +00:00
|
|
|
NativeModules.SourceCode.scriptURL =
|
2016-01-06 16:45:34 +00:00
|
|
|
'assets://Path/To/Simulator/main.bundle';
|
2015-05-12 16:50:41 +00:00
|
|
|
Platform.OS = 'android';
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses pre-packed image', () => {
|
2018-05-11 02:06:46 +00:00
|
|
|
expectResolvesAsset(
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
fileSystemLocation: '/root/app/module/a',
|
|
|
|
httpServerLocation: '/assets/AwesomeModule/Subdir',
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
scales: [1],
|
|
|
|
hash: '5b6f00f',
|
|
|
|
name: '!@Logo#1_\u20ac', // Invalid chars shouldn't get passed to native
|
|
|
|
type: 'png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
uri: 'awesomemodule_subdir_logo1_',
|
|
|
|
scale: 1,
|
|
|
|
},
|
|
|
|
);
|
2015-05-12 16:50:41 +00:00
|
|
|
});
|
|
|
|
});
|
2016-03-06 06:42:14 +00:00
|
|
|
|
2016-01-06 16:45:34 +00:00
|
|
|
describe('bundle was loaded from file on Android', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
NativeModules.SourceCode.scriptURL =
|
|
|
|
'file:///sdcard/Path/To/Simulator/main.bundle';
|
|
|
|
Platform.OS = 'android';
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses pre-packed image', () => {
|
2018-05-11 02:06:46 +00:00
|
|
|
expectResolvesAsset(
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
fileSystemLocation: '/root/app/module/a',
|
|
|
|
httpServerLocation: '/assets/AwesomeModule/Subdir',
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
scales: [1],
|
|
|
|
hash: '5b6f00f',
|
|
|
|
name: '!@Logo#1_\u20ac',
|
|
|
|
type: 'png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
uri:
|
|
|
|
'file:///sdcard/Path/To/Simulator/drawable-mdpi/awesomemodule_subdir_logo1_.png',
|
|
|
|
scale: 1,
|
|
|
|
},
|
|
|
|
);
|
2016-01-06 16:45:34 +00:00
|
|
|
});
|
|
|
|
});
|
2015-05-12 16:50:41 +00:00
|
|
|
|
2016-03-06 06:42:14 +00:00
|
|
|
describe('bundle was loaded from raw file on Android', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
NativeModules.SourceCode.scriptURL =
|
|
|
|
'/sdcard/Path/To/Simulator/main.bundle';
|
|
|
|
Platform.OS = 'android';
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses sideloaded image', () => {
|
2018-05-11 02:06:46 +00:00
|
|
|
expectResolvesAsset(
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
fileSystemLocation: '/root/app/module/a',
|
|
|
|
httpServerLocation: '/assets/AwesomeModule/Subdir',
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
scales: [1],
|
|
|
|
hash: '5b6f00f',
|
|
|
|
name: '!@Logo#1_\u20ac',
|
|
|
|
type: 'png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
uri:
|
|
|
|
'file:///sdcard/Path/To/Simulator/drawable-mdpi/awesomemodule_subdir_logo1_.png',
|
|
|
|
scale: 1,
|
|
|
|
},
|
|
|
|
);
|
2016-03-06 06:42:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-03-16 03:19:38 +00:00
|
|
|
describe('source resolver can be customized', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
NativeModules.SourceCode.scriptURL =
|
|
|
|
'file:///sdcard/Path/To/Simulator/main.bundle';
|
|
|
|
Platform.OS = 'android';
|
|
|
|
});
|
|
|
|
|
|
|
|
it('uses bundled source, event when js is sideloaded', () => {
|
2018-05-11 02:06:46 +00:00
|
|
|
resolveAssetSource.setCustomSourceTransformer(resolver =>
|
|
|
|
resolver.resourceIdentifierWithoutScale(),
|
|
|
|
);
|
|
|
|
expectResolvesAsset(
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
fileSystemLocation: '/root/app/module/a',
|
|
|
|
httpServerLocation: '/assets/AwesomeModule/Subdir',
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
scales: [1],
|
|
|
|
hash: '5b6f00f',
|
|
|
|
name: '!@Logo#1_\u20ac',
|
|
|
|
type: 'png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
uri: 'awesomemodule_subdir_logo1_',
|
|
|
|
scale: 1,
|
|
|
|
},
|
2016-03-16 03:19:38 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows any customization', () => {
|
2018-05-11 02:06:46 +00:00
|
|
|
resolveAssetSource.setCustomSourceTransformer(resolver =>
|
|
|
|
resolver.fromSource('TEST'),
|
|
|
|
);
|
|
|
|
expectResolvesAsset(
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
fileSystemLocation: '/root/app/module/a',
|
|
|
|
httpServerLocation: '/assets/AwesomeModule/Subdir',
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
scales: [1],
|
|
|
|
hash: '5b6f00f',
|
|
|
|
name: '!@Logo#1_\u20ac',
|
|
|
|
type: 'png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__packager_asset: true,
|
|
|
|
width: 100,
|
|
|
|
height: 200,
|
|
|
|
uri: 'TEST',
|
|
|
|
scale: 1,
|
|
|
|
},
|
2016-03-16 03:19:38 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2015-04-21 17:48:54 +00:00
|
|
|
});
|
2015-04-22 23:31:13 +00:00
|
|
|
|
|
|
|
describe('resolveAssetSource.pickScale', () => {
|
|
|
|
it('picks matching scale', () => {
|
|
|
|
expect(resolveAssetSource.pickScale([1], 2)).toBe(1);
|
|
|
|
expect(resolveAssetSource.pickScale([1, 2, 3], 2)).toBe(2);
|
|
|
|
expect(resolveAssetSource.pickScale([1, 2], 3)).toBe(2);
|
|
|
|
expect(resolveAssetSource.pickScale([1, 2, 3, 4], 3.5)).toBe(4);
|
|
|
|
expect(resolveAssetSource.pickScale([3, 4], 2)).toBe(3);
|
|
|
|
expect(resolveAssetSource.pickScale([], 2)).toBe(1);
|
|
|
|
});
|
|
|
|
});
|