1/* 2 * Copyright (C) 2011 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 17package com.android.providers.contacts; 18 19import android.content.ContentProvider; 20import android.content.ContentValues; 21import android.content.Context; 22import android.content.ContextWrapper; 23import android.content.Intent; 24import android.content.pm.PackageManager; 25import android.provider.CallLog.Calls; 26import android.provider.VoicemailContract; 27 28import java.io.File; 29import java.util.ArrayList; 30import java.util.Collections; 31import java.util.List; 32 33/** 34 * Base class for all tests that require interacting with the voicemail content provider. 35 */ 36public abstract class BaseVoicemailProviderTest extends BaseContactsProvider2Test { 37 38 protected boolean mUseSourceUri = false; 39 private File mTestDirectory; 40 41 @Override 42 protected void setUp() throws Exception { 43 super.setUp(); 44 addProvider(TestVoicemailProvider.class, VoicemailContract.AUTHORITY); 45 TestVoicemailProvider.setVvmProviderCallDelegate(createMockProviderCalls()); 46 } 47 48 @Override 49 protected void tearDown() throws Exception { 50 removeTestDirectory(); 51 super.tearDown(); 52 } 53 54 @Override 55 protected Class<? extends ContentProvider> getProviderClass() { 56 return TestVoicemailProvider.class; 57 } 58 59 @Override 60 protected String getAuthority() { 61 return VoicemailContract.AUTHORITY; 62 } 63 64 protected void setUpForOwnPermission() { 65 mActor.removePermissions(READ_VOICEMAIL_PERMISSION); 66 mActor.removePermissions(WRITE_VOICEMAIL_PERMISSION); 67 mActor.addPermissions(ADD_VOICEMAIL_PERMISSION); 68 mUseSourceUri = true; 69 } 70 71 protected void setUpForFullPermission() { 72 mActor.addPermissions(ADD_VOICEMAIL_PERMISSION); 73 mActor.addPermissions(READ_VOICEMAIL_PERMISSION); 74 mActor.addPermissions(WRITE_VOICEMAIL_PERMISSION); 75 mUseSourceUri = false; 76 } 77 78 protected void setUpForNoPermission() { 79 mActor.removePermissions(ADD_VOICEMAIL_PERMISSION); 80 mActor.removePermissions(READ_VOICEMAIL_PERMISSION); 81 mActor.removePermissions(WRITE_VOICEMAIL_PERMISSION); 82 mUseSourceUri = true; 83 } 84 85 protected int countFilesInTestDirectory() { 86 return findAllFiles(mTestDirectory).size(); 87 } 88 89 // TODO: Use a mocking framework to mock these calls. 90 private VvmProviderCalls createMockProviderCalls() { 91 return new VvmProviderCalls() { 92 @Override 93 public void sendOrderedBroadcast(Intent intent, String receiverPermission) { 94 // Do nothing for now. 95 } 96 97 @Override 98 public File getDir(String name, int mode) { 99 return getTestDirectory(); 100 } 101 }; 102 } 103 104 /** Lazily construct the test directory when required. */ 105 private synchronized File getTestDirectory() { 106 if (mTestDirectory == null) { 107 File baseDirectory = getContext().getCacheDir(); 108 mTestDirectory = new File(baseDirectory, Long.toString(System.currentTimeMillis())); 109 assertFalse(mTestDirectory.exists()); 110 assertTrue(mTestDirectory.mkdirs()); 111 } 112 return mTestDirectory; 113 } 114 115 private void removeTestDirectory() { 116 if (mTestDirectory != null) { 117 recursiveDeleteAll(mTestDirectory); 118 } 119 } 120 121 private static void recursiveDeleteAll(File input) { 122 if (input.isDirectory()) { 123 for (File file : input.listFiles()) { 124 recursiveDeleteAll(file); 125 } 126 } 127 assertTrue("error deleting " + input.getAbsolutePath(), input.delete()); 128 } 129 130 private List<File> findAllFiles(File input) { 131 if (input == null) { 132 return Collections.emptyList(); 133 } 134 if (!input.isDirectory()) { 135 return Collections.singletonList(input); 136 } 137 List<File> results = new ArrayList<File>(); 138 for (File file : input.listFiles()) { 139 results.addAll(findAllFiles(file)); 140 } 141 return results; 142 } 143 144 /** The calls that we need to mock out for our VvmProvider, used by TestVoicemailProvider. */ 145 private interface VvmProviderCalls { 146 public void sendOrderedBroadcast(Intent intent, String receiverPermission); 147 public File getDir(String name, int mode); 148 } 149 150 public static class TestVoicemailProvider extends VoicemailContentProvider { 151 private static VvmProviderCalls mDelegate; 152 public static synchronized void setVvmProviderCallDelegate(VvmProviderCalls delegate) { 153 mDelegate = delegate; 154 } 155 156 @Override 157 protected ContactsDatabaseHelper getDatabaseHelper(Context context) { 158 return ContactsDatabaseHelper.getNewInstanceForTest(context); 159 } 160 161 @Override 162 protected Context context() { 163 return new ContextWrapper(getContext()) { 164 @Override 165 public File getDir(String name, int mode) { 166 return mDelegate.getDir(name, mode); 167 } 168 @Override 169 public void sendBroadcast(Intent intent, String receiverPermission) { 170 mDelegate.sendOrderedBroadcast(intent, receiverPermission); 171 } 172 @Override 173 public PackageManager getPackageManager() { 174 return new MockPackageManager("com.test.package1", "com.test.package2"); 175 } 176 }; 177 } 178 179 @Override 180 protected String getCallingPackage_() { 181 return getContext().getPackageName(); 182 } 183 184 @Override 185 CallLogInsertionHelper createCallLogInsertionHelper(Context context) { 186 return new CallLogInsertionHelper() { 187 @Override 188 public String getGeocodedLocationFor(String number, String countryIso) { 189 return "usa"; 190 } 191 192 @Override 193 public void addComputedValues(ContentValues values) { 194 values.put(Calls.COUNTRY_ISO, "us"); 195 values.put(Calls.GEOCODED_LOCATION, "usa"); 196 } 197 }; 198 } 199 } 200} 201