summaryrefslogtreecommitdiffstats
path: root/tests/AndroidTests/src/com/android/unit_tests/InstanceofTest.java
blob: 1f82df8d389836fd8e4d94373b5c355bec187b24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.unit_tests;

import junit.framework.TestCase;
import android.test.suitebuilder.annotation.MediumTest;


public class InstanceofTest extends TestCase {

    protected A mA;
    protected ChildOfAOne mOne;
    protected ChildOfAOne mTwo;
    protected ChildOfAOne mThree;
    protected ChildOfAOne mFour;
    protected ChildOfAFive mFive;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mA = new A();
        mOne = new ChildOfAOne();
        mTwo = new ChildOfATwo();
        mThree = new ChildOfAThree();
        mFour = new ChildOfAFour();
        mFive = new ChildOfAFive();
    }


    @MediumTest
    public void testNoInterface() throws Exception {
        A a = mA;
        for (int i = 0; i < 100000; i++) {
            assertFalse("m_a should not be a ChildOfAFive", a instanceof ChildOfAFive);
        }
    }

    @MediumTest
    public void testDerivedOne() throws Exception {
        InterfaceOne one = mOne;
        for (int i = 0; i < 100000; i++) {
            assertFalse("m_one should not be a ChildOfAFive", one instanceof ChildOfAFive);
        }
    }

    @MediumTest
    public void testDerivedTwo() throws Exception {
        InterfaceTwo two = mTwo;
        for (int i = 0; i < 100000; i++) {
            assertFalse("m_two should not be a ChildOfAFive", two instanceof ChildOfAFive);
        }
    }

    @MediumTest
    public void testDerivedThree() throws Exception {
        InterfaceThree three = mThree;
        for (int i = 0; i < 100000; i++) {
            assertFalse("m_three should not be a ChildOfAFive", three instanceof ChildOfAFive);
        }
    }

    @MediumTest
    public void testDerivedFour() throws Exception {
        InterfaceFour four = mFour;
        for (int i = 0; i < 100000; i++) {
            assertFalse("m_four should not be a ChildOfAFive", four instanceof ChildOfAFive);
        }
    }

    @MediumTest
    public void testSuccessClass() throws Exception {
        ChildOfAOne five = mFive;
        for (int i = 0; i < 100000; i++) {
            assertTrue("m_five is suppose to be a ChildOfAFive", five instanceof ChildOfAFive);
        }
    }

    @MediumTest
    public void testSuccessInterface() throws Exception {
        ChildOfAFive five = mFive;
        for (int i = 0; i < 100000; i++) {
            assertTrue("m_five is suppose to be a InterfaceFour", five instanceof InterfaceFour);
        }
    }

    @MediumTest
    public void testFailInterface() throws Exception {
        InterfaceOne one = mFive;
        for (int i = 0; i < 100000; i++) {
            assertFalse("m_five does not implement InterfaceFive", one instanceof InterfaceFive);
        }
    }

    private interface InterfaceOne {
    }

    private interface InterfaceTwo {
    }

    private interface InterfaceThree {
    }

    private interface InterfaceFour {
    }

    private interface InterfaceFive {
    }

    private static class A {
    }

    private static class ChildOfAOne extends A implements InterfaceOne, InterfaceTwo, InterfaceThree, InterfaceFour {
    }

    private static class ChildOfATwo extends ChildOfAOne {
    }

    private static class ChildOfAThree extends ChildOfATwo {
    }

    private static class ChildOfAFour extends ChildOfAThree {
    }

    private static class ChildOfAFive extends ChildOfAFour {
    }
}