Add CTS tests for assigning keyed View tags via XML

BUG: 12768823
Change-Id: I03dae38edfe14ec3edb21259953d1d83affc33f6
diff --git a/tests/res/layout/inflater_layout_tags.xml b/tests/res/layout/inflater_layout_tags.xml
new file mode 100644
index 0000000..dc3eb29
--- /dev/null
+++ b/tests/res/layout/inflater_layout_tags.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+ * Copyright (C) 2014 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:id="@+id/viewlayout_root"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical" >
+
+    <tag
+        android:id="@+id/tag_viewlayout_root"
+        android:value="@string/tag1" />
+
+    <View
+        android:id="@+id/mock_view"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content" >
+
+        <tag
+            android:id="@+id/tag_mock_view"
+            android:value="@string/tag2" />
+    </View>
+
+</LinearLayout>
\ No newline at end of file
diff --git a/tests/res/values/strings.xml b/tests/res/values/strings.xml
index 0ab3dd6..ebdb9b9 100644
--- a/tests/res/values/strings.xml
+++ b/tests/res/values/strings.xml
@@ -152,6 +152,8 @@
    <string name="version_v3">base</string>
    <string name="authenticator_label">Android CTS</string>
    <string name="search_label">Android CTS</string>
+   <string name="tag1">tag 1</string>
+   <string name="tag2">tag 2</string>
 
    <string name="button">Button</string>
    <string name="holo_test">Holo Test</string>
diff --git a/tests/tests/view/src/android/view/cts/LayoutInflaterTest.java b/tests/tests/view/src/android/view/cts/LayoutInflaterTest.java
index f5247dd..07c6e8c 100644
--- a/tests/tests/view/src/android/view/cts/LayoutInflaterTest.java
+++ b/tests/tests/view/src/android/view/cts/LayoutInflaterTest.java
@@ -30,6 +30,7 @@
 import android.content.res.Resources.Theme;
 import android.content.res.XmlResourceParser;
 import android.test.AndroidTestCase;
+import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.util.TypedValue;
 import android.util.Xml;
@@ -382,6 +383,28 @@
         assertEquals(tag + " has themeType " + type, type, outValue.data);
     }
 
+    public void testInflateTags() {
+        final View view = mLayoutInflater.inflate(
+                com.android.cts.stub.R.layout.inflater_layout_tags, null);
+        assertNotNull(view);
+
+        checkViewTag(view, R.id.viewlayout_root, R.id.tag_viewlayout_root, R.string.tag1);
+        checkViewTag(view, R.id.mock_view, R.id.tag_mock_view, R.string.tag2);
+    }
+
+    private void checkViewTag(View parent, int viewId, int tagId, int valueResId) {
+        final View target = parent.findViewById(viewId);
+        assertNotNull("Found target view for " + viewId, target);
+
+        final Object tag = target.getTag(tagId);
+        assertNotNull("Tag is set", tag);
+        assertTrue("Tag is a character sequence", tag instanceof CharSequence);
+
+        final Context targetContext = target.getContext();
+        final CharSequence expectedValue = targetContext.getString(valueResId);
+        assertEquals(tagId + " has tag " + expectedValue, expectedValue, tag);
+    }
+
     static class MockLayoutInflater extends LayoutInflater {
 
         public MockLayoutInflater(Context c) {