PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/brian0218/rk3188_rk3066_r-box_android4.4.2_sdk
Java | 156 lines | 113 code | 26 blank | 17 comment | 0 complexity | f77db880c8db55b2f70b239499760c03 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.ContentObservable;
  18. import android.database.ContentObserver;
  19. import android.net.Uri;
  20. import android.test.InstrumentationTestCase;
  21. public class ContentObservableTest extends InstrumentationTestCase {
  22. private static final Uri CONTENT_URI = Uri.parse("content://uri");
  23. ContentObservable mContentObservable;
  24. MyContentObserver mObserver;
  25. @Override
  26. protected void setUp() throws Exception {
  27. super.setUp();
  28. mContentObservable = new ContentObservable();
  29. mObserver = new MyContentObserver();
  30. }
  31. public void testNotifyChange() {
  32. mContentObservable.registerObserver(mObserver);
  33. mObserver.resetStatus();
  34. assertFalse(mObserver.hasChanged());
  35. mContentObservable.notifyChange(false);
  36. assertTrue(mObserver.hasChanged());
  37. try {
  38. mContentObservable.registerObserver(mObserver);
  39. fail("Re-registering observer did not cause exception.");
  40. } catch (IllegalStateException expected) {
  41. // expected
  42. }
  43. try {
  44. mContentObservable.registerObserver(null);
  45. fail("Registering null observer did not cause exception.");
  46. } catch (IllegalArgumentException expected) {
  47. // expected
  48. }
  49. MyContentObserver second = new MyContentObserver();
  50. mContentObservable.registerObserver(second);
  51. mContentObservable.unregisterObserver(mObserver);
  52. mObserver.resetStatus();
  53. assertFalse(mObserver.hasChanged());
  54. mContentObservable.notifyChange(false);
  55. assertFalse(mObserver.hasChanged());
  56. assertTrue(second.hasChanged());
  57. }
  58. public void testDispatchChange() {
  59. mContentObservable.registerObserver(mObserver);
  60. mObserver.resetStatus();
  61. assertFalse(mObserver.hasChanged());
  62. mContentObservable.dispatchChange(false);
  63. assertTrue(mObserver.hasChanged());
  64. assertNull(mObserver.getUri());
  65. mObserver.resetStatus();
  66. assertFalse(mObserver.hasChanged());
  67. mContentObservable.dispatchChange(true);
  68. assertFalse(mObserver.hasChanged());
  69. mObserver.setDeliverSelfNotifications(true);
  70. mContentObservable.dispatchChange(true);
  71. assertTrue(mObserver.hasChanged());
  72. assertNull(mObserver.getUri());
  73. mContentObservable.unregisterObserver(mObserver);
  74. mObserver.resetStatus();
  75. assertFalse(mObserver.hasChanged());
  76. mContentObservable.dispatchChange(false);
  77. assertFalse(mObserver.hasChanged());
  78. }
  79. public void testDispatchChangeWithUri() {
  80. mContentObservable.registerObserver(mObserver);
  81. mObserver.resetStatus();
  82. assertFalse(mObserver.hasChanged());
  83. mContentObservable.dispatchChange(false, CONTENT_URI);
  84. assertTrue(mObserver.hasChanged());
  85. assertEquals(CONTENT_URI, mObserver.getUri());
  86. mObserver.resetStatus();
  87. assertFalse(mObserver.hasChanged());
  88. mContentObservable.dispatchChange(true, CONTENT_URI);
  89. assertFalse(mObserver.hasChanged());
  90. mObserver.setDeliverSelfNotifications(true);
  91. mContentObservable.dispatchChange(true, CONTENT_URI);
  92. assertTrue(mObserver.hasChanged());
  93. assertEquals(CONTENT_URI, mObserver.getUri());
  94. mContentObservable.unregisterObserver(mObserver);
  95. mObserver.resetStatus();
  96. assertFalse(mObserver.hasChanged());
  97. mContentObservable.dispatchChange(false, CONTENT_URI);
  98. assertFalse(mObserver.hasChanged());
  99. }
  100. private static class MyContentObserver extends ContentObserver {
  101. private boolean mHasChanged = false;
  102. private boolean mDeliverSelfNotifications = false;
  103. private Uri mUri;
  104. public MyContentObserver() {
  105. super(null);
  106. }
  107. @Override
  108. public void onChange(boolean selfChange, Uri uri) {
  109. super.onChange(selfChange, uri);
  110. mHasChanged = true;
  111. mUri = uri;
  112. }
  113. public boolean deliverSelfNotifications() {
  114. return mDeliverSelfNotifications ;
  115. }
  116. protected boolean hasChanged() {
  117. return mHasChanged;
  118. }
  119. protected void resetStatus() {
  120. mHasChanged = false;
  121. mUri = null;
  122. }
  123. protected void setDeliverSelfNotifications(boolean b) {
  124. mDeliverSelfNotifications = b;
  125. }
  126. protected Uri getUri() {
  127. return mUri;
  128. }
  129. }
  130. }