Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/omaha/ResponseParserTest.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/omaha/ResponseParserTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/omaha/ResponseParserTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bac1f92d770700a1fdc163a9294ddc25077c6da8 |
| --- /dev/null |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/omaha/ResponseParserTest.java |
| @@ -0,0 +1,241 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.omaha; |
| + |
| +import android.test.InstrumentationTestCase; |
| +import android.test.suitebuilder.annotation.SmallTest; |
| + |
| +import org.chromium.base.test.util.Feature; |
| + |
| +public class ResponseParserTest extends InstrumentationTestCase { |
| + // Note that the Omaha server appends "/" to the end of the URL codebase. |
| + private static final String STRIPPED_MARKET_URL = |
| + "https://market.android.com/details?id=com.google.android.apps.chrome"; |
|
nyquist
2013/11/11 17:43:41
Is this still the URL we want?
Could we use https:
gone
2013/11/11 18:22:34
Done.
|
| + private static final String MARKET_URL = STRIPPED_MARKET_URL + "/"; |
| + |
| + private static final String UPDATE_STATUS_OK = "ok"; |
| + private static final String UPDATE_STATUS_NOUPDATE = "noupdate"; |
| + private static final String UPDATE_STATUS_ERROR = "error-osnotsupported"; |
| + private static final String UPDATE_STATUS_WTF = "omgwtfbbq"; |
| + |
| + private static final String VALID_XML = |
| + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| + "<response protocol=\"3.0\" server=\"prod\">" + |
| + " <daystart elapsed_seconds=\"12345\"/>" + |
| + " <app appid=\"{APP_ID}\" status=\"ok\">" + |
| + " <updatecheck status=\"" + UPDATE_STATUS_OK + "\">" + |
| + " <urls>" + |
| + " <url codebase=\"" + MARKET_URL + "\"/>" + |
| + " </urls>" + |
| + " <manifest version=\"1.2.3.4\">" + |
| + " <packages>" + |
| + " <package hash=\"0\" name=\"dummy.apk\" required=\"true\" size=\"0\"/>" + |
| + " </packages>" + |
| + " <actions>" + |
| + " <action event=\"install\" run=\"dummy.apk\"/>" + |
| + " <action event=\"postinstall\"/>" + |
| + " </actions>" + |
| + " </manifest>" + |
| + " </updatecheck>" + |
| + " <ping status=\"ok\"/>" + |
| + " </app>" + |
| + "</response>"; |
| + |
| + private static final String VALID_XML_WITH_INSTALL = |
| + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| + "<response protocol=\"3.0\" server=\"prod\">" + |
| + " <daystart elapsed_seconds=\"12345\"/>" + |
| + " <app appid=\"{APP_ID}\" status=\"ok\">" + |
| + " <event status=\"ok\"/>" + |
| + " </app>" + |
| + "</response>"; |
| + |
| + private static final String VALID_XML_UP_TO_DATE = |
| + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| + "<response protocol=\"3.0\" server=\"prod\">" + |
| + " <daystart elapsed_seconds=\"12345\"/>" + |
| + " <app appid=\"{APP_ID}\" status=\"ok\">" + |
| + " <updatecheck status=\"" + UPDATE_STATUS_NOUPDATE + "\"/>" + |
| + " <ping status=\"ok\"/>" + |
| + " </app>" + |
| + "</response>"; |
| + |
| + private static final String UPDATE_NO_PING = |
| + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| + "<response protocol=\"3.0\" server=\"prod\">" + |
| + " <daystart elapsed_seconds=\"12345\"/>" + |
| + " <app appid=\"{APP_ID}\" status=\"ok\">" + |
| + " <updatecheck status=\"" + UPDATE_STATUS_OK + "\">" + |
| + " <urls>" + |
| + " <url codebase=\"" + MARKET_URL + "\"/>" + |
| + " </urls>" + |
| + " <manifest version=\"1.2.3.4\">" + |
| + " <packages>" + |
| + " <package hash=\"0\" name=\"dummy.apk\" required=\"true\" size=\"0\"/>" + |
| + " </packages>" + |
| + " <actions>" + |
| + " <action event=\"install\" run=\"dummy.apk\"/>" + |
| + " <action event=\"postinstall\"/>" + |
| + " </actions>" + |
| + " </manifest>" + |
| + " </updatecheck>" + |
| + " </app>" + |
| + "</response>"; |
| + |
| + private static final String PING_NO_UPDATE = |
| + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| + "<response protocol=\"3.0\" server=\"prod\">" + |
| + " <daystart elapsed_seconds=\"12345\"/>" + |
| + " <app appid=\"{APP_ID}\" status=\"ok\">" + |
| + " <ping status=\"ok\"/>" + |
| + " </app>" + |
| + "</response>"; |
| + |
| + private static final String ERROR_UPDATE_STATUS = |
| + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| + "<response protocol=\"3.0\" server=\"prod\">" + |
| + " <daystart elapsed_seconds=\"12345\"/>" + |
| + " <app appid=\"{APP_ID}\" status=\"ok\">" + |
| + " <updatecheck status=\"" + UPDATE_STATUS_ERROR + "\"/>" + |
| + " <ping status=\"ok\"/>" + |
| + " </app>" + |
| + "</response>"; |
| + |
| + private static final String BAD_UPDATE_STATUS = |
| + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| + "<response protocol=\"3.0\" server=\"prod\">" + |
| + " <daystart elapsed_seconds=\"12345\"/>" + |
| + " <app appid=\"{APP_ID}\" status=\"ok\">" + |
| + " <updatecheck status=\"" + UPDATE_STATUS_WTF + "\"/>" + |
| + " <ping status=\"ok\"/>" + |
| + " </app>" + |
| + "</response>"; |
| + |
| + private static final String NO_DAYSTART = |
| + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| + "<response protocol=\"3.0\" server=\"prod\">" + |
| + " <app appid=\"{APP_ID}\" status=\"ok\">" + |
| + " <updatecheck status=\"" + UPDATE_STATUS_NOUPDATE + "\"/>" + |
| + " <ping status=\"ok\"/>" + |
| + " </app>" + |
| + "</response>"; |
| + |
| + private static final String INVALID_TAGS = |
| + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| + "<response protocol=\"3.0\" server=\"prod\">" + |
| + " <app appid=\"{APP_ID}\" status=\"ok\">" + |
| + " <dummy/>" + |
| + " </app>" + |
| + "</response>"; |
| + |
| + private static final String BOGUS_RESPONSE = "Bogus"; |
| + |
| + @SmallTest |
| + @Feature({"Omaha"}) |
| + public void testValidXML() throws RequestFailureException { |
| + ResponseParser parser = new ResponseParser(VALID_XML, "{APP_ID}", false); |
| + assertEquals("Version number doesn't match.", "1.2.3.4", parser.getMarketVersion()); |
| + assertEquals("elapsed_seconds doesn't match.", 12345, parser.getDaystartSeconds()); |
| + assertEquals("Market URL doesn't match.", STRIPPED_MARKET_URL, parser.getURL()); |
| + assertEquals("Update status doesn't match.", UPDATE_STATUS_OK, parser.getUpdateStatus()); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Omaha"}) |
| + public void testValidXMLUpdateError() throws RequestFailureException { |
| + ResponseParser parser = new ResponseParser(ERROR_UPDATE_STATUS, "{APP_ID}", false); |
| + assertEquals("Version number doesn't match.", null, parser.getMarketVersion()); |
| + assertEquals("elapsed_seconds doesn't match.", 12345, parser.getDaystartSeconds()); |
| + assertEquals("Market URL doesn't match.", null, parser.getURL()); |
| + assertEquals("Update status doesn't match.", UPDATE_STATUS_ERROR, parser.getUpdateStatus()); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Omaha"}) |
| + public void testValidXMLWithInstallEvent() throws RequestFailureException { |
| + ResponseParser parser = new ResponseParser(VALID_XML_WITH_INSTALL, "{APP_ID}", true); |
| + assertEquals("Version number doesn't match.", null, parser.getMarketVersion()); |
| + assertEquals("elapsed_seconds doesn't match.", 12345, parser.getDaystartSeconds()); |
| + assertEquals("Market URL doesn't match.", null, parser.getURL()); |
| + assertEquals("Update status doesn't match.", null, parser.getUpdateStatus()); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Omaha"}) |
| + public void testValidXMLNoUpdateNecessary() throws RequestFailureException { |
| + ResponseParser parser = new ResponseParser(VALID_XML_UP_TO_DATE, "{APP_ID}", false); |
| + assertEquals("Version number doesn't match.", null, parser.getMarketVersion()); |
| + assertEquals("elapsed_seconds doesn't match.", 12345, parser.getDaystartSeconds()); |
| + assertEquals("Market URL doesn't match.", null, parser.getURL()); |
| + assertEquals("Update status doesn't match.", UPDATE_STATUS_NOUPDATE, |
| + parser.getUpdateStatus()); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Omaha"}) |
| + public void testBogusResponse() throws RequestFailureException { |
| + failureTest(BOGUS_RESPONSE, |
| + "Failed to throw exception when bogus XML was given.", 0); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Omaha"}) |
| + public void testInvalidTags() throws RequestFailureException { |
| + failureTest(INVALID_TAGS, |
| + "Failed to throw exception when invalid tag was given.", 0); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Omaha"}) |
| + public void testDaystart() throws RequestFailureException { |
| + failureTest(NO_DAYSTART, |
| + "Failed to throw exception when <daystart> was missing.", 0); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Omaha"}) |
| + public void testUpdateNoPing() { |
| + failureTest(UPDATE_NO_PING, |
| + "Failed to throw exception when <ping> was missing.", 12345); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Omaha"}) |
| + public void testPingNoUpdate() { |
| + failureTest(PING_NO_UPDATE, |
| + "Failed to throw exception when <updatecheck> was missing.", 12345); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Omaha"}) |
| + public void testBadUpdateStatusIgnored() throws RequestFailureException { |
| + ResponseParser parser = new ResponseParser(BAD_UPDATE_STATUS, "{APP_ID}", false); |
| + assertEquals("Version number doesn't match.", null, parser.getMarketVersion()); |
| + assertEquals("elapsed_seconds doesn't match.", 12345, parser.getDaystartSeconds()); |
| + assertEquals("Market URL doesn't match.", null, parser.getURL()); |
| + assertEquals("Update status doesn't match.", UPDATE_STATUS_WTF, parser.getUpdateStatus()); |
| + } |
| + |
| + /** |
| + * Checks that the parser fails to handle certain inputs. |
| + * @param xml XML that will be parsed. |
| + * @param failureMessage Message that will be printed if the test fails to pass. |
| + * @param expectedDaystart Expected value of the server's elapsed_seconds value. |
| + */ |
| + private void failureTest(String xml, String failureMessage, int expectedDaystart) { |
| + boolean failed = false; |
| + ResponseParser parser = null; |
| + try { |
| + parser = new ResponseParser(xml, "{APP_ID}", false); |
| + } catch (RequestFailureException e) { |
| + failed = true; |
| + } |
| + assertTrue(failureMessage, failed); |
| + if (parser != null) { |
| + assertEquals("elapsed_seconds doesn't match.", expectedDaystart, |
| + parser.getDaystartSeconds()); |
| + } |
| + } |
| +} |