• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
17 package com.android.textclassifier.common;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.view.textclassifier.TextClassification;
22 import android.view.textclassifier.TextSelection;
23 import androidx.test.filters.SdkSuppress;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.JUnit4;
27 
28 @RunWith(JUnit4.class)
29 public final class TextSelectionCompatTest {
30 
31   @SdkSuppress(minSdkVersion = 30)
32   @Test
shouldIncludeTextClassification_negative()33   public void shouldIncludeTextClassification_negative() {
34     TextSelection.Request request =
35         new TextSelection.Request.Builder("text", /*startIndex=*/ 0, /*endIndex=*/ 1).build();
36 
37     assertThat(TextSelectionCompat.shouldIncludeTextClassification(request)).isFalse();
38   }
39 
40   @SdkSuppress(minSdkVersion = 31, codeName = "S")
41   @Test
shouldIncludeTextClassification_positive()42   public void shouldIncludeTextClassification_positive() {
43     TextSelection.Request request =
44         new TextSelection.Request.Builder("text", /*startIndex=*/ 0, /*endIndex=*/ 1)
45             .setIncludeTextClassification(true)
46             .build();
47 
48     assertThat(TextSelectionCompat.shouldIncludeTextClassification(request)).isTrue();
49   }
50 
51   @SdkSuppress(minSdkVersion = 30, maxSdkVersion = 30)
52   @Test
setTextClassification_api30()53   public void setTextClassification_api30() {
54     TextSelection.Builder selectionBuilder =
55         new TextSelection.Builder(/*startIndex=*/ 0, /*endIndex=*/ 1);
56 
57     // This should not crash.
58     TextSelectionCompat.setTextClassification(selectionBuilder, null);
59   }
60 
61   @SdkSuppress(minSdkVersion = 31)
62   @Test
setTextClassification_api31()63   public void setTextClassification_api31() {
64     TextSelection.Builder selectionBuilder =
65         new TextSelection.Builder(/*startIndex=*/ 0, /*endIndex=*/ 1);
66     TextClassification classification = new TextClassification.Builder().setText("text").build();
67 
68     TextSelectionCompat.setTextClassification(selectionBuilder, classification);
69 
70     assertThat(selectionBuilder.build().getTextClassification()).isSameInstanceAs(classification);
71   }
72 }
73