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.
5 package org.chromium.sync.notifier;
7 import android.accounts.Account;
8 import android.content.Context;
9 import android.test.InstrumentationTestCase;
10 import android.test.suitebuilder.annotation.SmallTest;
12 import com.google.ipc.invalidation.external.client.types.ObjectId;
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;
19 import java.util.Arrays;
20 import java.util.EnumSet;
21 import java.util.HashSet;
25 * Tests for the {@link InvalidationPreferences}.
27 * @author dsmyers@google.com (Daniel Myers)
29 public class InvalidationPreferencesTest extends InstrumentationTestCase {
30 private Context mContext;
33 protected void setUp() throws Exception {
35 mContext = new AdvancedMockContext(getInstrumentation().getContext());
40 public void testTranslateBasicSyncTypes() throws Exception {
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.
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);
54 public void testTranslateAllSyncTypes() {
56 * Test plan: convert the special all-types type to model types. Verify that it is
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);
67 public void testReadMissingData() {
69 * Test plan: read saved state from empty preferences. Verify that null is returned.
71 InvalidationPreferences invPreferences = new InvalidationPreferences(mContext);
72 assertNull(invPreferences.getSavedSyncedAccount());
73 assertNull(invPreferences.getSavedSyncedTypes());
74 assertNull(invPreferences.getSavedObjectIds());
75 assertNull(invPreferences.getInternalNotificationClientState());
80 public void testReadWriteAndReadData() {
82 * Test plan: write and read back saved state. Verify that the returned state is what
85 InvalidationPreferences invPreferences = new InvalidationPreferences(mContext);
86 InvalidationPreferences.EditContext editContext = invPreferences.edit();
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);
101 // Nothing should yet have been written.
102 assertNull(invPreferences.getSavedSyncedAccount());
103 assertNull(invPreferences.getSavedSyncedTypes());
104 assertNull(invPreferences.getSavedObjectIds());
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()));