Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.webkit.cts;
     18 
     19 
     20 import android.app.Service;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.os.Handler;
     24 import android.os.IBinder;
     25 import android.os.Message;
     26 import android.os.Messenger;
     27 import android.os.RemoteException;
     28 import android.util.Log;
     29 
     30 import junit.framework.Assert;
     31 import junit.framework.AssertionFailedError;
     32 
     33 // Subclasses are the ones that get actually used, so make this abstract
     34 abstract class TestProcessService extends Service {
     35     static final int MSG_RUN_TEST = 0;
     36     static final int MSG_EXIT_PROCESS = 1;
     37     static final String TEST_CLASS_KEY = "class";
     38 
     39     static final int REPLY_OK = 0;
     40     static final int REPLY_EXCEPTION = 1;
     41     static final String REPLY_EXCEPTION_KEY = "exception";
     42 
     43     @Override
     44     public IBinder onBind(Intent intent) {
     45         return mMessenger.getBinder();
     46     }
     47 
     48     final Messenger mMessenger = new Messenger(new IncomingHandler());
     49 
     50     private class IncomingHandler extends Handler {
     51         @Override
     52         public void handleMessage(Message msg) {
     53             if (msg.what == MSG_EXIT_PROCESS) {
     54                 System.exit(0);
     55             }
     56 
     57             try {
     58                 if (msg.what != MSG_RUN_TEST) {
     59                     throw new AssertionFailedError("Unknown service message " + msg.what);
     60                 }
     61 
     62                 String testClassName = msg.getData().getString(TEST_CLASS_KEY);
     63                 Class testClass = Class.forName(testClassName);
     64                 TestProcessClient.TestRunnable test =
     65                         (TestProcessClient.TestRunnable) testClass.newInstance();
     66                 test.run(TestProcessService.this);
     67             } catch (Throwable t) {
     68                 try {
     69                     Message m = Message.obtain(null, REPLY_EXCEPTION);
     70                     m.getData().putSerializable(REPLY_EXCEPTION_KEY, t);
     71                     msg.replyTo.send(m);
     72                 } catch (RemoteException e) {
     73                     throw new RuntimeException(e);
     74                 }
     75                 return;
     76             }
     77 
     78             try {
     79                 msg.replyTo.send(Message.obtain(null, REPLY_OK));
     80             } catch (RemoteException e) {
     81                 throw new RuntimeException(e);
     82             }
     83         }
     84     }
     85 }
     86