OSDN Git Service

Extract method to initialize start button in TaskbarController
authorutzcoz <utzcoz@outlook.com>
Wed, 13 May 2020 08:40:29 +0000 (16:40 +0800)
committerutzcoz <utzcoz@outlook.com>
Wed, 13 May 2020 08:40:29 +0000 (16:40 +0800)
Test: ./gradlew test

Signed-off-by: utzcoz <utzcoz@outlook.com>
app/src/main/java/com/farmerbb/taskbar/ui/TaskbarController.java
app/src/test/java/com/farmerbb/taskbar/ui/TaskbarControllerTest.java [new file with mode: 0644]
app/src/test/java/com/farmerbb/taskbar/ui/TaskbarUIHostService.java [new file with mode: 0644]

index 9e0e6a9..c8fedb8 100644 (file)
@@ -304,66 +304,7 @@ public class TaskbarController extends UIController {
         space.setOnClickListener(v -> toggleTaskbar(true));
 
         startButton = layout.findViewById(R.id.start_button);
-        int padding = 0;
-
-        switch(pref.getString(PREF_START_BUTTON_IMAGE, U.getDefaultStartButtonImage(context))) {
-            case "default":
-                startButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.tb_all_apps_button_icon));
-                padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding);
-                break;
-            case "app_logo":
-                Drawable drawable;
-
-                if(U.isBlissOs(context)) {
-                    drawable = ContextCompat.getDrawable(context, R.drawable.tb_bliss);
-                    drawable.setTint(accentColor);
-                } else {
-                    LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
-                    LauncherActivityInfo info = launcherApps.getActivityList(context.getPackageName(), Process.myUserHandle()).get(0);
-                    drawable = IconCache.getInstance(context).getIcon(context, context.getPackageManager(), info);
-                }
-
-                startButton.setImageDrawable(drawable);
-                padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding_alt);
-                break;
-            case "custom":
-                File file = new File(context.getFilesDir() + "/tb_images", "custom_image");
-                if(file.exists()) {
-                    Handler handler = new Handler();
-                    new Thread(() -> {
-                        Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
-                        handler.post(() -> {
-                            if(bitmap != null) {
-                                BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
-                                bitmapDrawable.setFilterBitmap(bitmap.getWidth() * bitmap.getHeight() > 2000);
-                                startButton.setImageDrawable(bitmapDrawable);
-                            } else {
-                                U.showToastLong(context, R.string.tb_error_reading_custom_start_image);
-                                startButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.tb_all_apps_button_icon));
-                            }
-                        });
-                    }).start();
-                } else
-                    startButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.tb_all_apps_button_icon));
-
-                padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding);
-                break;
-        }
-
-        startButton.setPadding(padding, padding, padding, padding);
-        startButton.setOnClickListener(ocl);
-        startButton.setOnLongClickListener(view -> {
-            openContextMenu();
-            return true;
-        });
-
-        startButton.setOnGenericMotionListener((view, motionEvent) -> {
-            if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
-                    && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY)
-                openContextMenu();
-
-            return false;
-        });
+        drawStartButton(context, startButton, pref, accentColor);
 
         refreshInterval = (int) (Float.parseFloat(pref.getString(PREF_REFRESH_FREQUENCY, "1")) * 1000);
         if(refreshInterval == 0)
@@ -634,6 +575,69 @@ public class TaskbarController extends UIController {
         isFirstStart = false;
     }
 
