OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
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.chrome.browser.gsa; | |
6 | |
7 import android.content.ComponentName; | |
8 import android.content.Context; | |
9 import android.content.Intent; | |
10 import android.content.ServiceConnection; | |
11 import android.os.IBinder; | |
12 import android.support.test.InstrumentationRegistry; | |
13 import android.support.test.filters.SmallTest; | |
14 | |
15 import org.junit.Assert; | |
16 import org.junit.Test; | |
17 import org.junit.runner.RunWith; | |
18 | |
19 import org.chromium.base.Log; | |
20 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | |
21 | |
22 import java.util.concurrent.Semaphore; | |
23 import java.util.concurrent.TimeUnit; | |
24 import java.util.concurrent.atomic.AtomicReference; | |
25 | |
26 /** Tests for GSAServiceClient. */ | |
27 @RunWith(ChromeJUnit4ClassRunner.class) | |
28 public class GSAServiceClientTest { | |
29 private static final String TAG = "GSAServiceClientTest"; | |
30 | |
31 @Test | |
32 @SmallTest | |
33 public void testGetPssForService() throws Exception { | |
34 Context context = InstrumentationRegistry.getInstrumentation().getTarget
Context(); | |
35 | |
36 if (!GSAState.getInstance(context).isGsaAvailable()) { | |
37 Log.w(TAG, "GSA is not available"); | |
38 return; | |
39 } | |
40 | |
41 final AtomicReference<ComponentName> componentNameRef = new AtomicRefere
nce<>(); | |
42 final Semaphore semaphore = new Semaphore(0); | |
43 Intent intent = | |
44 new Intent(GSAServiceClient.GSA_SERVICE).setPackage(GSAState.SEA
RCH_INTENT_PACKAGE); | |
45 | |
46 ServiceConnection connection = new ServiceConnection() { | |
47 @Override | |
48 public void onServiceConnected(ComponentName name, IBinder service)
{ | |
49 componentNameRef.set(name); | |
50 semaphore.release(); | |
51 } | |
52 | |
53 @Override | |
54 public void onServiceDisconnected(ComponentName name) {} | |
55 }; | |
56 context.bindService(intent, connection, Context.BIND_AUTO_CREATE); | |
57 try { | |
58 Assert.assertTrue("Timeout", semaphore.tryAcquire(10, TimeUnit.SECON
DS)); | |
59 | |
60 int pss = GSAServiceClient.getPssForService(componentNameRef.get()); | |
61 Assert.assertTrue(pss != GSAServiceClient.INVALID_PSS); | |
62 Assert.assertTrue(pss > 0); | |
63 } finally { | |
64 context.unbindService(connection); | |
65 } | |
66 } | |
67 | |
68 @Test | |
69 @SmallTest | |
70 public void testGetPssForServiceServiceNotFound() throws Exception { | |
71 ComponentName componentName = new ComponentName("unknown.package.name",
"UnknownClass"); | |
72 int pss = GSAServiceClient.getPssForService(componentName); | |
73 Assert.assertTrue(pss == GSAServiceClient.INVALID_PSS); | |
74 } | |
75 } | |
OLD | NEW |