PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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