• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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 com.android.unit_tests;
18 
19 import junit.framework.TestCase;
20 import android.test.suitebuilder.annotation.MediumTest;
21 
22 
23 public class InstanceofTest extends TestCase {
24 
25     protected A mA;
26     protected ChildOfAOne mOne;
27     protected ChildOfAOne mTwo;
28     protected ChildOfAOne mThree;
29     protected ChildOfAOne mFour;
30     protected ChildOfAFive mFive;
31 
32     @Override
setUp()33     protected void setUp() throws Exception {
34         super.setUp();
35 
36         mA = new A();
37         mOne = new ChildOfAOne();
38         mTwo = new ChildOfATwo();
39         mThree = new ChildOfAThree();
40         mFour = new ChildOfAFour();
41         mFive = new ChildOfAFive();
42     }
43 
44 
45     @MediumTest
testNoInterface()46     public void testNoInterface() throws Exception {
47         A a = mA;
48         for (int i = 0; i < 100000; i++) {
49             assertFalse("m_a should not be a ChildOfAFive", a instanceof ChildOfAFive);
50         }
51     }
52 
53     @MediumTest
testDerivedOne()54     public void testDerivedOne() throws Exception {
55         InterfaceOne one = mOne;
56         for (int i = 0; i < 100000; i++) {
57             assertFalse("m_one should not be a ChildOfAFive", one instanceof ChildOfAFive);
58         }
59     }
60 
61     @MediumTest
testDerivedTwo()62     public void testDerivedTwo() throws Exception {
63         InterfaceTwo two = mTwo;
64         for (int i = 0; i < 100000; i++) {
65             assertFalse("m_two should not be a ChildOfAFive", two instanceof ChildOfAFive);
66         }
67     }
68 
69     @MediumTest
testDerivedThree()70     public void testDerivedThree() throws Exception {
71         InterfaceThree three = mThree;
72         for (int i = 0; i < 100000; i++) {
73             assertFalse("m_three should not be a ChildOfAFive", three instanceof ChildOfAFive);
74         }
75     }
76 
77     @MediumTest
testDerivedFour()78     public void testDerivedFour() throws Exception {
79         InterfaceFour four = mFour;
80         for (int i = 0; i < 100000; i++) {
81             assertFalse("m_four should not be a ChildOfAFive", four instanceof ChildOfAFive);
82         }
83     }
84 
85     @MediumTest
testSuccessClass()86     public void testSuccessClass() throws Exception {
87         ChildOfAOne five = mFive;
88         for (int i = 0; i < 100000; i++) {
89             assertTrue("m_five is suppose to be a ChildOfAFive", five instanceof ChildOfAFive);
90         }
91     }
92 
93     @MediumTest
testSuccessInterface()94     public void testSuccessInterface() throws Exception {
95         ChildOfAFive five = mFive;
96         for (int i = 0; i < 100000; i++) {
97             assertTrue("m_five is suppose to be a InterfaceFour", five instanceof InterfaceFour);
98         }
99     }
100 
101     @MediumTest
testFailInterface()102     public void testFailInterface() throws Exception {
103         InterfaceOne one = mFive;
104         for (int i = 0; i < 100000; i++) {
105             assertFalse("m_five does not implement InterfaceFive", one instanceof InterfaceFive);
106         }
107     }
108 
109     private interface InterfaceOne {
110     }
111 
112     private interface InterfaceTwo {
113     }
114 
115     private interface InterfaceThree {
116     }
117 
118     private interface InterfaceFour {
119     }
120 
121     private interface InterfaceFive {
122     }
123 
124     private static class A {
125     }
126 
127     private static class ChildOfAOne extends A implements InterfaceOne, InterfaceTwo, InterfaceThree, InterfaceFour {
128     }
129 
130     private static class ChildOfATwo extends ChildOfAOne {
131     }
132 
133     private static class ChildOfAThree extends ChildOfATwo {
134     }
135 
136     private static class ChildOfAFour extends ChildOfAThree {
137     }
138 
139     private static class ChildOfAFive extends ChildOfAFour {
140     }
141 }
142