Last active
September 2, 2021 09:05
-
-
Save d4vidi/5980f7e1dc6d757bd349bef32b322b9b to your computer and use it in GitHub Desktop.
Android CI Blog: assert test butler
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
@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); | |
} | |
} |
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
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