Home | History | Annotate | Download | only in content
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.server.content;
     18 
     19 import android.accounts.Account;
     20 import android.content.ContentResolver;
     21 import android.content.Context;
     22 import android.os.Bundle;
     23 import android.os.SystemClock;
     24 import android.provider.Settings;
     25 import android.test.AndroidTestCase;
     26 import android.test.suitebuilder.annotation.SmallTest;
     27 
     28 /**
     29  * You can run those tests with:
     30  *
     31  * adb shell am instrument
     32  * -e debug false
     33  * -w
     34  * -e class android.content.SyncOperationTest com.android.frameworks.coretests/android.test.InstrumentationTestRunner
     35  */
     36 
     37 public class SyncOperationTest extends AndroidTestCase {
     38 
     39     Account mDummy;
     40     /** Indicate an unimportant long that we're not testing. */
     41     long mUnimportantLong = 0L;
     42     /** Empty bundle. */
     43     Bundle mEmpty;
     44     /** Silly authority. */
     45     String mAuthority;
     46 
     47     @Override
     48     public void setUp() {
     49         mDummy = new Account("account1", "type1");
     50         mEmpty = new Bundle();
     51         mAuthority = "authority1";
     52     }
     53 
     54     @SmallTest
     55     public void testToKey() {
     56         Account account1 = new Account("account1", "type1");
     57         Account account2 = new Account("account2", "type2");
     58 
     59         Bundle b1 = new Bundle();
     60         Bundle b2 = new Bundle();
     61         b2.putBoolean("b2", true);
     62 
     63         SyncOperation op1 = new SyncOperation(account1, 0,
     64                 1,
     65                 SyncOperation.REASON_PERIODIC,
     66                 "authority1",
     67                 b1,
     68                 100, /* run time from now*/
     69                 10, /* flex */
     70                 1000,
     71                 10000,
     72                 false);
     73 
     74         // Same as op1 but different time infos
     75         SyncOperation op2 = new SyncOperation(account1, 0,
     76                 1,
     77                 SyncOperation.REASON_PERIODIC,
     78                 "authority1",
     79                 b1,
     80                 200,
     81                 20,
     82                 2000,
     83                 20000,
     84                 false);
     85 
     86         // Same as op1 but different authority
     87         SyncOperation op3 = new SyncOperation(account1, 0,
     88                 1,
     89                 SyncOperation.REASON_PERIODIC,
     90                 "authority2",
     91                 b1,
     92                 100,
     93                 10,
     94                 1000,
     95                 10000,
     96                 false);
     97 
     98         // Same as op1 but different account
     99         SyncOperation op4 = new SyncOperation(account2, 0,
    100                 1,
    101                 SyncOperation.REASON_PERIODIC,
    102                 "authority1",
    103                 b1,
    104                 100,
    105                 10,
    106                 1000,
    107                 10000,
    108                 false);
    109 
    110         // Same as op1 but different bundle
    111         SyncOperation op5 = new SyncOperation(account1, 0,
    112                 1,
    113                 SyncOperation.REASON_PERIODIC,
    114                 "authority1",
    115                 b2,
    116                 100,
    117                 10,
    118                 1000,
    119                 10000,
    120                 false);
    121 
    122         assertEquals(op1.key, op2.key);
    123         assertNotSame(op1.key, op3.key);
    124         assertNotSame(op1.key, op4.key);
    125         assertNotSame(op1.key, op5.key);
    126     }
    127 
    128     @SmallTest
    129     public void testCompareTo() {
    130         long soon = 1000;
    131         long soonFlex = 50;
    132         long after = 1500;
    133         long afterFlex = 100;
    134         SyncOperation op1 = new SyncOperation(mDummy, 0, 0, SyncOperation.REASON_PERIODIC,
    135                 "authority1", mEmpty, soon, soonFlex, mUnimportantLong, mUnimportantLong, true);
    136 
    137         // Interval disjoint from and after op1.
    138         SyncOperation op2 = new SyncOperation(mDummy, 0, 0, SyncOperation.REASON_PERIODIC,
    139                 "authority1", mEmpty, after, afterFlex, mUnimportantLong, mUnimportantLong, true);
    140 
    141         // Interval equivalent to op1, but expedited.
    142         Bundle b2 = new Bundle();
    143         b2.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
    144         SyncOperation op3 = new SyncOperation(mDummy, 0, 0, 0,
    145                 "authority1", b2, -1, soonFlex, mUnimportantLong, mUnimportantLong, true);
    146 
    147         // Interval overlaps but not equivalent to op1.
    148         SyncOperation op4 = new SyncOperation(mDummy, 0, 0, SyncOperation.REASON_PERIODIC,
    149                 "authority1", mEmpty, soon + 100, soonFlex + 100, mUnimportantLong, mUnimportantLong, true);
    150 
    151         assertTrue(op1.compareTo(op2) == -1);
    152         assertTrue("less than not transitive.", op2.compareTo(op1) == 1);
    153         assertTrue("Expedited sync not smaller than non-expedited.", op1.compareTo(op3) == 1);
    154         assertTrue("greater than not transitive. ", op3.compareTo(op1) == -1);
    155         assertTrue("overlapping intervals not correctly compared.", op1.compareTo(op4) == -1);
    156         assertTrue("equality not transitive.", op4.compareTo(op1) == 1);
    157     }
    158 
    159     @SmallTest
    160     public void testCopyConstructor() {
    161         long fiveSecondsFromNow = 5 * 1000L;
    162         long twoSecondsFlex = 2 * 1000L;
    163         long eightSeconds = 8 * 1000L;
    164         long fourSeconds = 4 * 1000L;
    165 
    166         Bundle withExpedited = new Bundle();
    167         withExpedited.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
    168         SyncOperation op = new SyncOperation(mDummy, 0, 0, SyncOperation.REASON_USER_START,
    169                 mAuthority, withExpedited, fiveSecondsFromNow, twoSecondsFlex,
    170                 eightSeconds /* backoff */, fourSeconds /* delayUntil */, true);
    171         // Create another sync op to be rerun in 5 minutes.
    172         long now = SystemClock.elapsedRealtime();
    173         SyncOperation copy = new SyncOperation(op, fiveSecondsFromNow * 60);
    174         // Copying an expedited sync to be re-run should not keep expedited property.
    175         assertFalse("A rescheduled sync based off an expedited should not be expedited!",
    176                 copy.isExpedited());
    177         assertFalse("A rescheduled sync based off an expedited should not have expedited=true in"
    178                 + "its bundle.",
    179                 copy.extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false));
    180         assertTrue("Copied sync is not respecting new provided run-time.",
    181                 copy.latestRunTime == (now + fiveSecondsFromNow * 60));
    182         assertTrue("A rescheduled sync should not have any flex.",
    183                 copy.flexTime == 0L);
    184         assertTrue("A rescheduled op should honour the old op's backoff.",
    185                 copy.backoff == eightSeconds);
    186         assertTrue("A rescheduled op should honour the old op's delayUntil param.",
    187                 copy.delayUntil == fourSeconds);
    188 
    189     }
    190 }
    191