1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.android.launcher3.util.rule;
17 
18 import android.app.Activity;
19 
20 import com.android.launcher3.Launcher;
21 import com.android.launcher3.util.LauncherBindableItemsContainer.ItemOperator;
22 
23 import org.junit.runner.Description;
24 import org.junit.runners.model.Statement;
25 
26 import java.util.concurrent.Callable;
27 
28 /**
29  * Test rule to get the current Launcher activity.
30  */
31 public class LauncherActivityRule extends SimpleActivityRule<Launcher> {
32 
LauncherActivityRule()33     public LauncherActivityRule() {
34         super(Launcher.class);
35     }
36 
37     @Override
apply(Statement base, Description description)38     public Statement apply(Statement base, Description description) {
39 
40         return new MyStatement(base) {
41             @Override
42             public void onActivityStarted(Activity activity) {
43                 if (activity instanceof Launcher) {
44                     ((Launcher) activity).getRotationHelper().forceAllowRotationForTesting(true);
45                 }
46             }
47         };
48     }
49 
50     public Callable<Boolean> itemExists(final ItemOperator op) {
51         return () -> {
52             Launcher launcher = getActivity();
53             if (launcher == null) {
54                 return false;
55             }
56             return launcher.getWorkspace().getFirstMatch(op) != null;
57         };
58     }
59 }
60