nextcloud-android/androidTest/java/com/owncloud/android/uiautomator/InitialTest.java

158 lines
5.5 KiB
Java
Raw Normal View History

2016-05-06 11:09:32 +03:00
/**
2016-05-06 13:32:01 +03:00
* ownCloud Android client application
* <p/>
* Copyright (C) 2015 ownCloud Inc.
* <p/>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
* <p/>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p/>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-05-06 11:09:32 +03:00
*/
package com.owncloud.android.uiautomator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
2016-05-06 11:09:32 +03:00
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
2016-05-06 13:32:01 +03:00
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
2016-05-06 11:09:32 +03:00
import android.support.test.uiautomator.Until;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
2016-05-06 13:32:01 +03:00
* UI Automator tests
2016-05-06 11:09:32 +03:00
*/
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
2016-05-06 13:32:01 +03:00
public class InitialTest {
2016-05-06 11:09:32 +03:00
private static final String OWNCLOUD_APP_PACKAGE = "com.owncloud.android";
2016-05-06 13:32:01 +03:00
private static final String ANDROID_SETTINGS_PACKAGE = "com.android.settings";
2016-05-10 12:14:37 +03:00
private static final String SETTINGS_DATA_USAGE_OPTION = "Data usage";
2016-05-06 11:09:32 +03:00
private static final int LAUNCH_TIMEOUT = 5000;
private UiDevice mDevice;
@Before
2016-05-06 13:32:01 +03:00
public void initializeDevice() {
2016-05-06 11:09:32 +03:00
// Initialize UiDevice instance
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
2016-05-06 13:32:01 +03:00
}
@Test
public void checkPreconditions() {
assertThat(mDevice, notNullValue());
}
/**
* Start owncloud app
*/
@Test
public void startAppFromHomeScreen() {
2016-05-06 11:09:32 +03:00
// Perform a short press on the HOME button
mDevice.pressHome();
// Wait for launcher
final String launcherPackage = getLauncherPackageName();
assertThat(launcherPackage, notNullValue());
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
// Launch the app
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(OWNCLOUD_APP_PACKAGE);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
// Wait for the app to appear
mDevice.wait(Until.hasObject(By.pkg(OWNCLOUD_APP_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
}
2016-05-06 13:32:01 +03:00
/**
* Start Settings app
*
* @throws UiObjectNotFoundException
*/
2016-05-06 11:09:32 +03:00
@Test
2016-05-06 13:32:01 +03:00
public void startSettingsFromHomeScreen() throws UiObjectNotFoundException {
mDevice.pressHome();
// Wait for launcher
final String launcherPackage = getLauncherPackageName();
assertThat(launcherPackage, notNullValue());
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
// Launch the app
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(ANDROID_SETTINGS_PACKAGE);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
2016-05-10 12:14:37 +03:00
clickByText(SETTINGS_DATA_USAGE_OPTION);
2016-05-06 11:09:32 +03:00
}
/**
* Uses package manager to find the package name of the device launcher. Usually this package
* is "com.android.launcher" but can be different at times. This is a generic solution which
* works on all platforms.`
*/
private String getLauncherPackageName() {
// Create launcher Intent
final Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
// Use PackageManager to get the launcher package name
PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
ResolveInfo resolveInfo = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
return resolveInfo.activityInfo.packageName;
}
2016-05-06 13:32:01 +03:00
/**
2016-10-26 17:30:19 +03:00
* Helper to click on objects that match the content-description text.
2016-05-06 13:32:01 +03:00
*
2016-10-26 17:30:19 +03:00
* @param description the description
2016-05-06 13:32:01 +03:00
* @throws UiObjectNotFoundException
*/
2016-10-26 17:30:19 +03:00
private void clickByDescription(String description) throws UiObjectNotFoundException {
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject obj = device.findObject(new UiSelector().description(description));
2016-05-06 13:32:01 +03:00
obj.clickAndWaitForNewWindow();
}
/**
2016-10-26 17:30:19 +03:00
* Helper to click on object that match the text value.
2016-05-06 13:32:01 +03:00
*
2016-10-26 17:30:19 +03:00
* @param text the text
2016-05-06 13:32:01 +03:00
* @throws UiObjectNotFoundException
*/
private void clickByText(String text) throws UiObjectNotFoundException {
2016-10-26 17:30:19 +03:00
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject obj = device.findObject(new UiSelector().text(text));
2016-05-06 13:32:01 +03:00
obj.clickAndWaitForNewWindow();
}
2016-05-06 11:09:32 +03:00
}