Improve AppCompat's LayoutInflater tests
Now adds tests for AppCompat's declarative onClick
workaround, and themeing inheritance.
Change-Id: Ibe4c94d0eb637bd196d3d83e69ce573a65c2733f
diff --git a/v7/appcompat/tests/AndroidManifest.xml b/v7/appcompat/tests/AndroidManifest.xml
index 1c6a855..9671adf 100644
--- a/v7/appcompat/tests/AndroidManifest.xml
+++ b/v7/appcompat/tests/AndroidManifest.xml
@@ -29,6 +29,7 @@
<activity
android:name="android.support.v7.app.AppCompatActivity"/>
+
<activity
android:name="android.support.v7.app.WindowDecorActionBarActivity"/>
<activity
@@ -60,6 +61,9 @@
android:label="@string/app_compat_image_view_activity"
android:theme="@style/Theme.AppCompat.Light" />
+ <activity
+ android:name="android.support.v7.app.LayoutInflaterFactoryTestActivity"/>
+
</application>
<instrumentation
diff --git a/v7/appcompat/tests/res/layout/layout_android_theme_children.xml b/v7/appcompat/tests/res/layout/layout_android_theme_children.xml
new file mode 100644
index 0000000..3212ea3
--- /dev/null
+++ b/v7/appcompat/tests/res/layout/layout_android_theme_children.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:theme="@style/ThemeOverlay.AppCompat.Dark"
+ android:orientation="vertical">
+
+ <TextView android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:text="Test"/>
+
+ <TextView android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:text="Test"/>
+
+ <TextView android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:text="Test"/>
+
+ <TextView android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:text="Test"/>
+
+</LinearLayout>
\ No newline at end of file
diff --git a/v7/appcompat/tests/res/layout/layout_button_themed_onclick.xml b/v7/appcompat/tests/res/layout/layout_button_themed_onclick.xml
new file mode 100644
index 0000000..a58dfce
--- /dev/null
+++ b/v7/appcompat/tests/res/layout/layout_button_themed_onclick.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2015 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.
+-->
+
+<Button xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:theme="@style/Theme.AppCompat.Light"
+ android:onClick="declarativeOnClick"/>
\ No newline at end of file
diff --git a/v7/appcompat/tests/src/android/support/v7/app/LayoutInflaterFactoryTestActivity.java b/v7/appcompat/tests/src/android/support/v7/app/LayoutInflaterFactoryTestActivity.java
new file mode 100644
index 0000000..dd152c6
--- /dev/null
+++ b/v7/appcompat/tests/src/android/support/v7/app/LayoutInflaterFactoryTestActivity.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2015 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.support.v7.app;
+
+import android.view.View;
+
+public class LayoutInflaterFactoryTestActivity extends AppCompatActivity {
+
+ private boolean mDeclarativeOnClickCalled;
+
+ public void declarativeOnClick(View view) {
+ mDeclarativeOnClickCalled = true;
+ }
+
+ public boolean wasDeclarativeOnClickCalled() {
+ return mDeclarativeOnClickCalled;
+ }
+
+}
diff --git a/v7/appcompat/tests/src/android/support/v7/app/LayoutInflaterFactoryTestCase.java b/v7/appcompat/tests/src/android/support/v7/app/LayoutInflaterFactoryTestCase.java
index 785dcc1..1f57584 100644
--- a/v7/appcompat/tests/src/android/support/v7/app/LayoutInflaterFactoryTestCase.java
+++ b/v7/appcompat/tests/src/android/support/v7/app/LayoutInflaterFactoryTestCase.java
@@ -18,8 +18,11 @@
import org.junit.Test;
+import android.content.Context;
+import android.content.ContextWrapper;
import android.os.Build;
import android.support.v7.appcompat.test.R;
+import android.support.v7.view.ContextThemeWrapper;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatCheckBox;
@@ -31,26 +34,27 @@
import android.test.suitebuilder.annotation.SmallTest;
import android.view.LayoutInflater;
import android.view.View;
+import android.view.ViewGroup;
-public class LayoutInflaterFactoryTestCase extends BaseInstrumentationTestCase<AppCompatActivity> {
+public class LayoutInflaterFactoryTestCase
+ extends BaseInstrumentationTestCase<LayoutInflaterFactoryTestActivity> {
+
+ private static final String TINTCONTEXTWRAPPER_CLAZZ_NAME
+ = "android.support.v7.widget.TintContextWrapper";
public LayoutInflaterFactoryTestCase() {
- super(AppCompatActivity.class);
+ super(LayoutInflaterFactoryTestActivity.class);
}
@Test
@SmallTest
public void testAndroidThemeInflation() throws Throwable {
- if (Build.VERSION.SDK_INT < 10) {
- // Ignore this test if running on Gingerbread or below
- return;
- }
runTestOnUiThread(new Runnable() {
@Override
public void run() {
- LayoutInflater inflater = LayoutInflater.from(getActivity());
- View view = inflater.inflate(R.layout.layout_android_theme, null);
- assertTrue("View has themed Context", view.getContext() != getActivity());
+ final LayoutInflater inflater = LayoutInflater.from(getActivity());
+ assertThemedContext(inflater,
+ inflater.inflate(R.layout.layout_android_theme, null));
}
});
}
@@ -58,16 +62,36 @@
@Test
@SmallTest
public void testAppThemeInflation() throws Throwable {
- if (Build.VERSION.SDK_INT < 10) {
- // Ignore this test if running on Gingerbread or below
+ runTestOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ final LayoutInflater inflater = LayoutInflater.from(getActivity());
+ assertThemedContext(inflater, inflater.inflate(R.layout.layout_app_theme, null));
+ }
+ });
+ }
+
+ @Test
+ @SmallTest
+ public void testAndroidThemeWithChildrenInflation() throws Throwable {
+ if (Build.VERSION.SDK_INT < 11) {
+ // Propagation of themed context to children only works on API 11+. Ignoring test.
return;
}
runTestOnUiThread(new Runnable() {
@Override
public void run() {
LayoutInflater inflater = LayoutInflater.from(getActivity());
- View view = inflater.inflate(R.layout.layout_app_theme, null);
- assertTrue("View has themed Context", view.getContext() != getActivity());
+ final ViewGroup root = (ViewGroup) inflater.inflate(
+ R.layout.layout_android_theme_children, null);
+
+ assertThemedContext(inflater, root);
+
+ for (int i = 0; i < root.getChildCount(); i++) {
+ final View child = root.getChildAt(i);
+ assertSame("Child does not have parent's context",
+ root.getContext(), unwrapContextIfNeeded(child.getContext()));
+ }
}
});
}
@@ -121,6 +145,21 @@
testAppCompatWidgetInflation(R.layout.layout_ratingbar, AppCompatRatingBar.class);
}
+ @Test
+ @SmallTest
+ public void testDeclarativeOnClickWithContextWrapper() throws Throwable {
+ runTestOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ LayoutInflater inflater = LayoutInflater.from(getActivity());
+ View view = inflater.inflate(R.layout.layout_button_themed_onclick, null);
+
+ assertTrue(view.performClick());
+ assertTrue(getActivity().wasDeclarativeOnClickCalled());
+ }
+ });
+ }
+
private void testAppCompatWidgetInflation(final int layout, final Class<?> expectedClass)
throws Throwable {
runTestOnUiThread(new Runnable() {
@@ -128,9 +167,27 @@
public void run() {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(layout, null);
- assertEquals("View is " + expectedClass.getSimpleName(), expectedClass,
+ assertSame("View is " + expectedClass.getSimpleName(), expectedClass,
view.getClass());
}
});
}
+
+ private static void assertThemedContext(LayoutInflater inflater, View view) {
+ final Context viewContext = unwrapContextIfNeeded(view.getContext());
+
+ assertNotSame("View has same context to LayoutInflater",
+ inflater.getContext(), viewContext);
+ assertSame("View does not have ContextThemeWrapper context",
+ ContextThemeWrapper.class, viewContext.getClass());
+ }
+
+ private static Context unwrapContextIfNeeded(Context context) {
+ if (TINTCONTEXTWRAPPER_CLAZZ_NAME.equals(context.getClass().getName())) {
+ // TintContextWrapper is a special context wrapper used for resource hacking in
+ // AppCompat, we'll compare it's base class instead
+ return ((ContextWrapper) context).getBaseContext();
+ }
+ return context;
+ }
}