implements OnHeaderC
}
/**
- * Sets the max horizontal spans that are allowed for grouping more than one widgets in a table
- * row.
- *
- * If there is only one widget in a row, that widget horizontal span is allowed to exceed
- * {@code maxHorizontalSpans}.
- *
Let's say the max horizontal spans is set to 5. Widgets can be grouped in the same row if
- * their total horizontal spans added don't exceed 5.
- * Example 1: Row 1: 2x2, 2x3, 1x1. Total horizontal spans is 5. This is okay.
- * Example 2: Row 1: 2x2, 4x3, 1x1. the total horizontal spans is 7. This is wrong.
- * 4x3 and 1x1 should be moved to a new row.
- * Example 3: Row 1: 6x4. This is okay because this is the only item in the row.
+ * Sets the max horizontal span in cells that is allowed for grouping more than one widget in a
+ * table row.
*/
public void setMaxHorizontalSpansPerRow(int maxHorizontalSpans) {
- mWidgetsListTableViewHolderBinder.setMaxSpansPerRow(maxHorizontalSpans);
+ mMaxSpanSize = maxHorizontalSpans;
+ updateVisibleEntries();
}
/**
diff --git a/src/com/android/launcher3/widget/picker/WidgetsListTableViewHolderBinder.java b/src/com/android/launcher3/widget/picker/WidgetsListTableViewHolderBinder.java
index 7b526639f5..57dec1401e 100644
--- a/src/com/android/launcher3/widget/picker/WidgetsListTableViewHolderBinder.java
+++ b/src/com/android/launcher3/widget/picker/WidgetsListTableViewHolderBinder.java
@@ -49,7 +49,6 @@ public final class WidgetsListTableViewHolderBinder
private static final boolean DEBUG = false;
private static final String TAG = "WidgetsListRowViewHolderBinder";
- private int mMaxSpansPerRow = 4;
private final LayoutInflater mLayoutInflater;
private final OnClickListener mIconClickListener;
private final OnLongClickListener mIconLongClickListener;
@@ -82,10 +81,6 @@ public final class WidgetsListTableViewHolderBinder
mApplyBitmapDeferred = applyBitmapDeferred;
}
- public void setMaxSpansPerRow(int maxSpansPerRow) {
- mMaxSpansPerRow = maxSpansPerRow;
- }
-
@Override
public WidgetsRowViewHolder newViewHolder(ViewGroup parent) {
if (DEBUG) {
@@ -113,7 +108,8 @@ public final class WidgetsListTableViewHolderBinder
position == mWidgetsListAdapter.getItemCount() - 1 ? LAST : MIDDLE);
List> widgetItemsTable =
- WidgetsTableUtils.groupWidgetItemsIntoTable(entry.mWidgets, mMaxSpansPerRow);
+ WidgetsTableUtils.groupWidgetItemsIntoTable(
+ entry.mWidgets, entry.getMaxSpanSizeInCells());
recycleTableBeforeBinding(table, widgetItemsTable);
// Bind the widget items.
for (int i = 0; i < widgetItemsTable.size(); i++) {
diff --git a/src/com/android/launcher3/widget/util/WidgetsTableUtils.java b/src/com/android/launcher3/widget/util/WidgetsTableUtils.java
index 7294a3ab31..54aaf93ac0 100644
--- a/src/com/android/launcher3/widget/util/WidgetsTableUtils.java
+++ b/src/com/android/launcher3/widget/util/WidgetsTableUtils.java
@@ -56,6 +56,13 @@ public final class WidgetsTableUtils {
* 3. The order shortcuts are grouped together in the same row until their total horizontal
* spans exceed the {@code maxSpansPerRow} - 1.
* 4. If there is only one widget in a row, its width may exceed the {@code maxSpansPerRow}.
+ *
+ * Let's say the {@code maxSpansPerRow} is set to 6. Widgets can be grouped in the same row
+ * if their total horizontal spans added don't exceed 5.
+ * Example 1: Row 1: 2x2, 2x3, 1x1. Total horizontal spans is 5. This is okay.
+ * Example 2: Row 1: 2x2, 4x3, 1x1. the total horizontal spans is 7. This is wrong. 4x3 and 1x1
+ * should be moved to a new row.
+ * Example 3: Row 1: 6x4. This is okay because this is the only item in the row.
*/
public static List> groupWidgetItemsIntoTable(
List widgetItems, final int maxSpansPerRow) {
diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
index 2712bc02ce..bf4eba0f64 100644
--- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
+++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
@@ -65,6 +65,7 @@ import com.android.launcher3.util.PackageManagerHelper;
import com.android.launcher3.util.Wait;
import com.android.launcher3.util.rule.FailureWatcher;
import com.android.launcher3.util.rule.LauncherActivityRule;
+import com.android.launcher3.util.rule.ScreenRecordRule;
import com.android.launcher3.util.rule.ShellCommandRule;
import com.android.launcher3.util.rule.TestStabilityRule;
@@ -204,6 +205,9 @@ public abstract class AbstractLauncherUiTest {
public ShellCommandRule mDisableHeadsUpNotification =
ShellCommandRule.disableHeadsUpNotification();
+ @Rule
+ public ScreenRecordRule mScreenRecordRule = new ScreenRecordRule();
+
protected void clearPackageData(String pkg) throws IOException, InterruptedException {
final CountDownLatch count = new CountDownLatch(2);
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
diff --git a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
index 06bc26a2a2..4dd44f4193 100644
--- a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
+++ b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
@@ -36,6 +36,7 @@ import com.android.launcher3.tapl.AppIconMenu;
import com.android.launcher3.tapl.AppIconMenuItem;
import com.android.launcher3.tapl.Widgets;
import com.android.launcher3.tapl.Workspace;
+import com.android.launcher3.util.rule.ScreenRecordRule.ScreenRecord;
import com.android.launcher3.views.OptionsPopupView;
import com.android.launcher3.widget.picker.WidgetsFullSheet;
import com.android.launcher3.widget.picker.WidgetsRecyclerView;
@@ -92,6 +93,7 @@ public class TaplTestsLauncher3 extends AbstractLauncherUiTest {
}
@Test
+ @ScreenRecord //b/187080582
public void testDevicePressMenu() throws Exception {
mDevice.pressMenu();
mDevice.waitForIdle();
diff --git a/tests/src/com/android/launcher3/ui/widget/RequestPinItemTest.java b/tests/src/com/android/launcher3/ui/widget/RequestPinItemTest.java
index 822fefc208..745dc220ba 100644
--- a/tests/src/com/android/launcher3/ui/widget/RequestPinItemTest.java
+++ b/tests/src/com/android/launcher3/ui/widget/RequestPinItemTest.java
@@ -42,6 +42,7 @@ import com.android.launcher3.testcomponent.RequestPinItemActivity;
import com.android.launcher3.ui.AbstractLauncherUiTest;
import com.android.launcher3.util.Wait;
import com.android.launcher3.util.Wait.Condition;
+import com.android.launcher3.util.rule.ScreenRecordRule.ScreenRecord;
import com.android.launcher3.util.rule.ShellCommandRule;
import org.junit.Before;
@@ -77,6 +78,7 @@ public class RequestPinItemTest extends AbstractLauncherUiTest {
public void testEmpty() throws Throwable { /* needed while the broken tests are being fixed */ }
@Test
+ @ScreenRecord //b/192010616
public void testPinWidgetNoConfig() throws Throwable {
runTest("pinWidgetNoConfig", true, (info, view) -> info instanceof LauncherAppWidgetInfo &&
((LauncherAppWidgetInfo) info).appWidgetId == mAppWidgetId &&
@@ -85,6 +87,7 @@ public class RequestPinItemTest extends AbstractLauncherUiTest {
}
@Test
+ @ScreenRecord //b/192005114
public void testPinWidgetNoConfig_customPreview() throws Throwable {
// Command to set custom preview
Intent command = RequestPinItemActivity.getCommandIntent(
diff --git a/tests/src/com/android/launcher3/util/rule/FailureWatcher.java b/tests/src/com/android/launcher3/util/rule/FailureWatcher.java
index 4c4794789e..dc59bddabd 100644
--- a/tests/src/com/android/launcher3/util/rule/FailureWatcher.java
+++ b/tests/src/com/android/launcher3/util/rule/FailureWatcher.java
@@ -2,6 +2,8 @@ package com.android.launcher3.util.rule;
import static androidx.test.InstrumentationRegistry.getInstrumentation;
+import android.os.FileUtils;
+import android.os.ParcelFileDescriptor.AutoCloseInputStream;
import android.util.Log;
import androidx.test.uiautomator.UiDevice;
@@ -12,9 +14,12 @@ import com.android.launcher3.ui.AbstractLauncherUiTest;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
-import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
public class FailureWatcher extends TestWatcher {
private static final String TAG = "FailureWatcher";
@@ -26,20 +31,6 @@ public class FailureWatcher extends TestWatcher {
mLauncher = launcher;
}
- private static void dumpViewHierarchy(UiDevice device) {
- final ByteArrayOutputStream stream = new ByteArrayOutputStream();
- try {
- device.dumpWindowHierarchy(stream);
- stream.flush();
- stream.close();
- for (String line : stream.toString().split("\\r?\\n")) {
- Log.e(TAG, line.trim());
- }
- } catch (IOException e) {
- Log.e(TAG, "error dumping XML to logcat", e);
- }
- }
-
@Override
protected void succeeded(Description description) {
super.succeeded(description);
@@ -53,22 +44,41 @@ public class FailureWatcher extends TestWatcher {
public static void onError(UiDevice device, Description description, Throwable e) {
if (device == null) return;
- final String pathname = getInstrumentation().getTargetContext().
- getFilesDir().getPath() + "/TestScreenshot-" + description.getMethodName()
- + ".png";
- Log.e(TAG, "Failed test " + description.getMethodName() +
- ", screenshot will be saved to " + pathname +
- ", track trace is below, UI object dump is further below:\n" +
- Log.getStackTraceString(e));
- dumpViewHierarchy(device);
+ final File parentFile = getInstrumentation().getTargetContext().getFilesDir();
+ final File sceenshot = new File(parentFile,
+ "TestScreenshot-" + description.getMethodName() + ".png");
+ final File hierarchy = new File(parentFile,
+ "Hierarchy-" + description.getMethodName() + ".zip");
- try {
- final String dumpsysResult = device.executeShellCommand(
- "dumpsys activity service TouchInteractionService");
- Log.d(TAG, "TouchInteractionService: " + dumpsysResult);
- } catch (IOException ex) {
+ // Dump window hierarchy
+ try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(hierarchy))) {
+ out.putNextEntry(new ZipEntry("bugreport.txt"));
+ dumpStringCommand("dumpsys window windows", out);
+ dumpStringCommand("dumpsys package", out);
+ dumpStringCommand("dumpsys activity service TouchInteractionService", out);
+ out.closeEntry();
+
+ out.putNextEntry(new ZipEntry("visible_windows.zip"));
+ dumpCommand("cmd window dump-visible-window-views", out);
+ out.closeEntry();
+ } catch (IOException ex) { }
+
+ Log.e(TAG, "Failed test " + description.getMethodName()
+ + ",\nscreenshot will be saved to " + sceenshot
+ + ",\nUI dump at: " + hierarchy
+ + " (use go/web-hv to open the dump file)", e);
+ device.takeScreenshot(sceenshot);
+ }
+
+ private static void dumpStringCommand(String cmd, OutputStream out) throws IOException {
+ out.write(("\n\n" + cmd + "\n").getBytes());
+ dumpCommand(cmd, out);
+ }
+
+ private static void dumpCommand(String cmd, OutputStream out) throws IOException {
+ try (AutoCloseInputStream in = new AutoCloseInputStream(getInstrumentation()
+ .getUiAutomation().executeShellCommand(cmd))) {
+ FileUtils.copy(in, out);
}
-
- device.takeScreenshot(new File(pathname));
}
}
diff --git a/tests/src/com/android/launcher3/util/rule/ScreenRecordRule.java b/tests/src/com/android/launcher3/util/rule/ScreenRecordRule.java
new file mode 100644
index 0000000000..00b1cdd35e
--- /dev/null
+++ b/tests/src/com/android/launcher3/util/rule/ScreenRecordRule.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.launcher3.util.rule;
+
+import static androidx.test.InstrumentationRegistry.getInstrumentation;
+
+import android.app.Instrumentation;
+import android.app.UiAutomation;
+import android.os.ParcelFileDescriptor;
+import android.util.Log;
+
+import androidx.test.uiautomator.UiDevice;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import java.io.File;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Rule which captures a screen record for a test.
+ * After adding this rule to the test class, apply the annotation @ScreenRecord to individual tests
+ */
+public class ScreenRecordRule implements TestRule {
+
+ private static final String TAG = "ScreenRecordRule";
+
+ @Override
+ public Statement apply(Statement base, Description description) {
+ if (description.getAnnotation(ScreenRecord.class) == null) {
+ return base;
+ }
+
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ Instrumentation inst = getInstrumentation();
+ UiAutomation automation = inst.getUiAutomation();
+ UiDevice device = UiDevice.getInstance(inst);
+
+ File outputFile = new File(inst.getTargetContext().getFilesDir(),
+ "screenrecord-" + description.getMethodName() + ".mp4");
+ device.executeShellCommand("killall screenrecord");
+ ParcelFileDescriptor output =
+ automation.executeShellCommand("screenrecord " + outputFile);
+ String screenRecordPid = device.executeShellCommand("pidof screenrecord");
+ try {
+ base.evaluate();
+ } finally {
+ device.executeShellCommand("kill -INT " + screenRecordPid);
+ Log.e(TAG, "Screenrecord captured at: " + outputFile);
+ output.close();
+ }
+ }
+ };
+ }
+
+ /**
+ * Interface to indicate that the test should capture screenrecord
+ */
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(ElementType.METHOD)
+ public @interface ScreenRecord { }
+}