Log.Picture使用

Log不但可以记录Message,Error,还可以记录Picture。如Log.Picture(Sys.Desktop.ActiveWindow, "Message", "Extend Message", pmHighest)

详细情况可查阅Help文档

非常好,继续帮我编写下面代码的单元测试 package com.st.systemsettings.presenter; import android.util.Log; import com.st.systemsettings.base.BasePresenter; import com.st.systemsettings.contract.PictureContract; import com.st.systemsettings.manager.PictureManager; import com.st.systemsettings.manager.SystemNotifyManager; public class PicturePresenterImpl implements BasePresenter<PictureContract.View> { private static final String TAG = "PicturePresenterImpl"; private PictureContract.View mView; private PictureManager mPictureManager; @Override public void registerView(PictureContract.View view) { this.mView = view; } @Override public void unRegisterView() { mView = null; } @Override public void init() { mPictureManager = PictureManager.getInstance(); mPictureManager.setPictureModeChangedListener(mPictureModeChangedListener); int result = mPictureManager.getPictureMode(); if (mView != null) { mView.updatePictureMode(result); } } public void clickItem(int position) { Log.i(TAG, "clickItem: position = " + position); mPictureManager.setPictureMode(position); } public void updateSettingFocus(int type) { Log.i(TAG, "updateSettingFocus: type = " + type); SystemNotifyManager.getInstance().onSettingsClearFocus(type); } private final PictureManager.PictureModeChangedListener mPictureModeChangedListener = new PictureManager.PictureModeChangedListener() { @Override public void onPictureModeChanged(int mode) { Log.i(TAG, "onPictureModeChanged: mode = " + mode); if (mView != null) { mView.updatePictureMode(mode); } } }; }
07-24
package com.st.systemsettings.manager; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import android.content.Context; import android.util.Log; import java.lang.reflect.Field; import java.util.concurrent.CountDownLatch; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; @RunWith(PowerMockRunner.class) @PrepareForTest({Log.class, OptixDisplayManager.class}) public class PictureManagerTest { @Mock private Context mockContext; @Mock private PictureManager.PictureModeChangedListener mockListener; @Mock private OptixDisplayManager mockOptixDisplayManager; private PictureManager pictureManager; @Before public void setUp() { try { // 初始化 Mockito 注解 MockitoAnnotations.openMocks(this); // 准备静态模拟 prepareStaticMocks(); // 重置单例状态 resetSingleton(); // 创建实例并初始化 pictureManager = PictureManager.getInstance(); pictureManager.init(mockContext); // 注入模拟的 OptixDisplayManager injectMockOptixDisplayManager(); // 设置模拟监听器 pictureManager.setPictureModeChangedListener(mockListener); } catch (Exception e) { throw new RuntimeException("Setup failed", e); } } @After public void tearDown() { try { resetSingleton(); } catch (Exception e) { // 安全地忽略清理错误 } } // 准备静态模拟 private void prepareStaticMocks() { // 模拟 Log 类 PowerMockito.mockStatic(Log.class); PowerMockito.when(Log.i(anyString(), anyString())).thenReturn(0); // 模拟 OptixDisplayManager 的单例 PowerMockito.mockStatic(OptixDisplayManager.class); PowerMockito.when(OptixDisplayManager.getInstance()).thenReturn(mockOptixDisplayManager); } // 修复后的重置单例方法 - 不再抛出受检异常 private void resetSingleton() { try { Field instanceField = PictureManager.class.getDeclaredField("sManager"); instanceField.setAccessible(true); instanceField.set(null, null); } catch (Exception e) { // 将受检异常转换为运行时异常 throw new RuntimeException("Failed to reset singleton", e); } } // 修复后的注入方法 - 不再抛出受检异常 private void injectMockOptixDisplayManager() { try { Field optixField = PictureManager.class.getDeclaredField("mOptixDisplayManager"); optixField.setAccessible(true); optixField.set(pictureManager, mockOptixDisplayManager); } catch (Exception e) { throw new RuntimeException("Failed to inject mock", e); } } @Test public void testSingletonPattern() { PictureManager firstInstance = PictureManager.getInstance(); PictureManager secondInstance = PictureManager.getInstance(); assertSame(firstInstance, secondInstance); } @Test public void testInitSetsContextCorrectly() { try { Field contextField = PictureManager.class.getDeclaredField("mContext"); contextField.setAccessible(true); Context privateContext = (Context) contextField.get(pictureManager); assertSame(mockContext, privateContext); } catch (Exception e) { fail("Reflection failed: " + e.getMessage()); } } @Test public void testSetPictureModeCallsOptixManager() { int testMode = 2; pictureManager.setPictureMode(testMode); verify(mockOptixDisplayManager).setCanvasSize(testMode); } @Test public void testSetPictureModeTriggersListener() { int testMode = 3; pictureManager.setPictureMode(testMode); verify(mockListener).onPictureModeChanged(testMode); } @Test public void testSetPictureModeWithoutListener() { pictureManager.setPictureModeChangedListener(null); int testMode = 4; pictureManager.setPictureMode(testMode); verify(mockListener, never()).onPictureModeChanged(anyInt()); } @Test public void testGetPictureModeReturnsCorrectValue() { int expectedMode = 5; when(mockOptixDisplayManager.getCanvasSize()).thenReturn(expectedMode); int result = pictureManager.getPictureMode(); assertEquals(expectedMode, result); } @Test public void testListenerIsSetCorrectly() { PictureManager.PictureModeChangedListener newListener = mock(PictureManager.PictureModeChangedListener.class); pictureManager.setPictureModeChangedListener(newListener); pictureManager.setPictureMode(1); verify(newListener).onPictureModeChanged(1); verify(mockListener, never()).onPictureModeChanged(anyInt()); } @Test public void testReInitUpdatesContext() { try { Context newContext = Mockito.mock(Context.class); pictureManager.init(newContext); Field contextField = PictureManager.class.getDeclaredField("mContext"); contextField.setAccessible(true); Context privateContext = (Context) contextField.get(pictureManager); assertSame(newContext, privateContext); } catch (Exception e) { fail("Reflection failed: " + e.getMessage()); } } @Test public void testThreadSafety() throws InterruptedException { resetSingleton(); // 现在安全了 CountDownLatch latch = new CountDownLatch(2); PictureManager[] instances = new PictureManager[2]; Thread t1 = new Thread(() -> { instances[0] = PictureManager.getInstance(); latch.countDown(); }); Thread t2 = new Thread(() -> { instances[1] = PictureManager.getInstance(); latch.countDown(); }); t1.start(); t2.start(); latch.await(); assertNotNull(instances[0]); assertSame(instances[0], instances[1]); } @Test public void testLoggingInSetPictureMode() { int testMode = 6; pictureManager.setPictureMode(testMode); PowerMockito.verifyStatic(Log.class); Log.i("PictureManager", "setPictureMode: mode = " + testMode); } @Test public void testLoggingInGetPictureMode() { int expectedMode = 7; when(mockOptixDisplayManager.getCanvasSize()).thenReturn(expectedMode); pictureManager.getPictureMode(); PowerMockito.verifyStatic(Log.class); Log.i("PictureManager", "getPictureMode: result = " + expectedMode); } @Test public void testNullContextHandling() { resetSingleton(); // 现在安全了 PictureManager uninitialized = PictureManager.getInstance(); uninitialized.setPictureMode(1); verify(mockOptixDisplayManager, never()).setCanvasSize(anyInt()); } @Test public void testMultipleInitializations() { Context secondContext = Mockito.mock(Context.class); pictureManager.init(secondContext); // 应该成功执行,不会抛出异常 pictureManager.setPictureMode(8); verify(mockOptixDisplayManager).setCanvasSize(8); } @Test public void testListenerRemoval() { pictureManager.setPictureModeChangedListener(null); pictureManager.setPictureMode(9); verify(mockListener, never()).onPictureModeChanged(anyInt()); } } 上面是我的测试代码,下面是出现的报错,请告诉我怎么解决 Failed to transform class with name com.st.systemsettings.manager.OptixDisplayManager. Reason: [source error] cannot find constructor android.os.UserHandle(int) java.lang.IllegalStateException: Failed to transform class with name com.st.systemsettings.manager.OptixDisplayManager. Reason: [source error] cannot find constructor android.os.UserHandle(int)
07-23
package com.st.systemsettings.manager; import android.annotation.SuppressLint; import android.content.Context; import android.util.Log; public class PictureManager { private static final String TAG = “PictureManager”; @SuppressLint(“StaticFieldLeak”) private static PictureManager sManager; private Context mContext; private final String FRAME_MODE = “persist.elink.hw_frame_gear”; private PictureModeChangedListener mPictureModeChangedListener; private OptixDisplayManager mOptixDisplayManager; public static PictureManager getInstance() { if (sManager == null) { sManager = new PictureManager(); } return sManager; } public void init(Context context) { Log.i(TAG, "init: "); mContext = context; mOptixDisplayManager = OptixDisplayManager.getInstance(); } public void setPictureMode(int mode) { Log.i(TAG, "setPictureMode: mode = " + mode); mOptixDisplayManager.setCanvasSize(mode); if (mPictureModeChangedListener != null) { mPictureModeChangedListener.onPictureModeChanged(mode); } } public int getPictureMode() { int result = mOptixDisplayManager.getCanvasSize(); Log.i(TAG, "getPictureMode: result = " + result); return result; } public void setPictureModeChangedListener(PictureModeChangedListener pictureModeChangedListener) { mPictureModeChangedListener = pictureModeChangedListener; } public interface PictureModeChangedListener { void onPictureModeChanged(int mode); } } 帮我使用mockito和junit4编写上面代码的单元测试,具体依赖如下 //单元测试相关依赖 testImplementation ‘org.powermock:powermock-module-junit4:2.0.7’ testImplementation ‘org.powermock:powermock-module-junit4-rule:1.7.4’ testImplementation ‘org.powermock:powermock-api-mockito2:2.0.7’ testImplementation ‘org.mockito:mockito-core:3.3.3’ testImplementation ‘junit:junit:4.13.1’ testImplementation ‘android.arch.core:core-testing:1.1.1’ testImplementation “org.mockito:mockito-inline:3.11.2”
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值