OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.test; |
| 6 |
| 7 import android.annotation.TargetApi; |
| 8 import android.app.ActivityManager; |
| 9 import android.content.Context; |
| 10 import android.content.Intent; |
| 11 import android.os.Build; |
| 12 |
| 13 import junit.framework.Assert; |
| 14 |
| 15 import org.chromium.base.ApplicationState; |
| 16 import org.chromium.base.ApplicationStatus; |
| 17 import org.chromium.base.test.util.CommandLineFlags; |
| 18 import org.chromium.chrome.browser.ChromeSwitches; |
| 19 import org.chromium.chrome.browser.omaha.OmahaClient; |
| 20 import org.chromium.content.browser.test.util.Criteria; |
| 21 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 22 |
| 23 /** |
| 24 * Base for testing and interacting with multiple Activities (e.g. Document or W
ebapp Activities). |
| 25 */ |
| 26 @CommandLineFlags.Add({ |
| 27 ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE |
| 28 }) |
| 29 public abstract class MultiActivityTestBase extends RestrictedInstrumentationTes
tCase { |
| 30 @Override |
| 31 public void setUp() throws Exception { |
| 32 super.setUp(); |
| 33 |
| 34 // Disable Omaha related activities. |
| 35 OmahaClient.setEnableCommunication(false); |
| 36 OmahaClient.setEnableUpdateDetection(false); |
| 37 |
| 38 // Kill any tasks, if we have the API for it. |
| 39 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 40 Context context = getInstrumentation().getTargetContext(); |
| 41 MultiActivityTestBase.finishAllChromeTasks(context); |
| 42 } |
| 43 } |
| 44 |
| 45 @Override |
| 46 public void tearDown() throws Exception { |
| 47 super.tearDown(); |
| 48 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 49 Context context = getInstrumentation().getTargetContext(); |
| 50 MultiActivityTestBase.finishAllChromeTasks(context); |
| 51 } |
| 52 } |
| 53 |
| 54 /** Counts how many tasks Chrome has listed in Android's Overview. */ |
| 55 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 56 public static int getNumChromeTasks(Context context) { |
| 57 int count = 0; |
| 58 ActivityManager activityManager = |
| 59 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERV
ICE); |
| 60 return activityManager.getAppTasks().size(); |
| 61 } |
| 62 |
| 63 /** Finishes all tasks Chrome has listed in Android's Overview. */ |
| 64 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 65 private static void finishAllChromeTasks(final Context context) throws Excep
tion { |
| 66 ActivityManager activityManager = |
| 67 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERV
ICE); |
| 68 for (ActivityManager.AppTask task : activityManager.getAppTasks()) { |
| 69 task.finishAndRemoveTask(); |
| 70 } |
| 71 |
| 72 Assert.assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 73 @Override |
| 74 public boolean isSatisfied() { |
| 75 return getNumChromeTasks(context) == 0; |
| 76 } |
| 77 })); |
| 78 } |
| 79 |
| 80 /** Send the user to the home screen. */ |
| 81 public static void launchHomescreenIntent(Context context) throws Exception
{ |
| 82 Intent homeIntent = new Intent(Intent.ACTION_MAIN); |
| 83 homeIntent.addCategory(Intent.CATEGORY_HOME); |
| 84 homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 85 context.startActivity(homeIntent); |
| 86 waitUntilChromeInBackground(); |
| 87 } |
| 88 |
| 89 /** Waits until Chrome is in the foreground. */ |
| 90 public static void waitUntilChromeInForeground() throws Exception { |
| 91 Assert.assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 92 @Override |
| 93 public boolean isSatisfied() { |
| 94 int state = ApplicationStatus.getStateForApplication(); |
| 95 return state == ApplicationState.HAS_RUNNING_ACTIVITIES; |
| 96 } |
| 97 })); |
| 98 } |
| 99 |
| 100 /** Waits until Chrome is in the background. */ |
| 101 public static void waitUntilChromeInBackground() throws Exception { |
| 102 Assert.assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| 103 @Override |
| 104 public boolean isSatisfied() { |
| 105 int state = ApplicationStatus.getStateForApplication(); |
| 106 return state == ApplicationState.HAS_STOPPED_ACTIVITIES |
| 107 || state == ApplicationState.HAS_DESTROYED_ACTIVITIES; |
| 108 } |
| 109 })); |
| 110 } |
| 111 |
| 112 } |
OLD | NEW |