xref: /expo/apps/test-suite/TestUtils.js (revision cb0e1ce3)
1'use strict';
2
3import { Platform, NativeModules } from 'react-native';
4import Constants from 'expo-constants';
5
6const { ExponentTest } = NativeModules;
7
8function browserSupportsWebGL() {
9  try {
10    const canvas = document.createElement('canvas');
11    return (
12      !!window.WebGLRenderingContext &&
13      (canvas.getContext('webgl') || canvas.getContext('experimental-webgl'))
14    );
15  } catch (e) {
16    return false;
17  }
18}
19
20// List of all modules for tests. Each file path must be statically present for
21// the packager to pick them all up.
22export function getTestModules() {
23  if (Platform.OS === 'web') {
24    const modules = [
25      require('./tests/SVG'),
26      require('./tests/Basic'),
27      require('./tests/Contacts'),
28      require('./tests/Constants'),
29      require('./tests/Crypto'),
30      require('./tests/Font'),
31      require('./tests/Random'),
32    ];
33
34    if (browserSupportsWebGL()) {
35      modules.push(require('./tests/GLView'));
36    }
37    return modules;
38  }
39
40  const modules = [
41    require('./tests/Basic'),
42    require('./tests/Asset'),
43    require('./tests/Constants'),
44    require('./tests/Crypto'),
45    require('./tests/FileSystem'),
46    require('./tests/GLView'),
47    require('./tests/Haptics'),
48    require('./tests/Localization'),
49    require('./tests/Recording'),
50    require('./tests/ScreenOrientation'),
51    require('./tests/SecureStore'),
52    require('./tests/Segment'),
53    require('./tests/Speech'),
54    require('./tests/SQLite'),
55    require('./tests/Random'),
56    require('./tests/Payments'),
57    require('./tests/AdMobInterstitial'),
58    require('./tests/AdMobBanner'),
59    require('./tests/AdMobPublisherBanner'),
60    require('./tests/AdMobRewarded'),
61    require('./tests/FBBannerAd'),
62  ];
63  if (ExponentTest && !ExponentTest.isInCI) {
64    // Invalid placementId in CI (all tests fail)
65    modules.push(require('./tests/FBNativeAd'));
66    // Requires interaction (sign in popup)
67    modules.push(require('./tests/GoogleSignIn'));
68    // Popup to request device's location which uses Google's location service
69    modules.push(require('./tests/Location'));
70    // Fails to redirect because of malformed URL in published version with release channel parameter
71    modules.push(require('./tests/Linking'));
72    // Requires permission
73    modules.push(require('./tests/Calendar'));
74    modules.push(require('./tests/Contacts'));
75    modules.push(require('./tests/Permissions'));
76    modules.push(require('./tests/MediaLibrary'));
77    modules.push(require('./tests/Notifications'));
78    if (Constants.isDevice) modules.push(require('./tests/Brightness'));
79    // Crashes app when mounting component
80    modules.push(require('./tests/Video'));
81    // "sdkUnversionedTestSuite failed: java.lang.NullPointerException: Attempt to invoke interface method
82    // 'java.util.Map org.unimodules.interfaces.taskManager.TaskInterface.getOptions()' on a null object reference"
83    modules.push(require('./tests/TaskManager'));
84    // Audio tests are flaky in CI due to asynchronous fetching of resources
85    modules.push(require('./tests/Audio'));
86    // The Camera tests are flaky on iOS, i.e. they fail randomly
87    if (Constants.isDevice && Platform.OS === 'android') modules.push(require('./tests/Camera'));
88  }
89  if (Platform.OS === 'android') modules.push(require('./tests/JSC'));
90  if (Constants.isDevice) {
91    modules.push(require('./tests/BarCodeScanner'));
92  }
93  return modules;
94}
95
96export async function acceptPermissionsAndRunCommandAsync(fn) {
97  if (!ExponentTest) {
98    return await fn();
99  }
100
101  const results = await Promise.all([
102    ExponentTest.action({
103      selectorType: 'text',
104      selectorValue: 'Allow',
105      actionType: 'click',
106      delay: 1000,
107      timeout: 100,
108    }),
109    fn(),
110  ]);
111
112  return results[1];
113}
114
115export async function shouldSkipTestsRequiringPermissionsAsync() {
116  if (!ExponentTest || !ExponentTest.shouldSkipTestsRequiringPermissionsAsync) {
117    return false;
118  }
119  return ExponentTest.shouldSkipTestsRequiringPermissionsAsync();
120}
121