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 org.chromium.base.CollectionUtil; |
13 | import org.chromium.base.test.util.AdvancedMockContext; |
14 | import org.chromium.base.test.util.Feature; |
15 | import org.chromium.sync.internal_api.pub.base.ModelType; |
16 | |
17 | import java.util.Arrays; |
18 | import java.util.EnumSet; |
19 | import java.util.HashSet; |
20 | import java.util.Set; |
21 | |
22 | /** |
23 | * Tests for the {@link InvalidationPreferences}. |
24 | * |
25 | * @author dsmyers@google.com (Daniel Myers) |
26 | */ |
27 | public class InvalidationPreferencesTest extends InstrumentationTestCase { |
28 | private Context mContext; |
29 | |
30 | @Override |
31 | protected void setUp() throws Exception { |
32 | super.setUp(); |
33 | mContext = new AdvancedMockContext(getInstrumentation().getContext()); |
34 | } |
35 | |
36 | @SmallTest |
37 | @Feature({"Sync"}) |
38 | public void testTranslateBasicSyncTypes() throws Exception { |
39 | /* |
40 | * Test plan: convert three strings to model types, one of which is invalid. Verify that |
41 | * the two valid strings are properly converted and that the invalid string is dropped. |
42 | */ |
43 | HashSet<ModelType> expectedTypes = CollectionUtil.newHashSet( |
44 | ModelType.BOOKMARK,ModelType.SESSION); |
45 | Set<ModelType> actualTypes = ModelType.syncTypesToModelTypes( |
46 | CollectionUtil.newHashSet("BOOKMARK", "SESSION", "0!!!INVALID")); |
47 | assertEquals(expectedTypes, actualTypes); |
48 | } |
49 | |
50 | @SmallTest |
51 | @Feature({"Sync"}) |
52 | public void testTranslateAllSyncTypes() { |
53 | /* |
54 | * Test plan: convert the special all-types type to model types. Verify that it is |
55 | * properly expanded. |
56 | */ |
57 | Set<ModelType> expectedTypes = EnumSet.allOf(ModelType.class); |
58 | Set<ModelType> actualTypes = ModelType.syncTypesToModelTypes( |
59 | CollectionUtil.newHashSet(ModelType.ALL_TYPES_TYPE)); |
60 | assertEquals(expectedTypes, actualTypes); |
61 | } |
62 | |
63 | @SmallTest |
64 | @Feature({"Sync"}) |
65 | public void testReadMissingData() { |
66 | /* |
67 | * Test plan: read saved state from empty preferences. Verify that null is returned. |
68 | */ |
69 | InvalidationPreferences invPreferences = new InvalidationPreferences(mContext); |
70 | assertNull(invPreferences.getSavedSyncedAccount()); |
71 | assertNull(invPreferences.getSavedSyncedTypes()); |
72 | assertNull(invPreferences.getInternalNotificationClientState()); |
73 | } |
74 | |
75 | @SmallTest |
76 | @Feature({"Sync"}) |
77 | public void testReadWriteAndReadData() { |
78 | /* |
79 | * Test plan: write and read back saved state. Verify that the returned state is what |
80 | * was written. |
81 | */ |
82 | InvalidationPreferences invPreferences = new InvalidationPreferences(mContext); |
83 | InvalidationPreferences.EditContext editContext = invPreferences.edit(); |
84 | |
85 | // We should never write both a real type and the all-types type in practice, but we test |
86 | // with them here to ensure that preferences are not interpreting the written data. |
87 | Set<String> syncTypes = CollectionUtil.newHashSet("BOOKMARK", ModelType.ALL_TYPES_TYPE); |
88 | Account account = new Account("test@example.com", "bogus"); |
89 | byte[] internalClientState = new byte[]{100,101,102}; |
90 | invPreferences.setSyncTypes(editContext, syncTypes); |
91 | invPreferences.setAccount(editContext, account); |
92 | invPreferences.setInternalNotificationClientState(editContext, internalClientState); |
93 | |
94 | // Nothing should yet have been written. |
95 | assertNull(invPreferences.getSavedSyncedAccount()); |
96 | assertNull(invPreferences.getSavedSyncedTypes()); |
97 | |
98 | // Write the new data and verify that they are correctly read back. |
99 | invPreferences.commit(editContext); |
100 | assertEquals(account, invPreferences.getSavedSyncedAccount()); |
101 | assertEquals(syncTypes, invPreferences.getSavedSyncedTypes()); |
102 | assertTrue(Arrays.equals( |
103 | internalClientState, invPreferences.getInternalNotificationClientState())); |
104 | } |
105 | } |