Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6330801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:56:12+00:00 2026-05-24T17:56:12+00:00

I’m using robotium for testing and I’m running into a bunch of timing issues

  • 0

I’m using robotium for testing and I’m running into a bunch of timing issues that are making it difficult for me to know when an activity (or view) is done loading. As a result the tests I’m writing are not very robust.

I’m looking for a way to instrument my application under test with events that can be pushed to the test framework. If my application under test could “tell” my tests when an expect event occurs, this would be very useful. I’ve used Event Tracing for Windows for windows\windows phone in the past to great effect.

The poor mans way of doing this that I’m looking at is to have my test application read the logcat in realtime and notify the test when an expected event occurs.

Does anyone have any other ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:56:13+00:00Added an answer on May 24, 2026 at 5:56 pm

    To solve this I’m relying on the OS logcat. I instrument portions of the code being tested with trace messages and wait for them to occur in the test code. Its perhaps not the most performant way of doing things, but it suits my needs. I first tried to make an asynctask that read from the logcat, but ran into timing issues. I finally implemented something like this:

        logger.info("Click on something");
        EmSingleton.get().setCheckPoint();
        solo.clickOnText(SOMETHING, 1, true); // writes RELOAD_COMPLETE to logcat when done
    
        logger.info("Validate that the event is done);
        EmSingleton.get().waitForEvent(GrouponLnConstants.TodaysDeal.RELOAD_COMPLETE, 15000);
        // Do other stuff... 
    

    EventManager code (That I wrapped in a singleton called EmSingleton):

    public class EventManager {
    private ArrayList<String> args = null;
    private long startTime;
    
    public EventManager() {
        args = new ArrayList<String>(Arrays.asList("-v", "time", "-d"));
        setCheckPoint();
    }
    
    public EventManager(ArrayList<String> _args) {
        this();
        for(String s: args) {
            if (!args.contains(s)) {
                args.add(s);
            }
        }
    }
    
    public void setCheckPoint()
    {
        Time startTimeOS = new android.text.format.Time();
        startTimeOS.setToNow();
        startTime = startTimeOS.toMillis(true);
    }
    
    public LogEvent checkEvent(String filter) throws Exception {
        ArrayList<LogEvent> events = gatherEvents();
    
        for(LogEvent event: events){
            if(event.checkMsgSubstring(filter)){
                return event;
            }
        }
    
        return null;
    }
    
    public LogEvent waitForEvent(String filter, int timeout) throws Exception
    {
        int retries = timeout/1000 == 0 ? 1 : timeout/1000;
        for(int i = 0; i < retries; i++)
        {
            LogEvent event = checkEvent(filter);
            if(event != null){
                return event;
            }
            Thread.sleep(1000);
        }
        return null;
    }
    
    public ArrayList<LogEvent> getEvents() {
        return gatherEvents();
    }
    
    public void clearEvents() throws Exception{
        ArrayList<String> commandLine = new ArrayList<String>();
        commandLine.add("logcat");
        commandLine.add("-c");
    
        ProcessBuilder pb = new ProcessBuilder(commandLine.toArray(new String[0]));
        pb.redirectErrorStream(true);
        Process logcatProcess = pb.start();
    
        logcatProcess.waitFor();
    }
    
    protected ArrayList<LogEvent> gatherEvents() {
        ArrayList<LogEvent> events = new ArrayList<LogEvent>();
        final StringBuilder log = new StringBuilder();
        BufferedReader br;
        try {
            ArrayList<String> commandLine = new ArrayList<String>();
            commandLine.add("logcat");
            if (null != args) {
                commandLine.addAll(args);
            }
    
            ProcessBuilder pb = new ProcessBuilder(commandLine.toArray(new String[0]));
            pb.redirectErrorStream(true);
            Process logcatProcess = pb.start();
    
            InputStream is = logcatProcess.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
    
            String line = null;
    
            while (true) {
                line = br.readLine();
                if(line == null){
                    break;
                }
                // Add events to the events arraylist if they occur after the LogCollector has started to run
                LogEvent event = new LogEvent(line);
                if(event.peekTimeMS() > startTime)
                {
                    events.add(event);
                }
            }
        } catch (Exception e) {
            TestLogger.get().error("GrouponTest", String.format("gatherEvents doInBackground failed: %s", e.toString()));
        }
    
        return events;
    }
    }
    

    You’ll need to set the following permission in your application:

    <uses-permission android:name="android.permission.READ_LOGS">
    

    I also created a LogEvent Object to parse events coming from the logcat:

    public class LogEvent {
    String orignalString = null;
    boolean parsed = false;
    
    long timeMs = 0;
    String timeString = null;
    String verbosity = null;
    String tag = null;
    int pid = 0;
    String msg = null;
    
    // Lazy factory design pattern
    // http://en.wikipedia.org/wiki/Lazy_initialization
    public LogEvent(String s) {
        orignalString = s;
    }
    
    public long getTimeMs() {
        checkInit();
        return timeMs;
    }
    
    public long peekTimeMS()
    {
        // Time is always formattted as such: MM-DD HH:MM:SS.XXX
        return parseTime(orignalString.substring(0,5), orignalString.substring(6,18));
    }
    
    public String getTimeString() {
        checkInit();
        return timeString;
    }
    
    public String getVerbosity() {
        checkInit();
        return verbosity;
    }
    
    public String getTag() {
        checkInit();
        return tag;
    }
    
     public int getPid() {
        checkInit();
        return pid;
    }
    
    public String getMsg() {
        checkInit();
        return msg;
    }
    
    /**
     * Checks to see if the event message contains a substring
     * @param check
     * @return
     */
    public Boolean checkMsgSubstring(String check)
    {
        int index = orignalString.indexOf(check);
        boolean isSubstring = (index >= 0);
        return isSubstring;
    }
    
    public String toString()
    {
        checkInit();
        return String.format("%s %s/%s(%d): %s", timeString, verbosity, tag, pid, msg);
    }
    
    private void checkInit()
    {
        if(!parsed)
        {
            parseEvent();
            parsed = true;
        }
    }
    
    private void parseEvent()
    {
        try{
        String [] splits = orignalString.split("[ ]+");
        // Sometimes the PID is of format ( XX) instead of (XX)
        if(splits[2].indexOf(")") < 0)
        {
            splits[2] += splits[3];
            ArrayList<String> formatted = new ArrayList<String>(Arrays.asList(splits));
            formatted.remove(3);
            splits = formatted.toArray(new String[formatted.size()]);
        }
    
        // Parse time
        timeMs = parseTime(splits[0], splits[1]);
        timeString = String.format("%s %s", splits[0], splits[1]);
        // Parse tag
        verbosity = parseVerbosity(splits[2]);
        tag = parseTag(splits[2]);
        pid = parsePid(splits[2]);
        // Parse message (may be empty)
        if (splits.length > 3) {
            msg = orignalString.substring(orignalString.indexOf(splits[3]));
        } else {
            msg = "";
        }
        }
        catch (Exception e)
        {
            // TODO: there are some strangely formated events in the system. need to deal with these?
        }
    }
    
    /**
     * Time comes in following format: 08-11 20:03:17.182:
     * Parse into milliseconds
     * @param day string of format 08-11
     * @param hours string of format 20:03:17.182:
     * @return
     */
    private long parseTime(String day, String hours)
    {
        Time timeToSet = new Time();
        Time currentTime = new Time();
        currentTime.setToNow();
    
        // Parse fields
        String[] daySplits = day.split("-");
        if(daySplits.length < 2)
            return 0;
    
        String[] hourSplits = hours.split(":");
        if(hourSplits.length < 2)
            return 0;
    
        String[] secondSplits = hourSplits[2].split("\\.");
        if(secondSplits.length < 2)
            return 0;
    
        int _year = currentTime.year;
        int _month = Integer.parseInt(daySplits[0])-1;
        int _day = Integer.parseInt(daySplits[1]);
        int _hour = Integer.parseInt(hourSplits[0]);
        int _min = Integer.parseInt(hourSplits[1]);
        int _sec = Integer.parseInt(secondSplits[0]);
        int _mili = Integer.parseInt(secondSplits[1]);
    
        //set(int second, int minute, int hour, int monthDay, int month, int year)
        timeToSet.set(_sec, _min, _hour, _day, _month, _year);
    
        // return calculated value
        long parsedTimeInMili = timeToSet.toMillis(true) + (long)_mili;
        return parsedTimeInMili;
    }
    
    private String parseVerbosity(String s)
    {
        return s.split("/")[0];
    }
    
    private String parseTag(String s)
    {
        int verbosityLength = parseVerbosity(s).length() +1;
        String tagPart = s.substring(verbosityLength);
        return tagPart.split("\\(")[0];
    }
    
    private int parsePid(String s)
    {
        try {
            String pidPart = s.split("\\(")[1];
            return Integer.parseInt(pidPart.split("\\)")[0]);
        } catch (Exception e) {
            e.toString();
        }
        return -1;
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Does anyone know how can I replace this 2 symbol below from the string
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.