PageRenderTime 83ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/brian0218/rk3188_rk3066_r-box_android4.4.2_sdk
Java | 1062 lines | 801 code | 160 blank | 101 comment | 21 complexity | 3ebeb46cc48cd1a9846af21ce299b4ee 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");
  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. package android.content.cts;
  17. import com.android.cts.stub.R;
  18. import android.accounts.Account;
  19. import android.content.ContentProviderClient;
  20. import android.content.ContentResolver;
  21. import android.content.ContentValues;
  22. import android.content.Context;
  23. import android.content.res.AssetFileDescriptor;
  24. import android.cts.util.PollingCheck;
  25. import android.database.ContentObserver;
  26. import android.database.Cursor;
  27. import android.database.sqlite.SQLiteQueryBuilder;
  28. import android.net.Uri;
  29. import android.os.Bundle;
  30. import android.os.CancellationSignal;
  31. import android.os.OperationCanceledException;
  32. import android.os.ParcelFileDescriptor;
  33. import android.os.RemoteException;
  34. import android.test.AndroidTestCase;
  35. import android.util.Log;
  36. import java.io.FileInputStream;
  37. import java.io.FileNotFoundException;
  38. import java.io.IOException;
  39. import java.io.InputStream;
  40. import java.io.InputStreamReader;
  41. import java.io.OutputStream;
  42. public class ContentResolverTest extends AndroidTestCase {
  43. private final static String COLUMN_ID_NAME = "_id";
  44. private final static String COLUMN_KEY_NAME = "key";
  45. private final static String COLUMN_VALUE_NAME = "value";
  46. private static final String AUTHORITY = "ctstest";
  47. private static final Uri TABLE1_URI = Uri.parse("content://" + AUTHORITY + "/testtable1/");
  48. private static final Uri TABLE1_CROSS_URI =
  49. Uri.parse("content://" + AUTHORITY + "/testtable1/cross");
  50. private static final Uri TABLE2_URI = Uri.parse("content://" + AUTHORITY + "/testtable2/");
  51. private static final Uri SELF_URI = Uri.parse("content://" + AUTHORITY + "/self/");
  52. private static final Uri CRASH_URI = Uri.parse("content://" + AUTHORITY + "/crash/");
  53. private static final String REMOTE_AUTHORITY = "remotectstest";
  54. private static final Uri REMOTE_TABLE1_URI = Uri.parse("content://"
  55. + REMOTE_AUTHORITY + "/testtable1/");
  56. private static final Uri REMOTE_SELF_URI = Uri.parse("content://"
  57. + REMOTE_AUTHORITY + "/self/");
  58. private static final Uri REMOTE_CRASH_URI = Uri.parse("content://"
  59. + REMOTE_AUTHORITY + "/crash/");
  60. private static final Account ACCOUNT = new Account("cts", "cts");
  61. private static final String KEY1 = "key1";
  62. private static final String KEY2 = "key2";
  63. private static final String KEY3 = "key3";
  64. private static final int VALUE1 = 1;
  65. private static final int VALUE2 = 2;
  66. private static final int VALUE3 = 3;
  67. private static final String TEST_PACKAGE_NAME = "com.android.cts.stub";
  68. private Context mContext;
  69. private ContentResolver mContentResolver;
  70. private Cursor mCursor;
  71. @Override
  72. protected void setUp() throws Exception {
  73. super.setUp();
  74. mContext = getContext();
  75. mContentResolver = mContext.getContentResolver();
  76. android.provider.Settings.System.putInt(mContentResolver, "__cts_crash_on_launch", 0);
  77. // add three rows to database when every test case start.
  78. ContentValues values = new ContentValues();
  79. values.put(COLUMN_KEY_NAME, KEY1);
  80. values.put(COLUMN_VALUE_NAME, VALUE1);
  81. mContentResolver.insert(TABLE1_URI, values);
  82. mContentResolver.insert(REMOTE_TABLE1_URI, values);
  83. values.put(COLUMN_KEY_NAME, KEY2);
  84. values.put(COLUMN_VALUE_NAME, VALUE2);
  85. mContentResolver.insert(TABLE1_URI, values);
  86. mContentResolver.insert(REMOTE_TABLE1_URI, values);
  87. values.put(COLUMN_KEY_NAME, KEY3);
  88. values.put(COLUMN_VALUE_NAME, VALUE3);
  89. mContentResolver.insert(TABLE1_URI, values);
  90. mContentResolver.insert(REMOTE_TABLE1_URI, values);
  91. }
  92. @Override
  93. protected void tearDown() throws Exception {
  94. mContentResolver.delete(TABLE1_URI, null, null);
  95. if ( null != mCursor && !mCursor.isClosed() ) {
  96. mCursor.close();
  97. }
  98. mContentResolver.delete(REMOTE_TABLE1_URI, null, null);
  99. if ( null != mCursor && !mCursor.isClosed() ) {
  100. mCursor.close();
  101. }
  102. super.tearDown();
  103. }
  104. public void testConstructor() {
  105. assertNotNull(mContentResolver);
  106. }
  107. public void testCrashOnLaunch() {
  108. // This test is going to make sure that the platform deals correctly
  109. // with a content provider process going away while a client is waiting
  110. // for it to come up.
  111. // First, we need to make sure our provider process is gone. Goodbye!
  112. ContentProviderClient client = mContentResolver.acquireContentProviderClient(
  113. REMOTE_AUTHORITY);
  114. // We are going to do something wrong here... release the client first,
  115. // so the act of killing it doesn't kill our own process.
  116. client.release();
  117. try {
  118. client.delete(REMOTE_SELF_URI, null, null);
  119. } catch (RemoteException e) {
  120. }
  121. // Now make sure the thing is actually gone.
  122. boolean gone = true;
  123. try {
  124. client.getType(REMOTE_TABLE1_URI);
  125. gone = false;
  126. } catch (RemoteException e) {
  127. }
  128. if (!gone) {
  129. fail("Content provider process is not gone!");
  130. }
  131. try {
  132. android.provider.Settings.System.putInt(mContentResolver, "__cts_crash_on_launch", 1);
  133. String type1 = mContentResolver.getType(REMOTE_TABLE1_URI);
  134. assertEquals(android.provider.Settings.System.getInt(mContentResolver,
  135. "__cts_crash_on_launch", 0), 0);
  136. assertTrue(type1.startsWith(ContentResolver.CURSOR_DIR_BASE_TYPE, 0));
  137. } finally {
  138. android.provider.Settings.System.putInt(mContentResolver, "__cts_crash_on_launch", 0);
  139. }
  140. }
  141. public void testUnstableToStableRefs() {
  142. // Get an unstable refrence on the remote content provider.
  143. ContentProviderClient uClient = mContentResolver.acquireUnstableContentProviderClient(
  144. REMOTE_AUTHORITY);
  145. // Verify we can access it.
  146. String type1 = mContentResolver.getType(REMOTE_TABLE1_URI);
  147. assertTrue(type1.startsWith(ContentResolver.CURSOR_DIR_BASE_TYPE, 0));
  148. // Get a stable reference on the remote content provider.
  149. ContentProviderClient sClient = mContentResolver.acquireContentProviderClient(
  150. REMOTE_AUTHORITY);
  151. // Verify we can still access it.
  152. type1 = mContentResolver.getType(REMOTE_TABLE1_URI);
  153. assertTrue(type1.startsWith(ContentResolver.CURSOR_DIR_BASE_TYPE, 0));
  154. // Release unstable reference.
  155. uClient.release();
  156. // Verify we can still access it.
  157. type1 = mContentResolver.getType(REMOTE_TABLE1_URI);
  158. assertTrue(type1.startsWith(ContentResolver.CURSOR_DIR_BASE_TYPE, 0));
  159. // Release stable reference, removing last ref.
  160. sClient.release();
  161. // Kill it. Note that a bug at this point where it causes our own
  162. // process to be killed will result in the entire test failing.
  163. try {
  164. Log.i("ContentResolverTest",
  165. "Killing remote client -- if test process goes away, that is why!");
  166. uClient.delete(REMOTE_SELF_URI, null, null);
  167. } catch (RemoteException e) {
  168. }
  169. // Make sure the remote client is actually gone.
  170. boolean gone = true;
  171. try {
  172. sClient.getType(REMOTE_TABLE1_URI);
  173. gone = false;
  174. } catch (RemoteException e) {
  175. }
  176. if (!gone) {
  177. fail("Content provider process is not gone!");
  178. }
  179. }
  180. public void testStableToUnstableRefs() {
  181. // Get a stable reference on the remote content provider.
  182. ContentProviderClient sClient = mContentResolver.acquireContentProviderClient(
  183. REMOTE_AUTHORITY);
  184. // Verify we can still access it.
  185. String type1 = mContentResolver.getType(REMOTE_TABLE1_URI);
  186. assertTrue(type1.startsWith(ContentResolver.CURSOR_DIR_BASE_TYPE, 0));
  187. // Get an unstable refrence on the remote content provider.
  188. ContentProviderClient uClient = mContentResolver.acquireUnstableContentProviderClient(
  189. REMOTE_AUTHORITY);
  190. // Verify we can access it.
  191. type1 = mContentResolver.getType(REMOTE_TABLE1_URI);
  192. assertTrue(type1.startsWith(ContentResolver.CURSOR_DIR_BASE_TYPE, 0));
  193. // Release stable reference, leaving only an unstable ref.
  194. sClient.release();
  195. // Kill it. Note that a bug at this point where it causes our own
  196. // process to be killed will result in the entire test failing.
  197. try {
  198. Log.i("ContentResolverTest",
  199. "Killing remote client -- if test process goes away, that is why!");
  200. uClient.delete(REMOTE_SELF_URI, null, null);
  201. } catch (RemoteException e) {
  202. }
  203. // Make sure the remote client is actually gone.
  204. boolean gone = true;
  205. try {
  206. uClient.getType(REMOTE_TABLE1_URI);
  207. gone = false;
  208. } catch (RemoteException e) {
  209. }
  210. if (!gone) {
  211. fail("Content provider process is not gone!");
  212. }
  213. // Release unstable reference.
  214. uClient.release();
  215. }
  216. public void testGetType() {
  217. String type1 = mContentResolver.getType(TABLE1_URI);
  218. assertTrue(type1.startsWith(ContentResolver.CURSOR_DIR_BASE_TYPE, 0));
  219. String type2 = mContentResolver.getType(TABLE2_URI);
  220. assertTrue(type2.startsWith(ContentResolver.CURSOR_DIR_BASE_TYPE, 0));
  221. Uri invalidUri = Uri.parse("abc");
  222. assertNull(mContentResolver.getType(invalidUri));
  223. try {
  224. mContentResolver.getType(null);
  225. fail("did not throw NullPointerException when Uri is null.");
  226. } catch (NullPointerException e) {
  227. //expected.
  228. }
  229. }
  230. public void testUnstableGetType() {
  231. // Get an unstable refrence on the remote content provider.
  232. ContentProviderClient client = mContentResolver.acquireUnstableContentProviderClient(
  233. REMOTE_AUTHORITY);
  234. // Verify we can access it.
  235. String type1 = mContentResolver.getType(REMOTE_TABLE1_URI);
  236. assertTrue(type1.startsWith(ContentResolver.CURSOR_DIR_BASE_TYPE, 0));
  237. // Kill it. Note that a bug at this point where it causes our own
  238. // process to be killed will result in the entire test failing.
  239. try {
  240. Log.i("ContentResolverTest",
  241. "Killing remote client -- if test process goes away, that is why!");
  242. client.delete(REMOTE_SELF_URI, null, null);
  243. } catch (RemoteException e) {
  244. }
  245. // Make sure the remote client is actually gone.
  246. boolean gone = true;
  247. try {
  248. client.getType(REMOTE_TABLE1_URI);
  249. gone = false;
  250. } catch (RemoteException e) {
  251. }
  252. if (!gone) {
  253. fail("Content provider process is not gone!");
  254. }
  255. // Now the remote client is gone, can we recover?
  256. // Release our old reference.
  257. client.release();
  258. // Get a new reference.
  259. client = mContentResolver.acquireUnstableContentProviderClient(REMOTE_AUTHORITY);
  260. // Verify we can access it.
  261. type1 = mContentResolver.getType(REMOTE_TABLE1_URI);
  262. assertTrue(type1.startsWith(ContentResolver.CURSOR_DIR_BASE_TYPE, 0));
  263. }
  264. public void testQuery() {
  265. mCursor = mContentResolver.query(TABLE1_URI, null, null, null, null);
  266. assertNotNull(mCursor);
  267. assertEquals(3, mCursor.getCount());
  268. assertEquals(3, mCursor.getColumnCount());
  269. mCursor.moveToLast();
  270. assertEquals(3, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  271. assertEquals(KEY3, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  272. assertEquals(VALUE3, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  273. mCursor.moveToPrevious();
  274. assertEquals(2, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  275. assertEquals(KEY2, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  276. assertEquals(VALUE2, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  277. mCursor.close();
  278. String selection = COLUMN_ID_NAME + "=1";
  279. mCursor = mContentResolver.query(TABLE1_URI, null, selection, null, null);
  280. assertNotNull(mCursor);
  281. assertEquals(1, mCursor.getCount());
  282. assertEquals(3, mCursor.getColumnCount());
  283. mCursor.moveToFirst();
  284. assertEquals(1, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  285. assertEquals(KEY1, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  286. assertEquals(VALUE1, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  287. mCursor.close();
  288. selection = COLUMN_KEY_NAME + "=\"" + KEY3 + "\"";
  289. mCursor = mContentResolver.query(TABLE1_URI, null, selection, null, null);
  290. assertNotNull(mCursor);
  291. assertEquals(1, mCursor.getCount());
  292. assertEquals(3, mCursor.getColumnCount());
  293. mCursor.moveToFirst();
  294. assertEquals(3, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  295. assertEquals(KEY3, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  296. assertEquals(VALUE3, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  297. mCursor.close();
  298. try {
  299. mContentResolver.query(null, null, null, null, null);
  300. fail("did not throw NullPointerException when uri is null.");
  301. } catch (NullPointerException e) {
  302. //expected.
  303. }
  304. }
  305. public void testCrashingQuery() {
  306. try {
  307. android.provider.Settings.System.putInt(mContentResolver, "__cts_crash_on_launch", 1);
  308. mCursor = mContentResolver.query(REMOTE_CRASH_URI, null, null, null, null);
  309. assertEquals(android.provider.Settings.System.getInt(mContentResolver,
  310. "__cts_crash_on_launch", 0), 0);
  311. } finally {
  312. android.provider.Settings.System.putInt(mContentResolver, "__cts_crash_on_launch", 0);
  313. }
  314. assertNotNull(mCursor);
  315. assertEquals(3, mCursor.getCount());
  316. assertEquals(3, mCursor.getColumnCount());
  317. mCursor.moveToLast();
  318. assertEquals(3, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  319. assertEquals(KEY3, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  320. assertEquals(VALUE3, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  321. mCursor.moveToPrevious();
  322. assertEquals(2, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  323. assertEquals(KEY2, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  324. assertEquals(VALUE2, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  325. mCursor.close();
  326. }
  327. public void testCancelableQuery_WhenNotCanceled_ReturnsResultSet() {
  328. CancellationSignal cancellationSignal = new CancellationSignal();
  329. Cursor cursor = mContentResolver.query(TABLE1_URI, null, null, null, null,
  330. cancellationSignal);
  331. assertEquals(3, cursor.getCount());
  332. cursor.close();
  333. }
  334. public void testCancelableQuery_WhenCanceledBeforeQuery_ThrowsImmediately() {
  335. CancellationSignal cancellationSignal = new CancellationSignal();
  336. cancellationSignal.cancel();
  337. try {
  338. mContentResolver.query(TABLE1_URI, null, null, null, null, cancellationSignal);
  339. fail("Expected OperationCanceledException");
  340. } catch (OperationCanceledException ex) {
  341. // expected
  342. }
  343. }
  344. public void testCancelableQuery_WhenCanceledDuringLongRunningQuery_CancelsQueryAndThrows() {
  345. // Populate a table with a bunch of integers.
  346. mContentResolver.delete(TABLE1_URI, null, null);
  347. ContentValues values = new ContentValues();
  348. for (int i = 0; i < 100; i++) {
  349. values.put(COLUMN_KEY_NAME, i);
  350. values.put(COLUMN_VALUE_NAME, i);
  351. mContentResolver.insert(TABLE1_URI, values);
  352. }
  353. for (int i = 0; i < 5; i++) {
  354. final CancellationSignal cancellationSignal = new CancellationSignal();
  355. Thread cancellationThread = new Thread() {
  356. @Override
  357. public void run() {
  358. try {
  359. Thread.sleep(300);
  360. } catch (InterruptedException ex) {
  361. }
  362. cancellationSignal.cancel();
  363. }
  364. };
  365. try {
  366. // Build an unsatisfiable 5-way cross-product query over 100 values but
  367. // produces no output. This should force SQLite to loop for a long time
  368. // as it tests 10^10 combinations.
  369. cancellationThread.start();
  370. final long startTime = System.nanoTime();
  371. try {
  372. mContentResolver.query(TABLE1_CROSS_URI, null,
  373. "a.value + b.value + c.value + d.value + e.value > 1000000",
  374. null, null, cancellationSignal);
  375. fail("Expected OperationCanceledException");
  376. } catch (OperationCanceledException ex) {
  377. // expected
  378. }
  379. // We want to confirm that the query really was running and then got
  380. // canceled midway.
  381. final long waitTime = System.nanoTime() - startTime;
  382. if (waitTime > 150 * 1000000L && waitTime < 600 * 1000000L) {
  383. return; // success!
  384. }
  385. } finally {
  386. try {
  387. cancellationThread.join();
  388. } catch (InterruptedException e) {
  389. }
  390. }
  391. }
  392. // Occasionally we might miss the timing deadline due to factors in the
  393. // environment, but if after several trials we still couldn't demonstrate
  394. // that the query was canceled, then the test must be broken.
  395. fail("Could not prove that the query actually canceled midway during execution.");
  396. }
  397. public void testOpenInputStream() throws IOException {
  398. final Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
  399. "://" + TEST_PACKAGE_NAME + "/" + R.drawable.pass);
  400. InputStream is = mContentResolver.openInputStream(uri);
  401. assertNotNull(is);
  402. is.close();
  403. final Uri invalidUri = Uri.parse("abc");
  404. try {
  405. mContentResolver.openInputStream(invalidUri);
  406. fail("did not throw FileNotFoundException when uri is invalid.");
  407. } catch (FileNotFoundException e) {
  408. //expected.
  409. }
  410. }
  411. public void testOpenOutputStream() throws IOException {
  412. Uri uri = Uri.parse(ContentResolver.SCHEME_FILE + "://" +
  413. getContext().getCacheDir().getAbsolutePath() +
  414. "/temp.jpg");
  415. OutputStream os = mContentResolver.openOutputStream(uri);
  416. assertNotNull(os);
  417. os.close();
  418. os = mContentResolver.openOutputStream(uri, "wa");
  419. assertNotNull(os);
  420. os.close();
  421. uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
  422. "://" + TEST_PACKAGE_NAME + "/" + R.raw.testimage);
  423. try {
  424. mContentResolver.openOutputStream(uri);
  425. fail("did not throw FileNotFoundException when scheme is not accepted.");
  426. } catch (FileNotFoundException e) {
  427. //expected.
  428. }
  429. try {
  430. mContentResolver.openOutputStream(uri, "w");
  431. fail("did not throw FileNotFoundException when scheme is not accepted.");
  432. } catch (FileNotFoundException e) {
  433. //expected.
  434. }
  435. Uri invalidUri = Uri.parse("abc");
  436. try {
  437. mContentResolver.openOutputStream(invalidUri);
  438. fail("did not throw FileNotFoundException when uri is invalid.");
  439. } catch (FileNotFoundException e) {
  440. //expected.
  441. }
  442. try {
  443. mContentResolver.openOutputStream(invalidUri, "w");
  444. fail("did not throw FileNotFoundException when uri is invalid.");
  445. } catch (FileNotFoundException e) {
  446. //expected.
  447. }
  448. }
  449. public void testOpenAssetFileDescriptor() throws IOException {
  450. Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
  451. "://" + TEST_PACKAGE_NAME + "/" + R.raw.testimage);
  452. AssetFileDescriptor afd = mContentResolver.openAssetFileDescriptor(uri, "r");
  453. assertNotNull(afd);
  454. afd.close();
  455. try {
  456. mContentResolver.openAssetFileDescriptor(uri, "d");
  457. fail("did not throw FileNotFoundException when mode is unknown.");
  458. } catch (FileNotFoundException e) {
  459. //expected.
  460. }
  461. Uri invalidUri = Uri.parse("abc");
  462. try {
  463. mContentResolver.openAssetFileDescriptor(invalidUri, "r");
  464. fail("did not throw FileNotFoundException when uri is invalid.");
  465. } catch (FileNotFoundException e) {
  466. //expected.
  467. }
  468. }
  469. private String consumeAssetFileDescriptor(AssetFileDescriptor afd)
  470. throws IOException {
  471. FileInputStream stream = null;
  472. try {
  473. stream = afd.createInputStream();
  474. InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
  475. // Got it... copy the stream into a local string and return it.
  476. StringBuilder builder = new StringBuilder(128);
  477. char[] buffer = new char[8192];
  478. int len;
  479. while ((len=reader.read(buffer)) > 0) {
  480. builder.append(buffer, 0, len);
  481. }
  482. return builder.toString();
  483. } finally {
  484. if (stream != null) {
  485. try {
  486. stream.close();
  487. } catch (IOException e) {
  488. }
  489. }
  490. }
  491. }
  492. public void testCrashingOpenAssetFileDescriptor() throws IOException {
  493. AssetFileDescriptor afd = null;
  494. try {
  495. android.provider.Settings.System.putInt(mContentResolver, "__cts_crash_on_launch", 1);
  496. afd = mContentResolver.openAssetFileDescriptor(REMOTE_CRASH_URI, "rw");
  497. assertEquals(android.provider.Settings.System.getInt(mContentResolver,
  498. "__cts_crash_on_launch", 0), 0);
  499. assertNotNull(afd);
  500. String str = consumeAssetFileDescriptor(afd);
  501. afd = null;
  502. assertEquals(str, "This is the openAssetFile test data!");
  503. } finally {
  504. android.provider.Settings.System.putInt(mContentResolver, "__cts_crash_on_launch", 0);
  505. if (afd != null) {
  506. afd.close();
  507. }
  508. }
  509. // Make sure a content provider crash at this point won't hurt us.
  510. ContentProviderClient uClient = mContentResolver.acquireUnstableContentProviderClient(
  511. REMOTE_AUTHORITY);
  512. // Kill it. Note that a bug at this point where it causes our own
  513. // process to be killed will result in the entire test failing.
  514. try {
  515. Log.i("ContentResolverTest",
  516. "Killing remote client -- if test process goes away, that is why!");
  517. uClient.delete(REMOTE_SELF_URI, null, null);
  518. } catch (RemoteException e) {
  519. }
  520. uClient.release();
  521. }
  522. public void testCrashingOpenTypedAssetFileDescriptor() throws IOException {
  523. AssetFileDescriptor afd = null;
  524. try {
  525. android.provider.Settings.System.putInt(mContentResolver, "__cts_crash_on_launch", 1);
  526. afd = mContentResolver.openTypedAssetFileDescriptor(
  527. REMOTE_CRASH_URI, "text/plain", null);
  528. assertEquals(android.provider.Settings.System.getInt(mContentResolver,
  529. "__cts_crash_on_launch", 0), 0);
  530. assertNotNull(afd);
  531. String str = consumeAssetFileDescriptor(afd);
  532. afd = null;
  533. assertEquals(str, "This is the openTypedAssetFile test data!");
  534. } finally {
  535. android.provider.Settings.System.putInt(mContentResolver, "__cts_crash_on_launch", 0);
  536. if (afd != null) {
  537. afd.close();
  538. }
  539. }
  540. // Make sure a content provider crash at this point won't hurt us.
  541. ContentProviderClient uClient = mContentResolver.acquireUnstableContentProviderClient(
  542. REMOTE_AUTHORITY);
  543. // Kill it. Note that a bug at this point where it causes our own
  544. // process to be killed will result in the entire test failing.
  545. try {
  546. Log.i("ContentResolverTest",
  547. "Killing remote client -- if test process goes away, that is why!");
  548. uClient.delete(REMOTE_SELF_URI, null, null);
  549. } catch (RemoteException e) {
  550. }
  551. uClient.release();
  552. }
  553. public void testOpenFileDescriptor() throws IOException {
  554. Uri uri = Uri.parse(ContentResolver.SCHEME_FILE + "://" +
  555. getContext().getCacheDir().getAbsolutePath() +
  556. "/temp.jpg");
  557. ParcelFileDescriptor pfd = mContentResolver.openFileDescriptor(uri, "w");
  558. assertNotNull(pfd);
  559. pfd.close();
  560. try {
  561. mContentResolver.openFileDescriptor(uri, "d");
  562. fail("did not throw IllegalArgumentException when mode is unknown.");
  563. } catch (IllegalArgumentException e) {
  564. //expected.
  565. }
  566. Uri invalidUri = Uri.parse("abc");
  567. try {
  568. mContentResolver.openFileDescriptor(invalidUri, "w");
  569. fail("did not throw FileNotFoundException when uri is invalid.");
  570. } catch (FileNotFoundException e) {
  571. //expected.
  572. }
  573. uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
  574. "://" + TEST_PACKAGE_NAME + "/" + R.raw.testimage);
  575. try {
  576. mContentResolver.openFileDescriptor(uri, "w");
  577. fail("did not throw FileNotFoundException when scheme is not accepted.");
  578. } catch (FileNotFoundException e) {
  579. //expected.
  580. }
  581. }
  582. public void testInsert() {
  583. String key4 = "key4";
  584. String key5 = "key5";
  585. int value4 = 4;
  586. int value5 = 5;
  587. String key4Selection = COLUMN_KEY_NAME + "=\"" + key4 + "\"";
  588. mCursor = mContentResolver.query(TABLE1_URI, null, key4Selection, null, null);
  589. assertEquals(0, mCursor.getCount());
  590. mCursor.close();
  591. ContentValues values = new ContentValues();
  592. values.put(COLUMN_KEY_NAME, key4);
  593. values.put(COLUMN_VALUE_NAME, value4);
  594. Uri uri = mContentResolver.insert(TABLE1_URI, values);
  595. assertNotNull(uri);
  596. mCursor = mContentResolver.query(TABLE1_URI, null, key4Selection, null, null);
  597. assertNotNull(mCursor);
  598. assertEquals(1, mCursor.getCount());
  599. mCursor.moveToFirst();
  600. assertEquals(4, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  601. assertEquals(key4, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  602. assertEquals(value4, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  603. mCursor.close();
  604. values.put(COLUMN_KEY_NAME, key5);
  605. values.put(COLUMN_VALUE_NAME, value5);
  606. uri = mContentResolver.insert(TABLE1_URI, values);
  607. assertNotNull(uri);
  608. // check returned uri
  609. mCursor = mContentResolver.query(uri, null, null, null, null);
  610. assertNotNull(mCursor);
  611. assertEquals(1, mCursor.getCount());
  612. mCursor.moveToLast();
  613. assertEquals(5, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  614. assertEquals(key5, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  615. assertEquals(value5, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  616. mCursor.close();
  617. try {
  618. mContentResolver.insert(null, values);
  619. fail("did not throw NullPointerException when uri is null.");
  620. } catch (NullPointerException e) {
  621. //expected.
  622. }
  623. }
  624. public void testBulkInsert() {
  625. String key4 = "key4";
  626. String key5 = "key5";
  627. int value4 = 4;
  628. int value5 = 5;
  629. mCursor = mContentResolver.query(TABLE1_URI, null, null, null, null);
  630. assertNotNull(mCursor);
  631. assertEquals(3, mCursor.getCount());
  632. mCursor.close();
  633. ContentValues[] cvs = new ContentValues[2];
  634. cvs[0] = new ContentValues();
  635. cvs[0].put(COLUMN_KEY_NAME, key4);
  636. cvs[0].put(COLUMN_VALUE_NAME, value4);
  637. cvs[1] = new ContentValues();
  638. cvs[1].put(COLUMN_KEY_NAME, key5);
  639. cvs[1].put(COLUMN_VALUE_NAME, value5);
  640. assertEquals(2, mContentResolver.bulkInsert(TABLE1_URI, cvs));
  641. mCursor = mContentResolver.query(TABLE1_URI, null, null, null, null);
  642. assertNotNull(mCursor);
  643. assertEquals(5, mCursor.getCount());
  644. mCursor.moveToLast();
  645. assertEquals(5, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  646. assertEquals(key5, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  647. assertEquals(value5, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  648. mCursor.moveToPrevious();
  649. assertEquals(4, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  650. assertEquals(key4, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  651. assertEquals(value4, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  652. mCursor.close();
  653. try {
  654. mContentResolver.bulkInsert(null, cvs);
  655. fail("did not throw NullPointerException when uri is null.");
  656. } catch (NullPointerException e) {
  657. //expected.
  658. }
  659. }
  660. public void testDelete() {
  661. mCursor = mContentResolver.query(TABLE1_URI, null, null, null, null);
  662. assertNotNull(mCursor);
  663. assertEquals(3, mCursor.getCount());
  664. mCursor.close();
  665. assertEquals(3, mContentResolver.delete(TABLE1_URI, null, null));
  666. mCursor = mContentResolver.query(TABLE1_URI, null, null, null, null);
  667. assertNotNull(mCursor);
  668. assertEquals(0, mCursor.getCount());
  669. mCursor.close();
  670. // add three rows to database.
  671. ContentValues values = new ContentValues();
  672. values.put(COLUMN_KEY_NAME, KEY1);
  673. values.put(COLUMN_VALUE_NAME, VALUE1);
  674. mContentResolver.insert(TABLE1_URI, values);
  675. values.put(COLUMN_KEY_NAME, KEY2);
  676. values.put(COLUMN_VALUE_NAME, VALUE2);
  677. mContentResolver.insert(TABLE1_URI, values);
  678. values.put(COLUMN_KEY_NAME, KEY3);
  679. values.put(COLUMN_VALUE_NAME, VALUE3);
  680. mContentResolver.insert(TABLE1_URI, values);
  681. // test delete row using selection
  682. String selection = COLUMN_ID_NAME + "=2";
  683. assertEquals(1, mContentResolver.delete(TABLE1_URI, selection, null));
  684. mCursor = mContentResolver.query(TABLE1_URI, null, null, null, null);
  685. assertNotNull(mCursor);
  686. assertEquals(2, mCursor.getCount());
  687. mCursor.moveToFirst();
  688. assertEquals(1, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  689. assertEquals(KEY1, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  690. assertEquals(VALUE1, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  691. mCursor.moveToNext();
  692. assertEquals(3, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  693. assertEquals(KEY3, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  694. assertEquals(VALUE3, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  695. mCursor.close();
  696. selection = COLUMN_VALUE_NAME + "=3";
  697. assertEquals(1, mContentResolver.delete(TABLE1_URI, selection, null));
  698. mCursor = mContentResolver.query(TABLE1_URI, null, null, null, null);
  699. assertNotNull(mCursor);
  700. assertEquals(1, mCursor.getCount());
  701. mCursor.moveToFirst();
  702. assertEquals(1, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  703. assertEquals(KEY1, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  704. assertEquals(VALUE1, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  705. mCursor.close();
  706. selection = COLUMN_KEY_NAME + "=\"" + KEY1 + "\"";
  707. assertEquals(1, mContentResolver.delete(TABLE1_URI, selection, null));
  708. mCursor = mContentResolver.query(TABLE1_URI, null, null, null, null);
  709. assertNotNull(mCursor);
  710. assertEquals(0, mCursor.getCount());
  711. mCursor.close();
  712. try {
  713. mContentResolver.delete(null, null, null);
  714. fail("did not throw NullPointerException when uri is null.");
  715. } catch (NullPointerException e) {
  716. //expected.
  717. }
  718. }
  719. public void testUpdate() {
  720. ContentValues values = new ContentValues();
  721. String key10 = "key10";
  722. String key20 = "key20";
  723. int value10 = 10;
  724. int value20 = 20;
  725. values.put(COLUMN_KEY_NAME, key10);
  726. values.put(COLUMN_VALUE_NAME, value10);
  727. // test update all the rows.
  728. assertEquals(3, mContentResolver.update(TABLE1_URI, values, null, null));
  729. mCursor = mContentResolver.query(TABLE1_URI, null, null, null, null);
  730. assertNotNull(mCursor);
  731. assertEquals(3, mCursor.getCount());
  732. mCursor.moveToFirst();
  733. assertEquals(1, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  734. assertEquals(key10, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  735. assertEquals(value10, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  736. mCursor.moveToNext();
  737. assertEquals(2, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  738. assertEquals(key10, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  739. assertEquals(value10, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  740. mCursor.moveToLast();
  741. assertEquals(3, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  742. assertEquals(key10, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  743. assertEquals(value10, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  744. mCursor.close();
  745. // test update one row using selection.
  746. String selection = COLUMN_ID_NAME + "=1";
  747. values.put(COLUMN_KEY_NAME, key20);
  748. values.put(COLUMN_VALUE_NAME, value20);
  749. assertEquals(1, mContentResolver.update(TABLE1_URI, values, selection, null));
  750. mCursor = mContentResolver.query(TABLE1_URI, null, null, null, null);
  751. assertNotNull(mCursor);
  752. assertEquals(3, mCursor.getCount());
  753. mCursor.moveToFirst();
  754. assertEquals(1, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  755. assertEquals(key20, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  756. assertEquals(value20, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  757. mCursor.moveToNext();
  758. assertEquals(2, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  759. assertEquals(key10, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  760. assertEquals(value10, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  761. mCursor.moveToLast();
  762. assertEquals(3, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_ID_NAME)));
  763. assertEquals(key10, mCursor.getString(mCursor.getColumnIndexOrThrow(COLUMN_KEY_NAME)));
  764. assertEquals(value10, mCursor.getInt(mCursor.getColumnIndexOrThrow(COLUMN_VALUE_NAME)));
  765. mCursor.close();
  766. try {
  767. mContentResolver.update(null, values, null, null);
  768. fail("did not throw NullPointerException when uri is null.");
  769. } catch (NullPointerException e) {
  770. //expected.
  771. }
  772. // javadoc says it will throw NullPointerException when values are null,
  773. // but actually, it throws IllegalArgumentException here.
  774. try {
  775. mContentResolver.update(TABLE1_URI, null, null, null);
  776. fail("did not throw IllegalArgumentException when values are null.");
  777. } catch (IllegalArgumentException e) {
  778. //expected.
  779. }
  780. }
  781. public void testRegisterContentObserver() {
  782. final MockContentObserver mco = new MockContentObserver();
  783. mContentResolver.registerContentObserver(TABLE1_URI, true, mco);
  784. assertFalse(mco.hadOnChanged());
  785. ContentValues values = new ContentValues();
  786. values.put(COLUMN_KEY_NAME, "key10");
  787. values.put(COLUMN_VALUE_NAME, 10);
  788. mContentResolver.update(TABLE1_URI, values, null, null);
  789. new PollingCheck() {
  790. @Override
  791. protected boolean check() {
  792. return mco.hadOnChanged();
  793. }
  794. }.run();
  795. mco.reset();
  796. mContentResolver.unregisterContentObserver(mco);
  797. assertFalse(mco.hadOnChanged());
  798. mContentResolver.update(TABLE1_URI, values, null, null);
  799. assertFalse(mco.hadOnChanged());
  800. try {
  801. mContentResolver.registerContentObserver(null, false, mco);
  802. fail("did not throw NullPointerException or IllegalArgumentException when uri is null.");
  803. } catch (NullPointerException e) {
  804. //expected.
  805. } catch (IllegalArgumentException e) {
  806. // also expected
  807. }
  808. try {
  809. mContentResolver.registerContentObserver(TABLE1_URI, false, null);
  810. fail("did not throw NullPointerException when register null content observer.");
  811. } catch (NullPointerException e) {
  812. //expected.
  813. }
  814. try {
  815. mContentResolver.unregisterContentObserver(null);
  816. fail("did not throw NullPointerException when unregister null content observer.");
  817. } catch (NullPointerException e) {
  818. //expected.
  819. }
  820. }
  821. public void testNotifyChange1() {
  822. final MockContentObserver mco = new MockContentObserver();
  823. mContentResolver.registerContentObserver(TABLE1_URI, true, mco);
  824. assertFalse(mco.hadOnChanged());
  825. mContentResolver.notifyChange(TABLE1_URI, mco);
  826. new PollingCheck() {
  827. @Override
  828. protected boolean check() {
  829. return mco.hadOnChanged();
  830. }
  831. }.run();
  832. mContentResolver.unregisterContentObserver(mco);
  833. }
  834. public void testNotifyChange2() {
  835. final MockContentObserver mco = new MockContentObserver();
  836. mContentResolver.registerContentObserver(TABLE1_URI, true, mco);
  837. assertFalse(mco.hadOnChanged());
  838. mContentResolver.notifyChange(TABLE1_URI, mco, false);
  839. new PollingCheck() {
  840. @Override
  841. protected boolean check() {
  842. return mco.hadOnChanged();
  843. }
  844. }.run();
  845. mContentResolver.unregisterContentObserver(mco);
  846. }
  847. public void testStartCancelSync() {
  848. Bundle extras = new Bundle();
  849. extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
  850. ContentResolver.requestSync(ACCOUNT, AUTHORITY, extras);
  851. //FIXME: how to get the result to assert.
  852. ContentResolver.cancelSync(ACCOUNT, AUTHORITY);
  853. //FIXME: how to assert.
  854. }
  855. public void testStartSyncFailure() {
  856. try {
  857. ContentResolver.requestSync(null, null, null);
  858. fail("did not throw IllegalArgumentException when extras is null.");
  859. } catch (IllegalArgumentException e) {
  860. //expected.
  861. }
  862. }
  863. public void testValidateSyncExtrasBundle() {
  864. Bundle extras = new Bundle();
  865. extras.putInt("Integer", 20);
  866. extras.putLong("Long", 10l);
  867. extras.putBoolean("Boolean", true);
  868. extras.putFloat("Float", 5.5f);
  869. extras.putDouble("Double", 2.5);
  870. extras.putString("String", "cts");
  871. extras.putCharSequence("CharSequence", null);
  872. ContentResolver.validateSyncExtrasBundle(extras);
  873. extras.putChar("Char", 'a'); // type Char is invalid
  874. try {
  875. ContentResolver.validateSyncExtrasBundle(extras);
  876. fail("did not throw IllegalArgumentException when extras is invalide.");
  877. } catch (IllegalArgumentException e) {
  878. //expected.
  879. }
  880. }
  881. private class MockContentObserver extends ContentObserver {
  882. private boolean mHadOnChanged = false;
  883. public MockContentObserver() {
  884. super(null);
  885. }
  886. @Override
  887. public boolean deliverSelfNotifications() {
  888. return true;
  889. }
  890. @Override
  891. public synchronized void onChange(boolean selfChange) {
  892. super.onChange(selfChange);
  893. mHadOnChanged = true;
  894. }
  895. public synchronized boolean hadOnChanged() {
  896. return mHadOnChanged;
  897. }
  898. public synchronized void reset() {
  899. mHadOnChanged = false;
  900. }
  901. }
  902. }