1 /* 2 * Copyright (C) 2008 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.email; 18 19 import android.net.Uri; 20 import android.test.AndroidTestCase; 21 import android.test.suitebuilder.annotation.SmallTest; 22 23 /** 24 * This is a series of unit tests for the Preferences class. 25 * 26 * Technically these are functional because they use the underlying preferences framework. It 27 * would be a really good idea if we could inject our own underlying preferences storage, to better 28 * test cases like zero accounts behavior (right now, we have to allow for any number of accounts 29 * already being on the device, and not trashing any.) 30 */ 31 @SmallTest 32 public class PreferencesUnitTests extends AndroidTestCase { 33 34 private Preferences mPreferences; 35 36 private Account mAccount; 37 38 @Override 39 protected void setUp() throws Exception { 40 super.setUp(); 41 42 mPreferences = Preferences.getPreferences(getContext()); 43 } 44 45 /** 46 * Delete any dummy accounts we set up for this test 47 */ 48 @Override 49 protected void tearDown() throws Exception { 50 super.tearDown(); 51 52 if (mAccount != null && mPreferences != null) { 53 mAccount.delete(mPreferences); 54 } 55 } 56 57 /** 58 * Test the new getAccountByContentUri() API. This should return null if no 59 * accounts are configured, or the Uri doesn't match, and it should return a desired account 60 * otherwise. 61 * 62 * TODO: Not actually testing the no-accounts case 63 */ 64 public void testGetAccountByContentUri() { 65 // Create a dummy account 66 createTestAccount(); 67 68 // test sunny-day lookup by Uri 69 Uri testAccountUri = mAccount.getContentUri(); 70 Account lookup = mPreferences.getAccountByContentUri(testAccountUri); 71 assertEquals(mAccount, lookup); 72 73 // now make it a bogus Uri - bad scheme, good path, good UUID 74 testAccountUri = Uri.parse("bogus://accounts/" + mAccount.getUuid()); 75 lookup = mPreferences.getAccountByContentUri(testAccountUri); 76 assertNull(lookup); 77 78 // now make it a bogus Uri - good scheme, bad path, good UUID 79 testAccountUri = Uri.parse("content://bogus/" + mAccount.getUuid()); 80 lookup = mPreferences.getAccountByContentUri(testAccountUri); 81 assertNull(lookup); 82 83 // now make it a bogus Uri - good scheme/path, bad UUID 84 testAccountUri = Uri.parse("content://accounts/" + mAccount.getUuid() + "-bogus"); 85 lookup = mPreferences.getAccountByContentUri(testAccountUri); 86 assertNull(lookup); 87 } 88 89 /** 90 * Create a dummy account with minimal fields 91 */ 92 private void createTestAccount() { 93 mAccount = new Account(getContext()); 94 mAccount.save(mPreferences); 95 } 96 97 } 98