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 
24 public class AbsSavedStateTest extends InstrumentationTestCase {
25 
26     // constant for test of writeToParcel
27     public static final int TEST_NUMBER = 1;
28 
testConstructor()29     public void testConstructor() {
30         MockParcelable superState = new MockParcelable();
31         assertNotNull(superState);
32         new MockAbsSavedState(superState);
33 
34         Parcel source = Parcel.obtain();
35         new MockAbsSavedState(source);
36 
37         MockAbsSavedState savedState = new MockAbsSavedState(source);
38         assertEquals(0, savedState.describeContents());
39     }
40 
testGetSuperState()41     public void testGetSuperState() {
42         MockParcelable superState = new MockParcelable();
43         assertNotNull(superState);
44         MockAbsSavedState savedState = new MockAbsSavedState(superState);
45 
46         assertSame(superState, savedState.getSuperState());
47     }
48 
testWriteToParcel()49     public void testWriteToParcel() {
50         MockParcelable superState = new MockParcelable();
51         assertNotNull(superState);
52         MockAbsSavedState savedState = new MockAbsSavedState(superState);
53 
54         Parcel dest = Parcel.obtain();
55         int flags = 2;
56         savedState.writeToParcel(dest, flags);
57 
58         // we instantiate the writeToParcel of Parcalable
59         // and give a return for test
60         assertEquals(TEST_NUMBER, superState.writeToParcelRunSymbol());
61         assertEquals(flags, superState.getFlags());
62     }
63 
64     static class MockAbsSavedState extends AbsSavedState {
65 
MockAbsSavedState(Parcelable superState)66         public MockAbsSavedState(Parcelable superState) {
67             super(superState);
68         }
69 
MockAbsSavedState(Parcel source)70         public MockAbsSavedState(Parcel source) {
71             super(source);
72         }
73     }
74 
75     static class MockParcelable implements Parcelable {
76 
77         // Test for writeToParcel
78         private int mTest;
79         private int mFlags;
80 
describeContents()81         public int describeContents() {
82             return 0;
83         }
84 
85         // Instantiate writeToParcel
writeToParcel(Parcel dest, int flags)86         public void writeToParcel(Parcel dest, int flags) {
87             mTest = TEST_NUMBER;
88             mFlags = flags;
89         }
90 
91         // For test of writeToParcel
writeToParcelRunSymbol()92         public int writeToParcelRunSymbol() {
93             return mTest;
94         }
95 
getFlags()96         public int getFlags() {
97             return mFlags;
98         }
99     }
100 }
101