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 17 package android.database.cts; 18 19 import android.database.Observable; 20 import android.test.AndroidTestCase; 21 22 public class ObservableTest extends AndroidTestCase { 23 public void testRegisterUnRegisterObserver() { 24 MockObservable observable = new MockObservable(); 25 26 // Test register a null observer object 27 try { 28 observable.registerObserver((null)); 29 fail("registerObserver should throw a IllegalArgumentException here."); 30 } catch (IllegalArgumentException e) { 31 } 32 33 Object observer = new Object(); 34 // In the beginning, Observable has no observer object, nothing to be unregistered. 35 try { 36 observable.unregisterObserver(observer); 37 fail("unregisterObserver should throw a IllegalStateException here."); 38 } catch (IllegalStateException e) { 39 } 40 41 // Test unregister a null object, the Observable will unregister nothing. 42 try { 43 observable.unregisterObserver(null); 44 fail("unregisterObserver should throw a IllegalArgumentException here."); 45 } catch (IllegalArgumentException e) { 46 } 47 48 // Test register a observer object 49 observable.registerObserver(observer); 50 // If previous registerObserver was executed successfully, the object can't be registered 51 // twice. 52 try { 53 observable.registerObserver(observer); 54 fail("registerObserver should throw a IllegalStateException here."); 55 } catch (IllegalStateException e) { 56 } 57 58 // Test unregister a observer object 59 observable.unregisterObserver(observer); 60 // If unregister function was executed successfully, the input observer object will be 61 // removed, so it can not be unregistered anymore, and it can be registered again. 62 try { 63 observable.unregisterObserver(observer); 64 fail("unregisterObserver should throw a IllegalStateException here."); 65 } catch (IllegalStateException e) { 66 } 67 observable.registerObserver(observer); 68 } 69 70 public void testUnregisterAll() { 71 MockObservable observable = new MockObservable(); 72 Object observer1 = new Object(); 73 Object observer2 = new Object(); 74 75 observable.registerObserver(observer1); 76 observable.registerObserver(observer2); 77 78 // If a observer was registered, it can't be registered again. 79 try { 80 observable.registerObserver(observer1); 81 fail("registerObserver should throw a IllegalStateException here."); 82 } catch (IllegalStateException e) { 83 } 84 try { 85 observable.registerObserver(observer2); 86 fail("registerObserver should throw a IllegalStateException here."); 87 } catch (IllegalStateException e) { 88 } 89 90 // unregisterAll will unregister all the registered observers. 91 observable.unregisterAll(); 92 // The Observable will be empty after unregisterAll, so it can register observers again. 93 observable.registerObserver(observer1); 94 observable.registerObserver(observer2); 95 } 96 97 private class MockObservable extends Observable<Object> { 98 } 99 } 100