OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.omaha; | 5 package org.chromium.chrome.browser.omaha; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.support.test.InstrumentationRegistry; |
8 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
9 import android.test.InstrumentationTestCase; | 10 |
| 11 import org.junit.Assert; |
| 12 import org.junit.Test; |
| 13 import org.junit.runner.RunWith; |
10 | 14 |
11 import org.chromium.base.test.util.AdvancedMockContext; | 15 import org.chromium.base.test.util.AdvancedMockContext; |
12 import org.chromium.base.test.util.Feature; | 16 import org.chromium.base.test.util.Feature; |
| 17 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
13 import org.chromium.chrome.test.omaha.AttributeFinder; | 18 import org.chromium.chrome.test.omaha.AttributeFinder; |
14 import org.chromium.chrome.test.omaha.MockRequestGenerator; | 19 import org.chromium.chrome.test.omaha.MockRequestGenerator; |
15 import org.chromium.chrome.test.omaha.MockRequestGenerator.DeviceType; | 20 import org.chromium.chrome.test.omaha.MockRequestGenerator.DeviceType; |
16 | 21 |
17 /** | 22 /** |
18 * Unit tests for the RequestGenerator class. | 23 * Unit tests for the RequestGenerator class. |
19 */ | 24 */ |
20 public class RequestGeneratorTest extends InstrumentationTestCase { | 25 @RunWith(ChromeJUnit4ClassRunner.class) |
| 26 public class RequestGeneratorTest { |
21 private static final String INSTALL_SOURCE = "install_source"; | 27 private static final String INSTALL_SOURCE = "install_source"; |
22 | 28 |
| 29 @Test |
23 @SmallTest | 30 @SmallTest |
24 @Feature({"Omaha"}) | 31 @Feature({"Omaha"}) |
25 public void testInstallAgeNewInstallation() { | 32 public void testInstallAgeNewInstallation() { |
26 long currentTimestamp = 201207310000L; | 33 long currentTimestamp = 201207310000L; |
27 long installTimestamp = 198401160000L; | 34 long installTimestamp = 198401160000L; |
28 boolean installing = true; | 35 boolean installing = true; |
29 long expectedAge = RequestGenerator.INSTALL_AGE_IMMEDIATELY_AFTER_INSTAL
LING; | 36 long expectedAge = RequestGenerator.INSTALL_AGE_IMMEDIATELY_AFTER_INSTAL
LING; |
30 checkInstallAge(currentTimestamp, installTimestamp, installing, expected
Age); | 37 checkInstallAge(currentTimestamp, installTimestamp, installing, expected
Age); |
31 } | 38 } |
32 | 39 |
| 40 @Test |
33 @SmallTest | 41 @SmallTest |
34 @Feature({"Omaha"}) | 42 @Feature({"Omaha"}) |
35 public void testInstallAge() { | 43 public void testInstallAge() { |
36 long currentTimestamp = 201207310000L; | 44 long currentTimestamp = 201207310000L; |
37 long installTimestamp = 198401160000L; | 45 long installTimestamp = 198401160000L; |
38 boolean installing = false; | 46 boolean installing = false; |
39 long expectedAge = 32; | 47 long expectedAge = 32; |
40 checkInstallAge(currentTimestamp, installTimestamp, installing, expected
Age); | 48 checkInstallAge(currentTimestamp, installTimestamp, installing, expected
Age); |
41 } | 49 } |
42 | 50 |
43 /** | 51 /** |
44 * Checks whether the install age function is behaving according to spec. | 52 * Checks whether the install age function is behaving according to spec. |
45 */ | 53 */ |
46 void checkInstallAge(long currentTimestamp, long installTimestamp, boolean i
nstalling, | 54 void checkInstallAge(long currentTimestamp, long installTimestamp, boolean i
nstalling, |
47 long expectedAge) { | 55 long expectedAge) { |
48 long actualAge = RequestGenerator.installAge(currentTimestamp, installTi
mestamp, | 56 long actualAge = RequestGenerator.installAge(currentTimestamp, installTi
mestamp, |
49 installing); | 57 installing); |
50 assertEquals("Install ages differed.", expectedAge, actualAge); | 58 Assert.assertEquals("Install ages differed.", expectedAge, actualAge); |
51 } | 59 } |
52 | 60 |
| 61 @Test |
53 @SmallTest | 62 @SmallTest |
54 @Feature({"Omaha"}) | 63 @Feature({"Omaha"}) |
55 public void testHandsetXMLCreationWithInstall() { | 64 public void testHandsetXMLCreationWithInstall() { |
56 createAndCheckXML(DeviceType.HANDSET, true); | 65 createAndCheckXML(DeviceType.HANDSET, true); |
57 } | 66 } |
58 | 67 |
| 68 @Test |
59 @SmallTest | 69 @SmallTest |
60 @Feature({"Omaha"}) | 70 @Feature({"Omaha"}) |
61 public void testHandsetXMLCreationWithoutInstall() { | 71 public void testHandsetXMLCreationWithoutInstall() { |
62 createAndCheckXML(DeviceType.HANDSET, false); | 72 createAndCheckXML(DeviceType.HANDSET, false); |
63 } | 73 } |
64 | 74 |
| 75 @Test |
65 @SmallTest | 76 @SmallTest |
66 @Feature({"Omaha"}) | 77 @Feature({"Omaha"}) |
67 public void testTabletXMLCreationWithInstall() { | 78 public void testTabletXMLCreationWithInstall() { |
68 createAndCheckXML(DeviceType.TABLET, true); | 79 createAndCheckXML(DeviceType.TABLET, true); |
69 } | 80 } |
70 | 81 |
| 82 @Test |
71 @SmallTest | 83 @SmallTest |
72 @Feature({"Omaha"}) | 84 @Feature({"Omaha"}) |
73 public void testTabletXMLCreationWithoutInstall() { | 85 public void testTabletXMLCreationWithoutInstall() { |
74 createAndCheckXML(DeviceType.TABLET, false); | 86 createAndCheckXML(DeviceType.TABLET, false); |
75 } | 87 } |
76 | 88 |
77 /** | 89 /** |
78 * Checks that the XML is being created properly. | 90 * Checks that the XML is being created properly. |
79 */ | 91 */ |
80 private RequestGenerator createAndCheckXML(DeviceType deviceType, boolean se
ndInstallEvent) { | 92 private RequestGenerator createAndCheckXML(DeviceType deviceType, boolean se
ndInstallEvent) { |
81 Context targetContext = getInstrumentation().getTargetContext(); | 93 Context targetContext = InstrumentationRegistry.getInstrumentation().get
TargetContext(); |
82 AdvancedMockContext context = new AdvancedMockContext(targetContext); | 94 AdvancedMockContext context = new AdvancedMockContext(targetContext); |
83 | 95 |
84 String sessionId = "random_session_id"; | 96 String sessionId = "random_session_id"; |
85 String requestId = "random_request_id"; | 97 String requestId = "random_request_id"; |
86 String version = "1.2.3.4"; | 98 String version = "1.2.3.4"; |
87 long installAge = 42; | 99 long installAge = 42; |
88 | 100 |
89 MockRequestGenerator generator = new MockRequestGenerator(context, devic
eType); | 101 MockRequestGenerator generator = new MockRequestGenerator(context, devic
eType); |
90 | 102 |
91 String xml = null; | 103 String xml = null; |
92 try { | 104 try { |
93 RequestData data = new RequestData(sendInstallEvent, 0, requestId, I
NSTALL_SOURCE); | 105 RequestData data = new RequestData(sendInstallEvent, 0, requestId, I
NSTALL_SOURCE); |
94 xml = generator.generateXML(sessionId, version, installAge, data); | 106 xml = generator.generateXML(sessionId, version, installAge, data); |
95 } catch (RequestFailureException e) { | 107 } catch (RequestFailureException e) { |
96 fail("XML generation failed."); | 108 Assert.fail("XML generation failed."); |
97 } | 109 } |
98 | 110 |
99 checkForAttributeAndValue(xml, "request", "sessionid", "{" + sessionId +
"}"); | 111 checkForAttributeAndValue(xml, "request", "sessionid", "{" + sessionId +
"}"); |
100 checkForAttributeAndValue(xml, "request", "requestid", "{" + requestId +
"}"); | 112 checkForAttributeAndValue(xml, "request", "requestid", "{" + requestId +
"}"); |
101 checkForAttributeAndValue(xml, "request", "installsource", INSTALL_SOURC
E); | 113 checkForAttributeAndValue(xml, "request", "installsource", INSTALL_SOURC
E); |
102 checkForAttributeAndValue(xml, "request", | 114 checkForAttributeAndValue(xml, "request", |
103 MockRequestGenerator.REQUEST_ATTRIBUTE_1, MockRequestGenerator.R
EQUEST_VALUE_1); | 115 MockRequestGenerator.REQUEST_ATTRIBUTE_1, MockRequestGenerator.R
EQUEST_VALUE_1); |
104 checkForAttributeAndValue(xml, "request", | 116 checkForAttributeAndValue(xml, "request", |
105 MockRequestGenerator.REQUEST_ATTRIBUTE_2, MockRequestGenerator.R
EQUEST_VALUE_2); | 117 MockRequestGenerator.REQUEST_ATTRIBUTE_2, MockRequestGenerator.R
EQUEST_VALUE_2); |
106 | 118 |
107 checkForAttributeAndValue(xml, "app", "version", version); | 119 checkForAttributeAndValue(xml, "app", "version", version); |
108 checkForAttributeAndValue(xml, "app", "lang", generator.getLanguage()); | 120 checkForAttributeAndValue(xml, "app", "lang", generator.getLanguage()); |
109 checkForAttributeAndValue(xml, "app", "brand", generator.getBrand()); | 121 checkForAttributeAndValue(xml, "app", "brand", generator.getBrand()); |
110 checkForAttributeAndValue(xml, "app", "client", generator.getClient()); | 122 checkForAttributeAndValue(xml, "app", "client", generator.getClient()); |
111 checkForAttributeAndValue(xml, "app", "appid", generator.getAppId()); | 123 checkForAttributeAndValue(xml, "app", "appid", generator.getAppId()); |
112 checkForAttributeAndValue(xml, "app", "installage", String.valueOf(insta
llAge)); | 124 checkForAttributeAndValue(xml, "app", "installage", String.valueOf(insta
llAge)); |
113 checkForAttributeAndValue(xml, "app", "ap", generator.getAdditionalParam
eters()); | 125 checkForAttributeAndValue(xml, "app", "ap", generator.getAdditionalParam
eters()); |
114 checkForAttributeAndValue(xml, "app", | 126 checkForAttributeAndValue(xml, "app", |
115 MockRequestGenerator.APP_ATTRIBUTE_1, MockRequestGenerator.APP_V
ALUE_1); | 127 MockRequestGenerator.APP_ATTRIBUTE_1, MockRequestGenerator.APP_V
ALUE_1); |
116 checkForAttributeAndValue(xml, "app", | 128 checkForAttributeAndValue(xml, "app", |
117 MockRequestGenerator.APP_ATTRIBUTE_2, MockRequestGenerator.APP_V
ALUE_2); | 129 MockRequestGenerator.APP_ATTRIBUTE_2, MockRequestGenerator.APP_V
ALUE_2); |
118 | 130 |
119 if (sendInstallEvent) { | 131 if (sendInstallEvent) { |
120 checkForAttributeAndValue(xml, "event", "eventtype", "2"); | 132 checkForAttributeAndValue(xml, "event", "eventtype", "2"); |
121 checkForAttributeAndValue(xml, "event", "eventresult", "1"); | 133 checkForAttributeAndValue(xml, "event", "eventresult", "1"); |
122 assertFalse("Ping and install event are mutually exclusive", | 134 Assert.assertFalse( |
123 checkForTag(xml, "ping")); | 135 "Ping and install event are mutually exclusive", checkForTag
(xml, "ping")); |
124 assertFalse("Update check and install event are mutually exclusive", | 136 Assert.assertFalse("Update check and install event are mutually excl
usive", |
125 checkForTag(xml, "updatecheck")); | 137 checkForTag(xml, "updatecheck")); |
126 } else { | 138 } else { |
127 assertFalse("Update check and install event are mutually exclusive", | 139 Assert.assertFalse("Update check and install event are mutually excl
usive", |
128 checkForTag(xml, "event")); | 140 checkForTag(xml, "event")); |
129 checkForAttributeAndValue(xml, "ping", "active", "1"); | 141 checkForAttributeAndValue(xml, "ping", "active", "1"); |
130 assertTrue("Update check and install event are mutually exclusive", | 142 Assert.assertTrue("Update check and install event are mutually exclu
sive", |
131 checkForTag(xml, "updatecheck")); | 143 checkForTag(xml, "updatecheck")); |
132 } | 144 } |
133 | 145 |
134 return generator; | 146 return generator; |
135 } | 147 } |
136 | 148 |
137 private boolean checkForTag(String xml, String tag) { | 149 private boolean checkForTag(String xml, String tag) { |
138 return new AttributeFinder(xml, tag, null).isTagFound(); | 150 return new AttributeFinder(xml, tag, null).isTagFound(); |
139 } | 151 } |
140 | 152 |
141 private void checkForAttributeAndValue( | 153 private void checkForAttributeAndValue( |
142 String xml, String tag, String attribute, String expectedValue) { | 154 String xml, String tag, String attribute, String expectedValue) { |
143 // Check that the attribute exists for the tag and that the value matche
s. | 155 // Check that the attribute exists for the tag and that the value matche
s. |
144 AttributeFinder finder = new AttributeFinder(xml, tag, attribute); | 156 AttributeFinder finder = new AttributeFinder(xml, tag, attribute); |
145 assertTrue("Couldn't find tag '" + tag + "'", finder.isTagFound()); | 157 Assert.assertTrue("Couldn't find tag '" + tag + "'", finder.isTagFound()
); |
146 assertEquals("Bad value found for tag '" + tag + "' and attribute '" + a
ttribute + "'", | 158 Assert.assertEquals( |
| 159 "Bad value found for tag '" + tag + "' and attribute '" + attrib
ute + "'", |
147 expectedValue, finder.getValue()); | 160 expectedValue, finder.getValue()); |
148 } | 161 } |
149 } | 162 } |
OLD | NEW |