| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.physicalweb; | 5 package org.chromium.chrome.browser.physicalweb; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.test.InstrumentationTestCase; | 8 import android.test.InstrumentationTestCase; |
| 9 import android.test.suitebuilder.annotation.SmallTest; | 9 import android.test.suitebuilder.annotation.SmallTest; |
| 10 import android.text.TextUtils; | 10 import android.text.TextUtils; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 // Ensure Accept-Language also contains the language code by itself. Inc
lude the separator | 73 // Ensure Accept-Language also contains the language code by itself. Inc
lude the separator |
| 74 // character so we don't match, for instance, the "en" in "en-US". | 74 // character so we don't match, for instance, the "en" in "en-US". |
| 75 assertTrue(acceptLanguage.startsWith(languageCode + ",") | 75 assertTrue(acceptLanguage.startsWith(languageCode + ",") |
| 76 || acceptLanguage.contains(languageCode + ";") | 76 || acceptLanguage.contains(languageCode + ";") |
| 77 || acceptLanguage.equals(languageCode)); | 77 || acceptLanguage.equals(languageCode)); |
| 78 } | 78 } |
| 79 | 79 |
| 80 @SmallTest | 80 @SmallTest |
| 81 public void testLanguageTagIsPrepended() { | 81 public void testLanguageTagIsPrepended() { |
| 82 String locale = new Locale("aa", "AA").toString(); | 82 String locale = new Locale("aa", "AA").toString(); |
| 83 String languageList = "xx-XX,xx,xx-YY"; | 83 String languageList = "xx-XX,xx-YY,xx"; |
| 84 | 84 |
| 85 // Should prepend the language tag "aa-AA" as well as the language code
"aa". | 85 // Should prepend the language tag "aa-AA" as well as the language code
"aa". |
| 86 String languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNec
essary(locale, | 86 String languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNec
essary(locale, |
| 87 languageList); | 87 languageList); |
| 88 assertEquals("aa-AA,aa,xx-XX,xx,xx-YY", languageListWithTag); | 88 assertEquals("aa-AA,aa,xx-XX,xx-YY,xx", languageListWithTag); |
| 89 } | 89 } |
| 90 | 90 |
| 91 @SmallTest | 91 @SmallTest |
| 92 public void testMultipleLanguageTagIsPrepended() { |
| 93 String locale = "aa_AA,bb_BB"; |
| 94 String languageList = "xx-XX,xx-YY,xx"; |
| 95 |
| 96 // Should prepend the language tag "aa-AA" as well as the language code
"aa". |
| 97 String languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNec
essary(locale, |
| 98 languageList); |
| 99 assertEquals("aa-AA,aa,bb-BB,bb,xx-XX,xx-YY,xx", languageListWithTag); |
| 100 |
| 101 // Make sure the language code is only inserted after the last languageT
ag that |
| 102 // contains that language. |
| 103 locale = "aa_AA,bb_BB,bb_XX"; |
| 104 languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNecessary(
locale, |
| 105 languageList); |
| 106 assertEquals("aa-AA,aa,bb-BB,bb-XX,bb,xx-XX,xx-YY,xx", languageListWithT
ag); |
| 107 } |
| 108 |
| 109 @SmallTest |
| 92 public void testLanguageTagIsPrependedWhenListContainsLanguageCode() { | 110 public void testLanguageTagIsPrependedWhenListContainsLanguageCode() { |
| 93 String locale = new Locale("xx", "XX").toString(); | 111 String locale = new Locale("xx", "XX").toString(); |
| 94 String languageList = "xx-YY,xx"; | 112 String languageList = "xx-YY,xx"; |
| 95 | 113 |
| 96 // Should prepend the language tag "xx-XX" but not the language code "xx
" as it's already | 114 // Should prepend the language tag "xx-XX" but not the language code "xx
" as it's already |
| 97 // included at the end of the list. | 115 // included at the end of the list. |
| 98 String languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNec
essary(locale, | 116 String languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNec
essary(locale, |
| 99 languageList); | 117 languageList); |
| 100 assertEquals("xx-XX,xx-YY,xx", languageListWithTag); | 118 assertEquals("xx-XX,xx-YY,xx", languageListWithTag); |
| 101 | 119 |
| 102 // Test again with the language code "xx" in the middle of the list. | 120 // Test again with the language code "xx" in the middle of the list. |
| 103 languageList = "xx-YY,xx,xx-ZZ"; | 121 languageList = "xx-YY,xx-ZZ,xx"; |
| 104 languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNecessary(
locale, | 122 languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNecessary(
locale, |
| 105 languageList); | 123 languageList); |
| 106 assertEquals("xx-XX,xx-YY,xx,xx-ZZ", languageListWithTag); | 124 assertEquals("xx-XX,xx-YY,xx-ZZ,xx", languageListWithTag); |
| 107 } | 125 } |
| 108 | 126 |
| 109 @SmallTest | 127 @SmallTest |
| 110 public void testInvalidLanguageTagNotPrepended() { | 128 public void testInvalidLanguageTagNotPrepended() { |
| 111 String locale = "not_valid"; | 129 String locale = "not_valid"; |
| 112 String languageList = "xx-XX,xx"; | 130 String languageList = "xx-XX,xx"; |
| 113 | 131 |
| 114 // Language list should be unmodified since the language tag is invalid. | 132 // Language list should be unmodified since the language tag is invalid. |
| 115 String languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNec
essary(locale, | 133 String languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNec
essary(locale, |
| 116 languageList); | 134 languageList); |
| 117 assertEquals(languageList, languageListWithTag); | 135 assertEquals(languageList, languageListWithTag); |
| 118 } | 136 } |
| 119 | 137 |
| 120 @SmallTest | 138 @SmallTest |
| 121 public void testLanguageTagNotPrependedWhenUnnecessary() { | 139 public void testLanguageTagNotPrependedWhenUnnecessary() { |
| 122 String locale = new Locale("xx", "XX").toString(); | 140 String locale = new Locale("xx", "XX").toString(); |
| 123 String languageList = "xx-XX,xx,xx-YY"; | 141 String languageList = "xx-XX,xx-YY,xx"; |
| 124 | 142 |
| 125 // Language list should be unmodified since the tag is already present. | 143 // Language list should be unmodified since the tag is already present. |
| 126 String languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNec
essary(locale, | 144 String languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNec
essary(locale, |
| 145 languageList); |
| 146 assertEquals(languageList, languageListWithTag); |
| 147 } |
| 148 |
| 149 @SmallTest |
| 150 public void testMultiLanguageTagNotPrependedWhenUnnecessary() { |
| 151 String locale = "xx-XX,yy-YY"; |
| 152 String languageList = "xx-XX,xx,yy-YY,yy"; |
| 153 |
| 154 // Language list should be unmodified since the tag is already present. |
| 155 String languageListWithTag = PwsClientImpl.prependToAcceptLanguagesIfNec
essary(locale, |
| 127 languageList); | 156 languageList); |
| 128 assertEquals(languageList, languageListWithTag); | 157 assertEquals(languageList, languageListWithTag); |
| 129 } | 158 } |
| 130 | 159 |
| 131 @SmallTest | 160 @SmallTest |
| 132 public void testAcceptLanguageQvalues() { | 161 public void testAcceptLanguageQvalues() { |
| 133 String languageList = "xx-XX,xx,xx-YY,zz-ZZ,zz"; | 162 String languageList = "xx-XX,xx-YY,xx,zz-ZZ,zz"; |
| 134 | 163 |
| 135 // Should insert q-values for each item except the first which implicitl
y has q=1.0. | 164 // Should insert q-values for each item except the first which implicitl
y has q=1.0. |
| 136 String acceptLanguage = PwsClientImpl.generateAcceptLanguageHeader(langu
ageList); | 165 String acceptLanguage = PwsClientImpl.generateAcceptLanguageHeader(langu
ageList); |
| 137 assertEquals("xx-XX,xx;q=0.8,xx-YY;q=0.6,zz-ZZ;q=0.4,zz;q=0.2", acceptLa
nguage); | 166 assertEquals("xx-XX,xx-YY;q=0.8,xx;q=0.6,zz-ZZ;q=0.4,zz;q=0.2", acceptLa
nguage); |
| 138 | 167 |
| 139 // When there are six or more items, the q-value should not go below 0.2
. | 168 // When there are six or more items, the q-value should not go below 0.2
. |
| 140 languageList = "xx-XA,xx-XB,xx-XC,xx-XD,xx-XE,xx-XF"; | 169 languageList = "xx-XA,xx-XB,xx-XC,xx-XD,xx-XE,xx-XF"; |
| 141 acceptLanguage = PwsClientImpl.generateAcceptLanguageHeader(languageList
); | 170 acceptLanguage = PwsClientImpl.generateAcceptLanguageHeader(languageList
); |
| 142 assertEquals("xx-XA,xx-XB;q=0.8,xx-XC;q=0.6,xx-XD;q=0.4,xx-XE;q=0.2,xx-X
F;q=0.2", | 171 assertEquals("xx-XA,xx-XB;q=0.8,xx-XC;q=0.6,xx-XD;q=0.4,xx-XE;q=0.2,xx-X
F;q=0.2", |
| 143 acceptLanguage); | 172 acceptLanguage); |
| 144 } | 173 } |
| 145 } | 174 } |
| OLD | NEW |