Skip to content

Instantly share code, notes, and snippets.

@d4vidi
Last active September 2, 2021 09:05
Show Gist options
  • Save d4vidi/5980f7e1dc6d757bd349bef32b322b9b to your computer and use it in GitHub Desktop.
Save d4vidi/5980f7e1dc6d757bd349bef32b322b9b to your computer and use it in GitHub Desktop.
Android CI Blog: assert test butler
@RunWith(AndroidJUnit4.class)
@LargeTest
public class DetoxTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, false, false);
@Test
public void runDetoxTests() {
// This is optional - in case you've decided to integrate TestButler
// See https://github.com/wix/Detox/blob/master/docs/Introduction.Android.md#8-test-butler-support-optional
TestButlerProbe.assertReadyIfInstalled();
Detox.runTests(mActivityRule);
}
}
class TestButlerProbe {
private static final String LOG_TAG = TestButlerProbe.class.getSimpleName();
private static final String TEST_BUTLER_PACKAGE_NAME = "com.linkedin.android.testbutler";
private TestButlerProbe() {
}
static void assertReadyIfInstalled() {
Log.i(LOG_TAG, "Test butler service verification started...");
if (!isTestButlerServiceInstalled()) {
Log.w(LOG_TAG, "Test butler not installed on device - skipping verification");
return;
}
assertTestButlerServiceReady();
Log.i(LOG_TAG, "Test butler service is up and running!");
}
static private boolean isTestButlerServiceInstalled() {
try {
PackageManager pm = InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager();
pm.getPackageInfo(TEST_BUTLER_PACKAGE_NAME, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
static private void assertTestButlerServiceReady() {
try {
// This has no effect if test-butler is running. However, if it is not, then unlike TestButler.setup(), it would hard-fail.
TestButler.setRotation(Surface.ROTATION_0);
} catch (Exception e) {
throw new RuntimeException("Test butler service is NOT ready!", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment