- add sources.
[platform/framework/web/crosswalk.git] / src / sync / android / javatests / src / org / chromium / sync / notifier / InvalidationPreferencesTest.java
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.sync.notifier;
6
7 import android.accounts.Account;
8 import android.content.Context;
9 import android.test.InstrumentationTestCase;
10 import android.test.suitebuilder.annotation.SmallTest;
11
12 import com.google.ipc.invalidation.external.client.types.ObjectId;
13
14 import org.chromium.base.CollectionUtil;
15 import org.chromium.base.test.util.AdvancedMockContext;
16 import org.chromium.base.test.util.Feature;
17 import org.chromium.sync.internal_api.pub.base.ModelType;
18
19 import java.util.Arrays;
20 import java.util.EnumSet;
21 import java.util.HashSet;
22 import java.util.Set;
23
24 /**
25  * Tests for the {@link InvalidationPreferences}.
26  *
27  * @author dsmyers@google.com (Daniel Myers)
28  */
29 public class InvalidationPreferencesTest extends InstrumentationTestCase {
30     private Context mContext;
31
32     @Override
33     protected void setUp() throws Exception {
34         super.setUp();
35         mContext = new AdvancedMockContext(getInstrumentation().getContext());
36     }
37
38     @SmallTest
39     @Feature({"Sync"})
40     public void testTranslateBasicSyncTypes() throws Exception {
41         /*
42          * Test plan: convert three strings to model types, one of which is invalid. Verify that
43          * the two valid strings are properly converted and that the invalid string is dropped.
44          */
45         HashSet<ModelType> expectedTypes = CollectionUtil.newHashSet(
46                 ModelType.BOOKMARK,ModelType.SESSION);
47         Set<ModelType> actualTypes = ModelType.syncTypesToModelTypes(
48                 CollectionUtil.newHashSet("BOOKMARK", "SESSION", "0!!!INVALID"));
49         assertEquals(expectedTypes, actualTypes);
50     }
51
52     @SmallTest
53     @Feature({"Sync"})
54     public void testTranslateAllSyncTypes() {
55         /*
56          * Test plan: convert the special all-types type to model types. Verify that it is
57          * properly expanded.
58          */
59         Set<ModelType> expectedTypes = EnumSet.allOf(ModelType.class);
60         Set<ModelType> actualTypes = ModelType.syncTypesToModelTypes(
61                 CollectionUtil.newHashSet(ModelType.ALL_TYPES_TYPE));
62         assertEquals(expectedTypes, actualTypes);
63     }
64
65     @SmallTest
66     @Feature({"Sync"})
67     public void testReadMissingData() {
68         /*
69          * Test plan: read saved state from empty preferences. Verify that null is returned.
70          */
71         InvalidationPreferences invPreferences = new InvalidationPreferences(mContext);
72         assertNull(invPreferences.getSavedSyncedAccount());
73         assertNull(invPreferences.getSavedSyncedTypes());
74         assertNull(invPreferences.getSavedObjectIds());
75         assertNull(invPreferences.getInternalNotificationClientState());
76     }
77
78     @SmallTest
79     @Feature({"Sync"})
80     public void testReadWriteAndReadData() {
81         /*
82          * Test plan: write and read back saved state. Verify that the returned state is what
83          * was written.
84          */
85         InvalidationPreferences invPreferences = new InvalidationPreferences(mContext);
86         InvalidationPreferences.EditContext editContext = invPreferences.edit();
87
88         // We should never write both a real type and the all-types type in practice, but we test
89         // with them here to ensure that preferences are not interpreting the written data.
90         Set<String> syncTypes = CollectionUtil.newHashSet("BOOKMARK", ModelType.ALL_TYPES_TYPE);
91         Set<ObjectId> objectIds = CollectionUtil.newHashSet(
92                 ObjectId.newInstance(1, "obj1".getBytes()),
93                 ObjectId.newInstance(2, "obj2".getBytes()));
94         Account account = new Account("test@example.com", "bogus");
95         byte[] internalClientState = new byte[]{100,101,102};
96         invPreferences.setSyncTypes(editContext, syncTypes);
97         invPreferences.setObjectIds(editContext, objectIds);
98         invPreferences.setAccount(editContext, account);
99         invPreferences.setInternalNotificationClientState(editContext, internalClientState);
100
101         // Nothing should yet have been written.
102         assertNull(invPreferences.getSavedSyncedAccount());
103         assertNull(invPreferences.getSavedSyncedTypes());
104         assertNull(invPreferences.getSavedObjectIds());
105
106         // Write the new data and verify that they are correctly read back.
107         invPreferences.commit(editContext);
108         assertEquals(account, invPreferences.getSavedSyncedAccount());
109         assertEquals(syncTypes, invPreferences.getSavedSyncedTypes());
110         assertEquals(objectIds, invPreferences.getSavedObjectIds());
111         assertTrue(Arrays.equals(
112                 internalClientState, invPreferences.getInternalNotificationClientState()));
113     }
114 }