Home | History | Annotate | Download | only in appwithdata
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.cts.appwithdata;
     18 
     19 import android.content.ContentValues;
     20 import android.content.Context;
     21 import android.content.SharedPreferences;
     22 import android.database.Cursor;
     23 import android.database.sqlite.SQLiteDatabase;
     24 import android.database.sqlite.SQLiteOpenHelper;
     25 import android.test.AndroidTestCase;
     26 
     27 import java.io.FileOutputStream;
     28 import java.io.IOException;
     29 
     30 /**
     31  * Test that will create private app data.
     32  *
     33  * This is not really a test per-say. Its just used as a hook so the test controller can trigger
     34  * the creation of private app data.
     35  */
     36 public class CreatePrivateDataTest extends AndroidTestCase {
     37 
     38     /**
     39      * Name of private file to create.
     40      */
     41     private static final String PRIVATE_FILE_NAME = "private_file.txt";
     42 
     43     private static final String PREFERENCES_FILE_NAME = "preferences";
     44     private static final String PREFERENCE_KEY = "preference_key";
     45     private static final String PREFERENCE_VALUE = "preference_value";
     46 
     47     static final String DB_TABLE_NAME = "test_table";
     48     static final String DB_COLUMN = "test_column";
     49     static final String DB_VALUE = "test_value";
     50 
     51     /**
     52      * Creates a file private to this app
     53      * @throws IOException if any error occurred when creating the file
     54      */
     55     public void testCreatePrivateData() throws IOException {
     56         FileOutputStream outputStream = getContext().openFileOutput(PRIVATE_FILE_NAME,
     57                 Context.MODE_PRIVATE);
     58         outputStream.write("file contents".getBytes());
     59         outputStream.close();
     60         assertTrue(getContext().getFileStreamPath(PRIVATE_FILE_NAME).exists());
     61 
     62         writeToPreferences();
     63         writeToDatabase();
     64     }
     65 
     66     private void writeToPreferences() {
     67         SharedPreferences prefs = mContext.getSharedPreferences(PREFERENCES_FILE_NAME, 0);
     68         SharedPreferences.Editor editor = prefs.edit();
     69         editor.putString(PREFERENCE_KEY, PREFERENCE_VALUE);
     70         editor.commit();
     71         assertEquals(PREFERENCE_VALUE, prefs.getString(PREFERENCE_KEY, null));
     72     }
     73 
     74     private void writeToDatabase() {
     75         SQLiteDatabase db = null;
     76         Cursor cursor = null;
     77         try {
     78             db = new TestDatabaseOpenHelper(mContext).getWritableDatabase();
     79             ContentValues values = new ContentValues(1);
     80             values.put(DB_COLUMN, DB_VALUE);
     81             assertTrue(db.insert(DB_TABLE_NAME, null, values) != -1);
     82 
     83             cursor = db.query(DB_TABLE_NAME, new String[] {DB_COLUMN},
     84                     null, null, null, null, null);
     85             assertEquals(1, cursor.getCount());
     86         } finally {
     87             if (cursor != null) {
     88                 cursor.close();
     89             }
     90             if (db != null) {
     91                 db.close();
     92             }
     93         }
     94     }
     95 
     96     /**
     97      * Check to ensure the private file created in testCreatePrivateData does not exist.
     98      * Used to check that uninstall of an app deletes the app's data.
     99      */
    100     public void testEnsurePrivateDataNotExist() throws IOException {
    101         assertFalse(getContext().getFileStreamPath(PRIVATE_FILE_NAME).exists());
    102 
    103         assertPreferencesDataDoesNotExist();
    104         assertDatabaseDataDoesNotExist();
    105     }
    106 
    107     private void assertPreferencesDataDoesNotExist() {
    108         SharedPreferences prefs = mContext.getSharedPreferences(PREFERENCES_FILE_NAME, 0);
    109         assertNull(prefs.getString(PREFERENCE_KEY, null));
    110     }
    111 
    112     private void assertDatabaseDataDoesNotExist() {
    113         SQLiteDatabase db = null;
    114         Cursor cursor = null;
    115         try {
    116             db = new TestDatabaseOpenHelper(mContext).getWritableDatabase();
    117             cursor = db.query(DB_TABLE_NAME, new String[] {DB_COLUMN},
    118                     null, null, null, null, null);
    119             assertEquals(0, cursor.getCount());
    120         } finally {
    121             if (cursor != null) {
    122                 cursor.close();
    123             }
    124             if (db != null) {
    125                 db.close();
    126             }
    127         }
    128     }
    129 
    130     static class TestDatabaseOpenHelper extends SQLiteOpenHelper {
    131 
    132         static final String _ID = "_id";
    133 
    134         public TestDatabaseOpenHelper(Context context) {
    135             super(context, "test.db", null, 1337);
    136         }
    137 
    138         @Override
    139         public void onCreate(SQLiteDatabase db) {
    140             db.execSQL("CREATE TABLE " + DB_TABLE_NAME + " ("
    141                     + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
    142                     + DB_COLUMN + " TEXT);");
    143         }
    144 
    145         @Override
    146         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    147             db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE_NAME);
    148             onCreate(db);
    149         }
    150     }
    151 }
    152