Android: Inject fake camera preview for testing pu

2019-06-20 08:11发布

问题:

I am writing some black-box, acceptance tests that run on a physical android device. The application under test (AUT) relies on the camera's preview. Specifically, it uses the setOneShotPreviewCallback method of the android.hardware.Camera class. I am looking for a way to inject a fake preview picture to test the app's behavior.

I thought I could fool the Camera object by having the test code call the AUT's PreviewCallback object, but unfortunately, the instance variable mPreviewCallback is private to the Camera object and thus my test code has no way to get a reference to the PreviewCallback object.

Another way I thought about was to send a Message to the Camera's EventHandler, but again the field mEventHandler is private, so the test code has no way to get a reference to the Handler.

回答1:

Please see Android mock Camera and How to mock a picture in Android Emulator Camera? Both methods don't need rooted device, but require a change of the application. I believe you can make the necessary modification even if you don't have access to the source code, by decompiling the dex.