10d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li/*
20d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li * Copyright (C) 2009 The Android Open Source Project
30d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li *
40d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li * Licensed under the Apache License, Version 2.0 (the "License");
50d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li * you may not use this file except in compliance with the License.
60d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li * You may obtain a copy of the License at
70d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li *
80d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li *      http://www.apache.org/licenses/LICENSE-2.0
90d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li *
100d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li * Unless required by applicable law or agreed to in writing, software
110d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li * distributed under the License is distributed on an "AS IS" BASIS,
120d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li * See the License for the specific language governing permissions and
140d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li * limitations under the License.
150d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li */
160d604e2af9eeed22685fd62e2281c2cd9728d58bWu-cheng Li
17487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Laupackage com.android.camera.stress;
18487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau
19559f5cf9f42c4983eaef9e6e55ff3ba7c406426aWu-cheng Liimport com.android.camera.Camera;
20559f5cf9f42c4983eaef9e6e55ff3ba7c406426aWu-cheng Liimport com.android.camera.VideoCamera;
21559f5cf9f42c4983eaef9e6e55ff3ba7c406426aWu-cheng Li
223e2d95456def6a6d6e716af77e4708a15747d2edRay Chenimport android.app.Activity;
23487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lauimport android.app.Instrumentation;
24487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lauimport android.content.Intent;
25487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lauimport android.os.Debug;
263e2d95456def6a6d6e716af77e4708a15747d2edRay Chenimport android.os.Environment;
27487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lauimport android.test.InstrumentationTestCase;
28487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lauimport android.test.suitebuilder.annotation.LargeTest;
29487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lauimport android.util.Log;
303e2d95456def6a6d6e716af77e4708a15747d2edRay Chen
31487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lauimport java.io.FileWriter;
32487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lauimport java.io.BufferedWriter;
33487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau
34487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau/**
35487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau * Test cases to measure the camera and video recorder startup time.
36487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau */
37487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Laupublic class CameraStartUp extends InstrumentationTestCase {
38487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau
39487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    private static final int TOTAL_NUMBER_OF_STARTUP = 20;
40487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau
41487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    private String TAG = "CameraStartUp";
423e2d95456def6a6d6e716af77e4708a15747d2edRay Chen    private static final String CAMERA_TEST_OUTPUT_FILE =
433e2d95456def6a6d6e716af77e4708a15747d2edRay Chen            Environment.getExternalStorageDirectory().toString() + "/mediaStressOut.txt";
44487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    private static int WAIT_TIME_FOR_PREVIEW = 1500; //1.5 second
45487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau
46487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    private long launchCamera() {
47487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        long startupTime = 0;
48487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        try {
49487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            Intent intent = new Intent(Intent.ACTION_MAIN);
50559f5cf9f42c4983eaef9e6e55ff3ba7c406426aWu-cheng Li            intent.setClass(getInstrumentation().getTargetContext(), Camera.class);
51487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
52487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            long beforeStart = System.currentTimeMillis();
53487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            Instrumentation inst = getInstrumentation();
54487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            Activity cameraActivity = inst.startActivitySync(intent);
55487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            long cameraStarted = System.currentTimeMillis();
56487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            cameraActivity.finish();
57487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            startupTime = cameraStarted - beforeStart;
58487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            Thread.sleep(1000);
59487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            Log.v(TAG, "camera startup time: " + startupTime);
60487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        } catch (Exception e) {
612ff6c061fe55ad46b192961f27379853c20362bcWu-cheng Li            Log.v(TAG, "Got exception", e);
62487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            fail("Fails to get the output file");
63487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        }
64487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        return startupTime;
65487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    }
66487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau
67487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    private long launchVideo() {
68487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        long startupTime = 0;
69487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau
70487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        try {
71487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            Intent intent = new Intent(Intent.ACTION_MAIN);
72559f5cf9f42c4983eaef9e6e55ff3ba7c406426aWu-cheng Li            intent.setClass(getInstrumentation().getTargetContext(), VideoCamera.class);
73487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
74487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            long beforeStart = System.currentTimeMillis();
75487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            Instrumentation inst = getInstrumentation();
76487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            Activity recorderActivity = inst.startActivitySync(intent);
77487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            long cameraStarted = System.currentTimeMillis();
78487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            recorderActivity.finish();
79487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            startupTime = cameraStarted - beforeStart;
80487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            Log.v(TAG, "Video Startup Time = " + startupTime);
81487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            // wait for 1s to make sure it reach a clean stage
82487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            Thread.sleep(WAIT_TIME_FOR_PREVIEW);
83487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            Log.v(TAG, "video startup time: " + startupTime);
84487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        } catch (Exception e) {
852ff6c061fe55ad46b192961f27379853c20362bcWu-cheng Li            Log.v(TAG, "Got exception", e);
86487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            fail("Fails to launch video output file");
87487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        }
88487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        return startupTime;
89487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    }
90487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau
91499e3b0ab79d8c1b0042f3b5d0beea05ec5bdbc0Yu Shan Emily Lau    private void writeToOutputFile(long totalStartupTime,
92499e3b0ab79d8c1b0042f3b5d0beea05ec5bdbc0Yu Shan Emily Lau            String individualStartupTime, boolean firstStartUp, String Type) throws Exception {
93898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau        // TODO (yslau) : Need to integrate the output data with central
94898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau        // dashboard
95487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        try {
96487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            FileWriter fstream = null;
97487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            fstream = new FileWriter(CAMERA_TEST_OUTPUT_FILE, true);
98487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            BufferedWriter out = new BufferedWriter(fstream);
99898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau            if (firstStartUp) {
100499e3b0ab79d8c1b0042f3b5d0beea05ec5bdbc0Yu Shan Emily Lau                out.write("First " + Type + " Startup: " + totalStartupTime + "\n");
101898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau            } else {
102898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                long averageStartupTime = totalStartupTime / (TOTAL_NUMBER_OF_STARTUP -1);
103499e3b0ab79d8c1b0042f3b5d0beea05ec5bdbc0Yu Shan Emily Lau                out.write(Type + "startup time: " + "\n");
104898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                out.write("Number of loop: " + (TOTAL_NUMBER_OF_STARTUP -1)  + "\n");
105898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                out.write(individualStartupTime + "\n\n");
106499e3b0ab79d8c1b0042f3b5d0beea05ec5bdbc0Yu Shan Emily Lau                out.write(Type + " average startup time: " + averageStartupTime + " ms\n\n");
107898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau            }
108487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            out.close();
109487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            fstream.close();
110487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        } catch (Exception e) {
111487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau            fail("Camera write output to file");
112487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        }
113487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    }
114487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau
115487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    @LargeTest
116487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    public void testLaunchVideo() throws Exception {
1176dac837de88ba85addbb44728415f8e48013d5b5Yu Shan Emily Lau        String individualStartupTime;
1186dac837de88ba85addbb44728415f8e48013d5b5Yu Shan Emily Lau        individualStartupTime = "Individual Video Startup Time = ";
1196dac837de88ba85addbb44728415f8e48013d5b5Yu Shan Emily Lau        long totalStartupTime = 0;
1206dac837de88ba85addbb44728415f8e48013d5b5Yu Shan Emily Lau        long startupTime = 0;
121898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau        for (int i = 0; i < TOTAL_NUMBER_OF_STARTUP; i++) {
122898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau            if (i == 0) {
123898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                // Capture the first startup time individually
124898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                long firstStartUpTime = launchVideo();
125499e3b0ab79d8c1b0042f3b5d0beea05ec5bdbc0Yu Shan Emily Lau                writeToOutputFile(firstStartUpTime, "na", true, "Video");
126898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau            } else {
127898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                startupTime = launchVideo();
128898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                totalStartupTime += startupTime;
129898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                individualStartupTime += startupTime + " ,";
130898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau            }
131487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        }
132487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        Log.v(TAG, "totalStartupTime =" + totalStartupTime);
133499e3b0ab79d8c1b0042f3b5d0beea05ec5bdbc0Yu Shan Emily Lau        writeToOutputFile(totalStartupTime, individualStartupTime, false, "Video");
134487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    }
135487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau
136487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    @LargeTest
137487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    public void testLaunchCamera() throws Exception {
1386dac837de88ba85addbb44728415f8e48013d5b5Yu Shan Emily Lau        String individualStartupTime;
1396dac837de88ba85addbb44728415f8e48013d5b5Yu Shan Emily Lau        individualStartupTime = "Individual Camera Startup Time = ";
140898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau        long totalStartupTime = 0;
1416dac837de88ba85addbb44728415f8e48013d5b5Yu Shan Emily Lau        long startupTime = 0;
142898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau        for (int i = 0; i < TOTAL_NUMBER_OF_STARTUP; i++) {
143898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau            if (i == 0) {
144898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                // Capture the first startup time individually
145898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                long firstStartUpTime = launchCamera();
146499e3b0ab79d8c1b0042f3b5d0beea05ec5bdbc0Yu Shan Emily Lau                writeToOutputFile(firstStartUpTime, "na", true, "Camera");
147898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau            } else {
148898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                startupTime = launchCamera();
149898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                totalStartupTime += startupTime;
150898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau                individualStartupTime += startupTime + " ,";
151898d47682b32bfffaa47edec9be1364c6df67eb2Yu Shan Emily Lau            }
152487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        }
153487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau        Log.v(TAG, "totalStartupTime =" + totalStartupTime);
154499e3b0ab79d8c1b0042f3b5d0beea05ec5bdbc0Yu Shan Emily Lau        writeToOutputFile(totalStartupTime,
155499e3b0ab79d8c1b0042f3b5d0beea05ec5bdbc0Yu Shan Emily Lau                individualStartupTime, false, "Camera");
156487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau    }
157487f28c139588b69963eb6374228e7e8cf346ca3Yu Shan Emily Lau}