PageRenderTime 90ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/cts/tests/tests/content/src/android/content/cts/ContentProviderTest.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
Java | 446 lines | 337 code | 82 blank | 27 comment | 1 complexity | 2f67c78b39f25715e383cbc95ed646d6 MD5 | raw file
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package android.content.cts;
  17. import com.android.cts.stub.R;
  18. import android.content.ContentProvider;
  19. import android.content.ContentResolver;
  20. import android.content.ContentValues;
  21. import android.content.Context;
  22. import android.content.IContentProvider;
  23. import android.content.ContentProviderResult;
  24. import android.content.ContentProviderOperation;
  25. import android.content.OperationApplicationException;
  26. import android.content.pm.ProviderInfo;
  27. import android.content.res.AssetFileDescriptor;
  28. import android.database.Cursor;
  29. import android.database.sqlite.SQLiteDatabase;
  30. import android.net.Uri;
  31. import android.os.Bundle;
  32. import android.os.IBinder;
  33. import android.os.ICancellationSignal;
  34. import android.os.ParcelFileDescriptor;
  35. import android.os.RemoteException;
  36. import android.test.AndroidTestCase;
  37. import java.io.File;
  38. import java.io.FileNotFoundException;
  39. import java.io.IOException;
  40. import java.util.ArrayList;
  41. /**
  42. * Test {@link ContentProvider}.
  43. */
  44. public class ContentProviderTest extends AndroidTestCase {
  45. private static final String TEST_PACKAGE_NAME = "com.android.cts.stub";
  46. private static final String TEST_FILE_NAME = "testFile.tmp";
  47. private static final String TEST_DB_NAME = "test.db";
  48. @Override
  49. protected void tearDown() throws Exception {
  50. mContext.deleteDatabase(TEST_DB_NAME);
  51. mContext.deleteFile(TEST_FILE_NAME);
  52. super.tearDown();
  53. }
  54. public void testOpenAssetFile() throws IOException {
  55. MockContentProvider mockContentProvider = new MockContentProvider();
  56. Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
  57. "://" + TEST_PACKAGE_NAME + "/" + R.raw.testimage);
  58. try {
  59. mockContentProvider.openAssetFile(uri, "r");
  60. fail("Should always throw out FileNotFoundException!");
  61. } catch (FileNotFoundException e) {
  62. }
  63. try {
  64. mockContentProvider.openFile(null, null);
  65. fail("Should always throw out FileNotFoundException!");
  66. } catch (FileNotFoundException e) {
  67. }
  68. }
  69. public void testAttachInfo() {
  70. MockContentProvider mockContentProvider = new MockContentProvider();
  71. ProviderInfo info1 = new ProviderInfo();
  72. info1.readPermission = "android.permission.READ_SMS";
  73. info1.writePermission = "android.permission.WRITE_SMS";
  74. mockContentProvider.attachInfo(getContext(), info1);
  75. assertSame(getContext(), mockContentProvider.getContext());
  76. assertEquals(info1.readPermission, mockContentProvider.getReadPermission());
  77. assertEquals(info1.writePermission, mockContentProvider.getWritePermission());
  78. ProviderInfo info2 = new ProviderInfo();
  79. info2.readPermission = "android.permission.READ_CONTACTS";
  80. info2.writePermission = "android.permission.WRITE_CONTACTS";
  81. mockContentProvider.attachInfo(null, info2);
  82. assertSame(getContext(), mockContentProvider.getContext());
  83. assertEquals(info1.readPermission, mockContentProvider.getReadPermission());
  84. assertEquals(info1.writePermission, mockContentProvider.getWritePermission());
  85. mockContentProvider = new MockContentProvider();
  86. mockContentProvider.attachInfo(null, null);
  87. assertNull(mockContentProvider.getContext());
  88. assertNull(mockContentProvider.getReadPermission());
  89. assertNull(mockContentProvider.getWritePermission());
  90. mockContentProvider.attachInfo(null, info2);
  91. assertNull(mockContentProvider.getContext());
  92. assertEquals(info2.readPermission, mockContentProvider.getReadPermission());
  93. assertEquals(info2.writePermission, mockContentProvider.getWritePermission());
  94. mockContentProvider.attachInfo(getContext(), info1);
  95. assertSame(getContext(), mockContentProvider.getContext());
  96. assertEquals(info1.readPermission, mockContentProvider.getReadPermission());
  97. assertEquals(info1.writePermission, mockContentProvider.getWritePermission());
  98. }
  99. public void testBulkInsert() {
  100. MockContentProvider mockContentProvider = new MockContentProvider();
  101. int count = 2;
  102. ContentValues[] values = new ContentValues[count];
  103. for (int i = 0; i < count; i++) {
  104. values[i] = new ContentValues();
  105. }
  106. Uri uri = Uri.parse("content://browser/bookmarks");
  107. assertEquals(count, mockContentProvider.bulkInsert(uri, values));
  108. assertEquals(count, mockContentProvider.getInsertCount());
  109. mockContentProvider = new MockContentProvider();
  110. try {
  111. assertEquals(count, mockContentProvider.bulkInsert(null, values));
  112. } finally {
  113. assertEquals(count, mockContentProvider.getInsertCount());
  114. }
  115. }
  116. public void testGetContext() {
  117. MockContentProvider mockContentProvider = new MockContentProvider();
  118. assertNull(mockContentProvider.getContext());
  119. mockContentProvider.attachInfo(getContext(), null);
  120. assertSame(getContext(), mockContentProvider.getContext());
  121. mockContentProvider.attachInfo(null, null);
  122. assertSame(getContext(), mockContentProvider.getContext());
  123. }
  124. public void testAccessReadPermission() {
  125. MockContentProvider mockContentProvider = new MockContentProvider();
  126. assertNull(mockContentProvider.getReadPermission());
  127. String expected = "android.permission.READ_CONTACTS";
  128. mockContentProvider.setReadPermissionWrapper(expected);
  129. assertEquals(expected, mockContentProvider.getReadPermission());
  130. expected = "android.permission.READ_SMS";
  131. mockContentProvider.setReadPermissionWrapper(expected);
  132. assertEquals(expected, mockContentProvider.getReadPermission());
  133. mockContentProvider.setReadPermissionWrapper(null);
  134. assertNull(mockContentProvider.getReadPermission());
  135. }
  136. public void testAccessWritePermission() {
  137. MockContentProvider mockContentProvider = new MockContentProvider();
  138. assertNull(mockContentProvider.getWritePermission());
  139. String expected = "android.permission.WRITE_CONTACTS";
  140. mockContentProvider.setWritePermissionWrapper(expected);
  141. assertEquals(expected, mockContentProvider.getWritePermission());
  142. expected = "android.permission.WRITE_SMS";
  143. mockContentProvider.setWritePermissionWrapper(expected);
  144. assertEquals(expected, mockContentProvider.getWritePermission());
  145. mockContentProvider.setWritePermissionWrapper(null);
  146. assertNull(mockContentProvider.getWritePermission());
  147. }
  148. public void testIsTemporary() {
  149. MockContentProvider mockContentProvider = new MockContentProvider();
  150. assertFalse(mockContentProvider.isTemporary());
  151. }
  152. public void testOpenFile() {
  153. MockContentProvider mockContentProvider = new MockContentProvider();
  154. try {
  155. Uri uri = Uri.parse("content://test");
  156. mockContentProvider.openFile(uri, "r");
  157. fail("Should always throw out FileNotFoundException!");
  158. } catch (FileNotFoundException e) {
  159. }
  160. try {
  161. mockContentProvider.openFile(null, null);
  162. fail("Should always throw out FileNotFoundException!");
  163. } catch (FileNotFoundException e) {
  164. }
  165. }
  166. public void testOpenFileHelper() throws IOException {
  167. // create a temporary File
  168. mContext.openFileOutput(TEST_FILE_NAME, Context.MODE_PRIVATE).close();
  169. File file = mContext.getFileStreamPath(TEST_FILE_NAME);
  170. assertTrue(file.exists());
  171. ContentProvider cp = new OpenFileContentProvider(file.getAbsolutePath(), TEST_DB_NAME);
  172. Uri uri = Uri.parse("content://test");
  173. assertNotNull(cp.openFile(uri, "r"));
  174. try {
  175. uri = Uri.parse("content://test");
  176. cp.openFile(uri, "wrong");
  177. fail("Should throw FileNotFoundException!");
  178. } catch (FileNotFoundException e) {
  179. }
  180. // delete the temporary file
  181. file.delete();
  182. try {
  183. uri = Uri.parse("content://test");
  184. cp.openFile(uri, "r");
  185. fail("Should throw FileNotFoundException!");
  186. } catch (FileNotFoundException e) {
  187. }
  188. try {
  189. cp.openFile((Uri) null, "r");
  190. fail("Should always throw FileNotFoundException!");
  191. } catch (FileNotFoundException e) {
  192. }
  193. }
  194. public void testOnConfigurationChanged() {
  195. // cannot trigger this callback reliably
  196. }
  197. public void testOnLowMemory() {
  198. // cannot trigger this callback reliably
  199. }
  200. public void testCoerceToLocalContentProvider() {
  201. MockContentProvider mockContentProvider = new MockContentProvider();
  202. IContentProvider transport = mockContentProvider.getIContentProvider();
  203. assertSame(mockContentProvider, ContentProvider.coerceToLocalContentProvider(transport));
  204. IContentProvider iContentProvider = new IContentProvider() {
  205. @Override
  206. public int bulkInsert(Uri url, ContentValues[] initialValues) {
  207. return 0;
  208. }
  209. @Override
  210. public int delete(Uri url, String selection, String[] selectionArgs) {
  211. return 0;
  212. }
  213. @Override
  214. public String getType(Uri url) {
  215. return null;
  216. }
  217. @Override
  218. public Uri insert(Uri url, ContentValues initialValues) {
  219. return null;
  220. }
  221. @Override
  222. public ParcelFileDescriptor openFile(Uri url, String mode) {
  223. return null;
  224. }
  225. @Override
  226. public AssetFileDescriptor openAssetFile(Uri url, String mode) {
  227. return null;
  228. }
  229. @Override
  230. public ContentProviderResult[] applyBatch(
  231. ArrayList<ContentProviderOperation> operations)
  232. throws RemoteException, OperationApplicationException {
  233. return null;
  234. }
  235. @Override
  236. public Cursor query(Uri url, String[] projection, String selection,
  237. String[] selectionArgs, String sortOrder,
  238. ICancellationSignal cancellationSignal) {
  239. return null;
  240. }
  241. @Override
  242. public int update(Uri url, ContentValues values, String selection,
  243. String[] selectionArgs) {
  244. return 0;
  245. }
  246. @Override
  247. public IBinder asBinder() {
  248. return null;
  249. }
  250. @Override
  251. public Bundle call(String method, String request, Bundle args) {
  252. return null;
  253. }
  254. @Override
  255. public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException {
  256. return null;
  257. }
  258. @Override
  259. public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts)
  260. throws RemoteException, FileNotFoundException {
  261. return null;
  262. }
  263. @Override
  264. public ICancellationSignal createCancellationSignal() throws RemoteException {
  265. return null;
  266. }
  267. };
  268. assertNull(ContentProvider.coerceToLocalContentProvider(iContentProvider));
  269. }
  270. public void testGetIContentProvider() {
  271. MockContentProvider mockContentProvider = new MockContentProvider();
  272. assertNotNull(mockContentProvider.getIContentProvider());
  273. }
  274. private class MockContentProvider extends ContentProvider {
  275. private int mInsertCount = 0;
  276. @Override
  277. public int delete(Uri uri, String selection, String[] selectionArgs) {
  278. return 0;
  279. }
  280. @Override
  281. public String getType(Uri uri) {
  282. return null;
  283. }
  284. @Override
  285. public Uri insert(Uri uri, ContentValues values) {
  286. mInsertCount++;
  287. return null;
  288. }
  289. public int getInsertCount() {
  290. return mInsertCount;
  291. }
  292. @Override
  293. public Cursor query(Uri uri, String[] projection, String selection,
  294. String[] selectionArgs, String sortOrder) {
  295. return null;
  296. }
  297. @Override
  298. public int update(Uri uri, ContentValues values, String selection,
  299. String[] selectionArgs) {
  300. return 0;
  301. }
  302. @Override
  303. public boolean onCreate() {
  304. return false;
  305. }
  306. // wrapper or override for the protected methods
  307. public void setReadPermissionWrapper(String permission) {
  308. super.setReadPermission(permission);
  309. }
  310. public void setWritePermissionWrapper(String permission) {
  311. super.setWritePermission(permission);
  312. }
  313. @Override
  314. protected boolean isTemporary() {
  315. return super.isTemporary();
  316. }
  317. public ParcelFileDescriptor openFileHelperWrapper(Uri uri, String mode)
  318. throws FileNotFoundException {
  319. return super.openFileHelper(uri, mode);
  320. }
  321. }
  322. /**
  323. * This provider implements openFile() using ContentProvider.openFileHelper().
  324. */
  325. private class OpenFileContentProvider extends ContentProvider {
  326. private SQLiteDatabase mDb;
  327. OpenFileContentProvider(String fileName, String dbName) {
  328. // delete the database if it already exists
  329. mContext.deleteDatabase(dbName);
  330. mDb = mContext.openOrCreateDatabase(dbName, Context.MODE_PRIVATE, null);
  331. mDb.execSQL("CREATE TABLE files ( _data TEXT );");
  332. mDb.execSQL("INSERT INTO files VALUES ( \"" + fileName + "\");");
  333. }
  334. @Override
  335. public int delete(Uri uri, String selection, String[] selectionArgs) {
  336. throw new RuntimeException("not implemented");
  337. }
  338. @Override
  339. public String getType(Uri uri) {
  340. throw new RuntimeException("not implemented");
  341. }
  342. @Override
  343. public Uri insert(Uri uri, ContentValues values) {
  344. throw new RuntimeException("not implemented");
  345. }
  346. @Override
  347. public boolean onCreate() {
  348. return true;
  349. }
  350. @Override
  351. public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
  352. String sortOrder) {
  353. return mDb.query("files", projection, selection, selectionArgs, null, null, null);
  354. }
  355. @Override
  356. public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  357. throw new RuntimeException("not implemented");
  358. }
  359. @Override
  360. public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
  361. return openFileHelper(uri, mode);
  362. }
  363. }
  364. }