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