UiAutomator locks the device and stop respond to adb commands

622 views Asked by At

I'm currently trying to create a very simple UI dumper through UIautomator but having some really strange issues. In my current design I use nanohttpd to communicate with my apk.

So my uiautomator test look like this:

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class Start {

    public static final String Tag = "UiDuper";
    private int port;
    private UiAutomatorHttpServer server;

    public Start() {
        Bundle extras = InstrumentationRegistry.getArguments();
        port = 9008;
        if(extras.containsKey("port")) {
            port = Integer.parseInt(extras.getString("port"));
            Log.i(Tag, "UIAutomator server port is " + port);
        }

    }

    @Before
    public void setUp() throws IOException, RemoteException {
        Log.i(Tag, "Start server with port " + port);
        server = new UiAutomatorHttpServer(port);
        server.start();
    }

    @After
    public void TearDown() {
        Log.i(Tag, "Tear down server");
        if(server != null) {
            server.stop();
        }
    }

    @Test
    @LargeTest
    public void RunServer() throws InterruptedException {
        while(true) {
            Thread.sleep(100);
            if(server == null)
                return;
            if(!server.isAlive()) {
                return;
            }
            if(server.ShouldStopServer()) {
                return;
            }
        }
    }
}

And my "server" look like this:

public class UiAutomatorHttpServer extends NanoHTTPD {

    private final String stop = "/stop";
    private final String ping = "/ping";
    private final String dump = "/dump";

    private UiAutomatorService service;
    private boolean shouldStopServer;


    public UiAutomatorHttpServer(int port) throws RemoteException {
        super(port);
        service = new UiAutomatorService();
        stop = false;
    }

    @Override
    public Response serve(IHTTPSession session) {
        Log.i(Start.Tag, "New request");
        if(session.getUri().equals(ping)) {
            Log.i(Start.Tag, "Sending ping response");
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Hello human.");
        }

        if(session.getUri().equals(dump)) {
            Log.i(Start.Tag, "Sending dump response");
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT,  service.dumpWindowHierarchy());
        }

        if(session.getUri().equals(stop)) {
            stopServer();
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Stop");
        }

        Log.e(Start.Tag, "Something went wrong!");
        Log.e(Start.Tag, session.getUri());
        return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not found.");
    }

    public void stopServer() {
        Log.i(Start.Tag, "Stopping server");
        shouldStopServer = true;
    }

    public boolean ShouldStopServer() {
        return shouldStopServer;
    }

}

After installing the apk on the device I do a adb forward:

adb forward tcp:9008 tcp:9030

And then I can start the server like this:

 adb shell am instrument -w -r -e debug false -e port 9030 -e class   com.dumper.screenDumperServer.Start#RunServer com.dumper.screenDumperServer.test/android.support.test.runner.AndroidJUnitRunner

And dump UI like this:

http://localhost:9008/dump

The problem is that it seems to stop to work after a while and I can see following things:

  • http://localhost:9008/ping stop respond
  • All adb commands stop working (adb logcat, adb shell, etc). They don't give any errors but nothing happen.

I can then close the test/server window but it won't solve the problem - adb will still not respond. Sometimes it will start to respond after a while but then the server won't work again (I can start the test but I can't ping or dump).

Adb kill/start will also make it respond to adb commands again but same thing there where I can't ping the server again.

The only thing that seems to get it to work again is to:

  • Plug the usb in and out (work most of the time)
  • Restart the device

Reinstalling the APKs doesn't seem to do much.

So I'm starting to think that I'm doing something really wrong here because this shouldn't be such a big thing (or are uiautomator really bad?). The logcat or uiautomator event logs don't say anything either.

I have tried to add some sleep inside the server loop but that doesn't seem to change anything either.

0

There are 0 answers