Merge "Framework Tests Cleanup: Removing CTS duplicate files"

This commit is contained in:
Neal Nguyen
2010-01-13 14:18:48 -08:00
committed by Android (Google) Code Review
15 changed files with 0 additions and 2795 deletions

View File

@@ -1,626 +0,0 @@
/*
* 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 com.android.unit_tests;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.database.DataSetObserver;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteCursorDriver;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQuery;
import android.database.sqlite.SQLiteStatement;
import android.os.Looper;
import android.test.PerformanceTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.Suppress;
import android.util.Log;
import java.io.File;
import java.util.Arrays;
import java.util.Random;
import junit.framework.TestCase;
public class DatabaseCursorTest extends TestCase implements PerformanceTestCase {
private static final String sString1 = "this is a test";
private static final String sString2 = "and yet another test";
private static final String sString3 = "this string is a little longer, but still a test";
private static final int CURRENT_DATABASE_VERSION = 42;
private SQLiteDatabase mDatabase;
private File mDatabaseFile;
@Override
protected void setUp() throws Exception {
super.setUp();
mDatabaseFile = new File("/sqlite_stmt_journals", "database_test.db");
if (mDatabaseFile.exists()) {
mDatabaseFile.delete();
}
mDatabase = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null);
assertNotNull(mDatabase);
mDatabase.setVersion(CURRENT_DATABASE_VERSION);
}
@Override
protected void tearDown() throws Exception {
mDatabase.close();
mDatabaseFile.delete();
super.tearDown();
}
public boolean isPerformanceOnly() {
return false;
}
// These test can only be run once.
public int startPerformance(Intermediates intermediates) {
return 1;
}
private void populateDefaultTable() {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data TEXT);");
mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString1 + "');");
mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString2 + "');");
mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString3 + "');");
}
@MediumTest
public void testCursorUpdate() {
mDatabase.execSQL(
"CREATE TABLE test (_id INTEGER PRIMARY KEY, d INTEGER, s INTEGER);");
for(int i = 0; i < 20; i++) {
mDatabase.execSQL("INSERT INTO test (d, s) VALUES (" + i +
"," + i%2 + ");");
}
Cursor c = mDatabase.query("test", null, "s = 0", null, null, null, null);
int dCol = c.getColumnIndexOrThrow("d");
int sCol = c.getColumnIndexOrThrow("s");
int count = 0;
while (c.moveToNext()) {
assertTrue(c.updateInt(dCol, 3));
count++;
}
assertEquals(10, count);
assertTrue(c.commitUpdates());
assertTrue(c.requery());
count = 0;
while (c.moveToNext()) {
assertEquals(3, c.getInt(dCol));
count++;
}
assertEquals(10, count);
assertTrue(c.moveToFirst());
assertTrue(c.deleteRow());
assertEquals(9, c.getCount());
c.close();
}
@MediumTest
public void testBlob() throws Exception {
// create table
mDatabase.execSQL(
"CREATE TABLE test (_id INTEGER PRIMARY KEY, s TEXT, d REAL, l INTEGER, b BLOB);");
// insert blob
Object[] args = new Object[4];
byte[] blob = new byte[1000];
byte value = 99;
Arrays.fill(blob, value);
args[3] = blob;
String s = new String("text");
args[0] = s;
Double d = 99.9;
args[1] = d;
Long l = (long)1000;
args[2] = l;
String sql = "INSERT INTO test (s, d, l, b) VALUES (?,?,?,?)";
mDatabase.execSQL(sql, args);
// use cursor to access blob
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
c.moveToNext();
ContentValues cv = new ContentValues();
DatabaseUtils.cursorRowToContentValues(c, cv);
int bCol = c.getColumnIndexOrThrow("b");
int sCol = c.getColumnIndexOrThrow("s");
int dCol = c.getColumnIndexOrThrow("d");
int lCol = c.getColumnIndexOrThrow("l");
byte[] cBlob = c.getBlob(bCol);
assertTrue(Arrays.equals(blob, cBlob));
assertEquals(s, c.getString(sCol));
assertEquals((double)d, c.getDouble(dCol));
assertEquals((long)l, c.getLong(lCol));
// new byte[]
byte[] newblob = new byte[1000];
value = 98;
Arrays.fill(blob, value);
c.updateBlob(bCol, newblob);
cBlob = c.getBlob(bCol);
assertTrue(Arrays.equals(newblob, cBlob));
// commit
assertTrue(c.commitUpdates());
assertTrue(c.requery());
c.moveToNext();
cBlob = c.getBlob(bCol);
assertTrue(Arrays.equals(newblob, cBlob));
c.close();
}
@MediumTest
public void testRealColumns() throws Exception {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data REAL);");
ContentValues values = new ContentValues();
values.put("data", 42.11);
long id = mDatabase.insert("test", "data", values);
assertTrue(id > 0);
Cursor c = mDatabase.rawQuery("SELECT data FROM test", null);
assertNotNull(c);
assertTrue(c.moveToFirst());
assertEquals(42.11, c.getDouble(0));
c.close();
}
@MediumTest
public void testCursor1() throws Exception {
populateDefaultTable();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int dataColumn = c.getColumnIndexOrThrow("data");
// The cursor should ignore text before the last period when looking for a column. (This
// is a temporary hack in all implementations of getColumnIndex.)
int dataColumn2 = c.getColumnIndexOrThrow("junk.data");
assertEquals(dataColumn, dataColumn2);
assertSame(3, c.getCount());
assertTrue(c.isBeforeFirst());
try {
c.getInt(0);
fail("CursorIndexOutOfBoundsException expected");
} catch (CursorIndexOutOfBoundsException ex) {
// expected
}
c.moveToNext();
assertEquals(1, c.getInt(0));
String s = c.getString(dataColumn);
assertEquals(sString1, s);
c.moveToNext();
s = c.getString(dataColumn);
assertEquals(sString2, s);
c.moveToNext();
s = c.getString(dataColumn);
assertEquals(sString3, s);
c.moveToPosition(-1);
c.moveToNext();
s = c.getString(dataColumn);
assertEquals(sString1, s);
c.moveToPosition(2);
s = c.getString(dataColumn);
assertEquals(sString3, s);
int i;
for (c.moveToFirst(), i = 0; !c.isAfterLast(); c.moveToNext(), i++) {
c.getInt(0);
}
assertEquals(3, i);
try {
c.getInt(0);
fail("CursorIndexOutOfBoundsException expected");
} catch (CursorIndexOutOfBoundsException ex) {
// expected
}
c.close();
}
@MediumTest
public void testCursor2() throws Exception {
populateDefaultTable();
Cursor c = mDatabase.query("test", null, "_id > 1000", null, null, null, null);
assertEquals(0, c.getCount());
assertTrue(c.isBeforeFirst());
try {
c.getInt(0);
fail("CursorIndexOutOfBoundsException expected");
} catch (CursorIndexOutOfBoundsException ex) {
// expected
}
int i;
for (c.moveToFirst(), i = 0; !c.isAfterLast(); c.moveToNext(), i++) {
c.getInt(0);
}
assertEquals(0, i);
try {
c.getInt(0);
fail("CursorIndexOutOfBoundsException expected");
} catch (CursorIndexOutOfBoundsException ex) {
// expected
}
c.close();
}
@MediumTest
public void testLargeField() throws Exception {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data TEXT);");
StringBuilder sql = new StringBuilder(2100);
sql.append("INSERT INTO test (data) VALUES ('");
Random random = new Random(System.currentTimeMillis());
StringBuilder randomString = new StringBuilder(1979);
for (int i = 0; i < 1979; i++) {
randomString.append((random.nextInt() & 0xf) % 10);
}
sql.append(randomString);
sql.append("');");
mDatabase.execSQL(sql.toString());
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
assertNotNull(c);
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
assertEquals(0, c.getPosition());
String largeString = c.getString(c.getColumnIndexOrThrow("data"));
assertNotNull(largeString);
assertEquals(randomString.toString(), largeString);
c.close();
}
class TestObserver extends DataSetObserver {
int total;
SQLiteCursor c;
boolean quit = false;
public TestObserver(int total_, SQLiteCursor cursor) {
c = cursor;
total = total_;
}
@Override
public void onChanged() {
int count = c.getCount();
if (total == count) {
int i = 0;
while (c.moveToNext()) {
assertEquals(i, c.getInt(1));
i++;
}
assertEquals(count, i);
quit = true;
Looper.myLooper().quit();
}
}
@Override
public void onInvalidated() {
}
}
//@Large
@Suppress
public void testLoadingThreadDelayRegisterData() throws Exception {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data INT);");
final int count = 505;
String sql = "INSERT INTO test (data) VALUES (?);";
SQLiteStatement s = mDatabase.compileStatement(sql);
for (int i = 0; i < count; i++) {
s.bindLong(1, i);
s.execute();
}
int maxRead = 500;
int initialRead = 5;
SQLiteCursor c = (SQLiteCursor)mDatabase.rawQuery("select * from test;",
null, initialRead, maxRead);
TestObserver observer = new TestObserver(count, c);
c.getCount();
c.registerDataSetObserver(observer);
if (!observer.quit) {
Looper.loop();
}
c.close();
}
@LargeTest
public void testLoadingThread() throws Exception {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data INT);");
final int count = 50000;
String sql = "INSERT INTO test (data) VALUES (?);";
SQLiteStatement s = mDatabase.compileStatement(sql);
for (int i = 0; i < count; i++) {
s.bindLong(1, i);
s.execute();
}
int maxRead = 1000;
int initialRead = 5;
SQLiteCursor c = (SQLiteCursor)mDatabase.rawQuery("select * from test;",
null, initialRead, maxRead);
TestObserver observer = new TestObserver(count, c);
c.registerDataSetObserver(observer);
c.getCount();
Looper.loop();
c.close();
}
@LargeTest
public void testLoadingThreadClose() throws Exception {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data INT);");
final int count = 1000;
String sql = "INSERT INTO test (data) VALUES (?);";
SQLiteStatement s = mDatabase.compileStatement(sql);
for (int i = 0; i < count; i++) {
s.bindLong(1, i);
s.execute();
}
int maxRead = 11;
int initialRead = 5;
SQLiteCursor c = (SQLiteCursor)mDatabase.rawQuery("select * from test;",
null, initialRead, maxRead);
TestObserver observer = new TestObserver(count, c);
c.registerDataSetObserver(observer);
c.getCount();
c.close();
}
@LargeTest
public void testLoadingThreadDeactivate() throws Exception {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data INT);");
final int count = 1000;
String sql = "INSERT INTO test (data) VALUES (?);";
SQLiteStatement s = mDatabase.compileStatement(sql);
for (int i = 0; i < count; i++) {
s.bindLong(1, i);
s.execute();
}
int maxRead = 11;
int initialRead = 5;
SQLiteCursor c = (SQLiteCursor)mDatabase.rawQuery("select * from test;",
null, initialRead, maxRead);
TestObserver observer = new TestObserver(count, c);
c.registerDataSetObserver(observer);
c.getCount();
c.deactivate();
c.close();
}
@LargeTest
public void testManyRowsLong() throws Exception {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data INT);");
final int count = 36799;
for (int i = 0; i < count; i++) {
mDatabase.execSQL("INSERT INTO test (data) VALUES (" + i + ");");
}
Cursor c = mDatabase.query("test", new String[]{"data"}, null, null, null, null, null);
assertNotNull(c);
int i = 0;
while (c.moveToNext()) {
assertEquals(i, c.getInt(0));
i++;
}
assertEquals(count, i);
assertEquals(count, c.getCount());
Log.d("testManyRows", "count " + Integer.toString(i));
c.close();
}
@LargeTest
public void testManyRowsTxt() throws Exception {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data TEXT);");
StringBuilder sql = new StringBuilder(2100);
sql.append("INSERT INTO test (data) VALUES ('");
Random random = new Random(System.currentTimeMillis());
StringBuilder randomString = new StringBuilder(1979);
for (int i = 0; i < 1979; i++) {
randomString.append((random.nextInt() & 0xf) % 10);
}
sql.append(randomString);
sql.append("');");
// if cursor window size changed, adjust this value too
final int count = 600; // more than two fillWindow needed
for (int i = 0; i < count; i++) {
mDatabase.execSQL(sql.toString());
}
Cursor c = mDatabase.query("test", new String[]{"data"}, null, null, null, null, null);
assertNotNull(c);
int i = 0;
while (c.moveToNext()) {
assertEquals(randomString.toString(), c.getString(0));
i++;
}
assertEquals(count, i);
assertEquals(count, c.getCount());
c.close();
}
@LargeTest
public void testManyRowsTxtLong() throws Exception {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, txt TEXT, data INT);");
Random random = new Random(System.currentTimeMillis());
StringBuilder randomString = new StringBuilder(1979);
for (int i = 0; i < 1979; i++) {
randomString.append((random.nextInt() & 0xf) % 10);
}
// if cursor window size changed, adjust this value too
final int count = 600;
for (int i = 0; i < count; i++) {
StringBuilder sql = new StringBuilder(2100);
sql.append("INSERT INTO test (txt, data) VALUES ('");
sql.append(randomString);
sql.append("','");
sql.append(i);
sql.append("');");
mDatabase.execSQL(sql.toString());
}
Cursor c = mDatabase.query("test", new String[]{"txt", "data"}, null, null, null, null, null);
assertNotNull(c);
int i = 0;
while (c.moveToNext()) {
assertEquals(randomString.toString(), c.getString(0));
assertEquals(i, c.getInt(1));
i++;
}
assertEquals(count, i);
assertEquals(count, c.getCount());
c.close();
}
@MediumTest
public void testRequery() throws Exception {
populateDefaultTable();
Cursor c = mDatabase.rawQuery("SELECT * FROM test", null);
assertNotNull(c);
assertEquals(3, c.getCount());
c.deactivate();
c.requery();
assertEquals(3, c.getCount());
c.close();
}
@MediumTest
public void testRequeryWithSelection() throws Exception {
populateDefaultTable();
Cursor c = mDatabase.rawQuery("SELECT data FROM test WHERE data = '" + sString1 + "'",
null);
assertNotNull(c);
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
assertEquals(sString1, c.getString(0));
c.deactivate();
c.requery();
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
assertEquals(sString1, c.getString(0));
c.close();
}
@MediumTest
public void testRequeryWithSelectionArgs() throws Exception {
populateDefaultTable();
Cursor c = mDatabase.rawQuery("SELECT data FROM test WHERE data = ?",
new String[]{sString1});
assertNotNull(c);
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
assertEquals(sString1, c.getString(0));
c.deactivate();
c.requery();
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
assertEquals(sString1, c.getString(0));
c.close();
}
@MediumTest
public void testRequeryWithAlteredSelectionArgs() throws Exception {
/**
* Test the ability of a subclass of SQLiteCursor to change its query arguments.
*/
populateDefaultTable();
SQLiteDatabase.CursorFactory factory = new SQLiteDatabase.CursorFactory() {
public Cursor newCursor(
SQLiteDatabase db, SQLiteCursorDriver masterQuery, String editTable,
SQLiteQuery query) {
return new SQLiteCursor(db, masterQuery, editTable, query) {
@Override
public boolean requery() {
setSelectionArguments(new String[]{"2"});
return super.requery();
}
};
}
};
Cursor c = mDatabase.rawQueryWithFactory(
factory, "SELECT data FROM test WHERE _id <= ?", new String[]{"1"},
null);
assertNotNull(c);
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
assertEquals(sString1, c.getString(0));
// Our hacked requery() changes the query arguments in the cursor.
c.requery();
assertEquals(2, c.getCount());
assertTrue(c.moveToFirst());
assertEquals(sString1, c.getString(0));
assertTrue(c.moveToNext());
assertEquals(sString2, c.getString(0));
// Test that setting query args on a deactivated cursor also works.
c.deactivate();
c.requery();
}
}

View File

@@ -1,320 +0,0 @@
/*
* 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 com.android.unit_tests;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDoneException;
import android.database.sqlite.SQLiteStatement;
import android.test.PerformanceTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import junit.framework.TestCase;
import java.io.File;
public class DatabaseStatementTest extends TestCase implements PerformanceTestCase {
private static final String sString1 = "this is a test";
private static final String sString2 = "and yet another test";
private static final String sString3 = "this string is a little longer, but still a test";
private static final int CURRENT_DATABASE_VERSION = 42;
private SQLiteDatabase mDatabase;
private File mDatabaseFile;
@Override
protected void setUp() throws Exception {
super.setUp();
mDatabaseFile = new File("/sqlite_stmt_journals", "database_test.db");
if (mDatabaseFile.exists()) {
mDatabaseFile.delete();
}
mDatabase = SQLiteDatabase.openOrCreateDatabase(mDatabaseFile.getPath(), null);
assertNotNull(mDatabase);
mDatabase.setVersion(CURRENT_DATABASE_VERSION);
}
@Override
protected void tearDown() throws Exception {
mDatabase.close();
mDatabaseFile.delete();
super.tearDown();
}
public boolean isPerformanceOnly() {
return false;
}
// These test can only be run once.
public int startPerformance(Intermediates intermediates) {
return 1;
}
private void populateDefaultTable() {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data TEXT);");
mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString1 + "');");
mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString2 + "');");
mDatabase.execSQL("INSERT INTO test (data) VALUES ('" + sString3 + "');");
}
@MediumTest
public void testExecuteStatement() throws Exception {
populateDefaultTable();
SQLiteStatement statement = mDatabase.compileStatement("DELETE FROM test");
statement.execute();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
assertEquals(0, c.getCount());
c.deactivate();
statement.close();
}
@MediumTest
public void testSimpleQuery() throws Exception {
mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL, str TEXT NOT NULL);");
mDatabase.execSQL("INSERT INTO test VALUES (1234, 'hello');");
SQLiteStatement statement1 =
mDatabase.compileStatement("SELECT num FROM test WHERE str = ?");
SQLiteStatement statement2 =
mDatabase.compileStatement("SELECT str FROM test WHERE num = ?");
try {
statement1.bindString(1, "hello");
long value = statement1.simpleQueryForLong();
assertEquals(1234, value);
statement1.bindString(1, "world");
statement1.simpleQueryForLong();
fail("shouldn't get here");
} catch (SQLiteDoneException e) {
// expected
}
try {
statement2.bindLong(1, 1234);
String value = statement1.simpleQueryForString();
assertEquals("hello", value);
statement2.bindLong(1, 5678);
statement1.simpleQueryForString();
fail("shouldn't get here");
} catch (SQLiteDoneException e) {
// expected
}
statement1.close();
statement2.close();
}
@MediumTest
public void testStatementLongBinding() throws Exception {
mDatabase.execSQL("CREATE TABLE test (num INTEGER);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
for (int i = 0; i < 10; i++) {
statement.bindLong(1, i);
statement.execute();
}
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
c.moveToFirst();
for (long i = 0; i < 10; i++) {
long num = c.getLong(numCol);
assertEquals(i, num);
c.moveToNext();
}
c.close();
}
@MediumTest
public void testStatementStringBinding() throws Exception {
mDatabase.execSQL("CREATE TABLE test (num TEXT);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
for (long i = 0; i < 10; i++) {
statement.bindString(1, Long.toHexString(i));
statement.execute();
}
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
c.moveToFirst();
for (long i = 0; i < 10; i++) {
String num = c.getString(numCol);
assertEquals(Long.toHexString(i), num);
c.moveToNext();
}
c.close();
}
@MediumTest
public void testStatementClearBindings() throws Exception {
mDatabase.execSQL("CREATE TABLE test (num INTEGER);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
for (long i = 0; i < 10; i++) {
statement.bindLong(1, i);
statement.clearBindings();
statement.execute();
}
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, "ROWID");
int numCol = c.getColumnIndexOrThrow("num");
assertTrue(c.moveToFirst());
for (long i = 0; i < 10; i++) {
assertTrue(c.isNull(numCol));
c.moveToNext();
}
c.close();
}
@MediumTest
public void testSimpleStringBinding() throws Exception {
mDatabase.execSQL("CREATE TABLE test (num TEXT, value TEXT);");
String statement = "INSERT INTO test (num, value) VALUES (?,?)";
String[] args = new String[2];
for (int i = 0; i < 2; i++) {
args[i] = Integer.toHexString(i);
}
mDatabase.execSQL(statement, args);
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
int valCol = c.getColumnIndexOrThrow("value");
c.moveToFirst();
String num = c.getString(numCol);
assertEquals(Integer.toHexString(0), num);
String val = c.getString(valCol);
assertEquals(Integer.toHexString(1), val);
c.close();
}
@MediumTest
public void testStatementMultipleBindings() throws Exception {
mDatabase.execSQL("CREATE TABLE test (num INTEGER, str TEXT);");
SQLiteStatement statement =
mDatabase.compileStatement("INSERT INTO test (num, str) VALUES (?, ?)");
for (long i = 0; i < 10; i++) {
statement.bindLong(1, i);
statement.bindString(2, Long.toHexString(i));
statement.execute();
}
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, "ROWID");
int numCol = c.getColumnIndexOrThrow("num");
int strCol = c.getColumnIndexOrThrow("str");
assertTrue(c.moveToFirst());
for (long i = 0; i < 10; i++) {
long num = c.getLong(numCol);
String str = c.getString(strCol);
assertEquals(i, num);
assertEquals(Long.toHexString(i), str);
c.moveToNext();
}
c.close();
}
private static class StatementTestThread extends Thread {
private SQLiteDatabase mDatabase;
private SQLiteStatement mStatement;
public StatementTestThread(SQLiteDatabase db, SQLiteStatement statement) {
super();
mDatabase = db;
mStatement = statement;
}
@Override
public void run() {
mDatabase.beginTransaction();
for (long i = 0; i < 10; i++) {
mStatement.bindLong(1, i);
mStatement.bindString(2, Long.toHexString(i));
mStatement.execute();
}
mDatabase.setTransactionSuccessful();
mDatabase.endTransaction();
Cursor c = mDatabase.query("test", null, null, null, null, null, "ROWID");
int numCol = c.getColumnIndexOrThrow("num");
int strCol = c.getColumnIndexOrThrow("str");
assertTrue(c.moveToFirst());
for (long i = 0; i < 10; i++) {
long num = c.getLong(numCol);
String str = c.getString(strCol);
assertEquals(i, num);
assertEquals(Long.toHexString(i), str);
c.moveToNext();
}
c.close();
}
}
@MediumTest
public void testStatementMultiThreaded() throws Exception {
mDatabase.execSQL("CREATE TABLE test (num INTEGER, str TEXT);");
SQLiteStatement statement =
mDatabase.compileStatement("INSERT INTO test (num, str) VALUES (?, ?)");
StatementTestThread thread = new StatementTestThread(mDatabase, statement);
thread.start();
try {
thread.join();
} finally {
statement.close();
}
}
@MediumTest
public void testStatementConstraint() throws Exception {
mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
// Try to insert NULL, which violates the constraint
try {
statement.clearBindings();
statement.execute();
fail("expected exception not thrown");
} catch (SQLiteConstraintException e) {
// expected
}
// Make sure the statement can still be used
statement.bindLong(1, 1);
statement.execute();
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
c.moveToFirst();
long num = c.getLong(numCol);
assertEquals(1, num);
c.close();
}
}

View File

@@ -23,8 +23,6 @@ public class DatabaseTests {
TestSuite suite = new TestSuite(DatabaseTests.class.getName());
suite.addTestSuite(DatabaseGeneralTest.class);
suite.addTestSuite(DatabaseCursorTest.class);
suite.addTestSuite(DatabaseStatementTest.class);
suite.addTestSuite(DatabaseLocaleTest.class);
suite.addTestSuite(CursorWindowTest.class);
suite.addTestSuite(DatabaseLockTest.class);

View File

@@ -1,77 +0,0 @@
/*
* 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 com.android.unit_tests.content;
import android.content.res.Resources;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.TypedValue;
import com.android.unit_tests.R;
public class ArrayTest extends AndroidTestCase {
private Resources mResources;
@Override
protected void setUp() throws Exception {
super.setUp();
mResources = mContext.getResources();
}
private void checkEntry(int resid, int index, Object res, Object expected) {
assertEquals("in resource 0x" + Integer.toHexString(resid)
+ " at index " + index, expected, res);
}
private void checkStringArray(int resid, String[] expected) {
String[] res = mResources.getStringArray(resid);
assertEquals(res.length, expected.length);
for (int i=0; i<expected.length; i++) {
checkEntry(resid, i, res[i], expected[i]);
}
}
private void checkTextArray(int resid, String[] expected) {
CharSequence[] res = mResources.getTextArray(resid);
assertEquals(res.length, expected.length);
for (int i=0; i<expected.length; i++) {
checkEntry(resid, i, res[i], expected[i]);
}
}
private void checkIntArray(int resid, int[] expected) {
int[] res = mResources.getIntArray(resid);
assertEquals(res.length, expected.length);
for (int i=0; i<expected.length; i++) {
assertEquals("in resource 0x" + Integer.toHexString(resid)
+ " at index " + i, expected[i], res[i]);
}
}
@SmallTest
public void testStrings() throws Exception {
checkStringArray(R.array.strings, new String[] {"zero", "1", "here"});
checkTextArray(R.array.strings, new String[] {"zero", "1", "here"});
checkStringArray(R.array.integers, new String[] {null, null, null});
checkTextArray(R.array.integers, new String[] {null, null, null});
}
@SmallTest
public void testIntegers() throws Exception {
checkIntArray(R.array.strings, new int[] {0, 0, 0});
checkIntArray(R.array.integers, new int[] {0, 1, 101});
}
}

View File

@@ -1,540 +0,0 @@
/*
* 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 com.android.unit_tests.content;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import com.android.unit_tests.R;
import java.util.Locale;
public class ConfigTest extends AndroidTestCase {
enum properties {
LANGUAGE,
COUNTRY,
MCC,
MNC,
TOUCHSCREEN,
KEYBOARD,
KEYBOARDHIDDEN,
NAVIGATION,
ORIENTATION,
WIDTH,
HEIGHT,
DENSITY
}
private static void checkValue(Resources res, int resId, String expectedValue) {
try {
String actual = res.getString(resId);
assertNotNull("Returned wrong configuration-based simple value: expected <nothing>, got '"
+ actual + "' from resource 0x"
+ Integer.toHexString(resId),
expectedValue);
assertEquals("Returned wrong configuration-based simple value: expected '"
+ expectedValue + "', got '" + actual + "' from resource 0x"
+ Integer.toHexString(resId),
expectedValue, actual);
} catch (Resources.NotFoundException e) {
assertNull("Resource not found for configuration-based simple value: expecting \""
+ expectedValue + "\"",
expectedValue);
}
}
private static void checkValue(Resources res, int resId,
int[] styleable, String[] expectedValues) {
Resources.Theme theme = res.newTheme();
TypedArray sa = theme.obtainStyledAttributes(resId, styleable);
for (int i = 0; i < styleable.length; i++) {
String actual = sa.getString(i);
assertEquals("Returned wrong configuration-based style value: expected '"
+ expectedValues[i] + "', got '" + actual + "' from attr "
+ i + " of resource 0x" + Integer.toHexString(resId),
actual, expectedValues[i]);
}
sa.recycle();
}
class TotalConfig {
Configuration mConfig;
DisplayMetrics mMetrics;
TotalConfig() {
mConfig = new Configuration();
// don't rely on build settings - they may change
mConfig.locale = new Locale("en", "US");
mConfig.mcc = 310;
mConfig.mnc = 001; // unused
mConfig.touchscreen = Configuration.TOUCHSCREEN_FINGER;
mConfig.keyboard = Configuration.KEYBOARD_QWERTY;
mConfig.keyboardHidden = Configuration.KEYBOARDHIDDEN_YES;
mConfig.navigation = Configuration.NAVIGATION_TRACKBALL;
mConfig.orientation = Configuration.ORIENTATION_PORTRAIT;
mMetrics = new DisplayMetrics();
mMetrics.widthPixels = 200;
mMetrics.heightPixels = 320;
mMetrics.density = 1;
}
void setProperty(properties p, int value) {
switch(p) {
case MCC:
mConfig.mcc = value;
break;
case MNC:
mConfig.mnc = value;
break;
case TOUCHSCREEN:
mConfig.touchscreen = value;
break;
case KEYBOARD:
mConfig.keyboard = value;
break;
case KEYBOARDHIDDEN:
mConfig.keyboardHidden = value;
break;
case NAVIGATION:
mConfig.navigation = value;
break;
case ORIENTATION:
mConfig.orientation = value;
break;
case WIDTH:
mMetrics.widthPixels = value;
break;
case HEIGHT:
mMetrics.heightPixels = value;
break;
case DENSITY:
// this is the ratio from the standard
mMetrics.density = (((float)value)/((float)DisplayMetrics.DENSITY_DEFAULT));
break;
default:
assert(false);
break;
}
}
public void setProperty(properties p, String value) {
switch(p) {
case LANGUAGE:
String oldCountry = mConfig.locale.getCountry();
mConfig.locale = new Locale(value, oldCountry);
break;
case COUNTRY:
String oldLanguage = mConfig.locale.getLanguage();
mConfig.locale = new Locale(oldLanguage, value);
break;
default:
assert(false);
break;
}
}
public Resources getResources() {
AssetManager assmgr = new AssetManager();
assmgr.addAssetPath(mContext.getPackageResourcePath());
return new Resources(assmgr, mMetrics, mConfig);
}
}
private static void checkPair(Resources res, int[] notResIds,
int simpleRes, String simpleString,
int bagRes, String bagString) {
boolean willHave = true;
if (notResIds != null) {
for (int i : notResIds) {
if (i == simpleRes) {
willHave = false;
break;
}
}
}
checkValue(res, simpleRes, willHave ? simpleString : null);
checkValue(res, bagRes, R.styleable.TestConfig,
new String[]{willHave ? bagString : null});
}
@SmallTest
public void testAllConfigs() throws Exception {
/**
* Test a resource that contains a value for each possible single
* configuration value.
*/
TotalConfig config = new TotalConfig();
Resources res = config.getResources();
checkValue(res, R.configVarying.simple, "simple default");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag default"});
config = new TotalConfig();
config.setProperty(properties.LANGUAGE, "xx");
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple xx");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag xx"});
config = new TotalConfig();
config.setProperty(properties.LANGUAGE, "xx");
config.setProperty(properties.COUNTRY, "YY");
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple xx-rYY");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag xx-rYY"});
config = new TotalConfig();
config.setProperty(properties.MCC, 111);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple mcc111");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag mcc111"});
config = new TotalConfig();
config.setProperty(properties.MNC, 222);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple mnc222");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag mnc222"});
config = new TotalConfig();
config.setProperty(properties.TOUCHSCREEN, Configuration.TOUCHSCREEN_NOTOUCH);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple notouch");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag notouch"});
config = new TotalConfig();
config.setProperty(properties.TOUCHSCREEN, Configuration.TOUCHSCREEN_STYLUS);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple stylus");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag stylus"});
config = new TotalConfig();
config.setProperty(properties.KEYBOARD, Configuration.KEYBOARD_NOKEYS);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple nokeys");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag nokeys"});
config = new TotalConfig();
config.setProperty(properties.KEYBOARD, Configuration.KEYBOARD_12KEY);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple 12key");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag 12key"});
config = new TotalConfig();
config.setProperty(properties.KEYBOARDHIDDEN, Configuration.KEYBOARDHIDDEN_NO);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple keysexposed");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag keysexposed"});
config = new TotalConfig();
config.setProperty(properties.NAVIGATION, Configuration.NAVIGATION_NONAV);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple nonav");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag nonav"});
config = new TotalConfig();
config.setProperty(properties.NAVIGATION, Configuration.NAVIGATION_DPAD);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple dpad");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag dpad"});
config = new TotalConfig();
config.setProperty(properties.NAVIGATION, Configuration.NAVIGATION_WHEEL);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple wheel");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag wheel"});
config = new TotalConfig();
config.setProperty(properties.HEIGHT, 480);
config.setProperty(properties.WIDTH, 320);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple 480x320");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag 480x320"});
config = new TotalConfig();
config.setProperty(properties.DENSITY, 240);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple 240dpi");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag 240dpi"});
config = new TotalConfig();
config.setProperty(properties.ORIENTATION, Configuration.ORIENTATION_LANDSCAPE);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple landscape");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag landscape"});
config = new TotalConfig();
config.setProperty(properties.ORIENTATION, Configuration.ORIENTATION_SQUARE);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple square");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag square"});
}
@MediumTest
public void testDensity() throws Exception {
// have 32, 240 and the default 160 content.
// rule is that closest wins, with down scaling (larger content)
// being twice as nice as upscaling.
// transition at H/2 * (-1 +/- sqrt(1+8L/H))
// SO, X < 49 goes to 32
// 49 >= X < 182 goes to 160
// X >= 182 goes to 240
TotalConfig config = new TotalConfig();
config.setProperty(properties.DENSITY, 2);
Resources res = config.getResources();
checkValue(res, R.configVarying.simple, "simple 32dpi");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag 32dpi"});
config = new TotalConfig();
config.setProperty(properties.DENSITY, 32);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple 32dpi");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag 32dpi"});
config = new TotalConfig();
config.setProperty(properties.DENSITY, 48);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple 32dpi");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag 32dpi"});
config = new TotalConfig();
config.setProperty(properties.DENSITY, 49);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple default");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag default"});
config = new TotalConfig();
config.setProperty(properties.DENSITY, 150);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple default");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag default"});
config = new TotalConfig();
config.setProperty(properties.DENSITY, 181);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple default");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag default"});
config = new TotalConfig();
config.setProperty(properties.DENSITY, 182);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple 240dpi");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag 240dpi"});
config = new TotalConfig();
config.setProperty(properties.DENSITY, 239);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple 240dpi");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag 240dpi"});
config = new TotalConfig();
config.setProperty(properties.DENSITY, 490);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple 240dpi");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag 240dpi"});
}
// TODO - add tests for special cases - ie, other key params seem ignored if
// nokeys is set
@MediumTest
public void testCombinations() throws Exception {
/**
* Verify that proper strings are found for multiple-selectivity case
* (ie, a string set for locale and mcc is found only when both are
* true).
*/
TotalConfig config = new TotalConfig();
config.setProperty(properties.LANGUAGE, "xx");
config.setProperty(properties.COUNTRY, "YY");
config.setProperty(properties.MCC, 111);
Resources res = config.getResources();
checkValue(res, R.configVarying.simple, "simple mcc111 xx-rYY");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag mcc111 xx-rYY"});
config = new TotalConfig();
config.setProperty(properties.LANGUAGE, "xx");
config.setProperty(properties.COUNTRY, "YY");
config.setProperty(properties.MCC, 333);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple xx-rYY");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag xx-rYY"});
config = new TotalConfig();
config.setProperty(properties.MNC, 333);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple default");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag default"});
}
@MediumTest
public void testPrecidence() throws Exception {
/**
* Verify that in cases of ties, the specific ordering is followed
*/
/**
* Precidence order: mcc, mnc, locale, orientation, density,
* touchscreen, hidden, keyboard, navigation, width-height
*/
/**
* verify mcc trumps mnc. Have 110-xx, 220-xx but no 110-220
* so with is selected? Should be mcc110-xx.
*/
TotalConfig config = new TotalConfig();
config.setProperty(properties.MCC, 110);
config.setProperty(properties.MNC, 220);
config.setProperty(properties.LANGUAGE, "xx");
Resources res = config.getResources();
checkValue(res, R.configVarying.simple, "simple mcc110 xx");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag mcc110 xx"});
/* full A + B + C doesn't exist. Do we get A + C or B + C?
*/
config = new TotalConfig();
config.setProperty(properties.MCC, 111);
config.setProperty(properties.MNC, 222);
config.setProperty(properties.LANGUAGE, "xx");
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple mcc111 mnc222");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag mcc111 mnc222"});
config = new TotalConfig();
config.setProperty(properties.MNC, 222);
config.setProperty(properties.LANGUAGE, "xx");
config.setProperty(properties.ORIENTATION,
Configuration.ORIENTATION_SQUARE);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple mnc222 xx");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag mnc222 xx"});
config = new TotalConfig();
config.setProperty(properties.LANGUAGE, "xx");
config.setProperty(properties.ORIENTATION,
Configuration.ORIENTATION_SQUARE);
config.setProperty(properties.DENSITY, 32);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple xx square");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag xx square"});
config = new TotalConfig();
config.setProperty(properties.ORIENTATION,
Configuration.ORIENTATION_SQUARE);
config.setProperty(properties.DENSITY, 32);
config.setProperty(properties.TOUCHSCREEN,
Configuration.TOUCHSCREEN_STYLUS);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple square 32dpi");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag square 32dpi"});
config = new TotalConfig();
config.setProperty(properties.DENSITY, 32);
config.setProperty(properties.TOUCHSCREEN,
Configuration.TOUCHSCREEN_STYLUS);
config.setProperty(properties.KEYBOARDHIDDEN,
Configuration.KEYBOARDHIDDEN_NO);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple 32dpi stylus");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag 32dpi stylus"});
config = new TotalConfig();
config.setProperty(properties.TOUCHSCREEN,
Configuration.TOUCHSCREEN_STYLUS);
config.setProperty(properties.KEYBOARDHIDDEN,
Configuration.KEYBOARDHIDDEN_NO);
config.setProperty(properties.KEYBOARD, Configuration.KEYBOARD_12KEY);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple stylus keysexposed");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag stylus keysexposed"});
config = new TotalConfig();
config.setProperty(properties.KEYBOARDHIDDEN,
Configuration.KEYBOARDHIDDEN_NO);
config.setProperty(properties.KEYBOARD, Configuration.KEYBOARD_12KEY);
config.setProperty(properties.NAVIGATION,
Configuration.NAVIGATION_DPAD);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple keysexposed 12key");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag keysexposed 12key"});
config = new TotalConfig();
config.setProperty(properties.KEYBOARD, Configuration.KEYBOARD_12KEY);
config.setProperty(properties.NAVIGATION,
Configuration.NAVIGATION_DPAD);
config.setProperty(properties.HEIGHT, 63);
config.setProperty(properties.WIDTH, 57);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple 12key dpad");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag 12key dpad"});
config = new TotalConfig();
config.setProperty(properties.NAVIGATION,
Configuration.NAVIGATION_DPAD);
config.setProperty(properties.HEIGHT, 640);
config.setProperty(properties.WIDTH, 400);
res = config.getResources();
checkValue(res, R.configVarying.simple, "simple dpad");
checkValue(res, R.configVarying.bag,
R.styleable.TestConfig, new String[]{"bag dpad"});
}
}

