mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Adds functionality to RN's built-in version of the `getAndroidAssetSuffix` function. Open source uses a [fork of this code](https://github.com/react-native-community/cli/blob/df55a78f2d27e3443f15b15b87459b54a78e2c47/packages/cli/src/commands/bundle/assetPathUtils.ts#L19-L36) as part of the RN CLI and is therefore unaffected. Specifically, we can now package drawable assets for Android with nonstandard pixel densities by placing them in [`drawable-<nnn>dpi`](https://developer.android.com/guide/topics/resources/providing-resources#:~:text=nnndpi), where `nnn` is a number representing a pixel density. Previously using nonstandard scale factors like `1.25x` would be a build-time error when building for Android. We could send a corresponding PR to the RN CLI, but [Gradle doesn't support this convention](https://issuetracker.google.com/issues/72884435) (although it is documented and supported by Android) so it may not be of much use in OSS right away. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D30784087 fbshipit-source-id: ea82f924d14a316702cabba759722cf26f69eb21
86 lines
2.4 KiB
JavaScript
86 lines
2.4 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @emails oncall+react_native
|
|
* @flow strict-local
|
|
* @format
|
|
*/
|
|
|
|
import {getAndroidResourceFolderName} from '../path-support';
|
|
|
|
const DRAWABLE_ASSET = {
|
|
__packager_asset: true,
|
|
fileSystemLocation: 'foo.png',
|
|
httpServerLocation: '/assets/',
|
|
width: 150,
|
|
height: 150,
|
|
scales: [1],
|
|
hash: '__HASH__',
|
|
name: 'foo',
|
|
type: 'png',
|
|
};
|
|
|
|
const NON_DRAWABLE_ASSET = {
|
|
__packager_asset: true,
|
|
fileSystemLocation: 'foo.txt',
|
|
httpServerLocation: '/assets/',
|
|
width: 150,
|
|
height: 150,
|
|
scales: [1],
|
|
hash: '__HASH__',
|
|
name: 'foo',
|
|
type: 'txt',
|
|
};
|
|
|
|
describe('getAndroidResourceFolderName', () => {
|
|
test('supports the six primary density buckets', () => {
|
|
expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 0.75)).toBe(
|
|
'drawable-ldpi',
|
|
);
|
|
expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 1)).toBe(
|
|
'drawable-mdpi',
|
|
);
|
|
expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 1.5)).toBe(
|
|
'drawable-hdpi',
|
|
);
|
|
expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 2)).toBe(
|
|
'drawable-xhdpi',
|
|
);
|
|
expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 3)).toBe(
|
|
'drawable-xxhdpi',
|
|
);
|
|
expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 4)).toBe(
|
|
'drawable-xxxhdpi',
|
|
);
|
|
});
|
|
|
|
test('supports nonstandard densities', () => {
|
|
expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 1.25)).toBe(
|
|
'drawable-200dpi',
|
|
);
|
|
expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 1.66)).toBe(
|
|
'drawable-266dpi',
|
|
);
|
|
expect(getAndroidResourceFolderName(DRAWABLE_ASSET, 1.33)).toBe(
|
|
'drawable-213dpi',
|
|
); // ~tvdpi
|
|
});
|
|
|
|
test('throws if the density cannot be processed', () => {
|
|
expect(() => getAndroidResourceFolderName(DRAWABLE_ASSET, -1)).toThrow();
|
|
expect(() => getAndroidResourceFolderName(DRAWABLE_ASSET, 0)).toThrow();
|
|
expect(() =>
|
|
getAndroidResourceFolderName(DRAWABLE_ASSET, Infinity),
|
|
).toThrow();
|
|
});
|
|
|
|
test('returns "raw" for non-drawables', () => {
|
|
expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 0.75)).toBe('raw');
|
|
expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 1)).toBe('raw');
|
|
expect(getAndroidResourceFolderName(NON_DRAWABLE_ASSET, 1.25)).toBe('raw');
|
|
});
|
|
});
|