- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / android / javatests / src / org / chromium / chrome / browser / identity / SettingsSecureBasedIdentificationGeneratorTest.java
1 // Copyright (c) 2012 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.chrome.browser.identity;
6
7 import android.test.InstrumentationTestCase;
8 import android.test.suitebuilder.annotation.SmallTest;
9
10 import org.chromium.base.test.util.AdvancedMockContext;
11 import org.chromium.base.test.util.Feature;
12 import org.chromium.chrome.browser.util.HashUtil;
13
14 public class SettingsSecureBasedIdentificationGeneratorTest extends InstrumentationTestCase {
15
16     private static final String FLAG_ANDROID_ID = "android_id";
17
18     @SmallTest
19     @Feature({"ChromeToMobile", "Omaha"})
20     public void testAndroidIdSuccessWithSalt() {
21         String androidId = "42";
22         String salt = "mySalt";
23         String expected = HashUtil.getMd5Hash(new HashUtil.Params(androidId).withSalt(salt));
24         runTest(androidId, salt, expected);
25     }
26
27     @SmallTest
28     @Feature({"ChromeToMobile", "Omaha"})
29     public void testAndroidIdSuccessWithoutSalt() {
30         String androidId = "42";
31         String expected = HashUtil.getMd5Hash(new HashUtil.Params(androidId));
32         runTest(androidId, null, expected);
33     }
34
35     @SmallTest
36     @Feature({"ChromeToMobile", "Omaha"})
37     public void testAndroidIdFailureWithSalt() {
38         String androidId = null;
39         String salt = "mySalt";
40         String expected = "";
41         runTest(androidId, salt, expected);
42     }
43
44     @SmallTest
45     @Feature({"ChromeToMobile", "Omaha"})
46     public void testAndroidIdFailureWithoutSalt() {
47         String androidId = null;
48         String salt = null;
49         String expected = "";
50         runTest(androidId, salt, expected);
51     }
52
53     private void runTest(String androidId, String salt, String expectedUniqueId) {
54         AdvancedMockContext context = new AdvancedMockContext();
55         TestGenerator generator = new TestGenerator(context, androidId);
56
57         // Get a unique ID and ensure it is as expected.
58         String result = generator.getUniqueId(salt);
59         assertEquals(expectedUniqueId, result);
60     }
61
62     private static class TestGenerator extends SettingsSecureBasedIdentificationGenerator {
63         private final AdvancedMockContext mContext;
64         private final String mAndroidId;
65
66         TestGenerator(AdvancedMockContext context, String androidId) {
67             super(context);
68             mContext = context;
69             mAndroidId = androidId;
70         }
71
72         @Override
73         String getAndroidId() {
74             mContext.setFlag(FLAG_ANDROID_ID);
75             return mAndroidId;
76         }
77     }
78 }