Add CTS tests for Resources.getValue/DrawableForDensity()
Bug: 26059870
Change-Id: I5e2621cb5015b7b8c3ed88f5422d9412af653414
diff --git a/tests/tests/content/res/drawable-hdpi/density_test.xml b/tests/tests/content/res/drawable-hdpi/density_test.xml
new file mode 100644
index 0000000..e4a4b4a
--- /dev/null
+++ b/tests/tests/content/res/drawable-hdpi/density_test.xml
@@ -0,0 +1,24 @@
+<?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.
+ -->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+ <solid
+ android:color="#0000ff" />
+ <size
+ android:width="100px"
+ android:height="100px"/>
+</shape>
diff --git a/tests/tests/content/res/drawable-ldpi/density_test.xml b/tests/tests/content/res/drawable-ldpi/density_test.xml
new file mode 100644
index 0000000..302987f
--- /dev/null
+++ b/tests/tests/content/res/drawable-ldpi/density_test.xml
@@ -0,0 +1,24 @@
+<?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.
+ -->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+ <solid
+ android:color="#ff0000" />
+ <size
+ android:width="300px"
+ android:height="300px"/>
+</shape>
diff --git a/tests/tests/content/res/drawable-mdpi/density_test.xml b/tests/tests/content/res/drawable-mdpi/density_test.xml
new file mode 100644
index 0000000..60514c5
--- /dev/null
+++ b/tests/tests/content/res/drawable-mdpi/density_test.xml
@@ -0,0 +1,24 @@
+<?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.
+ -->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+ <solid
+ android:color="#00ff00" />
+ <size
+ android:width="200px"
+ android:height="200px"/>
+</shape>
diff --git a/tests/tests/content/res/drawable/density_test.xml b/tests/tests/content/res/drawable/density_test.xml
new file mode 100644
index 0000000..2b70eb8
--- /dev/null
+++ b/tests/tests/content/res/drawable/density_test.xml
@@ -0,0 +1,24 @@
+<?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.
+ -->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android">
+ <solid
+ android:color="#ffffff" />
+ <size
+ android:width="1px"
+ android:height="1px"/>
+</shape>
diff --git a/tests/tests/content/res/values-hdpi/strings.xml b/tests/tests/content/res/values-hdpi/strings.xml
new file mode 100644
index 0000000..0ea4779
--- /dev/null
+++ b/tests/tests/content/res/values-hdpi/strings.xml
@@ -0,0 +1,19 @@
+<?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.
+-->
+
+<resources>
+ <string name="density_string">hdpi</string>
+</resources>
diff --git a/tests/tests/content/res/values-ldpi/strings.xml b/tests/tests/content/res/values-ldpi/strings.xml
new file mode 100644
index 0000000..8a49f34
--- /dev/null
+++ b/tests/tests/content/res/values-ldpi/strings.xml
@@ -0,0 +1,19 @@
+<?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.
+-->
+
+<resources>
+ <string name="density_string">ldpi</string>
+</resources>
diff --git a/tests/tests/content/res/values-mdpi/strings.xml b/tests/tests/content/res/values-mdpi/strings.xml
new file mode 100644
index 0000000..721481f
--- /dev/null
+++ b/tests/tests/content/res/values-mdpi/strings.xml
@@ -0,0 +1,19 @@
+<?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.
+-->
+
+<resources>
+ <string name="density_string">mdpi</string>
+</resources>
diff --git a/tests/tests/content/res/values/strings.xml b/tests/tests/content/res/values/strings.xml
index 8ffb477..0d72c9f 100644
--- a/tests/tests/content/res/values/strings.xml
+++ b/tests/tests/content/res/values/strings.xml
@@ -182,4 +182,5 @@
<string name="permlab_callAbroad">Call abroad</string>
<string name="permdesc_callAbroad">Make calls abroad</string>
+ <string name="density_string">default</string>
</resources>
diff --git a/tests/tests/content/src/android/content/res/cts/ResourcesTest.java b/tests/tests/content/src/android/content/res/cts/ResourcesTest.java
index a3c1b49..1304b26 100644
--- a/tests/tests/content/src/android/content/res/cts/ResourcesTest.java
+++ b/tests/tests/content/src/android/content/res/cts/ResourcesTest.java
@@ -363,6 +363,20 @@
assertEquals(142 * targetDensity / defaultDensity, draw.getIntrinsicHeight(), 1);
}
+ public void testGetDrawableForDensity() {
+ final Drawable ldpi = mResources.getDrawableForDensity(
+ R.drawable.density_test, DisplayMetrics.DENSITY_LOW);
+ assertEquals(300, ldpi.getIntrinsicWidth());
+
+ final Drawable mdpi = mResources.getDrawableForDensity(
+ R.drawable.density_test, DisplayMetrics.DENSITY_MEDIUM);
+ assertEquals(200, mdpi.getIntrinsicWidth());
+
+ final Drawable hdpi = mResources.getDrawableForDensity(
+ R.drawable.density_test, DisplayMetrics.DENSITY_HIGH);
+ assertEquals(100, hdpi.getIntrinsicWidth());
+ }
+
public void testGetAnimation() throws Exception {
try {
mResources.getAnimation(-1);
@@ -431,6 +445,22 @@
assertEquals("res/raw/text.txt", tv.coerceToString());
}
+ public void testGetValueForDensity() {
+ final TypedValue tv = new TypedValue();
+
+ mResources.getValueForDensity(R.string.density_string,
+ DisplayMetrics.DENSITY_LOW, tv, false);
+ assertEquals("ldpi", tv.coerceToString());
+
+ mResources.getValueForDensity(R.string.density_string,
+ DisplayMetrics.DENSITY_MEDIUM, tv, false);
+ assertEquals("mdpi", tv.coerceToString());
+
+ mResources.getValueForDensity(R.string.density_string,
+ DisplayMetrics.DENSITY_HIGH, tv, false);
+ assertEquals("hdpi", tv.coerceToString());
+ }
+
public void testGetAssets() {
final AssetManager aM = mResources.getAssets();
assertNotNull(aM);