下面列出了org.junit.jupiter.api.condition.EnabledOnOs#com.sun.jna.NativeLong 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
void getPropertyDescEvfColorTemperature() {
final NativeLong[] desc = new NativeLong[128];
desc[0] = new NativeLong(100);
desc[1] = new NativeLong(1000);
desc[2] = new NativeLong(2000);
desc[3] = new NativeLong(3000);
final EdsPropertyDesc propertyDesc = new EdsPropertyDesc(
new NativeLong(0),
new NativeLong(0),
new NativeLong(4),
desc
);
propertyDescLogicDefaultExtended.setPropertyDescStructure(propertyDesc);
final List<Integer> propertyDescColorTemperature = propertyDescLogicDefaultExtended.getPropertyDescEvfColorTemperature(fakeCamera);
Assertions.assertEquals(4, propertyDescColorTemperature.size());
Assertions.assertEquals(100, propertyDescColorTemperature.get(0));
Assertions.assertEquals(3000, propertyDescColorTemperature.get(3));
}
/**
* Map the given region of the given file descriptor into memory.
* Returns a Pointer to the newly mapped memory throws an
* IOException on error.<br>
* <br>
* The contents of a file mapping (as opposed to an anonymous mapping;
* see MAP_ANONYMOUS below), are initialized using length bytes starting
* at offset offset in the file (or other object) referred to by the
* file descriptor fd. offset must be a multiple of the page size as
* returned by sysconf(_SC_PAGE_SIZE).
*
* @param len number of bytes to map
* @param prot describes the desired memory protection of the mapping: {@link #PROT_NONE}, {@link #PROT_READ}, {@link #PROT_WRITE}, {@link #PROT_EXEC}
* @param flags flags
* @param fd file descriptor
* @param off offset to start mapping, must be a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE)
* @return pointer
*/
public static Pointer mmap(long len, int prot, int flags, int fd, long off) {
// we don't really have a need to change the recommended pointer.
Pointer addr = new Pointer(0);
Pointer result = CLibrary.mmap(addr,
new NativeLong(len),
prot,
flags,
fd,
new NativeLong(off));
if(Pointer.nativeValue(result) == -1) {
throw new RuntimeException("mmap failed: " + errno.strerror());
}
return result;
}
@Test
void buildHandlerUseWeakReference() throws InterruptedException {
final EdsdkLibrary.EdsStateEventHandler handler;
try {
final Method buildHandler = MockFactory.initialCanonFactory.getCameraStateEventLogic().getClass().getDeclaredMethod("buildHandler", EdsdkLibrary.EdsCameraRef.class);
buildHandler.setAccessible(true);
handler = (EdsdkLibrary.EdsStateEventHandler) buildHandler.invoke(MockFactory.initialCanonFactory.getCameraStateEventLogic(), fakeCamera);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
Assertions.fail("Failed reflection", e);
throw new IllegalStateException("can not reach");
}
final NativeLong apply = handler.apply(new NativeLong(EdsStateEvent.kEdsStateEvent_Shutdown.value()), new NativeLong(0L), new Pointer(0));
fakeCamera = null;
for (int i = 0; i < 50; i++) {
System.gc();
Thread.sleep(50);
System.gc();
}
// Let's hope gc actually happened...
Assertions.assertThrows(IllegalStateException.class, () -> handler.apply(new NativeLong(EdsStateEvent.kEdsStateEvent_Shutdown.value()), new NativeLong(0L), new Pointer(0)));
}
@Test
void getPropertyDescColorTemperature() {
final NativeLong[] desc = new NativeLong[128];
desc[0] = new NativeLong(100);
desc[1] = new NativeLong(1000);
desc[2] = new NativeLong(2000);
desc[3] = new NativeLong(3000);
desc[4] = new NativeLong(6500);
final EdsPropertyDesc propertyDesc = new EdsPropertyDesc(
new NativeLong(0),
new NativeLong(0),
new NativeLong(5),
desc
);
propertyDescLogicDefaultExtended.setPropertyDescStructure(propertyDesc);
final List<Integer> propertyDescColorTemperature = propertyDescLogicDefaultExtended.getPropertyDescColorTemperature(fakeCamera);
Assertions.assertEquals(5, propertyDescColorTemperature.size());
Assertions.assertEquals(100, propertyDescColorTemperature.get(0));
Assertions.assertEquals(6500, propertyDescColorTemperature.get(4));
}
@Test
void getPropertyDescValues() {
final NativeLong[] desc = new NativeLong[128];
final EdsPropertyDesc propertyDesc = new EdsPropertyDesc(
new NativeLong(0),
new NativeLong(0),
new NativeLong(0),
desc
);
propertyDescLogicDefaultExtended.setPropertyDescStructure(propertyDesc);
final List<Integer> propertyDescValues = propertyDescLogicDefaultExtended.getPropertyDescValues(fakeCamera, EdsPropertyID.kEdsPropID_ISOSpeed);
Assertions.assertEquals(0, propertyDescValues.size());
}
@Disabled("Fail on Travis but work on local")
@Test
void setPropertyDataForInt32() {
final ArrayList<PropertyInfo> infos = Lists.newArrayList(new PropertyInfo(EdsDataType.kEdsDataType_Int32, 4), new PropertyInfo(EdsDataType.kEdsDataType_UInt32, 4));
for (PropertyInfo propertyInfo : infos) {
when(propertyLogic.getPropertyTypeAndSize(cameraRef, propertyID)).thenReturn(propertyInfo);
mockEdsdkLibrary();
when(canonLibrary.edsdkLibrary().EdsSetPropertyData(eq(cameraRef), any(NativeLong.class), any(NativeLong.class), any(NativeLong.class), any(Pointer.class))).thenReturn(new NativeLong(0));
propertySetLogic.setPropertyData(cameraRef, propertyID, nativeEnum);
propertySetLogic.setPropertyData(cameraRef, propertyID, nativeEnum.value().longValue());
propertySetLogic.setPropertyData(cameraRef, propertyID, inParam, nativeEnum);
propertySetLogic.setPropertyData(cameraRef, propertyID, inParam, nativeEnum.value().longValue());
propertySetLogic.setPropertyDataAdvanced(cameraRef, propertyID, 5L);
propertySetLogic.setPropertyDataAdvanced(cameraRef, propertyID, inParam, 6L);
}
}
@Test
void setPropertyDataForInt64() {
final ArrayList<PropertyInfo> infos = Lists.newArrayList(new PropertyInfo(EdsDataType.kEdsDataType_Int64, 8), new PropertyInfo(EdsDataType.kEdsDataType_UInt64, 8));
for (PropertyInfo propertyInfo : infos) {
when(propertyLogic.getPropertyTypeAndSize(cameraRef, propertyID)).thenReturn(propertyInfo);
mockEdsdkLibrary();
when(canonLibrary.edsdkLibrary().EdsSetPropertyData(eq(cameraRef), any(NativeLong.class), any(NativeLong.class), any(NativeLong.class), any(Pointer.class))).thenReturn(new NativeLong(0));
propertySetLogic.setPropertyData(cameraRef, propertyID, nativeEnum);
propertySetLogic.setPropertyData(cameraRef, propertyID, nativeEnum.value().longValue());
propertySetLogic.setPropertyData(cameraRef, propertyID, inParam, nativeEnum);
propertySetLogic.setPropertyData(cameraRef, propertyID, inParam, nativeEnum.value().longValue());
propertySetLogic.setPropertyDataAdvanced(cameraRef, propertyID, 5L);
propertySetLogic.setPropertyDataAdvanced(cameraRef, propertyID, inParam, 6L);
}
}
/**
* It's a heavy init method.
*/
public void init() {
for (int i = 0; i < poolSize; i++) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(fileSize);
final long address = ((DirectBuffer) byteBuffer).address();
Pointer pointer = new Pointer(address);
LibC.INSTANCE.mlock(pointer, new NativeLong(fileSize));
availableBuffers.offer(byteBuffer);
}
}
@Test
void closeSessionThrowsOnError() {
when(edsdkLibrary().EdsCloseSession(fakeCamera)).thenReturn(new NativeLong(EdsdkError.EDS_ERR_DEVICE_INVALID.value()));
when(edsdkLibrary().EdsRelease(fakeCamera)).thenReturn(new NativeLong(0L));
final CloseSessionOption option = new CloseSessionOptionBuilder()
.setCameraRef(fakeCamera)
.build();
Assertions.assertThrows(EdsdkDeviceInvalidErrorException.class, () -> spyCameraLogic.closeSession(option));
verify(edsdkLibrary(), times(0)).EdsRelease(same(fakeCamera));
}
public void destroy() {
for (ByteBuffer byteBuffer : availableBuffers) {
final long address = ((DirectBuffer) byteBuffer).address();
Pointer pointer = new Pointer(address);
LibC.INSTANCE.munlock(pointer, new NativeLong(fileSize));
}
}
@Test
void getCameraConnectedCount() {
when(edsdkLibrary().EdsGetCameraList(any(EdsdkLibrary.EdsCameraListRef.ByReference.class))).thenReturn(new NativeLong(0));
when(edsdkLibrary().EdsGetChildCount(any(), any(NativeLongByReference.class))).thenReturn(new NativeLong(0));
final int cameraConnectedCount = spyCameraLogic.getCameraConnectedCount();
Assertions.assertEquals(0, cameraConnectedCount);
}
/**
* It's a heavy init method.
*/
public void init() {
for (int i = 0; i < poolSize; i++) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(fileSize);
final long address = ((DirectBuffer) byteBuffer).address();
Pointer pointer = new Pointer(address);
LibC.INSTANCE.mlock(pointer, new NativeLong(fileSize));
availableBuffers.offer(byteBuffer);
}
}
@Test
void registerCameraPropertyEventThrowsOnError() {
when(edsdkLibrary().EdsSetPropertyEventHandler(eq(fakeCamera), any(), any(), (Pointer) isNull())).thenReturn(new NativeLong(EdsdkError.EDS_ERR_DEVICE_INVALID.value()));
Assertions.assertThrows(EdsdkDeviceInvalidErrorException.class, () -> spyCameraPropertyEventLogic.registerCameraPropertyEvent(fakeCamera));
verify(edsdkLibrary(), times(1)).EdsSetPropertyEventHandler(eq(fakeCamera), any(), any(), (Pointer) isNull());
}
private static boolean setFullScreenWindow(long wid, boolean fullScreen) {
//Ignore this method for now. Doesn't work with snaps.
if(true) return false;
// Use the JNA platform X11 binding
X11 x = X11.INSTANCE;
X11.Display display = null;
try {
// Open the display
display = x.XOpenDisplay(null);
// Send the message
int result = sendClientMessage(
display,
wid,
"_NET_WM_STATE",
new NativeLong[]{
new NativeLong(fullScreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE),
x.XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", false),
x.XInternAtom(display, "_NET_WM_STATE_ABOVE", false),
new NativeLong(0L),
new NativeLong(0L)
}
);
return (result != 0);
} finally {
if (display != null) {
// Close the display
x.XCloseDisplay(display);
}
}
}
/**
* It's a heavy init method.
*/
public void init() {
for (int i = 0; i < poolSize; i++) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(fileSize);
final long address = ((DirectBuffer) byteBuffer).address();
Pointer pointer = new Pointer(address);
LibC.INSTANCE.mlock(pointer, new NativeLong(fileSize));
availableBuffers.offer(byteBuffer);
}
}
@Test
void registerCameraObjectEvent() {
when(edsdkLibrary().EdsSetObjectEventHandler(eq(fakeCamera), any(), any(), (Pointer) isNull())).thenReturn(new NativeLong(0));
spyCameraObjectEventLogic.registerCameraObjectEvent(fakeCamera);
verify(edsdkLibrary(), times(1)).EdsSetObjectEventHandler(eq(fakeCamera), any(), any(), (Pointer) isNull());
}
public Termios() {
this.c_iflag = new NativeLong();
this.c_oflag = new NativeLong();
this.c_cflag = new NativeLong();
this.c_lflag = new NativeLong();
this.c_ispeed = new NativeLong();
this.c_ospeed = new NativeLong();
this.c_cc = new byte[NCCS];
}
public void munlock() {
final long beginTime = System.currentTimeMillis();
final long address = ((DirectBuffer) (this.mappedByteBuffer)).address();
Pointer pointer = new Pointer(address);
int ret = LibC.INSTANCE.munlock(pointer, new NativeLong(this.fileSize));
log.info("munlock {} {} {} ret = {} time consuming = {}", address, this.fileName, this.fileSize, ret, System.currentTimeMillis() - beginTime);
}
@BeforeEach
void setUp() {
TestShortcutUtil.registerCameraAddedHandler(inContext -> {
log.warn("Camera added called {}", inContext);
cameraEventCalledCount.incrementAndGet();
return new NativeLong(0);
});
TestShortcutUtil.registerPropertyEventHandler(cameraRef, (inEvent, inPropertyID, inParam, inContext) -> {
log.warn("Camera property called {}, {}, {}", EdsPropertyEvent.ofValue(inEvent.intValue()), EdsPropertyID.ofValue(inPropertyID.intValue()), inContext);
propertyEventCalledCount.incrementAndGet();
return new NativeLong(0);
});
TestShortcutUtil.registerObjectEventHandler(cameraRef, (inEvent, inRef, inContext) -> {
log.warn("Camera object called {}, {}, {}", inEvent, inRef, inContext);
objectEventCalledCount.incrementAndGet();
return new NativeLong(0);
});
TestShortcutUtil.registerStateEventHandler(cameraRef, (inEvent, inEventData, inContext) -> {
log.warn("Camera state called {}, {}, {}", inEvent, inEventData, inContext);
stateEventCalledCount.incrementAndGet();
return new NativeLong(0);
});
resetCounts();
}
@Test
void closeSession() {
when(edsdkLibrary().EdsCloseSession(fakeCamera)).thenReturn(new NativeLong(0));
when(edsdkLibrary().EdsRelease(fakeCamera)).thenReturn(new NativeLong(0L));
final CloseSessionOption option = new CloseSessionOptionBuilder()
.setCameraRef(fakeCamera)
.build();
spyCameraLogic.closeSession(option);
verify(edsdkLibrary()).EdsRelease(same(fakeCamera));
}
@Test
void sendCommandEdsDcRemoteShootingMode() {
when(edsdkLibrary().EdsSendCommand(eq(fakeCamera), any(), any())).thenReturn(new NativeLong(0));
spyCameraLogic.sendCommand(fakeCamera, EdsDcRemoteShootingMode.kDcRemoteShootingModeStart);
verify(edsdkLibrary()).EdsSendCommand(eq(fakeCamera), eq(new NativeLong(EdsCameraCommand.kEdsCameraCommand_SetRemoteShootingMode.value())), eq(new NativeLong(EdsDcRemoteShootingMode.kDcRemoteShootingModeStart.value())));
}
private EdsObjectEventHandler buildHandler(final EdsCameraRef cameraRef) {
final WeakReference<EdsCameraRef> cameraRefWeakReference = new WeakReference<>(cameraRef);
return (inEvent, inRef, inContext) -> {
final EdsCameraRef edsCameraRef = cameraRefWeakReference.get();
if (edsCameraRef == null) {
// this should not happen but who knows...
log.error("Received an event from a camera ref that is not referenced in the code anymore");
throw new IllegalStateException("Received an event from a camera ref that is not referenced in the code anymore");
// we throw or we just return doing nothing
}
this.handle(new CanonObjectEventImpl(edsCameraRef, EdsObjectEvent.ofValue(inEvent.intValue()), inRef));
return new NativeLong(0);
};
}
@Override
public void registerCameraObjectEvent(final EdsCameraRef cameraRef) {
Objects.requireNonNull(cameraRef);
final EdsObjectEventHandler objectEventHandler = buildHandler(cameraRef);
handlerLock.writeLock().lock();
try {
handlerMap.put(cameraRef, objectEventHandler);
final EdsdkError edsdkError = toEdsdkError(CanonFactory.edsdkLibrary().EdsSetObjectEventHandler(cameraRef, new NativeLong(EdsObjectEvent.kEdsObjectEvent_All.value()), objectEventHandler, Pointer.NULL));
if (edsdkError != EdsdkError.EDS_ERR_OK) {
throw edsdkError.getException();
}
} finally {
handlerLock.writeLock().unlock();
}
}
/**
* It's a heavy init method.
*/
public void init() {
for (int i = 0; i < poolSize; i++) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(fileSize);
final long address = ((DirectBuffer) byteBuffer).address();
Pointer pointer = new Pointer(address);
LibC.INSTANCE.mlock(pointer, new NativeLong(fileSize));
availableBuffers.offer(byteBuffer);
}
}
@Override
public NativeLong EdsRetain(final EdsBaseRef inRef) {
assertWasInitialized();
final Integer total = countRetain.compute(inRef, (edsBaseRef, integer) -> {
if (integer == null)
return 1;
else
return integer + 1;
});
return new NativeLong(total);
}
private static boolean setGuard(HANDLE hOtherProcess, MEMORY_BASIC_INFORMATION mbi) {
if (hasGuard(mbi)) {
return true;
}
int oldProt = mbi.protect.intValue();
int newProt = oldProt | WinNT.PAGE_GUARD;
IntByReference oldProtRef = new IntByReference();
boolean ok = Kernel32.INSTANCE.VirtualProtectEx(hOtherProcess, new WinDef.LPVOID(pointerToAddress(mbi.baseAddress)), mbi.regionSize, newProt, oldProtRef);
if (ok) {
mbi.protect = new NativeLong(newProt);
return true;
}
return false;
}
@Test
void getPropertyDataForPictureStyleDesc() {
final EdsPictureStyleDesc expectedResult = new EdsPictureStyleDesc(mockMemory);
final EdsPropertyID propertyID = EdsPropertyID.kEdsPropID_ISOSpeed;
final long inParam = 0L;
final int inPropertySize = 4;
propertyInfo = new PropertyInfo(EdsDataType.kEdsDataType_PictureStyleDesc, inPropertySize);
// mocks
when(CanonFactory.propertyLogic().getPropertyTypeAndSize(fakeBaseRef, propertyID, inParam)).thenReturn(propertyInfo);
returnNoErrorForEdsGetPropertyData(propertyID, inParam, inPropertySize);
// mock actual result
// when(mockMemory.getDouble(0)).thenReturn(expectedResult);
final EdsPictureStyleDesc result = propertyGetLogicDefaultExtended.getPropertyData(fakeBaseRef, propertyID);
// Assertions.assertEquals(expectedResult, result);
Assertions.assertEquals(expectedResult.colorTone, result.colorTone);
Assertions.assertEquals(expectedResult.contrast, result.contrast);
Assertions.assertEquals(expectedResult.filterEffect, result.filterEffect);
Assertions.assertEquals(expectedResult.sharpness, result.sharpness);
Assertions.assertEquals(expectedResult.sharpFineness, result.sharpFineness);
verify(CanonFactory.propertyLogic()).getPropertyTypeAndSize(fakeBaseRef, propertyID, inParam);
verify(CanonFactory.edsdkLibrary()).EdsGetPropertyData(eq(fakeBaseRef), eq(new NativeLong(propertyID.value())), eq(new NativeLong(inParam)), eq(new NativeLong(inPropertySize)), eq(mockMemory));
}
@Override
public void setNativeLong(long offset, NativeLong value) {
if (m_sealed)
throw new UnsupportedOperationException();
super.setNativeLong(offset, value);
}
private static boolean unsetGuard(HANDLE hOtherProcess, MEMORY_BASIC_INFORMATION mbi) {
if (!hasGuard(mbi)) {
return true;
}
int oldProt = mbi.protect.intValue();
int newProt = oldProt - WinNT.PAGE_GUARD;
IntByReference oldProtRef = new IntByReference();
boolean ok = Kernel32.INSTANCE.VirtualProtectEx(hOtherProcess, new WinDef.LPVOID(pointerToAddress(mbi.baseAddress)), mbi.regionSize, newProt, oldProtRef);
if (ok) {
mbi.protect = new NativeLong(newProt);
return true;
}
return false;
}
/**
* 激活
*/
private static void activation() {
if (INSTANCE == null) {
throw new RuntimeException("Face Engine is null");
}
NativeLong result = INSTANCE.ASFActivation(ConfUtil.appId, ConfUtil.appKey);
log.debug("------>engine is activated[{}]!<------", result.longValue());
}