/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.slice;

import static android.graphics.Typeface.BOLD;

import static org.junit.Assert.assertEquals;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.StyleSpan;

import androidx.test.filters.SdkSuppress;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@SmallTest
@SdkSuppress(minSdkVersion = 19)
public class SliceItemTest {

    @Test
    public void testNormalText() {
        String text = "Some normal text";
        SliceItem item = new SliceItem(text, android.app.slice.SliceItem.FORMAT_TEXT, null,
                new String[0]);

        assertEquals(text, item.getText());
        assertEquals(text, item.getRedactedText());
    }

    @Test
    public void testSpannedText() {
        // "Some [normal] text" where [] denotes bold.
        CharSequence text = new SpannableStringBuilder()
                .append("Some ")
                .append("normal", new StyleSpan(BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                .append(" text");
        SliceItem item = new SliceItem(text, android.app.slice.SliceItem.FORMAT_TEXT, null,
                new String[0]);

        assertEquals(text, item.getText());
        assertEquals(text, item.getRedactedText());
    }

    @Test
    public void testRedactedText() {
        // "Some [normal] text" where [] denotes sensitive
        CharSequence text = new SpannableStringBuilder()
                .append("Some ")
                .append("normal", SliceItem.createSensitiveSpan(),
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                .append(" text");
        String redactedText = "Some ****** text";
        SliceItem item = new SliceItem(text, android.app.slice.SliceItem.FORMAT_TEXT, null,
                new String[0]);

        assertEquals(text, item.getText());
        assertEquals(redactedText, item.getRedactedText().toString());
    }

}

@@ -1546,36 +1546,46 @@
 ld.%0A        
-CharSequence
+SpannableStringBuilder
  text = new 
@@ -1600,32 +1600,33 @@
 eStringBuilder()
+;
 %0A               
@@ -1614,32 +1614,28 @@
 ();%0A        
-        
+text
 .append(%22Som
@@ -1630,32 +1630,33 @@
 .append(%22Some %22)
+;
 %0A               
@@ -1652,34 +1652,158 @@
     
-        .append(%22normal%22, 
+int spanStart = text.length();%0A        text.append(%22normal%22);%0A        int spanEnd = text.length();%0A        text.append(%22 text%22);%0A        text.setSpan(
 new 
@@ -1818,16 +1818,36 @@
 n(BOLD),
+ spanStart, spanEnd,
  Spanned
@@ -1876,42 +1876,10 @@
 IVE)
-%0A                .append(%22 text%22)
 ;
+%0A
 %0A   
@@ -2221,20 +2221,30 @@
     
-CharSequence
+SpannableStringBuilder
  tex
@@ -2275,27 +2275,26 @@
 uilder()
+;
 %0A
-  
         
       .a
@@ -2277,38 +2277,36 @@
 lder();%0A        
-      
+text
 .append(%22Some %22)
@@ -2305,16 +2305,17 @@
 %22Some %22)
+;
 %0A       
@@ -2319,34 +2319,158 @@
     
-        .append(%22normal%22, 
+int spanStart = text.length();%0A        text.append(%22normal%22);%0A        int spanEnd = text.length();%0A        text.append(%22 text%22);%0A        text.setSpan(
 Slic
@@ -2497,25 +2497,37 @@
 eSpan(),
-%0A        
+ spanStart, spanEnd,%0A
         
@@ -2567,16 +2567,18 @@
 CLUSIVE)
+;%0A
 %0A       
@@ -2582,41 +2582,13 @@
     
-        .append(%22 text%22);%0A       
+final
  Str
@@ -2618,32 +2618,33 @@
 e ****** text%22;%0A
+%0A
         SliceIte

Fix SliceItemTest.test{Spanned,Redacted}Text on API 19