InitialTest.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 android.content.Intent;
  20. import android.content.pm.PackageManager;
  21. import android.content.pm.ResolveInfo;
  22. import android.support.test.InstrumentationRegistry;
  23. import android.support.test.filters.SdkSuppress;
  24. import android.support.test.runner.AndroidJUnit4;
  25. import android.support.test.uiautomator.UiDevice;
  26. import android.support.test.uiautomator.UiObject;
  27. import android.support.test.uiautomator.UiObjectNotFoundException;
  28. import android.support.test.uiautomator.UiSelector;
  29. import org.junit.Before;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import static org.hamcrest.CoreMatchers.notNullValue;
  33. import static org.junit.Assert.assertThat;
  34. /**
  35. * UI Automator tests
  36. */
  37. @RunWith(AndroidJUnit4.class)
  38. @SdkSuppress(minSdkVersion = 18)
  39. public class InitialTest {
  40. private static final String OWNCLOUD_APP_PACKAGE = "com.owncloud.android";
  41. private static final String ANDROID_SETTINGS_PACKAGE = "com.android.settings";
  42. private static final String SETTINGS_DATA_USAGE_OPTION = "Data usage";
  43. private static final int LAUNCH_TIMEOUT = 5000;
  44. private UiDevice mDevice;
  45. @Before
  46. public void initializeDevice() {
  47. // Initialize UiDevice instance
  48. mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
  49. }
  50. @Test
  51. public void checkPreconditions() {
  52. assertThat(mDevice, notNullValue());
  53. }
  54. /**
  55. * Start owncloud app
  56. */
  57. // @Test
  58. // public void startAppFromHomeScreen() {
  59. // // Perform a short press on the HOME button
  60. // mDevice.pressHome();
  61. //
  62. // // Wait for launcher
  63. // final String launcherPackage = getLauncherPackageName();
  64. // assertThat(launcherPackage, notNullValue());
  65. // mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
  66. //
  67. // // Launch the app
  68. // Context context = InstrumentationRegistry.getContext();
  69. // final Intent intent = context.getPackageManager()
  70. // .getLaunchIntentForPackage(OWNCLOUD_APP_PACKAGE);
  71. // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  72. // context.startActivity(intent);
  73. //
  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. //
  85. // mDevice.pressHome();
  86. //
  87. // // Wait for launcher
  88. // final String launcherPackage = getLauncherPackageName();
  89. // assertThat(launcherPackage, notNullValue());
  90. // mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);
  91. //
  92. // // Launch the app
  93. // Context context = InstrumentationRegistry.getContext();
  94. // final Intent intent = context.getPackageManager()
  95. // .getLaunchIntentForPackage(ANDROID_SETTINGS_PACKAGE);
  96. // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  97. // context.startActivity(intent);
  98. //
  99. // clickByText(SETTINGS_DATA_USAGE_OPTION);
  100. // }
  101. /**
  102. * Uses package manager to find the package name of the device launcher. Usually this package
  103. * is "com.android.launcher" but can be different at times. This is a generic solution which
  104. * works on all platforms.`
  105. */
  106. private String getLauncherPackageName() {
  107. // Create launcher Intent
  108. final Intent intent = new Intent(Intent.ACTION_MAIN);
  109. intent.addCategory(Intent.CATEGORY_HOME);
  110. // Use PackageManager to get the launcher package name
  111. PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();
  112. ResolveInfo resolveInfo = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
  113. return resolveInfo.activityInfo.packageName;
  114. }
  115. /**
  116. * Helper to click on object that match the text value.
  117. *
  118. * @param text the text
  119. * @throws UiObjectNotFoundException
  120. */
  121. private void clickByText(String text) throws UiObjectNotFoundException {
  122. UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
  123. UiObject obj = device.findObject(new UiSelector().text(text));
  124. obj.clickAndWaitForNewWindow();
  125. }
  126. }