1/*
2* Copyright (C) 2014 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
18package android.animation;
19
20import android.test.ActivityInstrumentationTestCase2;
21import android.test.UiThreadTest;
22import android.util.StateSet;
23import android.view.View;
24import android.view.ViewGroup;
25
26import com.android.frameworks.coretests.R;
27
28import java.util.concurrent.atomic.AtomicInteger;
29
30
31public class StateListAnimatorTest extends ActivityInstrumentationTestCase2<BasicAnimatorActivity> {
32
33    public StateListAnimatorTest() {
34        super(BasicAnimatorActivity.class);
35    }
36
37    @Override
38    protected void setUp() throws Exception {
39        super.setUp();
40    }
41
42    public void testInflateFromAnimator() throws Exception {
43        StateListAnimator stateListAnimator = AnimatorInflater
44                .loadStateListAnimator(getActivity(), R.anim.test_state_anim);
45        assertNotNull("A state list animator should be returned", stateListAnimator);
46        assertEquals("State list animator should have three items", 3,
47                stateListAnimator.getTuples().size());
48    }
49
50    @UiThreadTest
51    public void testAttachDetach() throws Exception {
52        View view = new View(getActivity());
53        final AtomicInteger setStateCount = new AtomicInteger(0);
54        StateListAnimator stateListAnimator = new StateListAnimator() {
55            @Override
56            public void setState(int[] state) {
57                setStateCount.incrementAndGet();
58                super.setState(state);
59            }
60        };
61        view.setStateListAnimator(stateListAnimator);
62        assertNotNull("State list animator should have a reference to view even if it is detached",
63                stateListAnimator.getTarget());
64        ViewGroup viewGroup = (ViewGroup) getActivity().findViewById(android.R.id.content);
65        int preSetStateCount = setStateCount.get();
66        viewGroup.addView(view);
67        assertTrue("When view is attached, state list drawable's setState should be called",
68                preSetStateCount < setStateCount.get());
69
70        StateListAnimator stateListAnimator2 = new StateListAnimator();
71        view.setStateListAnimator(stateListAnimator2);
72        assertNull("When a new state list animator is assigned, previous one should be detached",
73                stateListAnimator.getTarget());
74        assertNull("Any running animator should be removed on detach",
75                stateListAnimator.getRunningAnimator());
76        assertEquals("The new state list animator should be attached to the view",
77                view, stateListAnimator2.getTarget());
78        viewGroup.removeView(view);
79        assertNotNull("When view is detached from window, state list animator should still keep the"
80                        + " reference",
81                stateListAnimator2.getTarget());
82    }
83
84    public void testStateListLoading() throws InterruptedException {
85        StateListAnimator stateListAnimator = AnimatorInflater
86                .loadStateListAnimator(getActivity(), R.anim.test_state_anim);
87        assertNotNull("A state list animator should be returned", stateListAnimator);
88        assertEquals("Steate list animator should have two items", 3,
89                stateListAnimator.getTuples().size());
90        StateListAnimator.Tuple tuple1 = stateListAnimator.getTuples().get(0);
91        assertEquals("first tuple should have one state", 1, tuple1.getSpecs().length);
92        assertEquals("first spec in tuple 1 should be pressed",
93                com.android.internal.R.attr.state_pressed, tuple1.getSpecs()[0]);
94
95        StateListAnimator.Tuple tuple2 = stateListAnimator.getTuples().get(1);
96        assertEquals("Second tuple should have two specs", 2, tuple2.getSpecs().length);
97        assertTrue("Tuple two should match the expected state",
98                StateSet.stateSetMatches(tuple2.getSpecs(),
99                new int[]{-com.android.internal.R.attr.state_pressed,
100                com.android.internal.R.attr.state_enabled}));
101    }
102}
103