164

I found the following function in package android.app.ActivityManager.

public static boolean isUserAMonkey ()

The reference describes:

public static boolean isUserAMonkey () Since: API Level 8

Returns "true" if the user interface is currently being messed with by a monkey.

I was shocked by this strange function. And have a few questions.

  • What does this (user interface is currently being messed with by a monkey) mean?

  • What is the practical use of this function?

  • Why do they use isUserAMonkey for the function name?

Community
  • 1
  • 1
gtiwari333
  • 24,554
  • 15
  • 75
  • 102
  • 2
    There is no proper documentation. Then how can we use the function. They should get review on their documentation. – Mahendran Dec 31 '11 at 07:21
  • according to answer by HefferWolf, this method is used for some automatic testing of android framework (i think), so we do not need to use this function at all.. so no need of PROPER documentation. – gtiwari333 Dec 31 '11 at 11:51
  • 10
    Shock the monkey – Jeff Axelrod Oct 04 '12 at 01:35
  • 1
    It looks to me like test code that's leaked into production libraries, in this case the ActivityManager. Having your production code being knowledgeable of whether it is currently being tested, and presumably altering it's actions in any way based that knowledge works against the whole point of test code which easily leads to Hisenbugs. It also adds cruft to the API making it harder to understand and to maintain. The fact that this SO question even exists is evidence enough. – Melinda Green Jun 23 '13 at 22:52
  • 1
    I laughed when I saw this method – Marcos Vasconcelos Aug 05 '13 at 20:29
  • I would presume it's derived from the terminology 'monkey testing' which can be started via ADB. Utilised for randomly hitting buttons, entering text etc... to ensure the system doesn't fall over. – greysqrl Apr 21 '16 at 11:49
  • I defently will use that funny thing – Yanay Hollander Jul 26 '19 at 14:33

3 Answers3

111

This method is for checking whether the current user is a test user by some automatic testing, called 'monkey' by Android devs.

HefferWolf
  • 3,894
  • 1
  • 23
  • 29
  • 20
    Exactly. It's worthwhile seeing this page: http://developer.android.com/guide/developing/tools/monkey.html – Kibi Dec 13 '11 at 15:41
37

The Monkey is a program that runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. You can use the Monkey to stress-test applications that you are developing, in a random yet repeatable manner.

just refer these links also:

Community
  • 1
  • 1
harikrishnan
  • 1,985
  • 4
  • 32
  • 63
1

The story of how this method was created is quite interesting and i couldn't resist sharing it. I think it explains perfectly why such a method is needed.

"One day i walked into the monkey lab to hear a voice say, '911 -What's your emergency?" That situation resulted in Dianne adding a new function to the API, isUserAMonkey() which is used to gate actions that monkeys shouldn't take during tests (including dialing the phone and resetting the device).

Androids: The Team That Built the Android Operating System (Haase, Chet)

Sarantis Tofas
  • 5,097
  • 1
  • 23
  • 36