SyncOperationTest.java revision bb87ac7f9733ca9b490cb34e8a675dba083a57b7
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
17package com.android.server.content;
18
19import android.accounts.Account;
20import android.content.ContentResolver;
21import android.os.Bundle;
22import android.test.AndroidTestCase;
23import android.test.suitebuilder.annotation.SmallTest;
24
25import com.android.server.content.SyncOperation;
26
27/**
28 * You can run those tests with:
29 *
30 * adb shell am instrument
31 * -e debug false
32 * -w
33 * -e class android.content.SyncOperationTest com.android.frameworks.coretests/android.test.InstrumentationTestRunner
34 */
35
36public class SyncOperationTest extends AndroidTestCase {
37
38    @SmallTest
39    public void testToKey() {
40        Account account1 = new Account("account1", "type1");
41        Account account2 = new Account("account2", "type2");
42
43        Bundle b1 = new Bundle();
44        Bundle b2 = new Bundle();
45        b2.putBoolean("b2", true);
46
47        SyncOperation op1 = new SyncOperation(account1, 0,
48                1,
49                SyncOperation.REASON_PERIODIC,
50                "authority1",
51                b1,
52                100, /* run time from now*/
53                10, /* flex */
54                1000,
55                10000,
56                false);
57
58        // Same as op1 but different time infos
59        SyncOperation op2 = new SyncOperation(account1, 0,
60                1,
61                SyncOperation.REASON_PERIODIC,
62                "authority1",
63                b1,
64                200,
65                20,
66                2000,
67                20000,
68                false);
69
70        // Same as op1 but different authority
71        SyncOperation op3 = new SyncOperation(account1, 0,
72                1,
73                SyncOperation.REASON_PERIODIC,
74                "authority2",
75                b1,
76                100,
77                10,
78                1000,
79                10000,
80                false);
81
82        // Same as op1 but different account
83        SyncOperation op4 = new SyncOperation(account2, 0,
84                1,
85                SyncOperation.REASON_PERIODIC,
86                "authority1",
87                b1,
88                100,
89                10,
90                1000,
91                10000,
92                false);
93
94        // Same as op1 but different bundle
95        SyncOperation op5 = new SyncOperation(account1, 0,
96                1,
97                SyncOperation.REASON_PERIODIC,
98                "authority1",
99                b2,
100                100,
101                10,
102                1000,
103                10000,
104                false);
105
106        assertEquals(op1.key, op2.key);
107        assertNotSame(op1.key, op3.key);
108        assertNotSame(op1.key, op4.key);
109        assertNotSame(op1.key, op5.key);
110    }
111
112    @SmallTest
113    public void testCompareTo() {
114        Account dummy = new Account("account1", "type1");
115        Bundle b1 = new Bundle();
116        final long unimportant = 0L;
117        long soon = 1000;
118        long soonFlex = 50;
119        long after = 1500;
120        long afterFlex = 100;
121        SyncOperation op1 = new SyncOperation(dummy, 0, 0, SyncOperation.REASON_PERIODIC,
122                "authority1", b1, soon, soonFlex, unimportant, unimportant, true);
123
124        // Interval disjoint from and after op1.
125        SyncOperation op2 = new SyncOperation(dummy, 0, 0, SyncOperation.REASON_PERIODIC,
126                "authority1", b1, after, afterFlex, unimportant, unimportant, true);
127
128        // Interval equivalent to op1, but expedited.
129        Bundle b2 = new Bundle();
130        b2.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
131        SyncOperation op3 = new SyncOperation(dummy, 0, 0, 0,
132                "authority1", b2, soon, soonFlex, unimportant, unimportant, true);
133
134        // Interval overlaps but not equivalent to op1.
135        SyncOperation op4 = new SyncOperation(dummy, 0, 0, SyncOperation.REASON_PERIODIC,
136                "authority1", b1, soon + 100, soonFlex + 100, unimportant, unimportant, true);
137
138        assertTrue(op1.compareTo(op2) == -1);
139        assertTrue("Less than not transitive.", op2.compareTo(op1) == 1);
140        assertEquals("Expedited not smaller than non-expedited.", -1, op1.compareTo(op3));
141        assertEquals("Greater than not transitive for expedited. ", 1, op3.compareTo(op1));
142        assertTrue("overlapping intervals not compared based on start interval.",
143                op1.compareTo(op4) == -1);
144        assertTrue("overlapping interval comparison not transitive.", op4.compareTo(op1) == 1);
145    }
146}
147