diff options
author | Brett Chabot <brettchabot@android.com> | 2011-07-28 19:33:22 -0700 |
---|---|---|
committer | Brett Chabot <brettchabot@android.com> | 2011-07-28 19:52:49 -0700 |
commit | caf30a18b52471ebfbc6ae5e853c9a0b9d44905b (patch) | |
tree | 9b673babbe28d7c4531c5a99523785b46e168f35 /packages/SystemUI/tests | |
parent | 1d55c3343d6eb89176fb057233942887466c1b68 (diff) | |
download | frameworks_base-caf30a18b52471ebfbc6ae5e853c9a0b9d44905b.zip frameworks_base-caf30a18b52471ebfbc6ae5e853c9a0b9d44905b.tar.gz frameworks_base-caf30a18b52471ebfbc6ae5e853c9a0b9d44905b.tar.bz2 |
Add functional test for screenshots.
Change-Id: Ice2cbc656f9814da7d0634644a250c1af9243ad1
Diffstat (limited to 'packages/SystemUI/tests')
5 files changed, 263 insertions, 0 deletions
diff --git a/packages/SystemUI/tests/Android.mk b/packages/SystemUI/tests/Android.mk new file mode 100644 index 0000000..28e4b86 --- /dev/null +++ b/packages/SystemUI/tests/Android.mk @@ -0,0 +1,30 @@ +# Copyright (C) 2011 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := tests + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_JAVA_LIBRARIES := android.test.runner + +LOCAL_PACKAGE_NAME := SystemUITests + +# sign this with platform cert, so this test is allowed to inject key events into +# UI it doesn't own. This is necessary to allow screenshots to be taken +LOCAL_CERTIFICATE := platform + +include $(BUILD_PACKAGE) diff --git a/packages/SystemUI/tests/AndroidManifest.xml b/packages/SystemUI/tests/AndroidManifest.xml new file mode 100644 index 0000000..e52806d --- /dev/null +++ b/packages/SystemUI/tests/AndroidManifest.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2011 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.systemui.tests"> + + <uses-permission android:name="android.permission.INJECT_EVENTS" /> + + <application> + <uses-library android:name="android.test.runner" /> + <activity android:name="com.android.systemui.screenshot.ScreenshotStubActivity" /> + </application> + + <instrumentation android:name="android.test.InstrumentationTestRunner" + android:targetPackage="com.android.systemui.tests" + android:label="Tests for SystemUI"> + </instrumentation> +</manifest> diff --git a/packages/SystemUI/tests/res/layout/main.xml b/packages/SystemUI/tests/res/layout/main.xml new file mode 100644 index 0000000..56dffe6 --- /dev/null +++ b/packages/SystemUI/tests/res/layout/main.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2011 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + > + <TextView + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="this is a test activity" + /> + <EditText + android:layout_height="wrap_content" + android:id="@+id/editText1" + android:layout_width="match_parent"> + <requestFocus></requestFocus> + </EditText> +</LinearLayout> diff --git a/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScreenshotStubActivity.java b/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScreenshotStubActivity.java new file mode 100644 index 0000000..2935373 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScreenshotStubActivity.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.systemui.screenshot; + +import com.android.systemui.tests.R; + +import android.app.Activity; +import android.os.Bundle; + +/** + * A stub activity used in {@link ScreenshotTest}. + */ +public class ScreenshotStubActivity extends Activity { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + } +} diff --git a/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScreenshotTest.java b/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScreenshotTest.java new file mode 100644 index 0000000..a0bc4d7 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/screenshot/ScreenshotTest.java @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.systemui.screenshot; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.Environment; +import android.os.FileObserver; +import android.test.ActivityInstrumentationTestCase2; +import android.test.suitebuilder.annotation.LargeTest; +import android.util.Log; +import android.view.KeyEvent; + +import java.io.File; + +/** + * Functional tests for the global screenshot feature. + */ +@LargeTest +public class ScreenshotTest extends ActivityInstrumentationTestCase2<ScreenshotStubActivity> { + + private static final String LOG_TAG = "ScreenshotTest"; + private static final int SCREEN_WAIT_TIME_SEC = 5; + + public ScreenshotTest() { + super(ScreenshotStubActivity.class); + } + + /** + * A simple test for screenshots that launches an Activity, injects the key event combo + * to trigger the screenshot, and verifies the screenshot was taken successfully. + */ + public void testScreenshot() throws Exception { + Log.d(LOG_TAG, "starting testScreenshot"); + // launch the activity. + ScreenshotStubActivity activity = getActivity(); + assertNotNull(activity); + + File screenshotDir = getScreenshotDir(); + NewScreenshotObserver observer = new NewScreenshotObserver( + screenshotDir.getAbsolutePath()); + observer.startWatching(); + takeScreenshot(); + // unlikely, but check if a new screenshot file was already created + if (observer.getCreatedPath() == null) { + // wait for screenshot to be created + synchronized(observer) { + observer.wait(SCREEN_WAIT_TIME_SEC*1000); + } + } + assertNotNull(String.format("Could not find screenshot after %d seconds", + SCREEN_WAIT_TIME_SEC), observer.getCreatedPath()); + + File screenshotFile = new File(screenshotDir, observer.getCreatedPath()); + try { + assertTrue(String.format("Detected new screenshot %s but its not a file", + screenshotFile.getName()), screenshotFile.isFile()); + assertTrue(String.format("Detected new screenshot %s but its not an image", + screenshotFile.getName()), isValidImage(screenshotFile)); + } finally { + // delete the file to prevent external storage from filing up + screenshotFile.delete(); + } + } + + private static class NewScreenshotObserver extends FileObserver { + private String mAddedPath = null; + + NewScreenshotObserver(String path) { + super(path, FileObserver.CREATE); + } + + synchronized String getCreatedPath() { + return mAddedPath; + } + + @Override + public void onEvent(int event, String path) { + Log.d(LOG_TAG, String.format("Detected new file added %s", path)); + synchronized (this) { + mAddedPath = path; + notify(); + } + } + } + + /** + * Inject the key sequence to take a screenshot. + */ + private void takeScreenshot() { + getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, + KeyEvent.KEYCODE_POWER)); + getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, + KeyEvent.KEYCODE_VOLUME_DOWN)); + // the volume down key event will cause the 'volume adjustment' UI to appear in the + // foreground, and steal UI focus + // unfortunately this means the next key event will get directed to the + // 'volume adjustment' UI, instead of this test's activity + // for this reason this test must be signed with platform certificate, to grant this test + // permission to inject key events to another process + getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, + KeyEvent.KEYCODE_VOLUME_DOWN)); + getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, + KeyEvent.KEYCODE_POWER)); + } + + /** + * Get the directory where screenshot images are stored. + */ + private File getScreenshotDir() { + // TODO: get this dir location from a constant + return new File(Environment.getExternalStorageDirectory(), "Pictures" + File.separator + + "Screenshots"); + } + + /** + * Return true if file is valid image file + */ + private boolean isValidImage(File screenshotFile) { + Bitmap b = BitmapFactory.decodeFile(screenshotFile.getAbsolutePath()); + // TODO: do more checks on image + return b != null; + } +} |