Skip to content

[TM] Add spec for DeviceInfo #24878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions Libraries/Utilities/DeviceInfo.js
Original file line number Diff line number Diff line change
@@ -10,10 +10,6 @@

'use strict';

const DeviceInfo = require('../BatchedBridge/NativeModules').DeviceInfo;
import NativeDeviceInfo from './NativeDeviceInfo';

const invariant = require('invariant');

invariant(DeviceInfo, 'DeviceInfo native module is not installed correctly');

module.exports = DeviceInfo;
module.exports = NativeDeviceInfo;
6 changes: 1 addition & 5 deletions Libraries/Utilities/Dimensions.js
Original file line number Diff line number Diff line change
@@ -129,14 +129,10 @@ let dims: ?{[key: string]: any} =
let nativeExtensionsEnabled = true;
if (!dims) {
const DeviceInfo = require('./DeviceInfo');
dims = DeviceInfo.Dimensions;
dims = DeviceInfo.getConstants().Dimensions;
nativeExtensionsEnabled = false;
}

invariant(
dims,
'Either DeviceInfo native extension or DeviceInfo Native Module must be registered',
);
Dimensions.set(dims);
if (!nativeExtensionsEnabled) {
RCTDeviceEventEmitter.addListener('didUpdateDimensions', function(update) {
55 changes: 55 additions & 0 deletions Libraries/Utilities/NativeDeviceInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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.
*
* @flow
* @format
*/

'use strict';

import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';

export interface Spec extends TurboModule {
+getConstants: () => {|
Dimensions: {|
// iOS specific
window?: {|
width: number,
height: number,
scale: number,
fontScale: number,
|},
screen?: {|
width: number,
height: number,
scale: number,
fontScale: number,
|},

// Android specific
windowPhysicalPixels?: {|
width: number,
height: number,
scale: number,
fontScale: number,
densityDpi: number,
|},
screenPhysicalPixels?: {|
width: number,
height: number,
scale: number,
fontScale: number,
densityDpi: number,
|},
|},

// iOS specific
isIPhoneX_deprecated?: boolean,
|};
}

export default TurboModuleRegistry.getEnforcing<Spec>('DeviceInfo');
2 changes: 1 addition & 1 deletion Libraries/Utilities/__tests__/DeviceInfo-test.js
Original file line number Diff line number Diff line change
@@ -14,6 +14,6 @@ describe('DeviceInfo', () => {
const DeviceInfo = require('../DeviceInfo');

it('should give device info', () => {
expect(DeviceInfo).toHaveProperty('Dimensions');
expect(DeviceInfo.getConstants()).toHaveProperty('Dimensions');
});
});
2 changes: 1 addition & 1 deletion RNTester/js/SafeAreaViewExample.js
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ class IsIPhoneXExample extends React.Component<{}> {
<View>
<Text>
Is this an iPhone X:{' '}
{DeviceInfo.isIPhoneX_deprecated
{DeviceInfo.getConstants().isIPhoneX_deprecated
? 'Yeah!'
: 'Nope. (Or `isIPhoneX_deprecated` was already removed.)'}
</Text>
30 changes: 17 additions & 13 deletions jest/setup.js
Original file line number Diff line number Diff line change
@@ -153,19 +153,23 @@ const mockNativeModules = {
queryData: jest.fn(),
},
DeviceInfo: {
Dimensions: {
window: {
fontScale: 2,
height: 1334,
scale: 2,
width: 750,
},
screen: {
fontScale: 2,
height: 1334,
scale: 2,
width: 750,
},
getConstants() {
return {
Dimensions: {
window: {
fontScale: 2,
height: 1334,
scale: 2,
width: 750,
},
screen: {
fontScale: 2,
height: 1334,
scale: 2,
width: 750,
},
},
};
},
},
FacebookSDK: {