summaryrefslogtreecommitdiffstats
path: root/common/tests/src/com/android
diff options
context:
space:
mode:
Diffstat (limited to 'common/tests/src/com/android')
-rw-r--r--common/tests/src/com/android/common/OperationSchedulerTest.java116
-rw-r--r--common/tests/src/com/android/common/PatternsTest.java28
2 files changed, 130 insertions, 14 deletions
diff --git a/common/tests/src/com/android/common/OperationSchedulerTest.java b/common/tests/src/com/android/common/OperationSchedulerTest.java
new file mode 100644
index 0000000..13f710d
--- /dev/null
+++ b/common/tests/src/com/android/common/OperationSchedulerTest.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2009 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 com.android.common;
+
+import android.content.SharedPreferences;
+import android.test.AndroidTestCase;
+
+public class OperationSchedulerTest extends AndroidTestCase {
+ public void testScheduler() throws Exception {
+ String name = "OperationSchedulerTest.testScheduler";
+ SharedPreferences storage = getContext().getSharedPreferences(name, 0);
+ storage.edit().clear().commit();
+
+ OperationScheduler scheduler = new OperationScheduler(storage);
+ OperationScheduler.Options options = new OperationScheduler.Options();
+ assertEquals(Long.MAX_VALUE, scheduler.getNextTimeMillis(options));
+
+ long beforeTrigger = System.currentTimeMillis();
+ scheduler.setTriggerTimeMillis(beforeTrigger + 1000000);
+ assertEquals(beforeTrigger + 1000000, scheduler.getNextTimeMillis(options));
+
+ // It will schedule for the later of the trigger and the moratorium...
+ scheduler.setMoratoriumTimeMillis(beforeTrigger + 500000);
+ assertEquals(beforeTrigger + 1000000, scheduler.getNextTimeMillis(options));
+ scheduler.setMoratoriumTimeMillis(beforeTrigger + 1500000);
+ assertEquals(beforeTrigger + 1500000, scheduler.getNextTimeMillis(options));
+
+ // Test enable/disable toggle
+ scheduler.setEnabledState(false);
+ assertEquals(Long.MAX_VALUE, scheduler.getNextTimeMillis(options));
+ scheduler.setEnabledState(true);
+ assertEquals(beforeTrigger + 1500000, scheduler.getNextTimeMillis(options));
+
+ // Backoff interval after an error
+ long beforeError = System.currentTimeMillis();
+ scheduler.onTransientError();
+ long afterError = System.currentTimeMillis();
+ assertEquals(beforeTrigger + 1500000, scheduler.getNextTimeMillis(options));
+ options.backoffFixedMillis = 1000000;
+ options.backoffIncrementalMillis = 500000;
+ assertTrue(beforeError + 1500000 <= scheduler.getNextTimeMillis(options));
+ assertTrue(afterError + 1500000 >= scheduler.getNextTimeMillis(options));
+
+ // Two errors: backoff interval increases
+ beforeError = System.currentTimeMillis();
+ scheduler.onTransientError();
+ afterError = System.currentTimeMillis();
+ assertTrue(beforeError + 2000000 <= scheduler.getNextTimeMillis(options));
+ assertTrue(afterError + 2000000 >= scheduler.getNextTimeMillis(options));
+
+ // Permanent error holds true even if transient errors are reset
+ // However, we remember that the transient error was reset...
+ scheduler.onPermanentError();
+ assertEquals(Long.MAX_VALUE, scheduler.getNextTimeMillis(options));
+ scheduler.resetTransientError();
+ assertEquals(Long.MAX_VALUE, scheduler.getNextTimeMillis(options));
+ scheduler.resetPermanentError();
+ assertEquals(beforeTrigger + 1500000, scheduler.getNextTimeMillis(options));
+
+ // Success resets the trigger
+ long beforeSuccess = System.currentTimeMillis();
+ scheduler.onSuccess();
+ long afterSuccess = System.currentTimeMillis();
+ assertEquals(Long.MAX_VALUE, scheduler.getNextTimeMillis(options));
+
+ // The moratorium is not reset by success!
+ scheduler.setTriggerTimeMillis(beforeSuccess + 500000);
+ assertEquals(beforeTrigger + 1500000, scheduler.getNextTimeMillis(options));
+ scheduler.setMoratoriumTimeMillis(0);
+ assertEquals(beforeSuccess + 500000, scheduler.getNextTimeMillis(options));
+
+ // Periodic interval after success
+ options.periodicIntervalMillis = 250000;
+ assertTrue(beforeSuccess + 250000 <= scheduler.getNextTimeMillis(options));
+ assertTrue(afterSuccess + 250000 >= scheduler.getNextTimeMillis(options));
+
+ // Trigger minimum is also since the last success
+ options.minTriggerMillis = 1000000;
+ assertTrue(beforeSuccess + 1000000 <= scheduler.getNextTimeMillis(options));
+ assertTrue(afterSuccess + 1000000 >= scheduler.getNextTimeMillis(options));
+ }
+
+ public void testParseOptions() throws Exception {
+ OperationScheduler.Options options = new OperationScheduler.Options();
+ assertEquals(
+ "OperationScheduler.Options[backoff=0.0+5.0 max=86400.0 min=0.0 period=3600.0]",
+ OperationScheduler.parseOptions("3600", options).toString());
+
+ assertEquals(
+ "OperationScheduler.Options[backoff=0.0+2.5 max=86400.0 min=0.0 period=3700.0]",
+ OperationScheduler.parseOptions("backoff=+2.5 3700", options).toString());
+
+ assertEquals(
+ "OperationScheduler.Options[backoff=10.0+2.5 max=12345.6 min=7.0 period=3800.0]",
+ OperationScheduler.parseOptions("max=12345.6 min=7 backoff=10 period=3800",
+ options).toString());
+
+ assertEquals(
+ "OperationScheduler.Options[backoff=10.0+2.5 max=12345.6 min=7.0 period=3800.0]",
+ OperationScheduler.parseOptions("", options).toString());
+ }
+}
diff --git a/common/tests/src/com/android/common/PatternsTest.java b/common/tests/src/com/android/common/PatternsTest.java
index a89ad62..7fabe5e 100644
--- a/common/tests/src/com/android/common/PatternsTest.java
+++ b/common/tests/src/com/android/common/PatternsTest.java
@@ -28,10 +28,10 @@ public class PatternsTest extends TestCase {
public void testTldPattern() throws Exception {
boolean t;
- t = Patterns.TOP_LEVEL_DOMAIN_PATTERN.matcher("com").matches();
+ t = Patterns.TOP_LEVEL_DOMAIN.matcher("com").matches();
assertTrue("Missed valid TLD", t);
- t = Patterns.TOP_LEVEL_DOMAIN_PATTERN.matcher("xer").matches();
+ t = Patterns.TOP_LEVEL_DOMAIN.matcher("xer").matches();
assertFalse("Matched invalid TLD!", t);
}
@@ -39,19 +39,19 @@ public class PatternsTest extends TestCase {
public void testUrlPattern() throws Exception {
boolean t;
- t = Patterns.WEB_URL_PATTERN.matcher("http://www.google.com").matches();
+ t = Patterns.WEB_URL.matcher("http://www.google.com").matches();
assertTrue("Valid URL", t);
- t = Patterns.WEB_URL_PATTERN.matcher("ftp://www.example.com").matches();
+ t = Patterns.WEB_URL.matcher("ftp://www.example.com").matches();
assertFalse("Matched invalid protocol", t);
- t = Patterns.WEB_URL_PATTERN.matcher("http://www.example.com:8080").matches();
+ t = Patterns.WEB_URL.matcher("http://www.example.com:8080").matches();
assertTrue("Didn't match valid URL with port", t);
- t = Patterns.WEB_URL_PATTERN.matcher("http://www.example.com:8080/?foo=bar").matches();
+ t = Patterns.WEB_URL.matcher("http://www.example.com:8080/?foo=bar").matches();
assertTrue("Didn't match valid URL with port and query args", t);
- t = Patterns.WEB_URL_PATTERN.matcher("http://www.example.com:8080/~user/?foo=bar").matches();
+ t = Patterns.WEB_URL.matcher("http://www.example.com:8080/~user/?foo=bar").matches();
assertTrue("Didn't match valid URL with ~", t);
}
@@ -59,10 +59,10 @@ public class PatternsTest extends TestCase {
public void testIpPattern() throws Exception {
boolean t;
- t = Patterns.IP_ADDRESS_PATTERN.matcher("172.29.86.3").matches();
+ t = Patterns.IP_ADDRESS.matcher("172.29.86.3").matches();
assertTrue("Valid IP", t);
- t = Patterns.IP_ADDRESS_PATTERN.matcher("1234.4321.9.9").matches();
+ t = Patterns.IP_ADDRESS.matcher("1234.4321.9.9").matches();
assertFalse("Invalid IP", t);
}
@@ -70,10 +70,10 @@ public class PatternsTest extends TestCase {
public void testDomainPattern() throws Exception {
boolean t;
- t = Patterns.DOMAIN_NAME_PATTERN.matcher("mail.example.com").matches();
+ t = Patterns.DOMAIN_NAME.matcher("mail.example.com").matches();
assertTrue("Valid domain", t);
- t = Patterns.DOMAIN_NAME_PATTERN.matcher("__+&42.xer").matches();
+ t = Patterns.DOMAIN_NAME.matcher("__+&42.xer").matches();
assertFalse("Invalid domain", t);
}
@@ -81,10 +81,10 @@ public class PatternsTest extends TestCase {
public void testPhonePattern() throws Exception {
boolean t;
- t = Patterns.PHONE_PATTERN.matcher("(919) 555-1212").matches();
+ t = Patterns.PHONE.matcher("(919) 555-1212").matches();
assertTrue("Valid phone", t);
- t = Patterns.PHONE_PATTERN.matcher("2334 9323/54321").matches();
+ t = Patterns.PHONE.matcher("2334 9323/54321").matches();
assertFalse("Invalid phone", t);
String[] tests = {
@@ -115,7 +115,7 @@ public class PatternsTest extends TestCase {
};
for (String test : tests) {
- Matcher m = Patterns.PHONE_PATTERN.matcher(test);
+ Matcher m = Patterns.PHONE.matcher(test);
assertTrue("Valid phone " + test, m.find());
}