Skip to content

Instantly share code, notes, and snippets.

View Ansh1234's full-sized avatar

Anshul Jain Ansh1234

View GitHub Profile
@Ansh1234
Ansh1234 / activity_login.xml
Last active September 19, 2016 08:24
Xml code for LoginActivity.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/login_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
@Ansh1234
Ansh1234 / LoginActivityTest.java
Created September 19, 2016 09:05
UI Testing class for LoginActivity.java
public class LoginActivityTest {
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule(LoginActivity.class);
private Resources resources;
@Before
public void init() {
resources = mActivityRule.getActivity().getResources();
}
@Ansh1234
Ansh1234 / activity_nested_layout.xml
Created September 19, 2016 09:26
Xml file used for performing action on a nested layout when its id is not known
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/parent_container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
public class NestedLayoutActivityTest {
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule(NestedLayoutActivity.class);
@Test
public void performClickOnTextView() {
onView(withId(R.id.parent_container)).perform(new ViewAction() {
@Override
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule(LoginActivity.class, false, false);
@Before
public void init() {
Intent intent = new Intent();
intent.putExtra("key", "value");
mActivityRule.launchActivity(intent);
}
@Test
public void turnOffWifi() throws UiObjectNotFoundException {
String wiFi = "Wi‑Fi";
//Get an instance of device and press home button
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
device.pressHome();
//To get the list of all apps, find an object which has description "Apps" and click it
UiObject2 allApps = device.findObject(By.desc("Apps"));
/**
* Method to check whether to check Google Play Services is up to date.
*
* @return
*/
private boolean checkPlayServices() {
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int result = googleAPI.isGooglePlayServicesAvailable(this);
if (result != ConnectionResult.SUCCESS) {
//Google Play Services app is not available or version is not up to date. Error the
if (result != ConnectionResult.SUCCESS) {
//Any random request code
int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
//Google Play Services app is not available or version is not up to date. Error the
// error condition here
if (googleAPI.isUserResolvableError(result)) {
googleAPI.getErrorDialog(this, result,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
}
return false;
public class RandomClass{
private static final String characters = "abcdefghijklmnopqrstuvwxyz0123456789";
public static String getRandomString(int length) {
if (length < 0 || length > 10) {
return "Random";
}
StringBuilder stringBuilder = new StringBuilder();
Random random = new Random();
public class StringPrinter {
private final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 2, 0,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
public static void main(String[] args) {
new StringPrinter().printString();
}
public void printString() {