| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.translate; | 5 package org.chromium.chrome.browser.translate; |
| 6 | 6 |
| 7 import android.support.test.filters.SmallTest; | 7 import android.support.test.filters.SmallTest; |
| 8 import android.test.AndroidTestCase; | 8 import android.test.AndroidTestCase; |
| 9 | 9 |
| 10 import org.chromium.base.test.util.Feature; | 10 import org.chromium.base.test.util.Feature; |
| 11 import org.chromium.chrome.browser.infobar.TranslateOptions; | 11 import org.chromium.chrome.browser.infobar.TranslateOptions; |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * Test for TranslateOptions. | 14 * Test for TranslateOptions. |
| 15 */ | 15 */ |
| 16 public class TranslateOptionsTest extends AndroidTestCase { | 16 public class TranslateOptionsTest extends AndroidTestCase { |
| 17 private static final boolean ALWAYS_TRANSLATE = true; | 17 private static final boolean ALWAYS_TRANSLATE = true; |
| 18 private static final String[] LANGUAGES = {"English", "Spanish", "French"}; | 18 private static final String[] LANGUAGES = {"English", "Spanish", "French"}; |
| 19 private static final String[] CODES = {"en", "es", "fr"}; | 19 private static final String[] CODES = {"en", "es", "fr"}; |
| 20 private static final int[] UMA_HASH_CODES = {10, 20, 30}; |
| 20 | 21 |
| 21 @Override | 22 @Override |
| 22 public void setUp() throws Exception { | 23 public void setUp() throws Exception { |
| 23 super.setUp(); | 24 super.setUp(); |
| 24 } | 25 } |
| 25 | 26 |
| 26 @SmallTest | 27 @SmallTest |
| 27 @Feature({"Translate"}) | 28 @Feature({"Translate"}) |
| 28 public void testNoChanges() { | 29 public void testNoChanges() { |
| 29 TranslateOptions options = | 30 TranslateOptions options = TranslateOptions.create( |
| 30 TranslateOptions.create("en", "es", LANGUAGES, CODES, ALWAYS_TRA
NSLATE, false); | 31 "en", "es", LANGUAGES, CODES, ALWAYS_TRANSLATE, false, null); |
| 31 assertEquals("English", options.sourceLanguageName()); | 32 assertEquals("English", options.sourceLanguageName()); |
| 32 assertEquals("Spanish", options.targetLanguageName()); | 33 assertEquals("Spanish", options.targetLanguageName()); |
| 33 assertEquals("en", options.sourceLanguageCode()); | 34 assertEquals("en", options.sourceLanguageCode()); |
| 34 assertEquals("es", options.targetLanguageCode()); | 35 assertEquals("es", options.targetLanguageCode()); |
| 35 assertFalse(options.neverTranslateLanguageState()); | 36 assertFalse(options.neverTranslateLanguageState()); |
| 36 assertTrue(options.alwaysTranslateLanguageState()); | 37 assertTrue(options.alwaysTranslateLanguageState()); |
| 37 assertFalse(options.neverTranslateDomainState()); | 38 assertFalse(options.neverTranslateDomainState()); |
| 38 assertFalse(options.optionsChanged()); | 39 assertFalse(options.optionsChanged()); |
| 40 assertNull(options.getUMAHashCodeRepresentationFromCode("en")); |
| 39 } | 41 } |
| 40 | 42 |
| 41 @SmallTest | 43 @SmallTest |
| 42 @Feature({"Translate"}) | 44 @Feature({"Translate"}) |
| 43 public void testBasicLanguageChanges() { | 45 public void testBasicLanguageChanges() { |
| 44 TranslateOptions options = | 46 TranslateOptions options = TranslateOptions.create( |
| 45 TranslateOptions.create("en", "es", LANGUAGES, CODES, !ALWAYS_TR
ANSLATE, true); | 47 "en", "es", LANGUAGES, CODES, !ALWAYS_TRANSLATE, true, UMA_HASH_
CODES); |
| 46 options.setTargetLanguage("fr"); | 48 options.setTargetLanguage("fr"); |
| 47 options.setSourceLanguage("en"); | 49 options.setSourceLanguage("en"); |
| 48 assertEquals("English", options.sourceLanguageName()); | 50 assertEquals("English", options.sourceLanguageName()); |
| 49 assertEquals("French", options.targetLanguageName()); | 51 assertEquals("French", options.targetLanguageName()); |
| 50 assertEquals("en", options.sourceLanguageCode()); | 52 assertEquals("en", options.sourceLanguageCode()); |
| 51 assertEquals("fr", options.targetLanguageCode()); | 53 assertEquals("fr", options.targetLanguageCode()); |
| 52 assertTrue(options.triggeredFromMenu()); | 54 assertTrue(options.triggeredFromMenu()); |
| 55 assertEquals(Integer.valueOf(10), options.getUMAHashCodeRepresentationFr
omCode("en")); |
| 56 assertEquals("English", options.getRepresentationFromCode("en")); |
| 53 | 57 |
| 54 assertTrue(options.optionsChanged()); | 58 assertTrue(options.optionsChanged()); |
| 55 | 59 |
| 56 // Switch back to the original | 60 // Switch back to the original |
| 57 options.setSourceLanguage("en"); | 61 options.setSourceLanguage("en"); |
| 58 options.setTargetLanguage("es"); | 62 options.setTargetLanguage("es"); |
| 59 assertFalse(options.optionsChanged()); | 63 assertFalse(options.optionsChanged()); |
| 60 } | 64 } |
| 61 | 65 |
| 62 @SmallTest | 66 @SmallTest |
| 63 @Feature({"Translate"}) | 67 @Feature({"Translate"}) |
| 64 public void testInvalidLanguageChanges() { | 68 public void testInvalidLanguageChanges() { |
| 65 TranslateOptions options = | 69 TranslateOptions options = TranslateOptions.create( |
| 66 TranslateOptions.create("en", "es", LANGUAGES, CODES, ALWAYS_TRA
NSLATE, false); | 70 "en", "es", LANGUAGES, CODES, ALWAYS_TRANSLATE, false, null); |
| 67 | 71 |
| 68 // Same target language as source | 72 // Same target language as source |
| 69 assertFalse(options.setTargetLanguage("en")); | 73 assertFalse(options.setTargetLanguage("en")); |
| 70 assertFalse(options.optionsChanged()); | 74 assertFalse(options.optionsChanged()); |
| 71 | 75 |
| 72 // Target language does not exist | 76 // Target language does not exist |
| 73 assertFalse(options.setTargetLanguage("aaa")); | 77 assertFalse(options.setTargetLanguage("aaa")); |
| 74 assertFalse(options.optionsChanged()); | 78 assertFalse(options.optionsChanged()); |
| 75 | 79 |
| 76 // Same source and target | 80 // Same source and target |
| 77 assertFalse(options.setSourceLanguage("es")); | 81 assertFalse(options.setSourceLanguage("es")); |
| 78 assertFalse(options.optionsChanged()); | 82 assertFalse(options.optionsChanged()); |
| 79 | 83 |
| 80 // Source language does not exist | 84 // Source language does not exist |
| 81 assertFalse(options.setSourceLanguage("bbb")); | 85 assertFalse(options.setSourceLanguage("bbb")); |
| 82 assertFalse(options.optionsChanged()); | 86 assertFalse(options.optionsChanged()); |
| 83 } | 87 } |
| 84 | 88 |
| 85 @SmallTest | 89 @SmallTest |
| 86 @Feature({"Translate"}) | 90 @Feature({"Translate"}) |
| 87 public void testBasicOptionsChanges() { | 91 public void testBasicOptionsChanges() { |
| 88 TranslateOptions options = | 92 TranslateOptions options = TranslateOptions.create( |
| 89 TranslateOptions.create("en", "es", LANGUAGES, CODES, !ALWAYS_TR
ANSLATE, false); | 93 "en", "es", LANGUAGES, CODES, !ALWAYS_TRANSLATE, false, null); |
| 90 assertFalse(options.optionsChanged()); | 94 assertFalse(options.optionsChanged()); |
| 91 options.toggleNeverTranslateDomainState(true); | 95 options.toggleNeverTranslateDomainState(true); |
| 92 assertTrue(options.neverTranslateDomainState()); | 96 assertTrue(options.neverTranslateDomainState()); |
| 93 assertFalse(options.alwaysTranslateLanguageState()); | 97 assertFalse(options.alwaysTranslateLanguageState()); |
| 94 assertFalse(options.neverTranslateLanguageState()); | 98 assertFalse(options.neverTranslateLanguageState()); |
| 95 assertTrue(options.optionsChanged()); | 99 assertTrue(options.optionsChanged()); |
| 96 options.toggleNeverTranslateDomainState(false); | 100 options.toggleNeverTranslateDomainState(false); |
| 97 assertFalse(options.neverTranslateDomainState()); | 101 assertFalse(options.neverTranslateDomainState()); |
| 98 assertFalse(options.neverTranslateLanguageState()); | 102 assertFalse(options.neverTranslateLanguageState()); |
| 99 assertFalse(options.alwaysTranslateLanguageState()); | 103 assertFalse(options.alwaysTranslateLanguageState()); |
| 100 | 104 |
| 101 // We are back to the original state | 105 // We are back to the original state |
| 102 assertFalse(options.optionsChanged()); | 106 assertFalse(options.optionsChanged()); |
| 103 options.toggleAlwaysTranslateLanguageState(true); | 107 options.toggleAlwaysTranslateLanguageState(true); |
| 104 assertFalse(options.neverTranslateDomainState()); | 108 assertFalse(options.neverTranslateDomainState()); |
| 105 assertFalse(options.neverTranslateLanguageState()); | 109 assertFalse(options.neverTranslateLanguageState()); |
| 106 assertTrue(options.alwaysTranslateLanguageState()); | 110 assertTrue(options.alwaysTranslateLanguageState()); |
| 107 assertTrue(options.optionsChanged()); | 111 assertTrue(options.optionsChanged()); |
| 108 } | 112 } |
| 109 | 113 |
| 110 @SmallTest | 114 @SmallTest |
| 111 @Feature({"Translate"}) | 115 @Feature({"Translate"}) |
| 112 public void testInvalidOptionsChanges() { | 116 public void testInvalidOptionsChanges() { |
| 113 TranslateOptions options = | 117 TranslateOptions options = TranslateOptions.create( |
| 114 TranslateOptions.create("en", "es", LANGUAGES, CODES, ALWAYS_TRA
NSLATE, false); | 118 "en", "es", LANGUAGES, CODES, ALWAYS_TRANSLATE, false, null); |
| 115 | 119 |
| 116 // Never translate language should not work, but never translate domain | 120 // Never translate language should not work, but never translate domain |
| 117 // should | 121 // should |
| 118 assertFalse(options.toggleNeverTranslateLanguageState(true)); | 122 assertFalse(options.toggleNeverTranslateLanguageState(true)); |
| 119 assertTrue(options.toggleNeverTranslateDomainState(true)); | 123 assertTrue(options.toggleNeverTranslateDomainState(true)); |
| 120 assertTrue(options.optionsChanged()); | 124 assertTrue(options.optionsChanged()); |
| 121 | 125 |
| 122 assertTrue(options.toggleAlwaysTranslateLanguageState(false)); | 126 assertTrue(options.toggleAlwaysTranslateLanguageState(false)); |
| 123 | 127 |
| 124 // Never options are ok | 128 // Never options are ok |
| 125 assertTrue(options.toggleNeverTranslateLanguageState(true)); | 129 assertTrue(options.toggleNeverTranslateLanguageState(true)); |
| 126 assertTrue(options.toggleNeverTranslateDomainState(true)); | 130 assertTrue(options.toggleNeverTranslateDomainState(true)); |
| 127 | 131 |
| 128 // But always is not now | 132 // But always is not now |
| 129 assertFalse(options.toggleAlwaysTranslateLanguageState(true)); | 133 assertFalse(options.toggleAlwaysTranslateLanguageState(true)); |
| 130 } | 134 } |
| 131 } | 135 } |
| OLD | NEW |