Hide children in ViewGroup.createSnaphost via internal flag and add test for

it.

bug:27747923
Change-Id: I079b52b176b920bfa4c6749be31fbcd96a4dc42c
This commit is contained in:
sergeyv
2016-03-29 20:27:44 -07:00
parent 12bf75f354
commit b37d44e267
8 changed files with 185 additions and 5 deletions

View File

@@ -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;

View File

@@ -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);
}
}

View File

@@ -1093,7 +1093,12 @@
</intent-filter>
</activity>
<activity android:name="android.view.ViewCaptureTestActivity" android:label="ViewCaptureTestActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
</intent-filter>
</activity>
<!-- Activity-level metadata -->
<meta-data android:name="com.android.frameworks.coretests.isApp" android:value="true" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 329 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 343 B

View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
**
** Copyright 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.
*/
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/capture"
android:background="#00f"
android:orientation="vertical"
android:layout_width="100px"
android:layout_height="100px">
<View android:id="@+id/child1"
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="25px"/>
<View android:id="@+id/child2"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="25px"
android:visibility="invisible"/>
<View android:id="@+id/child3"
android:background="#ff0"
android:layout_width="match_parent"
android:layout_height="25px"
android:visibility="gone"/>
<View android:id="@+id/child4"
android:background="#0ff"
android:layout_width="match_parent"
android:layout_height="25px" />
</LinearLayout>
</FrameLayout>

View File

@@ -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<ViewCaptureTestActivity> 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());
}
}
}

View File

@@ -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);
}
}