1f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar/* 2f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* Copyright (C) 2014 The Android Open Source Project 3f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* 4f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* Licensed under the Apache License, Version 2.0 (the "License"); 5f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* you may not use this file except in compliance with the License. 6f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* You may obtain a copy of the License at 7f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* 8f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* http://www.apache.org/licenses/LICENSE-2.0 9f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* 10f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* Unless required by applicable law or agreed to in writing, software 11f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* distributed under the License is distributed on an "AS IS" BASIS, 12f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* See the License for the specific language governing permissions and 14f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar* limitations under the License. 15f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar*/ 16f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 17f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 18f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyarpackage android.animation; 19f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 20f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyarimport android.test.ActivityInstrumentationTestCase2; 21f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyarimport android.test.UiThreadTest; 22f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyarimport android.util.StateSet; 23f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyarimport android.view.View; 24f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyarimport android.view.ViewGroup; 25f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 26f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyarimport com.android.frameworks.coretests.R; 27f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 28f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyarimport java.util.concurrent.atomic.AtomicInteger; 29f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 30f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 31f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyarpublic class StateListAnimatorTest extends ActivityInstrumentationTestCase2<BasicAnimatorActivity> { 32f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 33f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar public StateListAnimatorTest() { 34f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar super(BasicAnimatorActivity.class); 35f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar } 36f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 37f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar @Override 38f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar protected void setUp() throws Exception { 39f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar super.setUp(); 40f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar } 41f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 42f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar public void testInflateFromAnimator() throws Exception { 43f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar StateListAnimator stateListAnimator = AnimatorInflater 44f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar .loadStateListAnimator(getActivity(), R.anim.test_state_anim); 45f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertNotNull("A state list animator should be returned", stateListAnimator); 46f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertEquals("State list animator should have three items", 3, 47f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar stateListAnimator.getTuples().size()); 48f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar } 49f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 50f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar @UiThreadTest 51f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar public void testAttachDetach() throws Exception { 52f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar View view = new View(getActivity()); 53f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar final AtomicInteger setStateCount = new AtomicInteger(0); 54f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar StateListAnimator stateListAnimator = new StateListAnimator() { 55f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar @Override 56f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar public void setState(int[] state) { 57f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar setStateCount.incrementAndGet(); 58f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar super.setState(state); 59f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar } 60f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar }; 61f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar view.setStateListAnimator(stateListAnimator); 62f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertNotNull("State list animator should have a reference to view even if it is detached", 63f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar stateListAnimator.getTarget()); 64f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar ViewGroup viewGroup = (ViewGroup) getActivity().findViewById(android.R.id.content); 65f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar int preSetStateCount = setStateCount.get(); 66f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar viewGroup.addView(view); 67f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertTrue("When view is attached, state list drawable's setState should be called", 68f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar preSetStateCount < setStateCount.get()); 69f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 70f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar StateListAnimator stateListAnimator2 = new StateListAnimator(); 71f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar view.setStateListAnimator(stateListAnimator2); 72f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertNull("When a new state list animator is assigned, previous one should be detached", 73f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar stateListAnimator.getTarget()); 74f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertNull("Any running animator should be removed on detach", 75f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar stateListAnimator.getRunningAnimator()); 76f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertEquals("The new state list animator should be attached to the view", 77f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar view, stateListAnimator2.getTarget()); 78f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar viewGroup.removeView(view); 79f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertNotNull("When view is detached from window, state list animator should still keep the" 80f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar + " reference", 81f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar stateListAnimator2.getTarget()); 82f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar } 83f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 84f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar public void testStateListLoading() throws InterruptedException { 85f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar StateListAnimator stateListAnimator = AnimatorInflater 86f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar .loadStateListAnimator(getActivity(), R.anim.test_state_anim); 87f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertNotNull("A state list animator should be returned", stateListAnimator); 88f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertEquals("Steate list animator should have two items", 3, 89f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar stateListAnimator.getTuples().size()); 90f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar StateListAnimator.Tuple tuple1 = stateListAnimator.getTuples().get(0); 91f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertEquals("first tuple should have one state", 1, tuple1.getSpecs().length); 92f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertEquals("first spec in tuple 1 should be pressed", 93f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar com.android.internal.R.attr.state_pressed, tuple1.getSpecs()[0]); 94f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar 95f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar StateListAnimator.Tuple tuple2 = stateListAnimator.getTuples().get(1); 96f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertEquals("Second tuple should have two specs", 2, tuple2.getSpecs().length); 97f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar assertTrue("Tuple two should match the expected state", 98f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar StateSet.stateSetMatches(tuple2.getSpecs(), 99f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar new int[]{-com.android.internal.R.attr.state_pressed, 100f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar com.android.internal.R.attr.state_enabled})); 101f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar } 102f4c5bf30b445874cf353e1b96cab94185a39ce6dYigit Boyar} 103