summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/camera/functional/ImageCaptureIntentTest.java
blob: 1de65d6134bd99d161b5f0e1b3f5dc10b1a69bf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.android.camera.functional;

import com.android.camera.Camera;
import com.android.camera.R;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.os.Process;
import android.provider.MediaStore;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.UiThreadTest;
import android.util.Log;
import android.view.KeyEvent;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

public class ImageCaptureIntentTest extends ActivityInstrumentationTestCase2 <Camera> {
    private static final String TAG = "ImageCaptureIntentTest";
    private Intent mIntent;

    public ImageCaptureIntentTest() {
        super(Camera.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    }

    @LargeTest
    public void testNoExtraOutput() throws Exception {
        setActivityIntent(mIntent);
        getActivity();

        takePicture();
        pressDone();

        assertTrue(getActivity().isFinishing());
        assertEquals(Activity.RESULT_OK, getActivity().getResultCode());
        Intent resultData = getActivity().getResultData();
        Bitmap bitmap = (Bitmap) resultData.getParcelableExtra("data");
        assertNotNull(bitmap);
        assertTrue(bitmap.getWidth() > 0);
        assertTrue(bitmap.getHeight() > 0);
    }

    @LargeTest
    public void testExtraOutput() throws Exception {
        File file = new File(Environment.getExternalStorageDirectory(),
            "test.jpg");
        BufferedInputStream stream = null;
        byte[] jpegData;

        try {
            mIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            setActivityIntent(mIntent);
            getActivity();

            takePicture();
            pressDone();

            assertTrue(getActivity().isFinishing());
            assertEquals(Activity.RESULT_OK, getActivity().getResultCode());

            // Verify the jpeg file
            int fileLength = (int) file.length();
            assertTrue(fileLength > 0);
            jpegData = new byte[fileLength];
            stream = new BufferedInputStream(new FileInputStream(file));
            stream.read(jpegData);
        } finally {
            if (stream != null) stream.close();
            file.delete();
        }

        Bitmap b = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
        assertTrue(b.getWidth() > 0);
        assertTrue(b.getHeight() > 0);
    }

    @LargeTest
    public void testRetake() throws Exception {
        setActivityIntent(mIntent);
        getActivity();

        takePicture();
        pressRetake();
        takePicture();
        pressDone();

        assertTrue(getActivity().isFinishing());
        assertEquals(Activity.RESULT_OK, getActivity().getResultCode());
        Intent resultData = getActivity().getResultData();
        Bitmap bitmap = (Bitmap) resultData.getParcelableExtra("data");
        assertNotNull(bitmap);
        assertTrue(bitmap.getWidth() > 0);
        assertTrue(bitmap.getHeight() > 0);
    }

    @LargeTest
    public void testCancel() throws Exception {
        setActivityIntent(mIntent);
        getActivity();

        pressCancel();

        assertTrue(getActivity().isFinishing());
        assertEquals(Activity.RESULT_CANCELED, getActivity().getResultCode());
    }

    @LargeTest
    public void testSnapshotCancel() throws Exception {
        setActivityIntent(mIntent);
        getActivity();

        takePicture();
        pressCancel();

        assertTrue(getActivity().isFinishing());
        assertEquals(Activity.RESULT_CANCELED, getActivity().getResultCode());
    }

    private void takePicture() throws Exception {
        getInstrumentation().sendKeySync(
                new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_FOCUS));
        getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_CAMERA);
        Thread.sleep(4000);
    }

    private void pressDone() {
        getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
                getActivity().findViewById(R.id.btn_done).performClick();
            }
        });
    }

    private void pressRetake() {
        getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
                getActivity().findViewById(R.id.btn_retake).performClick();
            }
        });
    }

    private void pressCancel() {
        getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
                getActivity().findViewById(R.id.btn_cancel).performClick();
            }
        });
    }
}