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 17package android.support.customtabs; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertTrue; 21import static org.junit.Assert.fail; 22 23import android.content.Context; 24import android.content.Intent; 25import android.support.test.filters.SmallTest; 26import android.support.test.rule.ActivityTestRule; 27import android.support.test.rule.ServiceTestRule; 28import android.support.test.runner.AndroidJUnit4; 29import android.support.testutils.PollingCheck; 30 31import org.junit.Before; 32import org.junit.Rule; 33import org.junit.Test; 34import org.junit.runner.RunWith; 35 36import java.util.concurrent.TimeoutException; 37 38/** 39 * Tests for {@link PostMessageServiceConnection} with no {@link CustomTabsService} component. 40 */ 41@RunWith(AndroidJUnit4.class) 42@SmallTest 43public class PostMessageServiceConnectionTest { 44 @Rule 45 public final ServiceTestRule mServiceRule; 46 @Rule 47 public final ActivityTestRule<TestActivity> mActivityTestRule; 48 private TestCustomTabsCallback mCallback; 49 private Context mContext; 50 private PostMessageServiceConnection mConnection; 51 private boolean mServiceConnected; 52 53 54 public PostMessageServiceConnectionTest() { 55 mActivityTestRule = new ActivityTestRule<TestActivity>(TestActivity.class); 56 mServiceRule = new ServiceTestRule(); 57 } 58 59 @Before 60 public void setup() { 61 mCallback = new TestCustomTabsCallback(); 62 mContext = mActivityTestRule.getActivity(); 63 mConnection = new PostMessageServiceConnection( 64 new CustomTabsSessionToken(mCallback.getStub())) { 65 @Override 66 public void onPostMessageServiceConnected() { 67 mServiceConnected = true; 68 } 69 70 @Override 71 public void onPostMessageServiceDisconnected() { 72 mServiceConnected = false; 73 } 74 }; 75 Intent intent = new Intent(); 76 intent.setClassName(mContext.getPackageName(), PostMessageService.class.getName()); 77 try { 78 mServiceRule.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 79 } catch (TimeoutException e) { 80 fail(); 81 } 82 } 83 84 @Test 85 public void testNotifyChannelCreationAndSendMessages() { 86 PollingCheck.waitFor(500, new PollingCheck.PollingCheckCondition() { 87 @Override 88 public boolean canProceed() { 89 return mServiceConnected; 90 } 91 }); 92 assertTrue(mServiceConnected); 93 mConnection.notifyMessageChannelReady(null); 94 assertTrue(mCallback.isMessageChannelReady()); 95 mConnection.postMessage("message1", null); 96 assertEquals(mCallback.getMessages().size(), 1); 97 mConnection.postMessage("message2", null); 98 assertEquals(mCallback.getMessages().size(), 2); 99 } 100} 101