PageRenderTime 87ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/cts/tests/tests/database/src/android/database/cts/ContentObserverTest.java

https://bitbucket.org/rlyspn/androidrr
Java | 215 lines | 164 code | 34 blank | 17 comment | 2 complexity | e0ee02c14f6bf14b559dd6080f40c0af 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.database.cts;
  17. import android.database.ContentObserver;
  18. import android.net.Uri;
  19. import android.os.Handler;
  20. import android.os.HandlerThread;
  21. import android.os.Looper;
  22. import android.test.InstrumentationTestCase;
  23. public class ContentObserverTest extends InstrumentationTestCase {
  24. private static final Uri CONTENT_URI = Uri.parse("content://uri");
  25. public void testContentObserver() throws InterruptedException {
  26. // Test constructor with null handler, dispatchChange will directly invoke onChange.
  27. MyContentObserver contentObserver;
  28. contentObserver = new MyContentObserver(null);
  29. assertFalse(contentObserver.hasChanged());
  30. assertFalse(contentObserver.getSelfChangeState());
  31. contentObserver.dispatchChange(true);
  32. assertTrue(contentObserver.hasChanged());
  33. assertTrue(contentObserver.getSelfChangeState());
  34. contentObserver.resetStatus();
  35. contentObserver.setSelfChangeState(true);
  36. assertFalse(contentObserver.hasChanged());
  37. assertTrue(contentObserver.getSelfChangeState());
  38. contentObserver.dispatchChange(false);
  39. assertTrue(contentObserver.hasChanged());
  40. assertFalse(contentObserver.getSelfChangeState());
  41. HandlerThread ht = new HandlerThread(getClass().getName());
  42. ht.start();
  43. Looper looper = ht.getLooper();
  44. Handler handler = new Handler(looper);
  45. final long timeout = 1000L;
  46. contentObserver = new MyContentObserver(handler);
  47. assertFalse(contentObserver.hasChanged());
  48. assertFalse(contentObserver.getSelfChangeState());
  49. contentObserver.dispatchChange(true);
  50. assertTrue(contentObserver.hasChanged(timeout));
  51. assertTrue(contentObserver.getSelfChangeState());
  52. contentObserver.resetStatus();
  53. contentObserver.setSelfChangeState(true);
  54. assertFalse(contentObserver.hasChanged());
  55. assertTrue(contentObserver.getSelfChangeState());
  56. contentObserver.dispatchChange(false);
  57. assertTrue(contentObserver.hasChanged(timeout));
  58. assertFalse(contentObserver.getSelfChangeState());
  59. looper.quit();
  60. }
  61. public void testContentObserverWithUri() throws InterruptedException {
  62. // Test constructor with null handler, dispatchChange will directly invoke onChange.
  63. MyContentObserverWithUri contentObserver;
  64. contentObserver = new MyContentObserverWithUri(null);
  65. assertFalse(contentObserver.hasChanged());
  66. assertFalse(contentObserver.getSelfChangeState());
  67. contentObserver.dispatchChange(true, CONTENT_URI);
  68. assertTrue(contentObserver.hasChanged());
  69. assertTrue(contentObserver.getSelfChangeState());
  70. assertEquals(CONTENT_URI, contentObserver.getUri());
  71. contentObserver.resetStatus();
  72. contentObserver.setSelfChangeState(true);
  73. assertFalse(contentObserver.hasChanged());
  74. assertTrue(contentObserver.getSelfChangeState());
  75. contentObserver.dispatchChange(false, CONTENT_URI);
  76. assertTrue(contentObserver.hasChanged());
  77. assertFalse(contentObserver.getSelfChangeState());
  78. assertEquals(CONTENT_URI, contentObserver.getUri());
  79. HandlerThread ht = new HandlerThread(getClass().getName());
  80. ht.start();
  81. Looper looper = ht.getLooper();
  82. Handler handler = new Handler(looper);
  83. final long timeout = 1000L;
  84. contentObserver = new MyContentObserverWithUri(handler);
  85. assertFalse(contentObserver.hasChanged());
  86. assertFalse(contentObserver.getSelfChangeState());
  87. contentObserver.dispatchChange(true, CONTENT_URI);
  88. assertTrue(contentObserver.hasChanged(timeout));
  89. assertTrue(contentObserver.getSelfChangeState());
  90. assertEquals(CONTENT_URI, contentObserver.getUri());
  91. contentObserver.resetStatus();
  92. contentObserver.setSelfChangeState(true);
  93. assertFalse(contentObserver.hasChanged());
  94. assertTrue(contentObserver.getSelfChangeState());
  95. contentObserver.dispatchChange(false, CONTENT_URI);
  96. assertTrue(contentObserver.hasChanged(timeout));
  97. assertFalse(contentObserver.getSelfChangeState());
  98. assertEquals(CONTENT_URI, contentObserver.getUri());
  99. looper.quit();
  100. }
  101. public void testDeliverSelfNotifications() {
  102. MyContentObserver contentObserver = new MyContentObserver(null);
  103. assertFalse(contentObserver.deliverSelfNotifications());
  104. }
  105. private static class MyContentObserver extends ContentObserver {
  106. private boolean mHasChanged;
  107. private boolean mSelfChange;
  108. public MyContentObserver(Handler handler) {
  109. super(handler);
  110. }
  111. @Override
  112. public void onChange(boolean selfChange) {
  113. super.onChange(selfChange);
  114. synchronized(this) {
  115. mHasChanged = true;
  116. mSelfChange = selfChange;
  117. notifyAll();
  118. }
  119. }
  120. protected synchronized boolean hasChanged(long timeout) throws InterruptedException {
  121. if (!mHasChanged) {
  122. wait(timeout);
  123. }
  124. return mHasChanged;
  125. }
  126. protected boolean hasChanged() {
  127. return mHasChanged;
  128. }
  129. protected void resetStatus() {
  130. mHasChanged = false;
  131. mSelfChange = false;
  132. }
  133. protected boolean getSelfChangeState() {
  134. return mSelfChange;
  135. }
  136. protected void setSelfChangeState(boolean state) {
  137. mSelfChange = state;
  138. }
  139. }
  140. private static class MyContentObserverWithUri extends ContentObserver {
  141. private boolean mHasChanged;
  142. private boolean mSelfChange;
  143. private Uri mUri;
  144. public MyContentObserverWithUri(Handler handler) {
  145. super(handler);
  146. }
  147. @Override
  148. public void onChange(boolean selfChange, Uri uri) {
  149. super.onChange(selfChange, uri);
  150. synchronized(this) {
  151. mHasChanged = true;
  152. mSelfChange = selfChange;
  153. mUri = uri;
  154. notifyAll();
  155. }
  156. }
  157. protected synchronized boolean hasChanged(long timeout) throws InterruptedException {
  158. if (!mHasChanged) {
  159. wait(timeout);
  160. }
  161. return mHasChanged;
  162. }
  163. protected boolean hasChanged() {
  164. return mHasChanged;
  165. }
  166. protected void resetStatus() {
  167. mHasChanged = false;
  168. mSelfChange = false;
  169. mUri = null;
  170. }
  171. protected boolean getSelfChangeState() {
  172. return mSelfChange;
  173. }
  174. protected void setSelfChangeState(boolean state) {
  175. mSelfChange = state;
  176. }
  177. protected Uri getUri() {
  178. return mUri;
  179. }
  180. }
  181. }