View File

@@ -23,10 +23,6 @@ public class ContentTests {
TestSuite suite = new TestSuite(ContentTests.class.getName());
suite.addTestSuite(AssetTest.class);
suite.addTestSuite(IntentFilterTest.class);
suite.addTest(ResourceTests.suite());
suite.addTestSuite(PluralResourcesTest.class);
suite.addTestSuite(ConfigTest.class);
return suite;
}
}

View File

@@ -1,92 +0,0 @@
/*
* 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 com.android.unit_tests.content;
import android.content.res.Resources;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.TypedValue;
import com.android.unit_tests.R;
public class FractionTest extends AndroidTestCase {
private Resources mResources;
private final TypedValue mValue = new TypedValue();
@Override
protected void setUp() throws Exception {
super.setUp();
mResources = mContext.getResources();
}
@SmallTest
public void testFractions() throws Exception {
tryFraction(R.dimen.frac100perc, 1, 1, 1);
tryFraction(R.dimen.frac1perc, 1, 1, .01f);
tryFraction(R.dimen.fracp1perc, 1, 1, .001f);
tryFraction(R.dimen.fracp01perc, 1, 1, .0001f);
tryFraction(R.dimen.frac0perc, 1, 1, 0);
tryFraction(R.dimen.frac1p1perc, 1, 1, .011f);
tryFraction(R.dimen.frac100p1perc, 1, 1, 1.001f);
tryFraction(R.dimen.frac25510perc, 1, 1, 255.1f);
tryFraction(R.dimen.frac25610perc, 1, 1, 256.1f);
tryFraction(R.dimen.frac6553510perc, 1, 1, 65535.1f);
tryFraction(R.dimen.frac6553610perc, 1, 1, 65536.1f);
tryFraction(R.dimen.frac100perc, 100, 1, 100);
tryFraction(R.dimen.frac1perc, 100, 1, .01f * 100);
tryFraction(R.dimen.fracp1perc, 100, 1, .001f * 100);
tryFraction(R.dimen.fracp01perc, 100, 1, .0001f * 100);
tryFraction(R.dimen.frac0perc, 100, 1, 0);
tryFraction(R.dimen.frac1p1perc, 100, 1, .011f * 100);
tryFraction(R.dimen.frac100p1perc, 100, 1, 1.001f * 100);
tryFraction(R.dimen.frac25510perc, 100, 1, 255.1f * 100);
tryFraction(R.dimen.frac25610perc, 100, 1, 256.1f * 100);
tryFraction(R.dimen.frac6553510perc, 100, 1, 65535.1f * 100);
tryFraction(R.dimen.frac6553610perc, 100, 1, 65536.1f * 100);
tryFraction(R.dimen.frac100pperc, 100, 2, 2);
tryFraction(R.dimen.frac1pperc, 100, 2, .01f * 2);
tryFraction(R.dimen.fracp1pperc, 100, 2, .001f * 2);
tryFraction(R.dimen.fracp01pperc, 100, 2, .0001f * 2);
tryFraction(R.dimen.frac0pperc, 100, 2, 0);
tryFraction(R.dimen.frac1p1pperc, 100, 2, .011f * 2);
tryFraction(R.dimen.frac100p1pperc, 100, 2, 1.001f * 2);
tryFraction(R.dimen.frac25510pperc, 100, 2, 255.1f * 2);
tryFraction(R.dimen.frac25610pperc, 100, 2, 256.1f * 2);
tryFraction(R.dimen.frac6553510pperc, 100, 2, 65535.1f * 2);
tryFraction(R.dimen.frac6553610pperc, 100, 2, 65536.1f * 2);
}
private void tryFraction(int resid, float base, float pbase, float expected) {
mResources.getValue(resid, mValue, true);
float res = mValue.getFraction(base, pbase);
float diff = Math.abs(expected - res);
float prec = expected * 1e-4f;
if (prec < 1e-5f) {
prec = 1e-5f;
}
//System.out.println(
// "Res 0x" + Integer.toHexString(resid) + ": got=" + res
// + ", expected=" + expected + ", diff=" + diff);
assertFalse("Expecting value " + expected + " got " + res
+ ": in resource 0x" + Integer.toHexString(resid)
+ " " + mValue,
diff > prec);
}
}

View File

@@ -1,570 +0,0 @@
/*
* 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 com.android.unit_tests.content;
import android.content.IntentFilter;
import android.test.suitebuilder.annotation.SmallTest;
import static android.os.PatternMatcher.PATTERN_LITERAL;
import static android.os.PatternMatcher.PATTERN_PREFIX;
import static android.os.PatternMatcher.PATTERN_SIMPLE_GLOB;
import android.net.Uri;
import android.util.StringBuilderPrinter;
import junit.framework.TestCase;
import java.util.HashSet;
public class IntentFilterTest extends TestCase {
public static class Match extends IntentFilter {
Match(String[] actions, String[] categories, String[] mimeTypes,
String[] schemes, String[] authorities, String[] ports) {
if (actions != null) {
for (int i = 0; i < actions.length; i++) {
addAction(actions[i]);
}
}
if (categories != null) {
for (int i = 0; i < categories.length; i++) {
addCategory(categories[i]);
}
}
if (mimeTypes != null) {
for (int i = 0; i < mimeTypes.length; i++) {
try {
addDataType(mimeTypes[i]);
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("Bad mime type", e);
}
}
}
if (schemes != null) {
for (int i = 0; i < schemes.length; i++) {
addDataScheme(schemes[i]);
}
}
if (authorities != null) {
for (int i = 0; i < authorities.length; i++) {
addDataAuthority(authorities[i],
ports != null ? ports[i] : null);
}
}
}
Match(String[] actions, String[] categories, String[] mimeTypes,
String[] schemes, String[] authorities, String[] ports,
String[] paths, int[] pathTypes) {
this(actions, categories, mimeTypes, schemes, authorities, ports);
if (paths != null) {
for (int i = 0; i < paths.length; i++) {
addDataPath(paths[i], pathTypes[i]);
}
}
}
}
public static class MatchCondition {
public final int result;
public final String action;
public final String mimeType;
public final Uri data;
public final String[] categories;
public MatchCondition(int _result, String _action, String[] _categories,
String _mimeType, String _data) {
result = _result;
action = _action;
mimeType = _mimeType;
data = _data != null ? Uri.parse(_data) : null;
categories = _categories;
}
}
public static void checkMatches(IntentFilter filter,
MatchCondition[] results) {
for (int i = 0; i < results.length; i++) {
MatchCondition mc = results[i];
HashSet<String> categories = null;
if (mc.categories != null) {
for (int j = 0; j < mc.categories.length; j++) {
if (categories == null) {
categories = new HashSet<String>();
}
categories.add(mc.categories[j]);
}
}
int result = filter.match(mc.action, mc.mimeType,
mc.data != null ? mc.data.getScheme() : null, mc.data,
categories, "test");
if ( (result & IntentFilter.MATCH_CATEGORY_MASK)
!= (mc.result & IntentFilter.MATCH_CATEGORY_MASK) ) {
StringBuilder msg = new StringBuilder();
msg.append("Error matching against IntentFilter:\n");
filter.dump(new StringBuilderPrinter(msg), " ");
msg.append("Match action: ");
msg.append(mc.action);
msg.append("\nMatch mimeType: ");
msg.append(mc.mimeType);
msg.append("\nMatch data: ");
msg.append(mc.data);
msg.append("\nMatch categories: ");
if (mc.categories != null) {
for (int j = 0; j < mc.categories.length; j++) {
if (j > 0) msg.append(", ");
msg.append(mc.categories[j]);
}
}
msg.append("\nExpected result: 0x");
msg.append(Integer.toHexString(mc.result));
msg.append(", got result: 0x");
msg.append(Integer.toHexString(result));
throw new RuntimeException(msg.toString());
}
}
}
@SmallTest
public void testActions() throws Exception {
IntentFilter filter = new Match(
new String[]{"action1"}, null, null, null, null, null);
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action1",
null, null, null),
new MatchCondition(IntentFilter.NO_MATCH_ACTION, "action2",
null, null, null),
});
filter = new Match(
new String[]{"action1", "action2"},
null, null, null, null, null);
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action1",
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, "action2",
null, null, null),
new MatchCondition(IntentFilter.NO_MATCH_ACTION, "action3",
null, null, null),
});
}
@SmallTest
public void testCategories() throws Exception {
IntentFilter filter = new Match(
null, new String[]{"category1"}, null, null, null, null);
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
new String[]{"category1"}, null, null),
new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null,
new String[]{"category2"}, null, null),
new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null,
new String[]{"category1", "category2"}, null, null),
});
filter = new Match(
null, new String[]{"category1", "category2"}, null, null,
null, null);
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
new String[]{"category1"}, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
new String[]{"category2"}, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_EMPTY, null,
new String[]{"category1", "category2"}, null, null),
new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null,
new String[]{"category3"}, null, null),
new MatchCondition(IntentFilter.NO_MATCH_CATEGORY, null,
new String[]{"category1", "category2", "category3"},
null, null),
});
}
@SmallTest
public void testMimeTypes() throws Exception {
IntentFilter filter = new Match(
null, null, new String[]{"which1/what1"}, null, null, null);
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which1/what1", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which1/*", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"*/*", null),
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
"which2/what2", null),
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
"which2/*", null),
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
"which1/what2", null),
});
filter = new Match(null, null,
new String[]{"which1/what1", "which2/what2"}, null, null,
null);
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which1/what1", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which1/*", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"*/*", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which2/what2", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which2/*", null),
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
"which1/what2", null),
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
"which3/what3", null),
});
filter = new Match(null, null,
new String[]{"which1/*"}, null, null, null);
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which1/what1", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which1/*", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"*/*", null),
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
"which2/what2", null),
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
"which2/*", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which1/what2", null),
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null, null,
"which3/what3", null),
});
filter = new Match(null, null,
new String[]{"*/*"}, null, null, null);
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_TYPE, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which1/what1", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which1/*", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"*/*", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which2/what2", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which2/*", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which1/what2", null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_TYPE, null, null,
"which3/what3", null),
});
}
@SmallTest
public void testSchemes() throws Exception {
IntentFilter filter = new Match(null, null, null,
new String[]{"scheme1"}, null, null);
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME, null,
null, null, "scheme1:foo"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme2:foo"),
});
filter = new Match(null, null, null,
new String[]{"scheme1", "scheme2"}, null, null);
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME, null,
null, null, "scheme1:foo"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_SCHEME, null,
null, null, "scheme2:foo"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme3:foo"),
});
}
@SmallTest
public void testAuthorities() throws Exception {
IntentFilter filter = new Match(null, null, null,
new String[]{"scheme1"},
new String[]{"authority1"}, new String[]{null});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme1:foo"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null,
null, null, "scheme1://authority1/"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme1://authority2/"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null,
null, null, "scheme1://authority1:100/"),
});
filter = new Match(null, null, null, new String[]{"scheme1"},
new String[]{"authority1"}, new String[]{"100"});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme1:foo"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme1://authority1/"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme1://authority2/"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PORT, null,
null, null, "scheme1://authority1:100/"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme1://authority1:200/"),
});
filter = new Match(null, null, null, new String[]{"scheme1"},
new String[]{"authority1", "authority2"},
new String[]{"100", null});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme1:foo"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme1://authority1/"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_HOST, null,
null, null, "scheme1://authority2/"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PORT, null,
null, null, "scheme1://authority1:100/"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme1://authority1:200/"),
});
}
@SmallTest
public void testPaths() throws Exception {
IntentFilter filter = new Match(null, null, null,
new String[]{"scheme"}, new String[]{"authority"}, null,
new String[]{"/literal1", "/2literal"},
new int[]{PATTERN_LITERAL, PATTERN_LITERAL});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/literal1"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/2literal"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/literal"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/literal12"),
});
filter = new Match(null, null, null,
new String[]{"scheme"}, new String[]{"authority"}, null,
new String[]{"/literal1", "/2literal"},
new int[]{PATTERN_PREFIX, PATTERN_PREFIX});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/literal1"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/2literal"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/literal"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/literal12"),
});
filter = new Match(null, null, null,
new String[]{"scheme"}, new String[]{"authority"}, null,
new String[]{"/.*"},
new int[]{PATTERN_SIMPLE_GLOB});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/literal1"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority"),
});
filter = new Match(null, null, null,
new String[]{"scheme"}, new String[]{"authority"}, null,
new String[]{".*"},
new int[]{PATTERN_SIMPLE_GLOB});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/literal1"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority"),
});
filter = new Match(null, null, null,
new String[]{"scheme"}, new String[]{"authority"}, null,
new String[]{"/a1*b"},
new int[]{PATTERN_SIMPLE_GLOB});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/ab"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a1b"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a11b"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/a2b"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/a1bc"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/"),
});
filter = new Match(null, null, null,
new String[]{"scheme"}, new String[]{"authority"}, null,
new String[]{"/a1*"},
new int[]{PATTERN_SIMPLE_GLOB});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a1"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/ab"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a11"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/a1b"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a11"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/a2"),
});
filter = new Match(null, null, null,
new String[]{"scheme"}, new String[]{"authority"}, null,
new String[]{"/a\\.*b"},
new int[]{PATTERN_SIMPLE_GLOB});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/ab"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a.b"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a..b"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/a2b"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/a.bc"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/"),
});
filter = new Match(null, null, null,
new String[]{"scheme"}, new String[]{"authority"}, null,
new String[]{"/a.*b"},
new int[]{PATTERN_SIMPLE_GLOB});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/ab"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a.b"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a.1b"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a2b"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/a.bc"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/"),
});
filter = new Match(null, null, null,
new String[]{"scheme"}, new String[]{"authority"}, null,
new String[]{"/a.*"},
new int[]{PATTERN_SIMPLE_GLOB});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/ab"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a.b"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a.1b"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a2b"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a.bc"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/"),
});
filter = new Match(null, null, null,
new String[]{"scheme"}, new String[]{"authority"}, null,
new String[]{"/a.\\*b"},
new int[]{PATTERN_SIMPLE_GLOB});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/ab"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a.*b"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a1*b"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/a2b"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/a.bc"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/"),
});
filter = new Match(null, null, null,
new String[]{"scheme"}, new String[]{"authority"}, null,
new String[]{"/a.\\*"},
new int[]{PATTERN_SIMPLE_GLOB});
checkMatches(filter, new MatchCondition[]{
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, null),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/ab"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a.*"),
new MatchCondition(IntentFilter.MATCH_CATEGORY_PATH, null,
null, null, "scheme://authority/a1*"),
new MatchCondition(IntentFilter.NO_MATCH_DATA, null,
null, null, "scheme://authority/a1b"),
});
}
}

