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