Tony Mak | 20fe187 | 2019-03-22 15:35:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package android.view.textclassifier; |
| 17 | |
| 18 | import static com.google.common.truth.Truth.assertThat; |
| 19 | |
| 20 | import androidx.test.filters.SmallTest; |
| 21 | import androidx.test.runner.AndroidJUnit4; |
| 22 | |
| 23 | import org.junit.Test; |
| 24 | import org.junit.runner.RunWith; |
| 25 | |
| 26 | import java.io.File; |
| 27 | import java.util.Collections; |
| 28 | import java.util.Locale; |
| 29 | |
| 30 | @SmallTest |
| 31 | @RunWith(AndroidJUnit4.class) |
| 32 | public class ActionsModelParamsSupplierTest { |
| 33 | |
| 34 | @Test |
| 35 | public void getSerializedPreconditions_validActionsModelParams() { |
| 36 | ModelFileManager.ModelFile modelFile = new ModelFileManager.ModelFile( |
| 37 | new File("/model/file"), |
| 38 | 200 /* version */, |
| 39 | Collections.singletonList(Locale.forLanguageTag("en")), |
| 40 | "en", |
| 41 | false); |
| 42 | byte[] serializedPreconditions = new byte[]{0x12, 0x24, 0x36}; |
| 43 | ActionsModelParamsSupplier.ActionsModelParams params = |
| 44 | new ActionsModelParamsSupplier.ActionsModelParams( |
| 45 | 200 /* version */, |
| 46 | "en", |
| 47 | serializedPreconditions); |
| 48 | |
| 49 | byte[] actual = params.getSerializedPreconditions(modelFile); |
| 50 | |
| 51 | assertThat(actual).isEqualTo(serializedPreconditions); |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void getSerializedPreconditions_invalidVersion() { |
| 56 | ModelFileManager.ModelFile modelFile = new ModelFileManager.ModelFile( |
| 57 | new File("/model/file"), |
| 58 | 201 /* version */, |
| 59 | Collections.singletonList(Locale.forLanguageTag("en")), |
| 60 | "en", |
| 61 | false); |
| 62 | byte[] serializedPreconditions = new byte[]{0x12, 0x24, 0x36}; |
| 63 | ActionsModelParamsSupplier.ActionsModelParams params = |
| 64 | new ActionsModelParamsSupplier.ActionsModelParams( |
| 65 | 200 /* version */, |
| 66 | "en", |
| 67 | serializedPreconditions); |
| 68 | |
| 69 | byte[] actual = params.getSerializedPreconditions(modelFile); |
| 70 | |
| 71 | assertThat(actual).isNull(); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void getSerializedPreconditions_invalidLocales() { |
| 76 | final String LANGUAGE_TAG = "zh"; |
| 77 | ModelFileManager.ModelFile modelFile = new ModelFileManager.ModelFile( |
| 78 | new File("/model/file"), |
| 79 | 200 /* version */, |
| 80 | Collections.singletonList(Locale.forLanguageTag(LANGUAGE_TAG)), |
| 81 | LANGUAGE_TAG, |
| 82 | false); |
| 83 | byte[] serializedPreconditions = new byte[]{0x12, 0x24, 0x36}; |
| 84 | ActionsModelParamsSupplier.ActionsModelParams params = |
| 85 | new ActionsModelParamsSupplier.ActionsModelParams( |
| 86 | 200 /* version */, |
| 87 | "en", |
| 88 | serializedPreconditions); |
| 89 | |
| 90 | byte[] actual = params.getSerializedPreconditions(modelFile); |
| 91 | |
| 92 | assertThat(actual).isNull(); |
| 93 | } |
| 94 | |
| 95 | } |