5

我尝试打包一个跨度,但是当我添加粗体跨度时,我写的测试不再通过。

这是测试:

@RunWith(Parameterized.class)
    
public class DNTest {
            
    private String testData;
            
    private Spanned spanned;
            
    private Intent intent;
    
    private Class<NI> itemClass;
        
            

    @Before
            
    public void initialize() throws IOException {
                
        testData = Tests.readTestData(getInstrumentation(), “file.json);
        intent = new Intent("ACTION");
                
        spanned = getDisplayableString(“thing”, "Wojtek Wntrzak");
        assertThat(testData).isNotNull().isNotEmpty();
            
    }
     

   
     @Parameterized.Parameters
            
    public static Collection<Object[]> testNI() {
                
        return Arrays.asList(new Object[][] {
                        
            { LANI.class },
                        
            { FANI.class },
                        
            { ACNI.class },
                        
            { CRNI.class },
                
        });
            
    }
        
            

    public DNTest(Class<NI> nItemClass){
                
        itemClass = nItemClass;
            
    }
        
            

    @Test
            
    public void testParcelable() {
                
        NI notification = mGson.fromJson(testData, itemClass);
              
        DN dN = new DN(spanned,
     intent,
     notification);
        
                
        Parcel parcel = Parcel.obtain();
        parcel.writeString(dN.getClass().getName());
             
        dN.writeToParcel(parcel, 0);
                
        parcel.setDataPosition(0);
        
                
        final DN parcelled = parcel.readParcelable(dN.getClass().getClassLoader());
                    
        assertThat(parcelled).isNotNull();
                
        assertEquals(parcelled.getSpanned(), spanned);
                
        assertEquals(new Intent.FilterComparison(parcelled.getIntent()),
                             
        new Intent.FilterComparison(intent));
                
        assertEquals(parcelled.getItem(), notification);
        
                
        assertThat(dN).isNotNull();
                
        assertParcelableCorrect(dN);
            
    }

    private Spanned getDisplayableString(String name, String actor) {
        
        final String string;
        
        string = actor + " blablabla " + name;
        
        SpannableString spannable = new SpannableString(string);
        
        Notifications.boldify(string, spannable, actor);
        
        Notifications.boldify(string, spannable, name);
 
        Spanned spanned = spannable;   
        
        return spanned;
    
    }
}

当我用加粗函数注释 2 行时,它通过了。

这是加粗功能:

public static void boldify(String source, SpannableString spannable, String text) {
        
     int start = source.indexOf(text);
        
     if (start == -1) {
            
          return;
        
     }
    
        
     spannable.setSpan(new StyleSpan(Typeface.BOLD), start, start + text.length(),
 Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    
 }

我尝试了 3 种分割 Spanned 的方法,但都没有奏效:

dest.writeString(Html.toHtml(mSpanned));
mSpanned = Html.fromHtml(in.readString());
    

dest.writeValue(mSpanned);
    
mSpanned = (Spanned) in.readValue(DisplayableNotification.class.getClassLoader());
        

TextUtils.writeToParcel(mSpanned, dest, flags);
    
mSpanned = (Spanned) TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
4

0 回答 0