View File

@@ -1,97 +0,0 @@
/*
* 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 com.android.unit_tests.content;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.TypedValue;
import android.util.Log;
import com.android.unit_tests.R;
import junit.framework.Assert;
import java.util.Locale;
public class PluralResourcesTest extends AndroidTestCase {
private static final String TAG = "PluralResourcesTest";
private Resources mResources;
@Override
protected void setUp() throws Exception {
super.setUp();
mResources = mContext.getResources();
}
Resources resourcesForLanguage(String lang) {
Configuration config = new Configuration();
config.updateFrom(mResources.getConfiguration());
config.locale = new Locale(lang);
return new Resources(mResources.getAssets(), mResources.getDisplayMetrics(), config);
}
@SmallTest
public void testPlurals() throws Exception {
CharSequence cs;
Resources res = resourcesForLanguage("en");
cs = res.getQuantityText(R.plurals.plurals_test, 0);
Log.d(TAG, "english 0 cs=" + cs);
Assert.assertEquals(cs.toString(), "Some dogs");
cs = res.getQuantityText(R.plurals.plurals_test, 1);
Log.d(TAG, "english 1 cs=" + cs);
Assert.assertEquals(cs.toString(), "A dog");
cs = res.getQuantityText(R.plurals.plurals_test, 2);
Assert.assertEquals(cs.toString(), "Some dogs");
cs = res.getQuantityText(R.plurals.plurals_test, 5);
Assert.assertEquals(cs.toString(), "Some dogs");
cs = res.getQuantityText(R.plurals.plurals_test, 500);
Assert.assertEquals(cs.toString(), "Some dogs");
}
@SmallTest
public void testCzech() throws Exception {
CharSequence cs;
Resources res = resourcesForLanguage("cs");
cs = res.getQuantityText(R.plurals.plurals_test, 0);
Log.d(TAG, "czech 0 cs=" + cs);
Assert.assertEquals(cs.toString(), "Some Czech dogs");
cs = res.getQuantityText(R.plurals.plurals_test, 1);
Log.d(TAG, "czech 1 cs=" + cs);
Assert.assertEquals(cs.toString(), "A Czech dog");
cs = res.getQuantityText(R.plurals.plurals_test, 2);
Log.d(TAG, "czech 2 cs=" + cs);
Assert.assertEquals(cs.toString(), "Few Czech dogs");
cs = res.getQuantityText(R.plurals.plurals_test, 5);
Assert.assertEquals(cs.toString(), "Some Czech dogs");
cs = res.getQuantityText(R.plurals.plurals_test, 500);
Assert.assertEquals(cs.toString(), "Some Czech dogs");
}
}

View File

@@ -1,141 +0,0 @@
/*
* 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 com.android.unit_tests.content;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.TypedValue;
import com.android.unit_tests.R;
public class PrimitiveTest extends AndroidTestCase {
private Resources mResources;
@Override
protected void setUp() throws Exception {
super.setUp();
mResources = mContext.getResources();
}
private void tryEnum(int resid, int expected) {
TypedArray sa =
mContext.obtainStyledAttributes(resid, R.styleable.EnumStyle);
int value = sa.getInt(R.styleable.EnumStyle_testEnum, -1);
sa.recycle();
assertEquals("Expecting value " + expected + " got " + value
+ ": in resource 0x" + Integer.toHexString(resid),
expected, value);
}
@SmallTest
public void testEnum() throws Exception {
tryEnum(R.style.TestEnum1, 1);
tryEnum(R.style.TestEnum2, 2);
tryEnum(R.style.TestEnum10, 10);
tryEnum(R.style.TestEnum1_EmptyInherit, 1);
}
private void tryFlag(int resid, int expected) {
TypedArray sa =
mContext.obtainStyledAttributes(resid, R.styleable.FlagStyle);
int value = sa.getInt(R.styleable.FlagStyle_testFlags, -1);
sa.recycle();
assertEquals("Expecting value " + expected + " got " + value
+ ": in resource 0x" + Integer.toHexString(resid),
expected, value);
}
@SmallTest
public void testFlags() throws Exception {
tryFlag(R.style.TestFlag1, 0x1);
tryFlag(R.style.TestFlag2, 0x2);
tryFlag(R.style.TestFlag31, 0x40000000);
tryFlag(R.style.TestFlag1And2, 0x3);
tryFlag(R.style.TestFlag1And2And31, 0x40000003);
}
private void tryBoolean(int resid, boolean expected) {
TypedValue v = new TypedValue();
mContext.getResources().getValue(resid, v, true);
assertEquals(TypedValue.TYPE_INT_BOOLEAN, v.type);
assertEquals("Expecting boolean value " + expected + " got " + v
+ " from TypedValue: in resource 0x" + Integer.toHexString(resid),
expected, v.data != 0);
assertEquals("Expecting boolean value " + expected + " got " + v
+ " from getBoolean(): in resource 0x" + Integer.toHexString(resid),
expected, mContext.getResources().getBoolean(resid));
}
@SmallTest
public void testBoolean() throws Exception {
tryBoolean(R.bool.trueRes, true);
tryBoolean(R.bool.falseRes, false);
}
private void tryString(int resid, String expected) {
TypedValue v = new TypedValue();
mContext.getResources().getValue(resid, v, true);
assertEquals(TypedValue.TYPE_STRING, v.type);
assertEquals("Expecting string value " + expected + " got " + v
+ ": in resource 0x" + Integer.toHexString(resid),
expected, v.string);
}
@SmallTest
public void testStringCoerce() throws Exception {
tryString(R.string.coerceIntegerToString, "100");
tryString(R.string.coerceBooleanToString, "true");
tryString(R.string.coerceColorToString, "#fff");
tryString(R.string.coerceFloatToString, "100.0");
tryString(R.string.coerceDimensionToString, "100px");
tryString(R.string.coerceFractionToString, "100%");
}
private static void checkString(int resid, String actual, String expected) {
assertEquals("Expecting string value \"" + expected + "\" got \""
+ actual + "\" in resources 0x" + Integer.toHexString(resid),
expected, actual);
}
@SmallTest
public void testFormattedString() throws Exception {
// Make sure the regular one doesn't format anything
checkString(R.string.formattedStringNone,
mResources.getString(R.string.formattedStringNone),
"Format[]");
checkString(R.string.formattedStringOne,
mResources.getString(R.string.formattedStringOne),
"Format[%d]");
checkString(R.string.formattedStringTwo,
mResources.getString(R.string.formattedStringTwo),
"Format[%3$d,%2$s]");
// Make sure the formatted one works
checkString(R.string.formattedStringNone,
mResources.getString(R.string.formattedStringNone),
"Format[]");
checkString(R.string.formattedStringOne,
mResources.getString(R.string.formattedStringOne, 42),
"Format[42]");
checkString(R.string.formattedStringTwo,
mResources.getString(R.string.formattedStringTwo, "unused", "hi", 43),
"Format[43,hi]");
}
}

View File

@@ -1,40 +0,0 @@
/*
* 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.unit_tests.content;
import android.content.res.Resources;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.unit_tests.R;
import java.io.InputStream;
public class RawResourceTest extends AndroidTestCase {
private Resources mResources;
@Override
protected void setUp() throws Exception {
super.setUp();
mResources = mContext.getResources();
}
@SmallTest
public void testReadToEnd() throws Exception {
InputStream is = mResources.openRawResource(R.raw.text);
AssetTest.verifyTextAsset(is);
}
}

View File

@@ -1,61 +0,0 @@
/*
* 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 com.android.unit_tests.content;
import android.content.res.Resources;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.unit_tests.R;
public class ResourceNameTest extends AndroidTestCase {
@SmallTest
public void testGetResourceName() {
Resources res = mContext.getResources();
String fullName = res.getResourceName(R.configVarying.simple);
assertEquals("com.android.unit_tests:configVarying/simple", fullName);
String packageName = res.getResourcePackageName(R.configVarying.simple);
assertEquals("com.android.unit_tests", packageName);
String typeName = res.getResourceTypeName(R.configVarying.simple);
assertEquals("configVarying", typeName);
String entryName = res.getResourceEntryName(R.configVarying.simple);
assertEquals("simple", entryName);
}
@SmallTest
public void testGetResourceIdentifier() {
Resources res = mContext.getResources();
int resid = res.getIdentifier(
"com.android.unit_tests:configVarying/simple",
null, null);
assertEquals(R.configVarying.simple, resid);
resid = res.getIdentifier("configVarying/simple", null,
"com.android.unit_tests");
assertEquals(R.configVarying.simple, resid);
resid = res.getIdentifier("simple", "configVarying",
"com.android.unit_tests");
assertEquals(R.configVarying.simple, resid);
}
}

View File

@@ -1,33 +0,0 @@
/*
* Copyright (C) 2006 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.unit_tests.content;
import junit.framework.TestSuite;
public class ResourceTests {
public static TestSuite suite() {
TestSuite suite = new TestSuite(ResourceTests.class.getName());
suite.addTestSuite(FractionTest.class);
suite.addTestSuite(PrimitiveTest.class);
suite.addTestSuite(ArrayTest.class);
suite.addTestSuite(ConfigTest.class);
suite.addTestSuite(RawResourceTest.class);
suite.addTestSuite(ResourceNameTest.class);
return suite;
}
}

View File

@@ -1,66 +0,0 @@
/*
* 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 com.android.frameworktest.expandablelistview;
import android.test.ActivityInstrumentationTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;
import android.view.KeyEvent;
import android.widget.ExpandableListView;
import com.android.frameworktest.expandablelistview.ExpandableListWithHeaders;
import com.android.frameworktest.util.ListUtil;
public class ExpandableListWithHeadersTest extends ActivityInstrumentationTestCase<ExpandableListWithHeaders> {
private ExpandableListView mExpandableListView;
private ListUtil mListUtil;
public ExpandableListWithHeadersTest() {
super("com.android.frameworktest",
ExpandableListWithHeaders.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mExpandableListView = getActivity().getExpandableListView();
mListUtil = new ListUtil(mExpandableListView, getInstrumentation());
}
@MediumTest
public void testPreconditions() {
assertNotNull(mExpandableListView);
}
@MediumTest
public void testExpandOnFirstPosition() {
// Should be a header, and hence the first group should NOT have expanded
mListUtil.arrowScrollToSelectedPosition(0);
sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
getInstrumentation().waitForIdleSync();
assertFalse(mExpandableListView.isGroupExpanded(0));
}
@LargeTest
public void testExpandOnFirstGroup() {
mListUtil.arrowScrollToSelectedPosition(getActivity().getNumOfHeadersAndFooters());
sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
getInstrumentation().waitForIdleSync();
assertTrue(mExpandableListView.isGroupExpanded(0));
}
}

View File

@@ -1,126 +0,0 @@
/*
* 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.frameworktest.view;
import android.os.Parcel;
import android.test.ActivityInstrumentationTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.view.InflateException;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RemoteViews;
import com.android.frameworktest.R;
import com.android.frameworktest.view.RemoteViewsActivity;
public class RemoteViewsActivityTest extends ActivityInstrumentationTestCase<RemoteViewsActivity> {
public RemoteViewsActivityTest() {
super("com.android.frameworktest", RemoteViewsActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
}
@MediumTest
public void testGood() throws Exception {
final RemoteViewsActivity activity = getActivity();
RemoteViews orig = new RemoteViews("com.android.frameworktest",
R.layout.remote_view_test_good);
Parcel p = Parcel.obtain();
orig.writeToParcel(p, 0);
p.setDataPosition(0);
RemoteViews r = RemoteViews.CREATOR.createFromParcel(p);
ViewGroup parent = (ViewGroup) activity.findViewById(R.id.parent);
View result = r.apply(activity, parent);
p.recycle();
assertTrue("LinearLayout not inflated", result.findViewById(R.id.linear) != null);
assertTrue("TextView not inflated", result.findViewById(R.id.text) != null);
assertTrue("ImageView not inflated", result.findViewById(R.id.image) != null);
assertTrue("FrameLayout not inflated", result.findViewById(R.id.frame) != null);
assertTrue("RelateiveLayout not inflated", result.findViewById(R.id.relative) != null);
assertTrue("AbsoluteLayout not inflated", result.findViewById(R.id.absolute) != null);
assertTrue("ProgressBar not inflated", result.findViewById(R.id.progress) != null);
assertTrue("ImageButton not inflated", result.findViewById(R.id.image_button) != null);
assertTrue("Button not inflated", result.findViewById(R.id.button) != null);
}
@MediumTest
public void testDerivedClass() throws Exception {
final RemoteViewsActivity activity = getActivity();
RemoteViews orig = new RemoteViews("com.android.frameworktest",
R.layout.remote_view_test_bad_1);
Parcel p = Parcel.obtain();
orig.writeToParcel(p, 0);
p.setDataPosition(0);
RemoteViews r = RemoteViews.CREATOR.createFromParcel(p);
ViewGroup parent = (ViewGroup) activity.findViewById(R.id.parent);
boolean exceptionThrown = false;
View result = null;
try {
result = r.apply(activity, parent);
} catch (InflateException e) {
exceptionThrown = true;
}
p.recycle();
assertTrue("Derived class (EditText) allowed to be inflated", exceptionThrown);
assertNull("Derived class (EditText) allowed to be inflated", result);
}
@MediumTest
public void testWebView() throws Exception {
final RemoteViewsActivity activity = getActivity();
RemoteViews orig = new RemoteViews("com.android.frameworktest",
R.layout.remote_view_test_bad_2);
Parcel p = Parcel.obtain();
orig.writeToParcel(p, 0);
p.setDataPosition(0);
RemoteViews r = RemoteViews.CREATOR.createFromParcel(p);
ViewGroup parent = (ViewGroup) activity.findViewById(R.id.parent);
boolean exceptionThrown = false;
View result = null;
try {
result = r.apply(activity, parent);
} catch (InflateException e) {
exceptionThrown = true;
}
p.recycle();
assertTrue("WebView allowed to be inflated", exceptionThrown);
assertNull("WebView allowed to be inflated", result);
}
}