1/*
2 * Copyright (C) 2007 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 android.content;
18
19import android.test.AndroidTestCase;
20import android.test.RenamingDelegatingContext;
21import android.test.mock.MockContext;
22import android.test.mock.MockContentResolver;
23import android.accounts.Account;
24import android.os.Bundle;
25import android.os.SystemClock;
26
27public class SyncQueueTest extends AndroidTestCase {
28    private static final Account ACCOUNT1 = new Account("test.account1", "test.type1");
29    private static final Account ACCOUNT2 = new Account("test.account2", "test.type2");
30    private static final String AUTHORITY1 = "test.authority1";
31    private static final String AUTHORITY2 = "test.authority2";
32    private static final String AUTHORITY3 = "test.authority3";
33
34    private SyncStorageEngine mSettings;
35    private SyncQueue mSyncQueue;
36
37    @Override
38    protected void setUp() throws Exception {
39        super.setUp();
40        MockContentResolver mockResolver = new MockContentResolver();
41        mSettings = SyncStorageEngine.newTestInstance(new TestContext(mockResolver, getContext()));
42        mSyncQueue = new SyncQueue(mSettings);
43    }
44
45    public void testSyncQueueOrder() throws Exception {
46        final SyncOperation op1 = new SyncOperation(
47                ACCOUNT1, SyncStorageEngine.SOURCE_USER, AUTHORITY1, newTestBundle("1"), 0);
48        final SyncOperation op2 = new SyncOperation(
49                ACCOUNT2, SyncStorageEngine.SOURCE_USER, AUTHORITY2, newTestBundle("2"), 100);
50        final SyncOperation op3 = new SyncOperation(
51                ACCOUNT1, SyncStorageEngine.SOURCE_USER, AUTHORITY1, newTestBundle("3"), 150);
52        final SyncOperation op4 = new SyncOperation(
53                ACCOUNT2, SyncStorageEngine.SOURCE_USER, AUTHORITY2, newTestBundle("4"), 60);
54        final SyncOperation op5 = new SyncOperation(
55                ACCOUNT1, SyncStorageEngine.SOURCE_USER, AUTHORITY1, newTestBundle("5"), 80);
56        final SyncOperation op6 = new SyncOperation(
57                ACCOUNT2, SyncStorageEngine.SOURCE_USER, AUTHORITY2, newTestBundle("6"), 0);
58        op6.expedited = true;
59
60        mSyncQueue.add(op1);
61        mSyncQueue.add(op2);
62        mSyncQueue.add(op3);
63        mSyncQueue.add(op4);
64        mSyncQueue.add(op5);
65        mSyncQueue.add(op6);
66
67        long now = SystemClock.elapsedRealtime() + 200;
68
69        assertEquals(op6, mSyncQueue.nextReadyToRun(now).first);
70        mSyncQueue.remove(op6);
71
72        assertEquals(op1, mSyncQueue.nextReadyToRun(now).first);
73        mSyncQueue.remove(op1);
74
75        assertEquals(op4, mSyncQueue.nextReadyToRun(now).first);
76        mSyncQueue.remove(op4);
77
78        assertEquals(op5, mSyncQueue.nextReadyToRun(now).first);
79        mSyncQueue.remove(op5);
80
81        assertEquals(op2, mSyncQueue.nextReadyToRun(now).first);
82        mSyncQueue.remove(op2);
83
84        assertEquals(op3, mSyncQueue.nextReadyToRun(now).first);
85        mSyncQueue.remove(op3);
86    }
87
88    public void testOrderWithBackoff() throws Exception {
89        final SyncOperation op1 = new SyncOperation(
90                ACCOUNT1, SyncStorageEngine.SOURCE_USER, AUTHORITY1, newTestBundle("1"), 0);
91        final SyncOperation op2 = new SyncOperation(
92                ACCOUNT2, SyncStorageEngine.SOURCE_USER, AUTHORITY2, newTestBundle("2"), 100);
93        final SyncOperation op3 = new SyncOperation(
94                ACCOUNT1, SyncStorageEngine.SOURCE_USER, AUTHORITY1, newTestBundle("3"), 150);
95        final SyncOperation op4 = new SyncOperation(
96                ACCOUNT2, SyncStorageEngine.SOURCE_USER, AUTHORITY3, newTestBundle("4"), 60);
97        final SyncOperation op5 = new SyncOperation(
98                ACCOUNT1, SyncStorageEngine.SOURCE_USER, AUTHORITY1, newTestBundle("5"), 80);
99        final SyncOperation op6 = new SyncOperation(
100                ACCOUNT2, SyncStorageEngine.SOURCE_USER, AUTHORITY2, newTestBundle("6"), 0);
101        op6.expedited = true;
102
103        mSyncQueue.add(op1);
104        mSyncQueue.add(op2);
105        mSyncQueue.add(op3);
106        mSyncQueue.add(op4);
107        mSyncQueue.add(op5);
108        mSyncQueue.add(op6);
109
110        long now = SystemClock.elapsedRealtime() + 200;
111
112        assertEquals(op6, mSyncQueue.nextReadyToRun(now).first);
113        mSyncQueue.remove(op6);
114
115        assertEquals(op1, mSyncQueue.nextReadyToRun(now).first);
116        mSyncQueue.remove(op1);
117
118        mSettings.setBackoff(ACCOUNT2,  AUTHORITY3, now + 200, 5);
119        assertEquals(op5, mSyncQueue.nextReadyToRun(now).first);
120
121        mSettings.setBackoff(ACCOUNT2,  AUTHORITY3, SyncStorageEngine.NOT_IN_BACKOFF_MODE, 0);
122        assertEquals(op4, mSyncQueue.nextReadyToRun(now).first);
123
124        mSettings.setDelayUntilTime(ACCOUNT2,  AUTHORITY3, now + 200);
125        assertEquals(op5, mSyncQueue.nextReadyToRun(now).first);
126
127        mSettings.setDelayUntilTime(ACCOUNT2,  AUTHORITY3, 0);
128        assertEquals(op4, mSyncQueue.nextReadyToRun(now).first);
129        mSyncQueue.remove(op4);
130
131        assertEquals(op5, mSyncQueue.nextReadyToRun(now).first);
132        mSyncQueue.remove(op5);
133
134        assertEquals(op2, mSyncQueue.nextReadyToRun(now).first);
135        mSyncQueue.remove(op2);
136
137        assertEquals(op3, mSyncQueue.nextReadyToRun(now).first);
138        mSyncQueue.remove(op3);
139    }
140
141    Bundle newTestBundle(String val) {
142        Bundle bundle = new Bundle();
143        bundle.putString("test", val);
144        return bundle;
145    }
146
147    static class TestContext extends ContextWrapper {
148        ContentResolver mResolver;
149
150        public TestContext(ContentResolver resolver, Context realContext) {
151            super(new RenamingDelegatingContext(new MockContext(), realContext, "test."));
152            mResolver = resolver;
153        }
154
155        @Override
156        public void enforceCallingOrSelfPermission(String permission, String message) {
157        }
158
159        @Override
160        public ContentResolver getContentResolver() {
161            return mResolver;
162        }
163    }
164}
165