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 android.net.Uri;
20import android.os.Bundle;
21import android.os.RemoteException;
22import android.support.customtabs.ICustomTabsCallback;
23
24import java.util.ArrayList;
25
26/**
27 * A test class to check the incoming messages through {@link CustomTabsCallback}.
28 */
29public class TestCustomTabsCallback extends CustomTabsCallback {
30    private boolean mOnMessageChannelReady;
31    private ArrayList<String> mMessageList = new ArrayList<>();
32    private ICustomTabsCallback.Stub mWrapper = new ICustomTabsCallback.Stub() {
33        @Override
34        public void onNavigationEvent(final int navigationEvent, final Bundle extras) {
35            TestCustomTabsCallback.this.onNavigationEvent(navigationEvent, extras);
36        }
37
38        @Override
39        public void extraCallback(final String callbackName, final Bundle args)
40                throws RemoteException {
41            TestCustomTabsCallback.this.extraCallback(callbackName, args);
42        }
43
44        @Override
45        public void onMessageChannelReady(final Bundle extras)
46                throws RemoteException {
47            TestCustomTabsCallback.this.onMessageChannelReady(extras);
48        }
49
50        @Override
51        public void onPostMessage(final String message, final Bundle extras)
52                throws RemoteException {
53            TestCustomTabsCallback.this.onPostMessage(message, extras);
54        }
55
56        @Override
57        public void onRelationshipValidationResult(int relation, Uri origin, boolean result,
58                                                   Bundle extras) throws RemoteException {
59            TestCustomTabsCallback.this.onRelationshipValidationResult(
60                    relation, origin, result, extras);
61        }
62    };
63
64    /* package */ ICustomTabsCallback getStub() {
65        return mWrapper;
66    }
67
68    @Override
69    public void onMessageChannelReady(Bundle extras) {
70        mOnMessageChannelReady = true;
71    }
72
73    /**
74     * @return Whether the message channel is ready.
75     */
76    public boolean isMessageChannelReady() {
77        return mOnMessageChannelReady;
78    }
79
80    @Override
81    public void onPostMessage(String message, Bundle extras) {
82        mMessageList.add(message);
83    }
84
85    /**
86     * @return A list of messages that have been sent so far.
87     */
88    public ArrayList<String> getMessages() {
89        return mMessageList;
90    }
91}
92