Search in sources :

Example 1 with CFMutableStringRef

use of net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableStringRef in project UniversalMediaServer by UniversalMediaServer.

the class CoreFoundationTest method testCFString.

/**
 * Tests {@link CFStringRef} and {@link CFMutableStringRef}.
 *
 * @throws Throwable if an error occurs during the test.
 */
@Test
public void testCFString() throws Throwable {
    assertEquals("FooBar", CFStringRef.toCFStringRef("FooBar").toString());
    assertEquals("", CFStringRef.toCFStringRef("").toString());
    assertNull(CFStringRef.toCFStringRef(null));
    assertEquals(IOKit.kIOPMAssertionTypePreventUserIdleDisplaySleep, CFStringRef.toCFStringRef(IOKit.kIOPMAssertionTypePreventUserIdleDisplaySleep).toString());
    CFStringRef cfStringRef = new CFStringRef(Pointer.NULL);
    CFStringRef cfStringRef2 = null;
    assertEquals(CFComparisonResult.kCFCompareLessThan, cfStringRef.compareTo(cfStringRef2));
    cfStringRef2 = new CFStringRef(Pointer.NULL);
    assertEquals(CFComparisonResult.kCFCompareEqualTo, cfStringRef.compareTo(cfStringRef2));
    cfStringRef = CFStringRef.toCFStringRef("a");
    assertEquals(CFComparisonResult.kCFCompareLessThan, cfStringRef.compareTo(cfStringRef2));
    cfStringRef2 = CFStringRef.toCFStringRef("b");
    assertEquals(CFComparisonResult.kCFCompareLessThan, cfStringRef.compareTo(cfStringRef2));
    CF.CFRelease(cfStringRef);
    CF.CFRelease(cfStringRef2);
    cfStringRef = CFStringRef.toCFStringRef("Foo-BAR");
    cfStringRef2 = CFStringRef.toCFStringRef("foo-bar");
    assertEquals(CFComparisonResult.kCFCompareLessThan, cfStringRef.compareTo(cfStringRef2));
    assertEquals(CFComparisonResult.kCFCompareEqualTo, cfStringRef.compareTo(cfStringRef2, CFStringCompareFlags.kCFCompareCaseInsensitive));
    assertEquals(CFComparisonResult.kCFCompareEqualTo, cfStringRef.compareTo(cfStringRef2, CFStringCompareFlags.kCFCompareCaseInsensitive, CFStringCompareFlags.kCFCompareCaseInsensitive));
    assertEquals(new NativeLong(4), CF.CFStringConvertEncodingToNSStringEncoding(CFStringBuiltInEncodings.kCFStringEncodingUTF8.getValue()));
    CF.CFRelease(cfStringRef);
    cfStringRef = CF.CFStringCreateWithCString(null, "test string", CFStringBuiltInEncodings.kCFStringEncodingUTF8.getValue());
    assertEquals("test string", cfStringRef.toString());
    byte[] stringBytes = "Test byte array string".getBytes(StandardCharsets.UTF_16BE);
    CF.CFRelease(cfStringRef2);
    cfStringRef2 = CF.CFStringCreateWithBytes(null, stringBytes, stringBytes.length, CFStringBuiltInEncodings.kCFStringEncodingUTF16BE.getValue(), false);
    assertEquals("Test byte array string", cfStringRef2.toString());
    StringByReference refString = new StringByReference(20);
    assertTrue(CF.CFStringGetCString(cfStringRef, refString, refString.getAllocatedSize(), CFStringBuiltInEncodings.kCFStringEncodingASCII.getValue()));
    assertEquals("test string", refString.getValue());
    refString = CF.CFStringGetCStringPtr(cfStringRef2, CFStringBuiltInEncodings.kCFStringEncodingMacRoman.getValue());
    assertEquals("Test byte array string", refString.toString());
    refString = new StringByReference(CF.CFStringGetMaximumSizeOfFileSystemRepresentation(cfStringRef2));
    assertTrue(CF.CFStringGetFileSystemRepresentation(cfStringRef2, refString, refString.getAllocatedSize()));
    CF.CFRelease(cfStringRef);
    cfStringRef = CF.CFStringCreateWithFileSystemRepresentation(null, refString);
    assertTrue(cfStringRef.compareTo(cfStringRef2) == CFComparisonResult.kCFCompareEqualTo);
    CFMutableStringRef mutableStringRef = CF.CFStringCreateMutableCopy(null, 0, cfStringRef2);
    assertTrue(mutableStringRef.compareTo(cfStringRef) == CFComparisonResult.kCFCompareEqualTo);
    CF.CFRelease(cfStringRef);
    CF.CFRelease(cfStringRef2);
    CF.CFRelease(mutableStringRef);
}
Also used : CFMutableStringRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableStringRef) CFStringRef(net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef) NativeLong(com.sun.jna.NativeLong) StringByReference(net.pms.util.jna.StringByReference) Test(org.junit.Test)

Aggregations

NativeLong (com.sun.jna.NativeLong)1 StringByReference (net.pms.util.jna.StringByReference)1 CFMutableStringRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFMutableStringRef)1 CFStringRef (net.pms.util.jna.macos.corefoundation.CoreFoundation.CFStringRef)1 Test (org.junit.Test)1