diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 83c6e9e1ab833..63035f26f33c2 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -16235,8 +16235,10 @@ public class View implements Drawable.Callback, KeyEvent.Callback, /** * Create a snapshot of the view into a bitmap. We should probably make * some form of this public, but should think about the API. + * + * @hide */ - Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) { + public Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) { int width = mRight - mLeft; int height = mBottom - mTop; diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java index 816d9c48539ec..ae9edc93d6b88 100644 --- a/core/java/android/view/ViewGroup.java +++ b/core/java/android/view/ViewGroup.java @@ -3251,8 +3251,11 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager } } + /** + * @hide + */ @Override - Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) { + public Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) { int count = mChildrenCount; int[] visibilities = null; @@ -3262,7 +3265,8 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager View child = getChildAt(i); visibilities[i] = child.getVisibility(); if (visibilities[i] == View.VISIBLE) { - child.setVisibility(INVISIBLE); + child.mViewFlags = (child.mViewFlags & ~View.VISIBILITY_MASK) + | (View.INVISIBLE & View.VISIBILITY_MASK); } } } @@ -3271,7 +3275,9 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager if (skipChildren) { for (int i = 0; i < count; i++) { - getChildAt(i).setVisibility(visibilities[i]); + View child = getChildAt(i); + child.mViewFlags = (child.mViewFlags & ~View.VISIBILITY_MASK) + | (visibilities[i] & View.VISIBILITY_MASK); } } diff --git a/core/tests/coretests/AndroidManifest.xml b/core/tests/coretests/AndroidManifest.xml index b78077892c9b7..2452cfd66c3f6 100644 --- a/core/tests/coretests/AndroidManifest.xml +++ b/core/tests/coretests/AndroidManifest.xml @@ -1093,7 +1093,12 @@ - + + + + + + diff --git a/core/tests/coretests/res/drawable-nodpi/view_capture_test_no_children_golden.png b/core/tests/coretests/res/drawable-nodpi/view_capture_test_no_children_golden.png new file mode 100644 index 0000000000000..52092c4479731 Binary files /dev/null and b/core/tests/coretests/res/drawable-nodpi/view_capture_test_no_children_golden.png differ diff --git a/core/tests/coretests/res/drawable-nodpi/view_capture_test_with_children_golden.png b/core/tests/coretests/res/drawable-nodpi/view_capture_test_with_children_golden.png new file mode 100644 index 0000000000000..3bcbd9a28627a Binary files /dev/null and b/core/tests/coretests/res/drawable-nodpi/view_capture_test_with_children_golden.png differ diff --git a/core/tests/coretests/res/layout/view_capture_snapshot.xml b/core/tests/coretests/res/layout/view_capture_snapshot.xml new file mode 100644 index 0000000000000..0b589a469520a --- /dev/null +++ b/core/tests/coretests/res/layout/view_capture_snapshot.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + diff --git a/core/tests/coretests/src/android/view/ViewCaptureTest.java b/core/tests/coretests/src/android/view/ViewCaptureTest.java new file mode 100644 index 0000000000000..15cfe230eb579 --- /dev/null +++ b/core/tests/coretests/src/android/view/ViewCaptureTest.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2016 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 android.view; + +import android.app.Activity; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.support.test.filters.SmallTest; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; +import android.util.SparseIntArray; + +import com.android.frameworks.coretests.R; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertTrue; + +@RunWith(AndroidJUnit4.class) +public class ViewCaptureTest { + + private static final SparseIntArray EXPECTED_CHILDREN_VISIBILITY = new SparseIntArray(); + static { + EXPECTED_CHILDREN_VISIBILITY.append(R.id.child1, View.VISIBLE); + EXPECTED_CHILDREN_VISIBILITY.append(R.id.child2, View.INVISIBLE); + EXPECTED_CHILDREN_VISIBILITY.append(R.id.child3, View.GONE); + EXPECTED_CHILDREN_VISIBILITY.append(R.id.child4, View.VISIBLE); + } + + @Rule + public ActivityTestRule mActivityRule = new ActivityTestRule<>( + ViewCaptureTestActivity.class); + + private Activity mActivity; + private ViewGroup mViewToCapture; + + @Before + public void setUp() throws Exception { + mActivity = mActivityRule.getActivity(); + mViewToCapture = (ViewGroup) mActivity.findViewById(R.id.capture); + } + + @Test + @SmallTest + public void testCreateSnapshot() { + assertChildrenVisibility(); + testCreateSnapshot(true, R.drawable.view_capture_test_no_children_golden); + assertChildrenVisibility(); + testCreateSnapshot(false, R.drawable.view_capture_test_with_children_golden); + assertChildrenVisibility(); + } + + private void testCreateSnapshot(boolean skipChildren, int goldenResId) { + Bitmap result = mViewToCapture.createSnapshot(Bitmap.Config.ARGB_8888, 0, skipChildren); + Bitmap golden = BitmapFactory.decodeResource(mActivity.getResources(), goldenResId); + assertTrue(golden.sameAs(result)); + } + + private void assertChildrenVisibility() { + for (int i = 0; i < EXPECTED_CHILDREN_VISIBILITY.size(); i++) { + int id = EXPECTED_CHILDREN_VISIBILITY.keyAt(i); + View child = mViewToCapture.findViewById(id); + Assert.assertEquals(EXPECTED_CHILDREN_VISIBILITY.get(id), child.getVisibility()); + } + } +} diff --git a/core/tests/coretests/src/android/view/ViewCaptureTestActivity.java b/core/tests/coretests/src/android/view/ViewCaptureTestActivity.java new file mode 100644 index 0000000000000..20e3eb5854354 --- /dev/null +++ b/core/tests/coretests/src/android/view/ViewCaptureTestActivity.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2016 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 android.view; + +import android.annotation.Nullable; +import android.app.Activity; +import android.os.Bundle; +import com.android.frameworks.coretests.R; + +public class ViewCaptureTestActivity extends Activity { + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.view_capture_snapshot); + } +}