-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make mediaWidth/height logic more device agnostic (#727)
- Loading branch information
Showing
9 changed files
with
191 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/react/src/components/Liveness/hooks/__tests__/useMediaDimensions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
|
||
import { useMediaDimensions } from '../useMediaDimensions'; | ||
import { getMockedFunction } from '../../utils/test-utils'; | ||
import { isMobileScreen, isPortrait } from '../../utils/device'; | ||
|
||
jest.mock('../../utils/device'); | ||
const mockIsMobileScreen = getMockedFunction(isMobileScreen); | ||
const mockIsPortrait = getMockedFunction(isPortrait); | ||
|
||
describe('useMediaDimensions mobile portrait', () => { | ||
beforeEach(() => { | ||
mockIsMobileScreen.mockReturnValue(true); | ||
mockIsPortrait.mockReturnValue(true); | ||
}); | ||
it('should return portrait sized dimensions when width is less than height', () => { | ||
const videoWidth = 360; | ||
const videoHeight = 640; | ||
|
||
const { result } = renderHook(() => { | ||
return useMediaDimensions(videoWidth, videoHeight); | ||
}); | ||
|
||
expect(result.current.width).toBe(360); | ||
expect(result.current.height).toBe(640); | ||
}); | ||
it('should return portrait sized dimensions when width is greater than height', () => { | ||
const videoWidth = 640; | ||
const videoHeight = 360; | ||
|
||
const { result } = renderHook(() => { | ||
return useMediaDimensions(videoWidth, videoHeight); | ||
}); | ||
|
||
expect(result.current.width).toBe(360); | ||
expect(result.current.height).toBe(640); | ||
}); | ||
}); | ||
|
||
describe('useMediaDimensions mobile landscape', () => { | ||
beforeEach(() => { | ||
mockIsMobileScreen.mockReturnValue(true); | ||
mockIsPortrait.mockReturnValue(false); | ||
}); | ||
it('should return landscape sized dimensions when width is greater than height', () => { | ||
const videoWidth = 640; | ||
const videoHeight = 480; | ||
|
||
const { result } = renderHook(() => { | ||
return useMediaDimensions(videoWidth, videoHeight); | ||
}); | ||
|
||
expect(result.current.width).toBe(640); | ||
expect(result.current.height).toBe(480); | ||
}); | ||
it('should return portrait sized dimensions when width is less than height', () => { | ||
const videoWidth = 480; | ||
const videoHeight = 640; | ||
|
||
const { result } = renderHook(() => { | ||
return useMediaDimensions(videoWidth, videoHeight); | ||
}); | ||
|
||
expect(result.current.width).toBe(640); | ||
expect(result.current.height).toBe(480); | ||
}); | ||
}); | ||
|
||
describe('useMediaDimensions desktop', () => { | ||
it('should return videoWidth and videoHeight as width and height on portrait', () => { | ||
mockIsMobileScreen.mockReturnValue(false); | ||
mockIsPortrait.mockReturnValue(true); | ||
|
||
const videoWidth = 640; | ||
const videoHeight = 480; | ||
|
||
const { result } = renderHook(() => { | ||
return useMediaDimensions(videoWidth, videoHeight); | ||
}); | ||
|
||
expect(result.current.width).toBe(videoWidth); | ||
expect(result.current.height).toBe(videoHeight); | ||
}); | ||
|
||
it('should return videoWidth and videoHeight as width and height on landscape', () => { | ||
mockIsMobileScreen.mockReturnValue(false); | ||
mockIsPortrait.mockReturnValue(true); | ||
|
||
const videoWidth = 480; | ||
const videoHeight = 640; | ||
|
||
const { result } = renderHook(() => { | ||
return useMediaDimensions(videoWidth, videoHeight); | ||
}); | ||
|
||
expect(result.current.width).toBe(videoWidth); | ||
expect(result.current.height).toBe(videoHeight); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './useLivenessActor'; | ||
export * from './useLivenessSelector'; | ||
export * from './useMediaStreamInVideo'; | ||
export * from './useMediaDimensions'; |
43 changes: 43 additions & 0 deletions
43
packages/react/src/components/Liveness/hooks/useMediaDimensions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useLayoutEffect, useState } from 'react'; | ||
import { isPortrait, isMobileScreen } from '../utils/device'; | ||
|
||
interface UseMediaDimensionsResults { | ||
width: number; | ||
height: number; | ||
} | ||
|
||
/** | ||
* Calculate video dimensions based on the width and height values | ||
* supplied in LivenessCameraModule from getUserMedia. | ||
* In our testing, some devices and browser combos on mobile return | ||
* opposite values than you would expect from getUserMedia so we need | ||
* to manually test dimensions before setting the mediaWidth/Height | ||
* for portrait and landscape. | ||
* Known affected browsers: | ||
* - Firefox on Android | ||
* - iOS | ||
*/ | ||
export function useMediaDimensions( | ||
videoWidth: number, | ||
videoHeight: number | ||
): UseMediaDimensionsResults { | ||
const [width, setWidth] = useState<number>(videoWidth); | ||
const [height, setHeight] = useState<number>(videoHeight); | ||
|
||
useLayoutEffect(() => { | ||
if (isMobileScreen()) { | ||
if (isPortrait()) { | ||
setWidth(videoWidth < videoHeight ? videoWidth : videoHeight); | ||
setHeight(videoWidth < videoHeight ? videoHeight : videoWidth); | ||
} else { | ||
setWidth(videoWidth > videoHeight ? videoWidth : videoHeight); | ||
setHeight(videoWidth > videoHeight ? videoHeight : videoWidth); | ||
} | ||
} else { | ||
setWidth(videoWidth); | ||
setHeight(videoHeight); | ||
} | ||
}, [videoWidth, videoHeight]); | ||
|
||
return { width, height }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters