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.view.cts; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 import android.test.InstrumentationTestCase; 22 import android.view.AbsSavedState; 23 import dalvik.annotation.TestTargets; 24 import dalvik.annotation.TestLevel; 25 import dalvik.annotation.TestTargetNew; 26 import dalvik.annotation.TestTargetClass; 27 28 @TestTargetClass(AbsSavedState.class) 29 public class AbsSavedStateTest extends InstrumentationTestCase { 30 31 // constant for test of writeToParcel 32 public static final int TEST_NUMBER = 1; 33 34 @TestTargets({ 35 @TestTargetNew( 36 level = TestLevel.COMPLETE, 37 notes = "Test constructor and describeContents of AbsSavedState", 38 method = "AbsSavedState", 39 args = {android.os.Parcelable.class} 40 ), 41 @TestTargetNew( 42 level = TestLevel.COMPLETE, 43 notes = "Test constructor and describeContents of AbsSavedState", 44 method = "AbsSavedState", 45 args = {android.os.Parcel.class} 46 ), 47 @TestTargetNew( 48 level = TestLevel.COMPLETE, 49 notes = "Test constructor and describeContents of AbsSavedState", 50 method = "describeContents", 51 args = {} 52 ) 53 }) testConstructor()54 public void testConstructor() { 55 MockParcelable superState = new MockParcelable(); 56 assertNotNull(superState); 57 new MockAbsSavedState(superState); 58 59 Parcel source = Parcel.obtain(); 60 new MockAbsSavedState(source); 61 62 MockAbsSavedState savedState = new MockAbsSavedState(source); 63 assertEquals(0, savedState.describeContents()); 64 } 65 66 @TestTargetNew( 67 level = TestLevel.COMPLETE, 68 notes = "Test getSuperState function", 69 method = "getSuperState", 70 args = {} 71 ) testGetSuperState()72 public void testGetSuperState() { 73 MockParcelable superState = new MockParcelable(); 74 assertNotNull(superState); 75 MockAbsSavedState savedState = new MockAbsSavedState(superState); 76 77 assertSame(superState, savedState.getSuperState()); 78 } 79 80 @TestTargetNew( 81 level = TestLevel.COMPLETE, 82 notes = "Test writeToParcel function", 83 method = "writeToParcel", 84 args = {android.os.Parcel.class, int.class} 85 ) testWriteToParcel()86 public void testWriteToParcel() { 87 MockParcelable superState = new MockParcelable(); 88 assertNotNull(superState); 89 MockAbsSavedState savedState = new MockAbsSavedState(superState); 90 91 Parcel dest = Parcel.obtain(); 92 int flags = 2; 93 savedState.writeToParcel(dest, flags); 94 95 // we instantiate the writeToParcel of Parcalable 96 // and give a return for test 97 assertEquals(TEST_NUMBER, superState.writeToParcelRunSymbol()); 98 assertEquals(flags, superState.getFlags()); 99 } 100 101 static class MockAbsSavedState extends AbsSavedState { 102 MockAbsSavedState(Parcelable superState)103 public MockAbsSavedState(Parcelable superState) { 104 super(superState); 105 } 106 MockAbsSavedState(Parcel source)107 public MockAbsSavedState(Parcel source) { 108 super(source); 109 } 110 } 111 112 static class MockParcelable implements Parcelable { 113 114 // Test for writeToParcel 115 private int mTest; 116 private int mFlags; 117 describeContents()118 public int describeContents() { 119 return 0; 120 } 121 122 // Instantiate writeToParcel writeToParcel(Parcel dest, int flags)123 public void writeToParcel(Parcel dest, int flags) { 124 mTest = TEST_NUMBER; 125 mFlags = flags; 126 } 127 128 // For test of writeToParcel writeToParcelRunSymbol()129 public int writeToParcelRunSymbol() { 130 return mTest; 131 } 132 getFlags()133 public int getFlags() { 134 return mFlags; 135 } 136 } 137 } 138