NotificationTestHelper.java revision 5cf1d0589a0045ee5f01802975f394847e891215
16fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek/*
26fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * Copyright (C) 2017 The Android Open Source Project
36fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek *
46fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * Licensed under the Apache License, Version 2.0 (the "License");
56fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * you may not use this file except in compliance with the License.
66fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * You may obtain a copy of the License at
76fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek *
86fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek *      http://www.apache.org/licenses/LICENSE-2.0
96fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek *
106fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * Unless required by applicable law or agreed to in writing, software
116fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * distributed under the License is distributed on an "AS IS" BASIS,
126fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * See the License for the specific language governing permissions and
146fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * limitations under the License
156fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek */
166fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
176fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekpackage com.android.systemui.statusbar;
186fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
196fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.app.ActivityManager;
205ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinekimport android.app.Instrumentation;
216fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.app.Notification;
226fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.content.Context;
236fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.os.UserHandle;
246fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.service.notification.StatusBarNotification;
255ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinekimport android.support.test.InstrumentationRegistry;
266fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.view.LayoutInflater;
275cf1d0589a0045ee5f01802975f394847e891215Selim Cinekimport android.widget.FrameLayout;
286fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.widget.RemoteViews;
296fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
306fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport com.android.systemui.R;
315cf1d0589a0045ee5f01802975f394847e891215Selim Cinekimport com.android.systemui.statusbar.notification.AboveShelfChangedListener;
325cf1d0589a0045ee5f01802975f394847e891215Selim Cinekimport com.android.systemui.statusbar.notification.AboveShelfObserver;
336fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport com.android.systemui.statusbar.notification.InflationException;
345ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinekimport com.android.systemui.statusbar.notification.NotificationInflaterTest;
356fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport com.android.systemui.statusbar.phone.NotificationGroupManager;
365cf1d0589a0045ee5f01802975f394847e891215Selim Cinekimport com.android.systemui.statusbar.policy.HeadsUpManager;
376fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
386fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek/**
396fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * A helper class to create {@link ExpandableNotificationRow}
406fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek */
416fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekpublic class NotificationTestHelper {
426fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
436fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    private final Context mContext;
445ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek    private final Instrumentation mInstrumentation;
456fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    private int mId;
466fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    private final NotificationGroupManager mGroupManager = new NotificationGroupManager();
475ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek    private ExpandableNotificationRow mRow;
485ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek    private InflationException mException;
495cf1d0589a0045ee5f01802975f394847e891215Selim Cinek    private HeadsUpManager mHeadsUpManager;
506fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
516fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    public NotificationTestHelper(Context context) {
526fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        mContext = context;
535ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek        mInstrumentation = InstrumentationRegistry.getInstrumentation();
545cf1d0589a0045ee5f01802975f394847e891215Selim Cinek        mHeadsUpManager = new HeadsUpManager(mContext, null, mGroupManager);
556fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    }
566fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
575ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek    public ExpandableNotificationRow createRow() throws Exception {
586fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        Notification publicVersion = new Notification.Builder(mContext).setSmallIcon(
596fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                R.drawable.ic_person)
606fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .setCustomContentView(new RemoteViews(mContext.getPackageName(),
616fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                        R.layout.custom_view_dark))
626fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .build();
636fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        Notification notification = new Notification.Builder(mContext).setSmallIcon(
646fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                R.drawable.ic_person)
656fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .setContentTitle("Title")
666fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .setContentText("Text")
676fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .setPublicVersion(publicVersion)
686fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .build();
6910790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek        return createRow(notification);
7010790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek    }
7110790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek
725ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek    public ExpandableNotificationRow createRow(Notification notification) throws Exception {
7310790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
7410790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek                mContext.LAYOUT_INFLATER_SERVICE);
755ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek        mInstrumentation.runOnMainSync(() -> {
765ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek            mRow = (ExpandableNotificationRow) inflater.inflate(
775ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek                    R.layout.status_bar_notification_row,
785ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek                    null, false);
795ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek        });
805ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek        ExpandableNotificationRow row = mRow;
8110790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek        row.setGroupManager(mGroupManager);
825cf1d0589a0045ee5f01802975f394847e891215Selim Cinek        row.setHeadsUpManager(mHeadsUpManager);
835cf1d0589a0045ee5f01802975f394847e891215Selim Cinek        row.setAboveShelfChangedListener(aboveShelf -> {});
846fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
856fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        StatusBarNotification sbn = new StatusBarNotification("com.android.systemui",
866fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                "com.android.systemui", mId++, null, 1000,
876fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                2000, notification, mUser, null, System.currentTimeMillis());
886fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        NotificationData.Entry entry = new NotificationData.Entry(sbn);
896fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        entry.row = row;
905ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek        entry.createIcons(mContext, sbn);
915ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek        NotificationInflaterTest.runThenWaitForInflation(() -> row.updateNotification(entry),
925ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek                row.getNotificationInflater());
936fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        return row;
946fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    }
956fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
965ba2254df1cb71355efb0a3831581e2f2babcdbeSelim Cinek    public ExpandableNotificationRow createGroup() throws Exception {
976fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        ExpandableNotificationRow row = createRow();
986fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        row.addChildNotification(createRow());
996fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        row.addChildNotification(createRow());
1006fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        return row;
1016fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    }
1026fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek}
103