Merge "Restore empty ZenRule#conditionId after a device restart" into tm-qpr-dev

This commit is contained in:
András Kurucz
2023-02-24 09:42:41 +00:00
committed by Android (Google) Code Review
2 changed files with 76 additions and 89 deletions

View File

@@ -953,7 +953,7 @@ public class ZenModeConfig implements Parcelable {
private static Uri safeUri(TypedXmlPullParser parser, String att) { private static Uri safeUri(TypedXmlPullParser parser, String att) {
final String val = parser.getAttributeValue(null, att); final String val = parser.getAttributeValue(null, att);
if (TextUtils.isEmpty(val)) return null; if (val == null) return null;
return Uri.parse(val); return Uri.parse(val);
} }

View File

@@ -32,22 +32,24 @@ import android.service.notification.Condition;
import android.service.notification.ZenModeConfig; import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.EventInfo; import android.service.notification.ZenModeConfig.EventInfo;
import android.service.notification.ZenPolicy; import android.service.notification.ZenPolicy;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.TypedXmlPullParser; import android.util.TypedXmlPullParser;
import android.util.TypedXmlSerializer; import android.util.TypedXmlSerializer;
import android.util.Xml; import android.util.Xml;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
import com.android.server.UiServiceTestCase; import com.android.server.UiServiceTestCase;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.xmlpull.v1.XmlPullParserException;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
@SmallTest @SmallTest
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@@ -189,8 +191,6 @@ public class ZenModeConfigTest extends UiServiceTestCase {
@Test @Test
public void testRuleXml() throws Exception { public void testRuleXml() throws Exception {
String tag = "tag";
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule(); ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
rule.configurationActivity = new ComponentName("a", "a"); rule.configurationActivity = new ComponentName("a", "a");
rule.component = new ComponentName("b", "b"); rule.component = new ComponentName("b", "b");
@@ -205,20 +205,11 @@ public class ZenModeConfigTest extends UiServiceTestCase {
rule.snoozing = true; rule.snoozing = true;
rule.pkg = "b"; rule.pkg = "b";
TypedXmlSerializer out = Xml.newFastSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
out.setOutput(new BufferedOutputStream(baos), "utf-8"); writeRuleXml(rule, baos);
out.startDocument(null, true); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
out.startTag(null, tag); ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
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);
assertEquals("b", fromXml.pkg); assertEquals("b", fromXml.pkg);
// always resets on reboot // always resets on reboot
assertFalse(fromXml.snoozing); assertFalse(fromXml.snoozing);
@@ -237,75 +228,41 @@ public class ZenModeConfigTest extends UiServiceTestCase {
@Test @Test
public void testRuleXml_pkg_component() throws Exception { public void testRuleXml_pkg_component() throws Exception {
String tag = "tag";
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule(); ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
rule.configurationActivity = new ComponentName("a", "a"); rule.configurationActivity = new ComponentName("a", "a");
rule.component = new ComponentName("b", "b"); rule.component = new ComponentName("b", "b");
TypedXmlSerializer out = Xml.newFastSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
out.setOutput(new BufferedOutputStream(baos), "utf-8"); writeRuleXml(rule, baos);
out.startDocument(null, true); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
out.startTag(null, tag); ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
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);
assertEquals("b", fromXml.pkg); assertEquals("b", fromXml.pkg);
} }
@Test @Test
public void testRuleXml_pkg_configActivity() throws Exception { public void testRuleXml_pkg_configActivity() throws Exception {
String tag = "tag";
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule(); ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
rule.configurationActivity = new ComponentName("a", "a"); rule.configurationActivity = new ComponentName("a", "a");
TypedXmlSerializer out = Xml.newFastSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
out.setOutput(new BufferedOutputStream(baos), "utf-8"); writeRuleXml(rule, baos);
out.startDocument(null, true); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
out.startTag(null, tag); ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
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);
assertNull(fromXml.pkg); assertNull(fromXml.pkg);
} }
@Test @Test
public void testRuleXml_getPkg_nullPkg() throws Exception { public void testRuleXml_getPkg_nullPkg() throws Exception {
String tag = "tag";
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule(); ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
rule.enabled = true; rule.enabled = true;
rule.configurationActivity = new ComponentName("a", "a"); rule.configurationActivity = new ComponentName("a", "a");
TypedXmlSerializer out = Xml.newFastSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
out.setOutput(new BufferedOutputStream(baos), "utf-8"); writeRuleXml(rule, baos);
out.startDocument(null, true); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
out.startTag(null, tag); ZenModeConfig.ZenRule fromXml = readRuleXml(bais);
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);
assertEquals("a", fromXml.getPkg()); assertEquals("a", fromXml.getPkg());
fromXml.condition = new Condition(Uri.EMPTY, "", Condition.STATE_TRUE); fromXml.condition = new Condition(Uri.EMPTY, "", Condition.STATE_TRUE);
@@ -313,25 +270,26 @@ public class ZenModeConfigTest extends UiServiceTestCase {
} }
@Test @Test
public void testZenPolicyXml_allUnset() throws Exception { public void testRuleXml_emptyConditionId() throws Exception {
String tag = "tag"; 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(); ZenPolicy policy = new ZenPolicy.Builder().build();
TypedXmlSerializer out = Xml.newFastSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
out.setOutput(new BufferedOutputStream(baos), "utf-8"); writePolicyXml(policy, baos);
out.startDocument(null, true); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
out.startTag(null, tag); ZenPolicy fromXml = readPolicyXml(bais);
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);
// nothing was set, so we should have nothing from the parser // nothing was set, so we should have nothing from the parser
assertNull(fromXml); assertNull(fromXml);
@@ -339,8 +297,6 @@ public class ZenModeConfigTest extends UiServiceTestCase {
@Test @Test
public void testZenPolicyXml() throws Exception { public void testZenPolicyXml() throws Exception {
String tag = "tag";
ZenPolicy policy = new ZenPolicy.Builder() ZenPolicy policy = new ZenPolicy.Builder()
.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS) .allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS)
.allowMessages(ZenPolicy.PEOPLE_TYPE_NONE) .allowMessages(ZenPolicy.PEOPLE_TYPE_NONE)
@@ -355,20 +311,10 @@ public class ZenModeConfigTest extends UiServiceTestCase {
.showVisualEffect(ZenPolicy.VISUAL_EFFECT_AMBIENT, true) .showVisualEffect(ZenPolicy.VISUAL_EFFECT_AMBIENT, true)
.build(); .build();
TypedXmlSerializer out = Xml.newFastSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
out.setOutput(new BufferedOutputStream(baos), "utf-8"); writePolicyXml(policy, baos);
out.startDocument(null, true); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
out.startTag(null, tag); ZenPolicy fromXml = readPolicyXml(bais);
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);
assertNotNull(fromXml); assertNotNull(fromXml);
assertEquals(policy.getPriorityCategoryCalls(), fromXml.getPriorityCategoryCalls()); assertEquals(policy.getPriorityCategoryCalls(), fromXml.getPriorityCategoryCalls());
@@ -457,4 +403,45 @@ public class ZenModeConfigTest extends UiServiceTestCase {
config.suppressedVisualEffects = 0; config.suppressedVisualEffects = 0;
return config; 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);
}
} }