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

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

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
Java | 373 lines | 274 code | 57 blank | 42 comment | 6 complexity | a5ace7b0f615e3907d3ae01c9b249beb MD5 | raw file
  1. /*
  2. * Copyright (C) 2009 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 android.content.ContentQueryMap;
  18. import android.content.ContentResolver;
  19. import android.content.ContentValues;
  20. import android.database.Cursor;
  21. import android.os.Handler;
  22. import android.os.HandlerThread;
  23. import android.os.Looper;
  24. import android.test.AndroidTestCase;
  25. import java.util.Map;
  26. import java.util.Observable;
  27. import java.util.Observer;
  28. /**
  29. * Test {@link ContentQueryMap}.
  30. */
  31. public class ContentQueryMapTest extends AndroidTestCase {
  32. private static final int TEST_TIME_OUT = 5000;
  33. private static final String NAME0 = "name0";
  34. private static final String VALUE0 = "value0";
  35. private static final String NAME1 = "name1";
  36. private static final String VALUE1 = "value1";
  37. private static final String NAME2 = "name2";
  38. private static final String VALUE2 = "value2";
  39. private static final String NAME3 = "name3";
  40. private static final String VALUE3 = "value3";
  41. private static final String[] PROJECTIONS = new String[] {
  42. DummyProvider.NAME, DummyProvider.VALUE};
  43. private static final int ORIGINAL_ROW_COUNT = 2;
  44. private ContentResolver mResolver;
  45. private Cursor mCursor;
  46. private ContentQueryMap mContentQueryMap;
  47. @Override
  48. protected void setUp() throws Exception {
  49. super.setUp();
  50. mResolver = mContext.getContentResolver();
  51. ContentValues values0 = new ContentValues();
  52. values0.put(DummyProvider.NAME, NAME0);
  53. values0.put(DummyProvider.VALUE, VALUE0);
  54. mResolver.insert(DummyProvider.CONTENT_URI, values0);
  55. ContentValues values1 = new ContentValues();
  56. values1.put(DummyProvider.NAME, NAME1);
  57. values1.put(DummyProvider.VALUE, VALUE1);
  58. mResolver.insert(DummyProvider.CONTENT_URI, values1);
  59. mCursor = mResolver.query(DummyProvider.CONTENT_URI, PROJECTIONS, null, null, null);
  60. assertNotNull(mCursor);
  61. }
  62. @Override
  63. protected void tearDown() throws Exception {
  64. if (mContentQueryMap != null) {
  65. mContentQueryMap.close();
  66. mContentQueryMap = null;
  67. }
  68. if (mCursor != null) {
  69. mCursor.close();
  70. mCursor = null;
  71. }
  72. mResolver.delete(DummyProvider.CONTENT_URI, null, null);
  73. super.tearDown();
  74. }
  75. public void testConstructor() {
  76. new ContentQueryMap(mCursor, DummyProvider.NAME, true, null);
  77. new ContentQueryMap(mCursor, DummyProvider.VALUE, false, new Handler());
  78. try {
  79. new ContentQueryMap(mCursor, null, false, new Handler());
  80. fail("Should throw NullPointerException if param columnNameOfKey is null");
  81. } catch (NullPointerException e) {
  82. }
  83. try {
  84. new ContentQueryMap(null, DummyProvider.NAME, false, new Handler());
  85. fail("Should throw NullPointerException if param cursor is null");
  86. } catch (NullPointerException e) {
  87. }
  88. }
  89. public void testGetRows() {
  90. // handler can be null
  91. mContentQueryMap = new ContentQueryMap(mCursor, DummyProvider.NAME, true, null);
  92. Map<String, ContentValues> rows = mContentQueryMap.getRows();
  93. assertEquals(ORIGINAL_ROW_COUNT, rows.size());
  94. assertTrue(rows.containsKey(NAME0));
  95. assertEquals(VALUE0, rows.get(NAME0).getAsString(DummyProvider.VALUE));
  96. assertTrue(rows.containsKey(NAME1));
  97. assertEquals(VALUE1, rows.get(NAME1).getAsString(DummyProvider.VALUE));
  98. mContentQueryMap.close();
  99. // the mCursor has been close
  100. mContentQueryMap = new ContentQueryMap(mCursor, DummyProvider.NAME, false, new Handler());
  101. rows = mContentQueryMap.getRows();
  102. assertFalse(rows.containsKey(NAME0));
  103. mContentQueryMap.requery();
  104. rows = mContentQueryMap.getRows();
  105. assertFalse(rows.containsKey(NAME0));
  106. }
  107. public void testRequery() {
  108. // Disable the keepUpdated to make sure requery() will not be called
  109. // from somewhere else
  110. mContentQueryMap = new ContentQueryMap(mCursor, DummyProvider.NAME, false, null);
  111. ContentValues contentValues = mContentQueryMap.getValues(NAME0);
  112. assertNotNull(contentValues);
  113. assertEquals(VALUE0, contentValues.getAsString(DummyProvider.VALUE));
  114. contentValues = mContentQueryMap.getValues(NAME1);
  115. assertNotNull(contentValues);
  116. assertEquals(VALUE1, contentValues.getAsString(DummyProvider.VALUE));
  117. contentValues = mContentQueryMap.getValues(NAME2);
  118. assertNull(contentValues);
  119. // update NAME0 and VALUE0
  120. ContentValues values = new ContentValues();
  121. values.put(DummyProvider.NAME, NAME2);
  122. values.put(DummyProvider.VALUE, VALUE2);
  123. mResolver.update(DummyProvider.CONTENT_URI, values,
  124. DummyProvider.NAME + " = '" + NAME0 + "'", null);
  125. mContentQueryMap.requery();
  126. contentValues = mContentQueryMap.getValues(NAME0);
  127. assertNull(contentValues);
  128. contentValues = mContentQueryMap.getValues(NAME1);
  129. assertNotNull(contentValues);
  130. assertEquals(VALUE1, contentValues.getAsString(DummyProvider.VALUE));
  131. contentValues = mContentQueryMap.getValues(NAME2);
  132. assertNotNull(contentValues);
  133. assertEquals(VALUE2, contentValues.getAsString(DummyProvider.VALUE));
  134. }
  135. public void testSetKeepUpdated() throws InterruptedException {
  136. MockObserver observer = new MockObserver();
  137. // keepUpdated is false
  138. mContentQueryMap = new ContentQueryMap(mCursor, DummyProvider.NAME, false, null);
  139. mContentQueryMap.addObserver(observer);
  140. assertFalse(observer.hadUpdated(0));
  141. ContentValues contentValues = mContentQueryMap.getValues(NAME0);
  142. assertNotNull(contentValues);
  143. assertEquals(VALUE0, contentValues.getAsString(DummyProvider.VALUE));
  144. contentValues = mContentQueryMap.getValues(NAME2);
  145. assertNull(contentValues);
  146. // update NAME0 and VALUE0
  147. ContentValues values = new ContentValues();
  148. values.put(DummyProvider.NAME, NAME2);
  149. values.put(DummyProvider.VALUE, VALUE2);
  150. mResolver.update(DummyProvider.CONTENT_URI, values,
  151. DummyProvider.NAME + " = '" + NAME0 + "'", null);
  152. // values have not been updated
  153. assertFalse(observer.hadUpdated(0));
  154. contentValues = mContentQueryMap.getValues(NAME0);
  155. assertNotNull(contentValues);
  156. assertEquals(VALUE0, contentValues.getAsString(DummyProvider.VALUE));
  157. contentValues = mContentQueryMap.getValues(NAME2);
  158. assertNull(contentValues);
  159. // have to update manually
  160. mContentQueryMap.requery();
  161. assertTrue(observer.hadUpdated(0));
  162. assertSame(mContentQueryMap, observer.getObservable());
  163. contentValues = mContentQueryMap.getValues(NAME0);
  164. assertNull(contentValues);
  165. contentValues = mContentQueryMap.getValues(NAME2);
  166. assertNotNull(contentValues);
  167. assertEquals(VALUE2, contentValues.getAsString(DummyProvider.VALUE));
  168. observer.reset();
  169. contentValues = mContentQueryMap.getValues(NAME3);
  170. assertNull(contentValues);
  171. new Thread(new Runnable() {
  172. public void run() {
  173. Looper.prepare();
  174. mContentQueryMap.setKeepUpdated(true);
  175. synchronized (ContentQueryMapTest.this) {
  176. //listener is ready, release the sender thread
  177. ContentQueryMapTest.this.notify();
  178. }
  179. Looper.loop();
  180. }
  181. }).start();
  182. synchronized (this) {
  183. wait(TEST_TIME_OUT);
  184. }//wait the listener to be ready before launching onChange event
  185. // insert NAME3 and VALUE3
  186. values = new ContentValues();
  187. values.put(DummyProvider.NAME, NAME3);
  188. values.put(DummyProvider.VALUE, VALUE3);
  189. mResolver.insert(DummyProvider.CONTENT_URI, values);
  190. // should be updated automatically
  191. assertTrue(observer.hadUpdated(TEST_TIME_OUT));
  192. assertSame(mContentQueryMap, observer.getObservable());
  193. contentValues = mContentQueryMap.getValues(NAME3);
  194. assertNotNull(contentValues);
  195. assertEquals(VALUE3, contentValues.getAsString(DummyProvider.VALUE));
  196. observer.reset();
  197. new Thread(new Runnable() {
  198. public void run() {
  199. Looper.prepare();
  200. mContentQueryMap.setKeepUpdated(false);
  201. synchronized (ContentQueryMapTest.this) {
  202. //listener is ready, release the sender thread
  203. ContentQueryMapTest.this.notify();
  204. }
  205. Looper.loop();
  206. }
  207. }).start();
  208. synchronized (this) {
  209. wait(TEST_TIME_OUT);
  210. }//wait the listener to be ready before launching onChange event
  211. // update NAME3 and VALUE3
  212. values = new ContentValues();
  213. values.put(DummyProvider.NAME, NAME0);
  214. values.put(DummyProvider.VALUE, VALUE0);
  215. mResolver.update(DummyProvider.CONTENT_URI, values,
  216. DummyProvider.NAME + " = '" + NAME3 + "'", null);
  217. // values have not been updated
  218. assertFalse(observer.hadUpdated(TEST_TIME_OUT));
  219. contentValues = mContentQueryMap.getValues(NAME3);
  220. assertNotNull(contentValues);
  221. assertEquals(VALUE3, contentValues.getAsString(DummyProvider.VALUE));
  222. }
  223. public void testSetKeepUpdatedWithHandler() throws InterruptedException {
  224. MockObserver observer = new MockObserver();
  225. HandlerThread thread = new HandlerThread("testSetKeepUpdatedWithHandler");
  226. thread.start();
  227. Handler handler = new Handler(thread.getLooper());
  228. // keepUpdated is false
  229. mContentQueryMap = new ContentQueryMap(mCursor, DummyProvider.NAME, false, handler);
  230. mContentQueryMap.addObserver(observer);
  231. assertFalse(observer.hadUpdated(0));
  232. ContentValues contentValues = mContentQueryMap.getValues(NAME0);
  233. assertNotNull(contentValues);
  234. assertEquals(VALUE0, contentValues.getAsString(DummyProvider.VALUE));
  235. contentValues = mContentQueryMap.getValues(NAME2);
  236. assertNull(contentValues);
  237. // update NAME0 and VALUE0
  238. ContentValues values = new ContentValues();
  239. values.put(DummyProvider.NAME, NAME2);
  240. values.put(DummyProvider.VALUE, VALUE2);
  241. mResolver.update(DummyProvider.CONTENT_URI, values,
  242. DummyProvider.NAME + " = '" + NAME0 + "'", null);
  243. // values have not been updated
  244. assertFalse(observer.hadUpdated(TEST_TIME_OUT));
  245. contentValues = mContentQueryMap.getValues(NAME0);
  246. assertNotNull(contentValues);
  247. assertEquals(VALUE0, contentValues.getAsString(DummyProvider.VALUE));
  248. contentValues = mContentQueryMap.getValues(NAME2);
  249. assertNull(contentValues);
  250. // have to update manually
  251. mContentQueryMap.requery();
  252. assertTrue(observer.hadUpdated(0));
  253. assertSame(mContentQueryMap, observer.getObservable());
  254. contentValues = mContentQueryMap.getValues(NAME0);
  255. assertNull(contentValues);
  256. contentValues = mContentQueryMap.getValues(NAME2);
  257. assertNotNull(contentValues);
  258. assertEquals(VALUE2, contentValues.getAsString(DummyProvider.VALUE));
  259. observer.reset();
  260. contentValues = mContentQueryMap.getValues(NAME3);
  261. assertNull(contentValues);
  262. mContentQueryMap.setKeepUpdated(true);
  263. // insert NAME3 and VALUE3
  264. values = new ContentValues();
  265. values.put(DummyProvider.NAME, NAME3);
  266. values.put(DummyProvider.VALUE, VALUE3);
  267. mResolver.insert(DummyProvider.CONTENT_URI, values);
  268. // should be updated automatically
  269. assertTrue(observer.hadUpdated(TEST_TIME_OUT));
  270. assertSame(mContentQueryMap, observer.getObservable());
  271. contentValues = mContentQueryMap.getValues(NAME3);
  272. assertNotNull(contentValues);
  273. assertEquals(VALUE3, contentValues.getAsString(DummyProvider.VALUE));
  274. observer.reset();
  275. mContentQueryMap.setKeepUpdated(false);
  276. // update NAME3 and VALUE3
  277. values = new ContentValues();
  278. values.put(DummyProvider.NAME, NAME0);
  279. values.put(DummyProvider.VALUE, VALUE0);
  280. mResolver.update(DummyProvider.CONTENT_URI, values,
  281. DummyProvider.NAME + " = '" + NAME3 + "'", null);
  282. // values have not been updated
  283. assertFalse(observer.hadUpdated(TEST_TIME_OUT));
  284. contentValues = mContentQueryMap.getValues(NAME3);
  285. assertNotNull(contentValues);
  286. assertEquals(VALUE3, contentValues.getAsString(DummyProvider.VALUE));
  287. }
  288. public void testGetValuesBoundary() {
  289. mContentQueryMap = new ContentQueryMap(mCursor, DummyProvider.NAME, false, null);
  290. assertNull(mContentQueryMap.getValues(null));
  291. assertNull(mContentQueryMap.getValues(""));
  292. }
  293. private static final class MockObserver implements Observer {
  294. private boolean mHadUpdated = false;
  295. private Observable mObservable;
  296. public void reset() {
  297. mHadUpdated = false;
  298. mObservable = null;
  299. }
  300. public synchronized void update(Observable observable, Object data) {
  301. mObservable = observable;
  302. mHadUpdated = true;
  303. notify();
  304. }
  305. public Observable getObservable() {
  306. return mObservable;
  307. }
  308. public synchronized boolean hadUpdated(long timeout) throws InterruptedException {
  309. // do not wait if timeout is 0
  310. if (timeout > 0 && !mHadUpdated) {
  311. wait(timeout);
  312. }
  313. return mHadUpdated;
  314. }
  315. }
  316. }