summaryrefslogtreecommitdiffstats
path: root/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationPreferencesTest.java
blob: 4bcfd20f0844a8a25508da66ef323606d009bd22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.sync.notifier;

import android.accounts.Account;
import android.content.Context;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import com.google.ipc.invalidation.external.client.types.ObjectId;

import org.chromium.base.CollectionUtil;
import org.chromium.base.test.util.AdvancedMockContext;
import org.chromium.base.test.util.Feature;

import java.util.Arrays;
import java.util.Set;

/**
 * Tests for the {@link InvalidationPreferences}.
 *
 * @author dsmyers@google.com (Daniel Myers)
 */
public class InvalidationPreferencesTest extends InstrumentationTestCase {
    private Context mContext;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mContext = new AdvancedMockContext(getInstrumentation().getContext());
    }

    @SmallTest
    @Feature({"Sync"})
    public void testReadMissingData() {
        /*
         * Test plan: read saved state from empty preferences. Verify that null is returned.
         */
        InvalidationPreferences invPreferences = new InvalidationPreferences(mContext);
        assertNull(invPreferences.getSavedSyncedAccount());
        assertNull(invPreferences.getSavedSyncedTypes());
        assertNull(invPreferences.getSavedObjectIds());
        assertNull(invPreferences.getInternalNotificationClientState());
    }

    @SmallTest
    @Feature({"Sync"})
    public void testReadWriteAndReadData() {
        /*
         * Test plan: write and read back saved state. Verify that the returned state is what
         * was written.
         */
        InvalidationPreferences invPreferences = new InvalidationPreferences(mContext);
        InvalidationPreferences.EditContext editContext = invPreferences.edit();

        // Write mix of valid and invalid types to disk to test that preferences are not
        // interpreting the data. Invalid types should never be written to disk in practice.
        Set<String> syncTypes = CollectionUtil.newHashSet("BOOKMARK", "INVALID");
        Set<ObjectId> objectIds = CollectionUtil.newHashSet(
                ObjectId.newInstance(1, "obj1".getBytes()),
                ObjectId.newInstance(2, "obj2".getBytes()));
        Account account = new Account("test@example.com", "bogus");
        byte[] internalClientState = new byte[]{100, 101, 102};
        invPreferences.setSyncTypes(editContext, syncTypes);
        invPreferences.setObjectIds(editContext, objectIds);
        invPreferences.setAccount(editContext, account);
        invPreferences.setInternalNotificationClientState(editContext, internalClientState);

        // Nothing should yet have been written.
        assertNull(invPreferences.getSavedSyncedAccount());
        assertNull(invPreferences.getSavedSyncedTypes());
        assertNull(invPreferences.getSavedObjectIds());

        // Write the new data and verify that they are correctly read back.
        invPreferences.commit(editContext);
        assertEquals(account, invPreferences.getSavedSyncedAccount());
        assertEquals(syncTypes, invPreferences.getSavedSyncedTypes());
        assertEquals(objectIds, invPreferences.getSavedObjectIds());
        assertTrue(Arrays.equals(
                internalClientState, invPreferences.getInternalNotificationClientState()));
    }
}