diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:05:43 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:05:43 -0800 |
commit | f013e1afd1e68af5e3b868c26a653bbfb39538f8 (patch) | |
tree | 7ad6c8fd9c7b55f4b4017171dec1cb760bbd26bf /tests/framework-tests/src | |
parent | e70cfafe580c6f2994c4827cd8a534aabf3eb05c (diff) | |
download | frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.zip frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.tar.gz frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.tar.bz2 |
Code drop from //branches/cupcake/...@124589
Diffstat (limited to 'tests/framework-tests/src')
8 files changed, 1022 insertions, 0 deletions
diff --git a/tests/framework-tests/src/Android.mk b/tests/framework-tests/src/Android.mk new file mode 100644 index 0000000..54e33a4 --- /dev/null +++ b/tests/framework-tests/src/Android.mk @@ -0,0 +1,10 @@ +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := $(call all-subdir-java-files) + +LOCAL_MODULE := framework-tests + +LOCAL_JAVA_LIBRARIES := android.policy_phone android.test.runner + +include $(BUILD_JAVA_LIBRARY) diff --git a/tests/framework-tests/src/android/test/FrameworkTests.java b/tests/framework-tests/src/android/test/FrameworkTests.java new file mode 100644 index 0000000..b5f6292 --- /dev/null +++ b/tests/framework-tests/src/android/test/FrameworkTests.java @@ -0,0 +1,28 @@ +package android.test; + +import com.android.internal.os.LoggingPrintStreamTest; +import android.util.EventLogFunctionalTest; +import android.util.EventLogTest; +import junit.framework.TestSuite; +import com.android.internal.http.multipart.MultipartTest; +import com.android.internal.policy.impl.LockPatternKeyguardViewTest; + +/** + * Tests that are loaded in the boot classpath along with the Android framework + * classes. This enables you to access package-private members in the framework + * classes; doing so is not possible when the test classes are loaded in an + * application classloader. + */ +public class FrameworkTests { + public static TestSuite suite() { + TestSuite suite = new TestSuite(FrameworkTests.class.getName()); + + suite.addTestSuite(MultipartTest.class); + suite.addTestSuite(EventLogTest.class); + suite.addTestSuite(EventLogFunctionalTest.class); + suite.addTestSuite(LoggingPrintStreamTest.class); + suite.addTestSuite(LockPatternKeyguardViewTest.class); + + return suite; + } +} diff --git a/tests/framework-tests/src/android/text/PackedIntVectorTest.java b/tests/framework-tests/src/android/text/PackedIntVectorTest.java new file mode 100644 index 0000000..78cdee9 --- /dev/null +++ b/tests/framework-tests/src/android/text/PackedIntVectorTest.java @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2007 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 android.text; + +import android.text.PackedIntVector; +import junit.framework.TestCase; + +/** + * PackedIntVectorTest tests the features of android.util.PackedIntVector. + */ +public class PackedIntVectorTest extends TestCase { + + public void testBasic() throws Exception { + for (int width = 0; width < 10; width++) { + PackedIntVector p = new PackedIntVector(width); + int[] ins = new int[width]; + + for (int height = width * 2; height < width * 4; height++) { + assertEquals(p.width(), width); + + // Test adding rows. + + for (int i = 0; i < height; i++) { + int at; + + if (i % 2 == 0) { + at = i; + } else { + at = p.size() - i; + } + + for (int j = 0; j < width; j++) { + ins[j] = i + j; + } + + if (i == height / 2) { + p.insertAt(at, null); + } else { + p.insertAt(at, ins); + } + + assertEquals(p.size(), i + 1); + + for (int j = 0; j < width; j++) { + if (i == height / 2) { + assertEquals(0, p.getValue(at, j)); + } else { + assertEquals(p.getValue(at, j), i + j); + } + } + } + + // Test setting values. + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + p.setValue(i, j, i * j); + + assertEquals(p.getValue(i, j), i * j); + } + } + + // Test offsetting values. + + for (int j = 0; j < width; j++) { + p.adjustValuesBelow(j * 2, j, j + 27); + } + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + int expect = i * j; + + if (i >= j * 2) { + expect += j + 27; + } + + assertEquals(p.getValue(i, j), expect); + } + } + + for (int j = 0; j < width; j++) { + p.adjustValuesBelow(j, j, j * j + 14); + } + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + int expect = i * j; + + if (i >= j * 2) { + expect += j + 27; + } + if (i >= j) { + expect += j * j + 14; + } + + assertEquals(p.getValue(i, j), expect); + } + } + + // Test undoing offsets. + + for (int j = 0; j < width; j++) { + p.adjustValuesBelow(j * 2, j, -(j + 27)); + p.adjustValuesBelow(j, j, -(j * j + 14)); + } + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + assertEquals(p.getValue(i, j), i * j); + } + } + + // Test deleting rows. + + while (p.size() > 0) { + int osize = p.size(); + int del = osize / 3; + + if (del == 0) { + del = 1; + } + + int at = (osize - del) / 2; + p.deleteAt(at, del); + + assertEquals(p.size(), osize - del); + + for (int i = 0; i < at; i++) { + for (int j = 0; j < width; j++) { + assertEquals(p.getValue(i, j), i * j); + } + } + + for (int i = at; i < p.size(); i++) { + for (int j = 0; j < width; j++) { + assertEquals(p.getValue(i, j), (i + height - p.size()) * j); + } + } + } + + assertEquals(0, p.size()); + } + } + } +} diff --git a/tests/framework-tests/src/android/util/EventLogFunctionalTest.java b/tests/framework-tests/src/android/util/EventLogFunctionalTest.java new file mode 100644 index 0000000..8263083 --- /dev/null +++ b/tests/framework-tests/src/android/util/EventLogFunctionalTest.java @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2007 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 android.util; + +import android.os.Process; + +import com.google.android.collect.Lists; + +import junit.framework.TestCase; +import junit.framework.Assert; + +import java.util.ArrayList; + +/** + * Functional tests of EventLog. + */ + +public class EventLogFunctionalTest extends TestCase { + private static final String TAG = "EventLogFunctionalTest"; + + private static final int TAG_SIZE = 4; + private static final int TYPE_FIELD_SIZE = 1; + private static final int STARTING_POS_OF_PAYLOAD = TAG_SIZE + TYPE_FIELD_SIZE; + + private static final int TEST_TAG = 42; + private static final int TEST_TAG2 = 314; + + //todo: For now all we do is test the returned length. More to come. + public void testLogOfPosInt() throws Exception { + final int numBytes = EventLog.writeEvent(TEST_TAG, 0x01020304); + Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 4, numBytes); + } + + //todo: For now all we do is test the returned length. More to come. + public void testLogOfPosLong() throws Exception { + final int numBytes = EventLog.writeEvent(TEST_TAG2, 0x0102030405060708L); + Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 8, numBytes); + } + + //todo: For now all we do is test the returned length. More to come. + public void testLogOfString() throws Exception { + final String valueStr = "foo bar baz"; + final int numBytes = EventLog.writeEvent(TEST_TAG, valueStr); + Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 4 + valueStr.length() + 1, numBytes); + } + + public void testLogOfListWithOneInt() throws Exception { + final EventLog.List list = new EventLog.List(1234); + final int numBytes = EventLog.writeEvent(TEST_TAG, list); + Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 1 + 1 + 4 + 1, numBytes); + } + + public void testLogOfListWithMultipleInts() throws Exception { + final EventLog.List list = new EventLog.List(1234, 2345, 3456); + final int numBytes = EventLog.writeEvent(TEST_TAG, list); + Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 1 + 1 + 4 + 1 + 4 + 1 + 4 + 1, numBytes); + } + + public void testLogOfListWithEmbeddedList() throws Exception { + final EventLog.List list = new EventLog.List( + new EventLog.List(1234, 2345, 3456)); + final int numBytes = EventLog.writeEvent(TEST_TAG, list); + Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 2 + 1 + 1 + 4 + 1 + 4 + 1 + 4 + 1, numBytes); + } + + public void testEventLargerThanInitialBufferCapacity() throws Exception { + final Integer[] array = new Integer[127]; + for (int i = 0; i < array.length; i++) { + array[i] = i; + } + final EventLog.List list = new EventLog.List((Object[]) array); + final int numBytes = EventLog.writeEvent(TEST_TAG, list); + Assert.assertEquals(STARTING_POS_OF_PAYLOAD + 1 + (5 * array.length) + 1, numBytes); + } + + // This test is obsolete. See http://b/issue?id=1262082 + public void disableTestReadSimpleEvent() throws Exception { + long when = System.currentTimeMillis(); + EventLog.writeEvent(2718, 12345); + Log.i(TAG, "Wrote simple event at T=" + when); + + ArrayList<EventLog.Event> list = new ArrayList<EventLog.Event>(); + EventLog.readEvents(new int[] { 2718 }, list); + + boolean found = false; + for (EventLog.Event event : list) { + assertEquals(event.getTag(), 2718); + long eventTime = event.getTimeNanos() / 1000000; + Log.i(TAG, " Found event T=" + eventTime); + if (eventTime > when - 100 && eventTime < when + 1000) { + assertEquals(event.getProcessId(), Process.myPid()); + assertEquals(event.getThreadId(), Process.myTid()); + assertEquals(event.getData(), 12345); + + assertFalse(found); + found = true; + } + } + + assertTrue(found); + } + + // This test is obsolete. See http://b/issue?id=1262082 + public void disableTestReadCompoundEntry() throws Exception { + long when = System.currentTimeMillis(); + EventLog.writeEvent(2719, + new EventLog.List(1l, new EventLog.List("2", "three", "4"), 5)); + Log.i(TAG, "Wrote compound event at T=" + when); + + ArrayList<EventLog.Event> list = new ArrayList<EventLog.Event>(); + EventLog.readEvents(new int[] { 2719 }, list); + + boolean found = false; + for (EventLog.Event event : list) { + long eventTime = event.getTimeNanos() / 1000000; + Log.i(TAG, " Found event T=" + eventTime); + if (eventTime > when - 100 && eventTime < when + 1000) { + EventLog.List data = (EventLog.List) event.getData(); + assertEquals(data.getNumItems(), 3); + + EventLog.List nested = (EventLog.List) data.getItem(1); + assertEquals(nested.getNumItems(), 3); + + assertEquals(data.getItem(0), 1l); + assertEquals(nested.getItem(0), "2"); + assertEquals(nested.getItem(1), "three"); + assertEquals(nested.getItem(2), "4"); + assertEquals(data.getItem(2), 5); + + assertFalse(found); + found = true; + } + } + + assertTrue(found); + } + + public void testEventLogTagsFile() throws Exception { + EventLogTags tags = new EventLogTags(); + assertEquals(tags.get("answer").mTag, 42); + assertEquals(tags.get("pi").mTag, 314); + assertEquals(tags.get("e").mTag, 2718); + assertEquals(tags.get(42).mName, "answer"); + assertEquals(tags.get(314).mName, "pi"); + assertEquals(tags.get(2718).mName, "e"); + } +} diff --git a/tests/framework-tests/src/android/util/EventLogTest.java b/tests/framework-tests/src/android/util/EventLogTest.java new file mode 100644 index 0000000..4a5d888 --- /dev/null +++ b/tests/framework-tests/src/android/util/EventLogTest.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2007 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 android.util; + +import com.google.android.collect.Lists; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +/** + * tests for {@link EventLog} + */ + +public class EventLogTest extends TestCase { + private static final int TEST_TAG = 42; + + public void testIllegalListTypesThrowException() throws Exception { + try { + EventLog.writeEvent(TEST_TAG, new EventLog.List(new Object())); + fail("Can't create List with any old Object"); + } catch (IllegalArgumentException e) { + // expected + } + try { + EventLog.writeEvent(TEST_TAG, new EventLog.List((byte) 1)); + fail("Can't create List with any old byte"); + } catch (IllegalArgumentException e) { + // expected + } + } + + void assertIntInByteArrayEquals(int expected, byte[] buf, int pos) { + ByteBuffer computedBuf = ByteBuffer.wrap(buf).order(ByteOrder.nativeOrder()); + int computed = computedBuf.getInt(pos); + Assert.assertEquals(expected, computed); + } + + void assertLongInByteArrayEquals(long expected, byte[] buf, int pos) { + ByteBuffer computedBuf = ByteBuffer.wrap(buf).order(ByteOrder.nativeOrder()); + long computed = computedBuf.getLong(pos); + Assert.assertEquals(expected, computed); + } + + void assertStringInByteArrayEquals(String expected, byte[] buf, int pos) { + byte[] expectedBytes = expected.getBytes(); + Assert.assertTrue(expectedBytes.length <= buf.length - pos); + for (byte expectedByte : expectedBytes) { + Assert.assertEquals(expectedByte, buf[pos++]); + } + } +} diff --git a/tests/framework-tests/src/com/android/internal/http/multipart/MultipartTest.java b/tests/framework-tests/src/com/android/internal/http/multipart/MultipartTest.java new file mode 100644 index 0000000..8b48ed0 --- /dev/null +++ b/tests/framework-tests/src/com/android/internal/http/multipart/MultipartTest.java @@ -0,0 +1,102 @@ +package com.android.internal.http.multipart; + +import junit.framework.TestCase; +import org.apache.http.Header; +import org.apache.http.util.EncodingUtils; + +import java.io.BufferedWriter; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileWriter; + +public class MultipartTest extends TestCase { + + public void testParts() throws Exception { + StringBuffer filebuffer = new StringBuffer(); + String filepartStr = "this is file part"; + filebuffer.append(filepartStr); + File upload = File.createTempFile("Multipart", "test"); + + FileWriter outFile = new FileWriter(upload); + BufferedWriter out = new BufferedWriter(outFile); + try { + out.write(filebuffer.toString()); + out.flush(); + } finally { + out.close(); + } + + Part[] parts = new Part[3]; + parts[0] = new StringPart("stringpart", "PART1!!"); + parts[1] = new FilePart(upload.getName(), upload); + parts[2] = new StringPart("stringpart", "PART2!!"); + + MultipartEntity me = new MultipartEntity(parts); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + me.writeTo(os); + Header h = me.getContentType(); + String boundry = EncodingUtils.getAsciiString(me.getMultipartBoundary()); + StringBuffer contentType = new StringBuffer("multipart/form-data"); + contentType.append("; boundary="); + contentType.append(boundry); + assertEquals("Multipart content type error", contentType.toString(), h.getValue()); + final String CRLF = "\r\n"; + StringBuffer output = new StringBuffer(); + + output.append("--"); + output.append(boundry); + output.append(CRLF); + + output.append("Content-Disposition: form-data; name=\"stringpart\""); + output.append(CRLF); + output.append("Content-Type: text/plain; charset=US-ASCII"); + output.append(CRLF); + output.append("Content-Transfer-Encoding: 8bit"); + output.append(CRLF); + output.append(CRLF); + output.append("PART1!!"); + output.append(CRLF); + + output.append("--"); + output.append(boundry); + output.append(CRLF); + + output.append("Content-Disposition: form-data; name=\""); + output.append(upload.getName()); + output.append("\"; filename=\""); + output.append(upload.getName()); + output.append("\""); + + output.append(CRLF); + output.append("Content-Type: application/octet-stream; charset=ISO-8859-1"); + output.append(CRLF); + output.append("Content-Transfer-Encoding: binary"); + output.append(CRLF); + output.append(CRLF); + output.append(filepartStr); + output.append(CRLF); + + output.append("--"); + output.append(boundry); + output.append(CRLF); + + output.append("Content-Disposition: form-data; name=\"stringpart\""); + output.append(CRLF); + output.append("Content-Type: text/plain; charset=US-ASCII"); + output.append(CRLF); + output.append("Content-Transfer-Encoding: 8bit"); + output.append(CRLF); + output.append(CRLF); + output.append("PART2!!"); + output.append(CRLF); + + output.append("--"); + output.append(boundry); + output.append("--"); + output.append(CRLF); + // System.out.print(output.toString()); + assertEquals("Multipart content error", output.toString(), os.toString()); + + // System.out.print(os.toString()); + } +} diff --git a/tests/framework-tests/src/com/android/internal/os/LoggingPrintStreamTest.java b/tests/framework-tests/src/com/android/internal/os/LoggingPrintStreamTest.java new file mode 100644 index 0000000..8e3a034 --- /dev/null +++ b/tests/framework-tests/src/com/android/internal/os/LoggingPrintStreamTest.java @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2008 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.internal.os; + +import junit.framework.TestCase; + +import java.util.Arrays; +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; +import java.io.StringWriter; +import java.io.PrintWriter; + +public class LoggingPrintStreamTest extends TestCase { + + TestPrintStream out = new TestPrintStream(); + + public void testPrintException() { + @SuppressWarnings("ThrowableInstanceNeverThrown") + Throwable t = new Throwable("Ignore me."); + + StringWriter sout = new StringWriter(); + t.printStackTrace(new PrintWriter(sout)); + + t.printStackTrace(out); + // t.printStackTrace(); + + String[] lines = sout.toString().split("\\n"); + assertEquals(Arrays.asList(lines), out.lines); + } + + public void testPrintObject() { + Object o = new Object(); + out.print(4); + out.print(o); + out.print(2); + out.flush(); + assertEquals(Arrays.asList("4" + o + "2"), out.lines); + } + + public void testPrintlnObject() { + Object o = new Object(); + out.print(4); + out.println(o); + out.print(2); + out.flush(); + assertEquals(Arrays.asList("4" + o, "2"), out.lines); + } + + public void testPrintf() { + out.printf("Name: %s\nEmployer: %s", "Bob", "Google"); + assertEquals(Arrays.asList("Name: Bob"), out.lines); + out.flush(); + assertEquals(Arrays.asList("Name: Bob", "Employer: Google"), out.lines); + } + + public void testPrintInt() { + out.print(4); + out.print(2); + assertTrue(out.lines.isEmpty()); + out.flush(); + assertEquals(Collections.singletonList("42"), out.lines); + } + + public void testPrintlnInt() { + out.println(4); + out.println(2); + assertEquals(Arrays.asList("4", "2"), out.lines); + } + + public void testPrintCharArray() { + out.print("Foo\nBar\nTee".toCharArray()); + assertEquals(Arrays.asList("Foo", "Bar"), out.lines); + out.flush(); + assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines); + } + + public void testPrintString() { + out.print("Foo\nBar\nTee"); + assertEquals(Arrays.asList("Foo", "Bar"), out.lines); + out.flush(); + assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines); + } + + public void testPrintlnCharArray() { + out.println("Foo\nBar\nTee".toCharArray()); + assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines); + } + + public void testPrintlnString() { + out.println("Foo\nBar\nTee"); + assertEquals(Arrays.asList("Foo", "Bar", "Tee"), out.lines); + } + + public void testPrintlnStringWithBufferedData() { + out.print(5); + out.println("Foo\nBar\nTee"); + assertEquals(Arrays.asList("5Foo", "Bar", "Tee"), out.lines); + } + + public void testAppend() { + out.append("Foo\n") + .append('4') + .append('\n') + .append("Bar", 1, 2) + .append('\n'); + assertEquals(Arrays.asList("Foo", "4", "a"), out.lines); + } + + static class TestPrintStream extends LoggingPrintStream { + + final List<String> lines = new ArrayList<String>(); + + protected void log(String line) { + lines.add(line); + } + } + +} diff --git a/tests/framework-tests/src/com/android/internal/policy/impl/LockPatternKeyguardViewTest.java b/tests/framework-tests/src/com/android/internal/policy/impl/LockPatternKeyguardViewTest.java new file mode 100644 index 0000000..4994845 --- /dev/null +++ b/tests/framework-tests/src/com/android/internal/policy/impl/LockPatternKeyguardViewTest.java @@ -0,0 +1,361 @@ +/* + * Copyright (C) 2008 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.internal.policy.impl; + +import android.content.Context; +import com.android.internal.telephony.SimCard; +import android.test.AndroidTestCase; +import android.view.View; +import android.view.KeyEvent; +import com.android.internal.widget.LockPatternUtils; +import com.google.android.collect.Lists; + +import java.util.List; + +/** + * Tests for {@link com.android.internal.policy.impl.LockPatternKeyguardView}, + * which handles the management of screens while the keyguard is showing. + */ +public class LockPatternKeyguardViewTest extends AndroidTestCase { + private MockUpdateMonitor mUpdateMonitor; + private LockPatternUtils mLockPatternUtils; + private TestableLockPatternKeyguardView mLPKV; + private MockKeyguardCallback mKeyguardViewCallback; + + private static class MockUpdateMonitor extends KeyguardUpdateMonitor { + + public SimCard.State simState = SimCard.State.READY; + public boolean inPortrait = false; + public boolean keyboardOpen = false; + + private MockUpdateMonitor(Context context) { + super(context); + } + + @Override + public SimCard.State getSimState() { + return simState; + } + + @Override + public boolean isInPortrait() { + return inPortrait; + } + + @Override + public boolean isKeyboardOpen() { + return keyboardOpen; + } + + @Override + boolean queryInPortrait() { + return inPortrait; + } + + @Override + boolean queryKeyboardOpen() { + return keyboardOpen; + } + } + + private static class MockLockPatternUtils extends LockPatternUtils { + boolean isLockPatternEnabled = true; + public boolean isPermanentlyLocked = false; + + public MockLockPatternUtils() { + super(null); + } + + @Override + public boolean isLockPatternEnabled() { + return isLockPatternEnabled; + } + + @Override + public void setLockPatternEnabled(boolean lockPatternEnabled) { + isLockPatternEnabled = lockPatternEnabled; + } + + @Override + public boolean isPermanentlyLocked() { + return isPermanentlyLocked; + } + + public void setPermanentlyLocked(boolean permanentlyLocked) { + isPermanentlyLocked = permanentlyLocked; + } + } + + private static class MockKeyguardScreen extends View implements KeyguardScreen { + + private int mOnPauseCount = 0; + private int mOnResumeCount = 0; + private int mCleanupCount = 0; + + private MockKeyguardScreen(Context context) { + super(context); + setFocusable(true); + } + + /** {@inheritDoc} */ + public void onPause() { + mOnPauseCount++; + } + + /** {@inheritDoc} */ + public void onResume() { + mOnResumeCount++; + } + + /** {@inheritDoc} */ + public void cleanUp() { + mCleanupCount++; + } + + public int getOnPauseCount() { + return mOnPauseCount; + } + + public int getOnResumeCount() { + return mOnResumeCount; + } + + public int getCleanupCount() { + return mCleanupCount; + } + } + + /** + * Allows us to inject the lock and unlock views to simulate their behavior + * and detect their creation. + */ + private static class TestableLockPatternKeyguardView extends LockPatternKeyguardView { + private List<MockKeyguardScreen> mInjectedLockScreens; + private List<MockKeyguardScreen> mInjectedUnlockScreens; + + + + private TestableLockPatternKeyguardView(Context context, KeyguardUpdateMonitor updateMonitor, LockPatternUtils lockPatternUtils) { + super(context, updateMonitor, lockPatternUtils); + } + + @Override + View createLockScreen() { + final MockKeyguardScreen newView = new MockKeyguardScreen(getContext()); + if (mInjectedLockScreens == null) mInjectedLockScreens = Lists.newArrayList(); + mInjectedLockScreens.add(newView); + return newView; + } + + @Override + View createUnlockScreenFor(UnlockMode unlockMode) { + final MockKeyguardScreen newView = new MockKeyguardScreen(getContext()); + if (mInjectedUnlockScreens == null) mInjectedUnlockScreens = Lists.newArrayList(); + mInjectedUnlockScreens.add(newView); + return newView; + } + + public List<MockKeyguardScreen> getInjectedLockScreens() { + return mInjectedLockScreens; + } + + public List<MockKeyguardScreen> getInjectedUnlockScreens() { + return mInjectedUnlockScreens; + } + } + + private static class MockKeyguardCallback implements KeyguardViewCallback { + + private int mPokeWakelockCount = 0; + private int mKeyguardDoneCount = 0; + + public void pokeWakelock() { + mPokeWakelockCount++; + } + + public void pokeWakelock(int millis) { + mPokeWakelockCount++; + } + + public void keyguardDone(boolean authenticated) { + mKeyguardDoneCount++; + } + + public void keyguardDoneDrawing() { + + } + + public int getPokeWakelockCount() { + return mPokeWakelockCount; + } + + public int getKeyguardDoneCount() { + return mKeyguardDoneCount; + } + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + mUpdateMonitor = new MockUpdateMonitor(getContext()); + mLockPatternUtils = new MockLockPatternUtils(); + + mLPKV = new TestableLockPatternKeyguardView(getContext(), mUpdateMonitor, mLockPatternUtils); + mKeyguardViewCallback = new MockKeyguardCallback(); + mLPKV.setCallback(mKeyguardViewCallback); + } + + public void testStateAfterCreatedWhileScreenOff() { + + assertEquals(1, mLPKV.getInjectedLockScreens().size()); + assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); + + MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); + MockKeyguardScreen unlockScreen = mLPKV.getInjectedUnlockScreens().get(0); + + assertEquals(0, lockScreen.getOnPauseCount()); + assertEquals(0, lockScreen.getOnResumeCount()); + assertEquals(0, lockScreen.getCleanupCount()); + + assertEquals(0, unlockScreen.getOnPauseCount()); + assertEquals(0, unlockScreen.getOnResumeCount()); + assertEquals(0, unlockScreen.getCleanupCount()); + + assertEquals(0, mKeyguardViewCallback.getPokeWakelockCount()); + assertEquals(0, mKeyguardViewCallback.getKeyguardDoneCount()); + } + + public void testWokenByNonMenuKey() { + mLPKV.wakeWhenReadyTq(0); + + // should have poked the wakelock to turn on the screen + assertEquals(1, mKeyguardViewCallback.getPokeWakelockCount()); + + // shouldn't be any additional views created + assertEquals(1, mLPKV.getInjectedLockScreens().size()); + assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); + MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); + MockKeyguardScreen unlockScreen = mLPKV.getInjectedUnlockScreens().get(0); + + // lock screen should be only visible one + assertEquals(View.VISIBLE, lockScreen.getVisibility()); + assertEquals(View.GONE, unlockScreen.getVisibility()); + + // on resume not called until screen turns on + assertEquals(0, lockScreen.getOnPauseCount()); + assertEquals(0, lockScreen.getOnResumeCount()); + assertEquals(0, lockScreen.getCleanupCount()); + + assertEquals(0, unlockScreen.getOnPauseCount()); + assertEquals(0, unlockScreen.getOnResumeCount()); + assertEquals(0, unlockScreen.getCleanupCount()); + + // simulate screen turning on + mLPKV.onScreenTurnedOn(); + + assertEquals(0, lockScreen.getOnPauseCount()); + assertEquals(1, lockScreen.getOnResumeCount()); + assertEquals(0, lockScreen.getCleanupCount()); + + assertEquals(0, unlockScreen.getOnPauseCount()); + assertEquals(0, unlockScreen.getOnResumeCount()); + assertEquals(0, unlockScreen.getCleanupCount()); + } + + public void testWokenByMenuKeyWhenPatternSet() { + assertEquals(true, mLockPatternUtils.isLockPatternEnabled()); + + mLPKV.wakeWhenReadyTq(KeyEvent.KEYCODE_MENU); + + // should have poked the wakelock to turn on the screen + assertEquals(1, mKeyguardViewCallback.getPokeWakelockCount()); + + // shouldn't be any additional views created + assertEquals(1, mLPKV.getInjectedLockScreens().size()); + assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); + MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); + MockKeyguardScreen unlockScreen = mLPKV.getInjectedUnlockScreens().get(0); + + // unlock screen should be only visible one + assertEquals(View.GONE, lockScreen.getVisibility()); + assertEquals(View.VISIBLE, unlockScreen.getVisibility()); + } + + public void testScreenRequestsRecreation() { + mLPKV.wakeWhenReadyTq(0); + mLPKV.onScreenTurnedOn(); + + assertEquals(1, mLPKV.getInjectedLockScreens().size()); + assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); + MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); + + assertEquals(0, lockScreen.getOnPauseCount()); + assertEquals(1, lockScreen.getOnResumeCount()); + + // simulate screen asking to be recreated + mLPKV.mKeyguardScreenCallback.recreateMe(); + + // should have been recreated + assertEquals(2, mLPKV.getInjectedLockScreens().size()); + assertEquals(2, mLPKV.getInjectedUnlockScreens().size()); + + // both old screens should have been cleaned up + assertEquals(1, mLPKV.getInjectedLockScreens().get(0).getCleanupCount()); + assertEquals(1, mLPKV.getInjectedUnlockScreens().get(0).getCleanupCount()); + + // old lock screen should have been paused + assertEquals(1, mLPKV.getInjectedLockScreens().get(0).getOnPauseCount()); + assertEquals(0, mLPKV.getInjectedUnlockScreens().get(0).getOnPauseCount()); + + // new lock screen should have been resumed + assertEquals(1, mLPKV.getInjectedLockScreens().get(1).getOnResumeCount()); + assertEquals(0, mLPKV.getInjectedUnlockScreens().get(1).getOnResumeCount()); + } + + public void testMenuDoesntGoToUnlockScreenOnWakeWhenPukLocked() { + // PUK locked + mUpdateMonitor.simState = SimCard.State.PUK_REQUIRED; + + // wake by menu + mLPKV.wakeWhenReadyTq(KeyEvent.KEYCODE_MENU); + + assertEquals(1, mLPKV.getInjectedLockScreens().size()); + assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); + MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); + MockKeyguardScreen unlockScreen = mLPKV.getInjectedUnlockScreens().get(0); + + // lock screen should be only visible one + assertEquals(View.VISIBLE, lockScreen.getVisibility()); + assertEquals(View.GONE, unlockScreen.getVisibility()); + } + + public void testMenuGoesToLockScreenWhenDeviceNotSecure() { + mLockPatternUtils.setLockPatternEnabled(false); + + // wake by menu + mLPKV.wakeWhenReadyTq(KeyEvent.KEYCODE_MENU); + + assertEquals(1, mLPKV.getInjectedLockScreens().size()); + assertEquals(1, mLPKV.getInjectedUnlockScreens().size()); + MockKeyguardScreen lockScreen = mLPKV.getInjectedLockScreens().get(0); + MockKeyguardScreen unlockScreen = mLPKV.getInjectedUnlockScreens().get(0); + + // lock screen should be only visible one + assertEquals(View.VISIBLE, lockScreen.getVisibility()); + assertEquals(View.GONE, unlockScreen.getVisibility()); + } +} |