packages/apps/Taskbar
修订版 | 1589bf2ac164bcd0b09b9414d7731764ed17ee70 (tree) |
---|---|
时间 | 2020-04-14 13:09:45 |
作者 | Braden Farmer <farmerbb@gmai...> |
Commiter | GitHub |
Merge pull request #104 from utzcoz/master
Add unit tests for widget package
@@ -5,8 +5,11 @@ import android.accessibilityservice.AccessibilityService; | ||
5 | 5 | import android.app.ActivityOptions; |
6 | 6 | import android.app.AlertDialog; |
7 | 7 | import android.app.Application; |
8 | +import android.content.BroadcastReceiver; | |
8 | 9 | import android.content.ComponentName; |
9 | 10 | import android.content.Context; |
11 | +import android.content.Intent; | |
12 | +import android.content.IntentFilter; | |
10 | 13 | import android.content.SharedPreferences; |
11 | 14 | import android.content.pm.PackageManager; |
12 | 15 | import android.content.res.Resources; |
@@ -17,6 +20,7 @@ import android.view.WindowManager; | ||
17 | 20 | import android.widget.Button; |
18 | 21 | import android.widget.Toast; |
19 | 22 | |
23 | +import androidx.localbroadcastmanager.content.LocalBroadcastManager; | |
20 | 24 | import androidx.test.core.app.ApplicationProvider; |
21 | 25 | |
22 | 26 | import com.farmerbb.taskbar.R; |
@@ -964,4 +968,29 @@ public class UTest { | ||
964 | 968 | 0 |
965 | 969 | ); |
966 | 970 | } |
971 | + | |
972 | + @Test | |
973 | + public void testSendBroadcast() { | |
974 | + TestBroadcastReceiver receiver = new TestBroadcastReceiver(); | |
975 | + IntentFilter filter = new IntentFilter(TestBroadcastReceiver.ACTION); | |
976 | + LocalBroadcastManager.getInstance(context).registerReceiver(receiver, filter); | |
977 | + U.sendBroadcast(context, TestBroadcastReceiver.ACTION); | |
978 | + assertTrue(receiver.onReceived); | |
979 | + receiver.onReceived = false; | |
980 | + U.sendBroadcast(context, new Intent(TestBroadcastReceiver.ACTION)); | |
981 | + assertTrue(receiver.onReceived); | |
982 | + } | |
983 | + | |
984 | + private static final class TestBroadcastReceiver extends BroadcastReceiver { | |
985 | + private static final String ACTION = "test-broadcast-receiver-action"; | |
986 | + private boolean onReceived; | |
987 | + | |
988 | + @Override | |
989 | + public void onReceive(Context context, Intent intent) { | |
990 | + if (intent == null || !ACTION.equals(intent.getAction())) { | |
991 | + return; | |
992 | + } | |
993 | + onReceived = true; | |
994 | + } | |
995 | + } | |
967 | 996 | } |
\ No newline at end of file |
@@ -0,0 +1,73 @@ | ||
1 | +package com.farmerbb.taskbar.widget; | |
2 | + | |
3 | +import android.content.Context; | |
4 | +import android.os.SystemClock; | |
5 | +import android.view.MotionEvent; | |
6 | +import android.view.ViewConfiguration; | |
7 | + | |
8 | +import androidx.test.core.app.ApplicationProvider; | |
9 | + | |
10 | +import com.farmerbb.taskbar.widget.DashboardCell.OnInterceptedLongPressListener; | |
11 | + | |
12 | +import org.junit.Before; | |
13 | +import org.junit.Test; | |
14 | +import org.junit.runner.RunWith; | |
15 | +import org.robolectric.RobolectricTestRunner; | |
16 | +import org.robolectric.Shadows; | |
17 | + | |
18 | +import static org.junit.Assert.assertEquals; | |
19 | +import static org.junit.Assert.assertNotNull; | |
20 | +import static org.junit.Assert.assertNull; | |
21 | +import static org.junit.Assert.assertSame; | |
22 | + | |
23 | +@RunWith(RobolectricTestRunner.class) | |
24 | +public class DashboardCellTest { | |
25 | + private DashboardCell cell; | |
26 | + private Context context; | |
27 | + private TestOnInterceptedLongPressListener listener; | |
28 | + private int longPressTimeout; | |
29 | + | |
30 | + @Before | |
31 | + public void setUp() { | |
32 | + context = ApplicationProvider.getApplicationContext(); | |
33 | + cell = new DashboardCell(context); | |
34 | + listener = new TestOnInterceptedLongPressListener(); | |
35 | + cell.setOnInterceptedLongPressListener(listener); | |
36 | + longPressTimeout = ViewConfiguration.getLongPressTimeout(); | |
37 | + } | |
38 | + | |
39 | + @Test | |
40 | + public void testOnInterceptTouchEvent() { | |
41 | + long downTime = SystemClock.uptimeMillis(); | |
42 | + dispatchTouchEvent(downTime, MotionEvent.ACTION_DOWN); | |
43 | + dispatchTouchEvent(downTime + longPressTimeout - 1, MotionEvent.ACTION_UP); | |
44 | + assertNull(listener.longPressedCell); | |
45 | + downTime = SystemClock.uptimeMillis(); | |
46 | + dispatchTouchEvent(downTime, MotionEvent.ACTION_DOWN); | |
47 | + dispatchTouchEvent(downTime + longPressTimeout, MotionEvent.ACTION_UP); | |
48 | + assertNull(listener.longPressedCell); | |
49 | + downTime = SystemClock.uptimeMillis(); | |
50 | + dispatchTouchEvent(downTime, MotionEvent.ACTION_DOWN); | |
51 | + dispatchTouchEvent(downTime + longPressTimeout + 1, MotionEvent.ACTION_UP); | |
52 | + assertNotNull(listener.longPressedCell); | |
53 | + assertSame(cell, listener.longPressedCell); | |
54 | + } | |
55 | + | |
56 | + private void dispatchTouchEvent(long downTime, int event) { | |
57 | + float x = 0.0f; | |
58 | + float y = 0.0f; | |
59 | + int metaState = 0; | |
60 | + MotionEvent motionEvent = MotionEvent.obtain(downTime, downTime, event, x, y, metaState); | |
61 | + cell.onInterceptTouchEvent(motionEvent); | |
62 | + } | |
63 | + | |
64 | + private static class TestOnInterceptedLongPressListener | |
65 | + implements OnInterceptedLongPressListener { | |
66 | + private DashboardCell longPressedCell; | |
67 | + | |
68 | + @Override | |
69 | + public void onInterceptedLongPress(DashboardCell cell) { | |
70 | + longPressedCell = cell; | |
71 | + } | |
72 | + } | |
73 | +} | |
\ No newline at end of file |
@@ -0,0 +1,56 @@ | ||
1 | +package com.farmerbb.taskbar.widget; | |
2 | + | |
3 | +import android.content.BroadcastReceiver; | |
4 | +import android.content.Context; | |
5 | +import android.content.Intent; | |
6 | +import android.content.IntentFilter; | |
7 | +import android.view.KeyEvent; | |
8 | + | |
9 | +import androidx.localbroadcastmanager.content.LocalBroadcastManager; | |
10 | +import androidx.test.core.app.ApplicationProvider; | |
11 | + | |
12 | +import org.junit.Before; | |
13 | +import org.junit.Test; | |
14 | +import org.junit.runner.RunWith; | |
15 | +import org.robolectric.RobolectricTestRunner; | |
16 | + | |
17 | +import static com.farmerbb.taskbar.util.TaskbarIntent.ACTION_HIDE_START_MENU; | |
18 | +import static org.junit.Assert.assertFalse; | |
19 | +import static org.junit.Assert.assertTrue; | |
20 | + | |
21 | +@RunWith(RobolectricTestRunner.class) | |
22 | +public class StartMenuLayoutTest { | |
23 | + private Context context; | |
24 | + private StartMenuLayout layout; | |
25 | + | |
26 | + @Before | |
27 | + public void setUp() { | |
28 | + context = ApplicationProvider.getApplicationContext(); | |
29 | + layout = new StartMenuLayout(context); | |
30 | + } | |
31 | + | |
32 | + @Test | |
33 | + public void testDispatchKeyEvent() { | |
34 | + IntentFilter filter = new IntentFilter(ACTION_HIDE_START_MENU); | |
35 | + TestBroadcastReceiver receiver = new TestBroadcastReceiver(); | |
36 | + LocalBroadcastManager.getInstance(context).registerReceiver(receiver, filter); | |
37 | + KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK); | |
38 | + layout.dispatchKeyEvent(keyEvent); | |
39 | + assertFalse(receiver.onReceived); | |
40 | + layout.viewHandlesBackButton(); | |
41 | + layout.dispatchKeyEvent(keyEvent); | |
42 | + assertTrue(receiver.onReceived); | |
43 | + } | |
44 | + | |
45 | + private static class TestBroadcastReceiver extends BroadcastReceiver { | |
46 | + private boolean onReceived; | |
47 | + | |
48 | + @Override | |
49 | + public void onReceive(Context context, Intent intent) { | |
50 | + if (intent == null || !ACTION_HIDE_START_MENU.equals(intent.getAction())) { | |
51 | + return; | |
52 | + } | |
53 | + onReceived = true; | |
54 | + } | |
55 | + } | |
56 | +} |