• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base.process_launcher;
6 
7 import android.content.ComponentName;
8 import android.content.Intent;
9 import android.os.Bundle;
10 
11 /** An implementation of ChildProcessConnection that does not connect to a real service. */
12 public class TestChildProcessConnection extends ChildProcessConnection {
13     private static class MockChildServiceConnection implements ChildServiceConnection {
14         private boolean mBound;
15 
16         @Override
bindServiceConnection()17         public boolean bindServiceConnection() {
18             mBound = true;
19             return true;
20         }
21 
22         @Override
unbindServiceConnection()23         public void unbindServiceConnection() {
24             mBound = false;
25         }
26 
27         @Override
isBound()28         public boolean isBound() {
29             return mBound;
30         }
31 
32         @Override
updateGroupImportance(int group, int importanceInGroup)33         public void updateGroupImportance(int group, int importanceInGroup) {}
34 
35         @Override
retire()36         public void retire() {}
37     }
38 
39     private int mPid;
40     private boolean mConnected;
41     private ServiceCallback mServiceCallback;
42     private boolean mRebindCalled;
43 
44     /**
45      * Creates a mock binding corresponding to real ManagedChildProcessConnection after the
46      * connection is established: with initial binding bound and no strong binding.
47      */
TestChildProcessConnection(ComponentName serviceName, boolean bindToCaller, boolean bindAsExternalService, Bundle serviceBundle)48     public TestChildProcessConnection(ComponentName serviceName, boolean bindToCaller,
49             boolean bindAsExternalService, Bundle serviceBundle) {
50         super(null /* context */, serviceName, null, bindToCaller, bindAsExternalService,
51                 serviceBundle, new ChildServiceConnectionFactory() {
52                     @Override
53                     public ChildServiceConnection createConnection(Intent bindIntent, int bindFlags,
54                             ChildServiceConnectionDelegate delegate, String instanceName) {
55                         return new MockChildServiceConnection();
56                     }
57                 }, null /* instanceName */);
58     }
59 
setPid(int pid)60     public void setPid(int pid) {
61         mPid = pid;
62     }
63 
64     @Override
getPid()65     public int getPid() {
66         return mPid;
67     }
68 
69     // We don't have a real service so we have to mock the connection status.
70     @Override
start(boolean useStrongBinding, ServiceCallback serviceCallback)71     public void start(boolean useStrongBinding, ServiceCallback serviceCallback) {
72         super.start(useStrongBinding, serviceCallback);
73         mConnected = true;
74         mServiceCallback = serviceCallback;
75     }
76 
77     @Override
rebind()78     public void rebind() {
79         super.rebind();
80         mRebindCalled = true;
81     }
82 
83     @Override
stop()84     public void stop() {
85         super.stop();
86         mConnected = false;
87     }
88 
89     @Override
isConnected()90     public boolean isConnected() {
91         return mConnected;
92     }
93 
getServiceCallback()94     public ServiceCallback getServiceCallback() {
95         return mServiceCallback;
96     }
97 
getAndResetRebindCalled()98     public boolean getAndResetRebindCalled() {
99         boolean called = mRebindCalled;
100         mRebindCalled = false;
101         return called;
102     }
103 }
104