sergeyv | b37d44e | 2016-03-29 20:27:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.BitmapFactory; |
| 22 | import android.support.test.filters.SmallTest; |
| 23 | import android.support.test.rule.ActivityTestRule; |
| 24 | import android.support.test.runner.AndroidJUnit4; |
| 25 | import android.util.SparseIntArray; |
| 26 | |
| 27 | import com.android.frameworks.coretests.R; |
| 28 | |
| 29 | import org.junit.Assert; |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Rule; |
| 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | |
| 35 | import static org.junit.Assert.assertTrue; |
| 36 | |
| 37 | @RunWith(AndroidJUnit4.class) |
| 38 | public class ViewCaptureTest { |
| 39 | |
| 40 | private static final SparseIntArray EXPECTED_CHILDREN_VISIBILITY = new SparseIntArray(); |
| 41 | static { |
| 42 | EXPECTED_CHILDREN_VISIBILITY.append(R.id.child1, View.VISIBLE); |
| 43 | EXPECTED_CHILDREN_VISIBILITY.append(R.id.child2, View.INVISIBLE); |
| 44 | EXPECTED_CHILDREN_VISIBILITY.append(R.id.child3, View.GONE); |
| 45 | EXPECTED_CHILDREN_VISIBILITY.append(R.id.child4, View.VISIBLE); |
| 46 | } |
| 47 | |
| 48 | @Rule |
| 49 | public ActivityTestRule<ViewCaptureTestActivity> mActivityRule = new ActivityTestRule<>( |
| 50 | ViewCaptureTestActivity.class); |
| 51 | |
| 52 | private Activity mActivity; |
| 53 | private ViewGroup mViewToCapture; |
| 54 | |
| 55 | @Before |
| 56 | public void setUp() throws Exception { |
| 57 | mActivity = mActivityRule.getActivity(); |
| 58 | mViewToCapture = (ViewGroup) mActivity.findViewById(R.id.capture); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | @SmallTest |
| 63 | public void testCreateSnapshot() { |
| 64 | assertChildrenVisibility(); |
| 65 | testCreateSnapshot(true, R.drawable.view_capture_test_no_children_golden); |
| 66 | assertChildrenVisibility(); |
| 67 | testCreateSnapshot(false, R.drawable.view_capture_test_with_children_golden); |
| 68 | assertChildrenVisibility(); |
| 69 | } |
| 70 | |
| 71 | private void testCreateSnapshot(boolean skipChildren, int goldenResId) { |
| 72 | Bitmap result = mViewToCapture.createSnapshot(Bitmap.Config.ARGB_8888, 0, skipChildren); |
| 73 | Bitmap golden = BitmapFactory.decodeResource(mActivity.getResources(), goldenResId); |
| 74 | assertTrue(golden.sameAs(result)); |
| 75 | } |
| 76 | |
| 77 | private void assertChildrenVisibility() { |
| 78 | for (int i = 0; i < EXPECTED_CHILDREN_VISIBILITY.size(); i++) { |
| 79 | int id = EXPECTED_CHILDREN_VISIBILITY.keyAt(i); |
| 80 | View child = mViewToCapture.findViewById(id); |
| 81 | Assert.assertEquals(EXPECTED_CHILDREN_VISIBILITY.get(id), child.getVisibility()); |
| 82 | } |
| 83 | } |
| 84 | } |