1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.cts.refapp;
18 
19 import android.app.Activity;
20 import android.test.ActivityInstrumentationTestCase2;
21 import android.util.Log;
22 
23 import java.lang.InterruptedException;
24 import java.lang.Thread;
25 
26 /**
27  * Base Class that provides common functionality for all Reference Application Tests.
28  */
29 public class ReferenceAppTestCase<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
30     /** The time to wait for the test host to finish taking the snapshot. */
31     private static final int SNAPSHOT_TIMEOUT_MS = 1000;
32     /** The default time that the applicaiton has to start in. */
33     private static final int DEFAULT_MAX_STATUP_TIME_MS = 5000;
34 
35     private int maxStartupTimeMs;
36 
37     /**
38      * Create a ReferenceAppTestCase for the specified activity.
39      *
40      * @param pkg the java package the class is contained in.
41      * @param activityClass the class of the activity to instrument.
42      * @param maxStartupTimeMs the startup time the activity should start in.
43      */
ReferenceAppTestCase(String pkg, Class<T> activityClass, int maxStartupTimeMs)44     public ReferenceAppTestCase(String pkg, Class<T> activityClass, int maxStartupTimeMs) {
45         super(pkg, activityClass);
46         this.maxStartupTimeMs = maxStartupTimeMs;
47     }
48 
49     /**
50      * Create a ReferenceAppTestCase for the specified activity.
51      *
52      * @param pkg the java package the class is contained in.
53      * @param activityClass the class of the activity to instrument.
54      */
ReferenceAppTestCase(String pkg, Class<T> activityClass)55     public ReferenceAppTestCase(String pkg, Class<T> activityClass) {
56        this(pkg, activityClass, DEFAULT_MAX_STATUP_TIME_MS);
57     }
58 
testActivityStartupTime()59     public void testActivityStartupTime() {
60         // Test activity startup time.
61 
62         long start = System.currentTimeMillis();
63         Activity activity = getActivity();
64         long end = System.currentTimeMillis();
65 
66         long startupTime = end - start;
67         assertTrue("Activity Startup took more than " + maxStartupTimeMs +
68                    " ms",
69                    startupTime <= maxStartupTimeMs);
70     }
71 
72     /**
73      * Allows tests to record screen snapshots for inclusion in the
74      * CTS test report.
75      *
76      * @param name the name to save the snapshot under
77      */
takeSnapshot(String name)78     public void takeSnapshot(String name) {
79         // request a snapshot from the CTS host
80         Log.d("ReferenceAppTestCase", "takeSnapshot:" + name);
81         // Give the host enough time to take the picture
82         try {
83             Thread.sleep(SNAPSHOT_TIMEOUT_MS);
84         } catch (InterruptedException e) {
85             // ok
86         }
87     }
88 }
89