/
ImagePicker.js
132 lines (108 loc) · 4.1 KB
/
ImagePicker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { Platform } from 'react-native';
import Constants from 'expo-constants';
import * as ImagePicker from 'expo-image-picker';
import * as Permissions from 'expo-permissions';
import * as TestUtils from '../TestUtils';
import { isDeviceFarm } from '../utils/Environment';
export const name = 'ImagePicker';
export async function test({ it, xit, beforeAll, expect, jasmine, xdescribe, describe, afterAll }) {
function testMediaObjectShape(shape, type) {
expect(shape).toBeDefined();
expect(typeof shape.cancelled).toBe('boolean');
if (!shape.cancelled) {
expect(typeof shape.uri).toBe('string');
expect(typeof shape.width).toBe('number');
expect(typeof shape.height).toBe('number');
expect(typeof shape.type).toBe('string');
expect(shape.uri).not.toBe('');
expect(shape.width).toBeGreaterThan(0);
expect(shape.height).toBeGreaterThan(0);
expect(typeof shape.type).toBe('string');
expect(shape.type).toBe(type);
if (shape.type === 'video') {
expect(typeof shape.duration).toBe('number');
expect(shape.duration).toBeGreaterThan(0);
}
}
}
describe(name, () => {
if (isDeviceFarm()) return;
let originalTimeout;
beforeAll(async () => {
await Permissions.askAsync(Permissions.CAMERA_ROLL);
await Permissions.askAsync(Permissions.CAMERA);
await TestUtils.acceptPermissionsAndRunCommandAsync(() => {
return Permissions.askAsync(Permissions.CAMERA_ROLL);
});
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout * 10;
});
if (Constants.isDevice) {
it('launches the camera', async () => {
const { cancelled } = await ImagePicker.launchCameraAsync();
expect(cancelled).toBe(true);
});
} else {
it('natively prevents the camera from launching on a simulator', async () => {
let err;
try {
await ImagePicker.launchCameraAsync();
} catch ({ code }) {
err = code;
}
expect(err).toBe('CAMERA_MISSING');
});
}
describe('launchImageLibraryAsync', async () => {
it('mediaType: image', async () => {
const image = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
});
testMediaObjectShape(image, 'image');
});
it('mediaType: video', async () => {
const video = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Videos,
});
testMediaObjectShape(video, 'video');
});
it('allows editing', async () => {
const image = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true,
});
testMediaObjectShape(image, 'image');
});
it('allows editing and returns base64', async () => {
const image = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
allowsEditing: true,
base64: true,
});
testMediaObjectShape(image, 'image');
if (!image.cancelled) {
expect(typeof image.base64).toBe('string');
expect(image.base64).not.toBe('');
expect(image.base64).not.toContain('\n');
expect(image.base64).not.toContain('\r');
}
});
if (Platform.OS === 'ios' && parseInt(Platform.Version, 10) > 10) {
it('videoExportPreset should affect video dimensions', async () => {
const video = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Videos,
videoExportPreset: ImagePicker.VideoExportPreset.H264_640x480,
});
testMediaObjectShape(video, 'video');
if (!video.cancelled) {
expect(video.width).toBeLessThanOrEqual(640);
expect(video.height).toBeLessThanOrEqual(480);
}
});
}
});
afterAll(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});
});
}