+    private void drawStartButton(Context context, ImageView startButton, SharedPreferences pref, int accentColor) {
+        int padding = 0;
+
+        switch(pref.getString(PREF_START_BUTTON_IMAGE, U.getDefaultStartButtonImage(context))) {
+            case "default":
+                startButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.tb_all_apps_button_icon));
+                padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding);
+                break;
+            case "app_logo":
+                Drawable drawable;
+
+                if(U.isBlissOs(context)) {
+                    drawable = ContextCompat.getDrawable(context, R.drawable.tb_bliss);
+                    drawable.setTint(accentColor);
+                } else {
+                    LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
+                    LauncherActivityInfo info = launcherApps.getActivityList(context.getPackageName(), Process.myUserHandle()).get(0);
+                    drawable = IconCache.getInstance(context).getIcon(context, context.getPackageManager(), info);
+                }
+
+                startButton.setImageDrawable(drawable);
+                padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding_alt);
+                break;
+            case "custom":
+                File file = new File(context.getFilesDir() + "/tb_images", "custom_image");
+                if(file.exists()) {
+                    Handler handler = new Handler();
+                    new Thread(() -> {
+                        Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
+                        handler.post(() -> {
+                            if(bitmap != null) {
+                                BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
+                                bitmapDrawable.setFilterBitmap(bitmap.getWidth() * bitmap.getHeight() > 2000);
+                                startButton.setImageDrawable(bitmapDrawable);
+                            } else {
+                                U.showToastLong(context, R.string.tb_error_reading_custom_start_image);
+                                startButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.tb_all_apps_button_icon));
+                            }
+                        });
+                    }).start();
+                } else
+                    startButton.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.tb_all_apps_button_icon));
+
+                padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding);
+                break;
+        }
+
+        startButton.setPadding(padding, padding, padding, padding);
+        startButton.setOnClickListener(ocl);
+        startButton.setOnLongClickListener(view -> {
+            openContextMenu();
+            return true;
+        });
+
+        startButton.setOnGenericMotionListener((view, motionEvent) -> {
+            if(motionEvent.getAction() == MotionEvent.ACTION_BUTTON_PRESS
+                    && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY)
+                openContextMenu();
+
+            return false;
+        });
+    }
+
     private void startRefreshingRecents() {
         if(thread != null) thread.interrupt();
         stopThread2 = true;
diff --git a/app/src/test/java/com/farmerbb/taskbar/ui/TaskbarControllerTest.java b/app/src/test/java/com/farmerbb/taskbar/ui/TaskbarControllerTest.java
new file mode 100644 (file)
index 0000000..7f19531
--- /dev/null
@@ -0,0 +1,114 @@
+package com.farmerbb.taskbar.ui;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.graphics.Color;
+import android.widget.ImageView;
+
+import androidx.test.core.app.ApplicationProvider;
+
+import com.farmerbb.taskbar.R;
+import com.farmerbb.taskbar.util.U;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.rule.PowerMockRule;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.android.controller.ServiceController;
+
+import static com.farmerbb.taskbar.util.Constants.PREF_START_BUTTON_IMAGE;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.powermock.api.mockito.PowerMockito.when;
+import static org.robolectric.util.ReflectionHelpers.ClassParameter.from;
+import static org.robolectric.util.ReflectionHelpers.callInstanceMethod;
+
+@RunWith(RobolectricTestRunner.class)
+@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "androidx.*"})
+@PrepareForTest(U.class)
+public class TaskbarControllerTest {
+    @Rule
+    public PowerMockRule rule = new PowerMockRule();
+
+    private ServiceController<TaskbarUIHostService> controller;
+    private TaskbarUIHostService hostService;
+    private TaskbarController uiController;
+    private Context context;
+    SharedPreferences prefs;
+
+    @Before
+    public void setUp() {
+        controller = Robolectric.buildService(TaskbarUIHostService.class);
+        hostService = controller.create().get();
+        uiController = hostService.controller;
+        context = ApplicationProvider.getApplicationContext();
+        prefs = U.getSharedPreferences(context);
+    }
+
+    @After
+    public void tearDown() {
+        prefs.edit().remove(PREF_START_BUTTON_IMAGE).apply();
+    }
+
+    @Test
+    public void testInitialization() {
+        assertNotNull(uiController);
+    }
+
+    @Test
+    public void testDrawStartButtonPadding() {
+        ImageView startButton = new ImageView(context);
+        prefs = U.getSharedPreferences(context);
+        prefs.edit().putString(PREF_START_BUTTON_IMAGE, "default").apply();
+        callDrawStartButton(context, startButton, prefs);
+        int padding =
+                context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding);
+        checkStartButtonPadding(padding, startButton);
+
+        PowerMockito.spy(U.class);
+        // Use bliss os logic to avoid using LauncherApps, that robolectric doesn't support
+        when(U.isBlissOs(context)).thenReturn(true);
+        prefs.edit().putString(PREF_START_BUTTON_IMAGE, "app_logo").apply();
+        callDrawStartButton(context, startButton, prefs);
+        padding =
+                context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding_alt);
+        checkStartButtonPadding(padding, startButton);
+
+        prefs.edit().putString(PREF_START_BUTTON_IMAGE, "custom").apply();
+        callDrawStartButton(context, startButton, prefs);
+        padding = context.getResources().getDimensionPixelSize(R.dimen.tb_app_drawer_icon_padding);
+        checkStartButtonPadding(padding, startButton);
+
+        prefs.edit().putString(PREF_START_BUTTON_IMAGE, "non-support").apply();
+        callDrawStartButton(context, startButton, prefs);
+        checkStartButtonPadding(0, startButton);
+    }
+
+    private void callDrawStartButton(Context context,
+                                     ImageView startButton,
+                                     SharedPreferences prefs) {
+        callInstanceMethod(
+                uiController,
+                "drawStartButton",
+                from(Context.class, context),
+                from(ImageView.class, startButton),
+                from(SharedPreferences.class, prefs),
+                from(int.class, Color.RED)
+        );
+    }
+
+
+    private void checkStartButtonPadding(int padding, ImageView startButton) {
+        assertEquals(padding, startButton.getPaddingLeft());
+        assertEquals(padding, startButton.getPaddingTop());
+        assertEquals(padding, startButton.getPaddingRight());
+        assertEquals(padding, startButton.getPaddingBottom());
+    }
+}
\ No newline at end of file
diff --git a/app/src/test/java/com/farmerbb/taskbar/ui/TaskbarUIHostService.java b/app/src/test/java/com/farmerbb/taskbar/ui/TaskbarUIHostService.java
new file mode 100644 (file)
index 0000000..1986782
--- /dev/null
@@ -0,0 +1,15 @@
+package com.farmerbb.taskbar.ui;
+
+import androidx.test.core.app.ApplicationProvider;
+
+public class TaskbarUIHostService extends UIHostService {
+    TaskbarController controller;
+
+    @Override
+    public UIController newController() {
+        if (controller == null) {
+            controller = new TaskbarController(ApplicationProvider.getApplicationContext());
+        }
+        return controller;
+    }
+}