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