diff --git a/core/java/android/service/notification/ZenModeConfig.java b/core/java/android/service/notification/ZenModeConfig.java index e285b1c771221..9d8c963e2f8b5 100644 --- a/core/java/android/service/notification/ZenModeConfig.java +++ b/core/java/android/service/notification/ZenModeConfig.java @@ -953,7 +953,7 @@ public class ZenModeConfig implements Parcelable { private static Uri safeUri(TypedXmlPullParser parser, String att) { final String val = parser.getAttributeValue(null, att); - if (TextUtils.isEmpty(val)) return null; + if (val == null) return null; return Uri.parse(val); } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeConfigTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeConfigTest.java index 949455a18570e..b2754d237f425 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeConfigTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeConfigTest.java @@ -32,22 +32,24 @@ import android.service.notification.Condition; import android.service.notification.ZenModeConfig; import android.service.notification.ZenModeConfig.EventInfo; import android.service.notification.ZenPolicy; -import android.test.suitebuilder.annotation.SmallTest; import android.util.TypedXmlPullParser; import android.util.TypedXmlSerializer; import android.util.Xml; +import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; import com.android.server.UiServiceTestCase; import org.junit.Test; import org.junit.runner.RunWith; +import org.xmlpull.v1.XmlPullParserException; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; @SmallTest @RunWith(AndroidJUnit4.class) @@ -189,8 +191,6 @@ public class ZenModeConfigTest extends UiServiceTestCase { @Test public void testRuleXml() throws Exception { - String tag = "tag"; - ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule(); rule.configurationActivity = new ComponentName("a", "a"); rule.component = new ComponentName("b", "b"); @@ -205,20 +205,11 @@ public class ZenModeConfigTest extends UiServiceTestCase { rule.snoozing = true; rule.pkg = "b"; - TypedXmlSerializer out = Xml.newFastSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - out.setOutput(new BufferedOutputStream(baos), "utf-8"); - out.startDocument(null, true); - out.startTag(null, tag); - ZenModeConfig.writeRuleXml(rule, out); - out.endTag(null, tag); - out.endDocument(); + writeRuleXml(rule, baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ZenModeConfig.ZenRule fromXml = readRuleXml(bais); - TypedXmlPullParser parser = Xml.newFastPullParser(); - parser.setInput(new BufferedInputStream( - new ByteArrayInputStream(baos.toByteArray())), null); - parser.nextTag(); - ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser); assertEquals("b", fromXml.pkg); // always resets on reboot assertFalse(fromXml.snoozing); @@ -237,75 +228,41 @@ public class ZenModeConfigTest extends UiServiceTestCase { @Test public void testRuleXml_pkg_component() throws Exception { - String tag = "tag"; - ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule(); rule.configurationActivity = new ComponentName("a", "a"); rule.component = new ComponentName("b", "b"); - TypedXmlSerializer out = Xml.newFastSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - out.setOutput(new BufferedOutputStream(baos), "utf-8"); - out.startDocument(null, true); - out.startTag(null, tag); - ZenModeConfig.writeRuleXml(rule, out); - out.endTag(null, tag); - out.endDocument(); + writeRuleXml(rule, baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ZenModeConfig.ZenRule fromXml = readRuleXml(bais); - TypedXmlPullParser parser = Xml.newFastPullParser(); - parser.setInput(new BufferedInputStream( - new ByteArrayInputStream(baos.toByteArray())), null); - parser.nextTag(); - ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser); assertEquals("b", fromXml.pkg); } @Test public void testRuleXml_pkg_configActivity() throws Exception { - String tag = "tag"; - ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule(); rule.configurationActivity = new ComponentName("a", "a"); - TypedXmlSerializer out = Xml.newFastSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - out.setOutput(new BufferedOutputStream(baos), "utf-8"); - out.startDocument(null, true); - out.startTag(null, tag); - ZenModeConfig.writeRuleXml(rule, out); - out.endTag(null, tag); - out.endDocument(); + writeRuleXml(rule, baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ZenModeConfig.ZenRule fromXml = readRuleXml(bais); - TypedXmlPullParser parser = Xml.newFastPullParser(); - parser.setInput(new BufferedInputStream( - new ByteArrayInputStream(baos.toByteArray())), null); - parser.nextTag(); - ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser); assertNull(fromXml.pkg); } @Test public void testRuleXml_getPkg_nullPkg() throws Exception { - String tag = "tag"; - ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule(); rule.enabled = true; rule.configurationActivity = new ComponentName("a", "a"); - TypedXmlSerializer out = Xml.newFastSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - out.setOutput(new BufferedOutputStream(baos), "utf-8"); - out.startDocument(null, true); - out.startTag(null, tag); - ZenModeConfig.writeRuleXml(rule, out); - out.endTag(null, tag); - out.endDocument(); - - TypedXmlPullParser parser = Xml.newFastPullParser(); - parser.setInput(new BufferedInputStream( - new ByteArrayInputStream(baos.toByteArray())), null); - parser.nextTag(); - ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser); + writeRuleXml(rule, baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ZenModeConfig.ZenRule fromXml = readRuleXml(bais); assertEquals("a", fromXml.getPkg()); fromXml.condition = new Condition(Uri.EMPTY, "", Condition.STATE_TRUE); @@ -313,25 +270,26 @@ public class ZenModeConfigTest extends UiServiceTestCase { } @Test - public void testZenPolicyXml_allUnset() throws Exception { - String tag = "tag"; + public void testRuleXml_emptyConditionId() throws Exception { + ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule(); + rule.conditionId = Uri.EMPTY; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + writeRuleXml(rule, baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ZenModeConfig.ZenRule fromXml = readRuleXml(bais); + + assertEquals(rule.condition, fromXml.condition); + } + + @Test + public void testZenPolicyXml_allUnset() throws Exception { ZenPolicy policy = new ZenPolicy.Builder().build(); - TypedXmlSerializer out = Xml.newFastSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - out.setOutput(new BufferedOutputStream(baos), "utf-8"); - out.startDocument(null, true); - out.startTag(null, tag); - ZenModeConfig.writeZenPolicyXml(policy, out); - out.endTag(null, tag); - out.endDocument(); - - TypedXmlPullParser parser = Xml.newFastPullParser(); - parser.setInput(new BufferedInputStream( - new ByteArrayInputStream(baos.toByteArray())), null); - parser.nextTag(); - ZenPolicy fromXml = ZenModeConfig.readZenPolicyXml(parser); + writePolicyXml(policy, baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ZenPolicy fromXml = readPolicyXml(bais); // nothing was set, so we should have nothing from the parser assertNull(fromXml); @@ -339,8 +297,6 @@ public class ZenModeConfigTest extends UiServiceTestCase { @Test public void testZenPolicyXml() throws Exception { - String tag = "tag"; - ZenPolicy policy = new ZenPolicy.Builder() .allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS) .allowMessages(ZenPolicy.PEOPLE_TYPE_NONE) @@ -355,20 +311,10 @@ public class ZenModeConfigTest extends UiServiceTestCase { .showVisualEffect(ZenPolicy.VISUAL_EFFECT_AMBIENT, true) .build(); - TypedXmlSerializer out = Xml.newFastSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); - out.setOutput(new BufferedOutputStream(baos), "utf-8"); - out.startDocument(null, true); - out.startTag(null, tag); - ZenModeConfig.writeZenPolicyXml(policy, out); - out.endTag(null, tag); - out.endDocument(); - - TypedXmlPullParser parser = Xml.newFastPullParser(); - parser.setInput(new BufferedInputStream( - new ByteArrayInputStream(baos.toByteArray())), null); - parser.nextTag(); - ZenPolicy fromXml = ZenModeConfig.readZenPolicyXml(parser); + writePolicyXml(policy, baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ZenPolicy fromXml = readPolicyXml(bais); assertNotNull(fromXml); assertEquals(policy.getPriorityCategoryCalls(), fromXml.getPriorityCategoryCalls()); @@ -457,4 +403,45 @@ public class ZenModeConfigTest extends UiServiceTestCase { config.suppressedVisualEffects = 0; return config; } + + private void writeRuleXml(ZenModeConfig.ZenRule rule, ByteArrayOutputStream os) + throws IOException { + String tag = "tag"; + + TypedXmlSerializer out = Xml.newFastSerializer(); + out.setOutput(new BufferedOutputStream(os), "utf-8"); + out.startDocument(null, true); + out.startTag(null, tag); + ZenModeConfig.writeRuleXml(rule, out); + out.endTag(null, tag); + out.endDocument(); + } + + private ZenModeConfig.ZenRule readRuleXml(ByteArrayInputStream is) + throws XmlPullParserException, IOException { + TypedXmlPullParser parser = Xml.newFastPullParser(); + parser.setInput(new BufferedInputStream(is), null); + parser.nextTag(); + return ZenModeConfig.readRuleXml(parser); + } + + private void writePolicyXml(ZenPolicy policy, ByteArrayOutputStream os) throws IOException { + String tag = "tag"; + + TypedXmlSerializer out = Xml.newFastSerializer(); + out.setOutput(new BufferedOutputStream(os), "utf-8"); + out.startDocument(null, true); + out.startTag(null, tag); + ZenModeConfig.writeZenPolicyXml(policy, out); + out.endTag(null, tag); + out.endDocument(); + } + + private ZenPolicy readPolicyXml(ByteArrayInputStream is) + throws XmlPullParserException, IOException { + TypedXmlPullParser parser = Xml.newFastPullParser(); + parser.setInput(new BufferedInputStream(is), null); + parser.nextTag(); + return ZenModeConfig.readZenPolicyXml(parser); + } }