24

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.

lacton
  • 2,316
  • 2
  • 22
  • 24
  • 1
    I think this suggestion might break your physical device rule but maybe running on genymotion can achieve the trick you desire. http://www.genymotion.com/features/ – Machinarius Dec 16 '13 at 18:40
  • Is your device rooted? – Alex Cohn Dec 17 '13 at 00:03
  • @Machinarius: I have followed your suggestion and have installed genymotion. Although it is a fine piece of software, I could not find a way to programmatically inject an image in the camera's preview stream. It seems the only action genymotions allows is to connect my physical webcam's stream to the simulated camera. Have I overlooked something? – lacton Dec 18 '13 at 14:02
  • @Alex My device is not (yet) rooted, but I am ready to do it if it solves my problem. – lacton Dec 18 '13 at 14:03
  • @Machinarius With some driver trickery you could trick genymotion into feeding android the image you want. That highly depends on the OS you want to run this on though – Machinarius Dec 18 '13 at 16:46
  • Possible duplicate of [Android mock Camera](http://stackoverflow.com/questions/18572130/android-mock-camera) – muneikh Jul 20 '16 at 12:41

1 Answers1

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.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • The first question (Android mock Camera) suggests using Mockito to mock the Camera class. Yet, [the mockito offical doc](http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html) states that "Mockito doesn't mock final methods". Most Camera's methods being final, it seems mocking Camera is not an option. – lacton Jan 28 '14 at 21:55
  • The final methods like [setOneShotPreviewCallback()](http://developer.android.com/reference/android/hardware/Camera.html#setOneShotPreviewCallback(android.hardware.Camera.PreviewCallback)) are only thin wrappers around native methods. You can create the mock Camera class that will load an alternative native library to implement this method. – Alex Cohn Jan 30 '14 at 11:01