InitialTest.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * ownCloud Android client application
  3. * <p/>
  4. * Copyright (C) 2015 ownCloud Inc.
  5. * <p/>
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. * <p/>
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. * <p/>
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package com.owncloud.android.uiautomator;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. import org.junit.runner.RunWith;
  22. import android.content.Context;
  23. import android.content.Intent;
  24. import android.content.pm.PackageManager;
  25. import android.content.pm.ResolveInfo;
  26. import android.support.test.InstrumentationRegistry;
  27. import android.support.test.filters.SdkSuppress;
  28. import android.support.test.runner.AndroidJUnit4;
  29. import android.support.test.uiautomator.By;
  30. import android.support.test.uiautomator.UiDevice;
  31. import android.support.test.uiautomator.UiObject;
  32. import android.support.test.uiautomator.UiObjectNotFoundException;
  33. import android.support.test.uiautomator.UiSelector;
  34. import android.support.test.uiautomator.Until;
  35. import static org.hamcrest.CoreMatchers.notNullValue;
  36. import static org.junit.Assert.assertThat;
  37. /**
  38. * UI Automator tests
  39. */
  40. @RunWith(AndroidJUnit4.class)
  41. @SdkSuppress(minSdkVersion = 18)
  42. public class InitialTest {
  43. private static final String OWNCLOUD_APP_PACKAGE = "com.owncloud.android";
  44. private static final String ANDROID_SETTINGS_PACKAGE = "com.android.settings";
  45. private static final String SETTINGS_DATA_USAGE_OPTION = "Data usage";
  46. private static final int LAUNCH_TIMEOUT = 5000;
  47. private UiDevice mDevice;
  48. @Before
  49. public void initializeDevice() {
  50. // Initialize UiDevice instance
  51. mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
  52. }
  53. @Test
  54. public void checkPreconditions() {
  55. assertThat(mDevice, notNullValue());
  56. }
  57. /**
  58. * Start owncloud app
  59. */
  60. @Test
  61. public void startAppFromHomeScreen() {
  62. // Perform a short press on the HOME button
  63. mDevice.pressHome();
  64. // Wait for launcher
  65. final String launcherPackage = getLauncherPackageName();
  66. assertThat(launcherPackage, notNullValue());
  67. mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
  68. // Launch the app
  69. Context context = InstrumentationRegistry.getContext();
  70. final Intent intent = context.getPackageManager()
  71. .getLaunchIntentForPackage(OWNCLOUD_APP_PACKAGE);
  72. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  73. context.startActivity(intent);
  74. // Wait for the app to appear
  75. mDevice.wait(Until.hasObject(By.pkg(OWNCLOUD_APP_PACKAGE).depth(0)), LAUNCH_TIMEOUT);
  76. }
  77. /**
  78. * Start Settings app
  79. *
  80. * @throws UiObjectNotFoundException
  81. */
  82. @Test
  83. public void startSettingsFromHomeScreen() throws UiObjectNotFoundException {
  84. mDevice.pressHome();
  85. // Wait for launcher
  86. final String launcherPackage = getLauncherPackageName();
  87. assertThat(launcherPackage, notNullValue());
  88. mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
  89. // Launch the app
  90. Context context = InstrumentationRegistry.getContext();
  91. final Intent intent = context.getPackageManager()
  92. .getLaunchIntentForPackage(ANDROID_SETTINGS_PACKAGE);
  93. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  94. context.startActivity(intent);
  95. clickByText(SETTINGS_DATA_USAGE_OPTION);
  96. }
  97. /**
  98. * Uses package manager to find the package name of the device launcher. Usually this package
  99. * is "com.android.launcher" but can be different at times. This is a generic solution which
  100. * works on all platforms.`
  101. */
  102. private String getLauncherPackageName() {
  103. // Create launcher Intent
  104. final Intent intent = new Intent(Intent.ACTION_MAIN);
  105. intent.addCategory(Intent.CATEGORY_HOME);
  106. // Use PackageManager to get the launcher package name
  107. PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
  108. ResolveInfo resolveInfo = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
  109. return resolveInfo.activityInfo.packageName;
  110. }
  111. /**
  112. * Helper to click on objects that match the content-description text
  113. *
  114. * @param text
  115. * @throws UiObjectNotFoundException
  116. */
  117. private void clickByDescription(String text) throws UiObjectNotFoundException {
  118. UiObject obj = new UiObject(new UiSelector().description(text));
  119. obj.clickAndWaitForNewWindow();
  120. }
  121. /**
  122. * Helper to click on object that match the text value
  123. *
  124. * @param text
  125. * @throws UiObjectNotFoundException
  126. */
  127. private void clickByText(String text) throws UiObjectNotFoundException {
  128. UiObject obj = new UiObject(new UiSelector().text(text));
  129. obj.clickAndWaitForNewWindow();
  130. }
  131. }