1import { readAndroidManifestAsync } from '@expo/config-plugins/build/android/Manifest';
2import path from 'path';
3
4import { setImagePickerInfoPlist, withAndroidImagePickerPermissions } from '../withImagePicker';
5
6function getMockAndroidManifest() {
7  return readAndroidManifestAsync(
8    path.resolve(__dirname, 'fixtures/react-native-AndroidManifest.xml')
9  );
10}
11
12describe(withAndroidImagePickerPermissions, () => {
13  it(`defaults to adding permissions`, async () => {
14    const config = withAndroidImagePickerPermissions(
15      {
16        slug: '',
17        name: '',
18      },
19      {}
20    );
21
22    const { modResults } = await (config as any).mods.android.manifest({
23      modRequest: {},
24      modResults: await getMockAndroidManifest(),
25    });
26
27    expect(config.android.permissions).toEqual(['android.permission.RECORD_AUDIO']);
28
29    expect(modResults).toEqual({
30      manifest: {
31        $: {
32          'xmlns:android': expect.any(String),
33          package: expect.any(String),
34          // Added tools
35          'xmlns:tools': 'http://schemas.android.com/tools',
36        },
37        'uses-permission': [
38          expect.anything(),
39          // Added two blocked permissions
40        ],
41        queries: expect.anything(),
42        application: expect.anything(),
43      },
44    });
45  });
46  it(`adds blocked permissions when the user passes in false for properties`, async () => {
47    const config = withAndroidImagePickerPermissions(
48      {
49        slug: '',
50        name: '',
51      },
52      {
53        cameraPermission: false,
54        microphonePermission: false,
55
56        // Does nothing...
57        photosPermission: false,
58      }
59    );
60
61    const { modResults } = await (config as any).mods.android.manifest({
62      modRequest: {},
63      modResults: await getMockAndroidManifest(),
64    });
65
66    expect(modResults).toEqual({
67      manifest: {
68        $: {
69          'xmlns:android': expect.any(String),
70          package: expect.any(String),
71          // Added tools
72          'xmlns:tools': 'http://schemas.android.com/tools',
73        },
74        'uses-permission': [
75          expect.anything(),
76          // Added two blocked permissions
77          {
78            $: {
79              'android:name': 'android.permission.RECORD_AUDIO',
80              'tools:node': 'remove',
81            },
82          },
83          {
84            $: {
85              'android:name': 'android.permission.CAMERA',
86              'tools:node': 'remove',
87            },
88          },
89        ],
90        queries: expect.anything(),
91        application: expect.anything(),
92      },
93    });
94  });
95});
96
97describe(setImagePickerInfoPlist, () => {
98  it(`adds defaults to the plist`, () => {
99    expect(setImagePickerInfoPlist({}, {})).toStrictEqual({
100      NSCameraUsageDescription: 'Allow $(PRODUCT_NAME) to access your camera',
101      NSMicrophoneUsageDescription: 'Allow $(PRODUCT_NAME) to access your microphone',
102      NSPhotoLibraryUsageDescription: 'Allow $(PRODUCT_NAME) to access your photos',
103    });
104  });
105
106  it(`uses custom messages`, () => {
107    expect(
108      setImagePickerInfoPlist(
109        {
110          NSPhotoLibraryUsageDescription: 'foobar',
111          NSCameraUsageDescription: 'foobar',
112          NSMicrophoneUsageDescription: 'foobar',
113        },
114        {
115          cameraPermission: 'yolo',
116        }
117      )
118    ).toStrictEqual({
119      NSCameraUsageDescription: 'yolo',
120      NSMicrophoneUsageDescription: 'foobar',
121      NSPhotoLibraryUsageDescription: 'foobar',
122    });
123  });
124
125  it(`disables defaults explicitly`, () => {
126    expect(
127      setImagePickerInfoPlist(
128        {
129          NSPhotoLibraryUsageDescription: 'foobar',
130          NSCameraUsageDescription: 'foobar',
131          NSMicrophoneUsageDescription: 'foobar',
132        },
133        {
134          cameraPermission: false,
135          microphonePermission: false,
136          photosPermission: false,
137        }
138      )
139    ).toStrictEqual({});
140  });
141});
142