Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,7 @@ private void renderPrimary(
Drawable bg = busy.getBackground();
if (bg instanceof LayerDrawable) {
Drawable fill = ((LayerDrawable) bg).findDrawableByLayerId(R.id.extract_busy_fill);
if (fill != null)
fill.setLevel(
MaintenanceFooterPolicy.progressLevel(
view.completed(), view.total(), view.phase == DeployStages.Phase.RUNNING));
if (fill != null) fill.setLevel(MaintenanceFooterPolicy.progressLevel(view));
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ public synchronized void update(long expected, Status next, String message) {
if (expected != id || !busy()) return;
status = next;
detail = SensitiveData.redact(message == null ? "" : message);
if (next == Status.RUNNING) lastStage = detail;
if (next == Status.RUNNING && shouldRememberStage(detail)) lastStage = detail;
}

/** 环境阶段单调保留;细粒度文案仍更新 detail,无阶段表的任务沿用原始进度。 */
private boolean shouldRememberStage(String progress) {
if (!DeployStages.environmentKind(kind)) return true;
DeployStages.Mode mode =
DeployStages.infer(
DeployStages.infer(DeployStages.modeFor(kind, true), lastStage), progress);
int previous = DeployStages.match(mode, lastStage);
return previous < 0 || DeployStages.match(mode, progress) >= previous;
}

public synchronized boolean confirm(long expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public static final class View {
public final Mode mode;
public final Phase phase;

/** 当前(或失败时最后到达的)阶段序号;没有阶段表时为 -1。 */
/** 当前(或失败时最后到达的)阶段序号;没有阶段表或阶段未知时为 -1。 */
public final int current;

public final List<Stage> stages;
Expand All @@ -290,7 +290,7 @@ public static final class View {
this.mode = mode == null ? Mode.GENERIC : mode;
this.phase = phase == null ? Phase.IDLE : phase;
this.stages = stages(this.mode);
this.current = stages.isEmpty() ? -1 : Math.max(0, Math.min(current, stages.size() - 1));
this.current = stages.isEmpty() || current < 0 ? -1 : Math.min(current, stages.size() - 1);
this.detail = detail == null ? "" : detail;
this.lastStage = lastStage == null ? "" : lastStage;
this.taskId = taskId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public static int progressLevel(int completed, int total, boolean running) {
return (int) Math.round(Math.min(1.0, done / total) * 10_000);
}

/** 只有已识别的运行阶段才能增加半步填充,未知阶段不能伪造进度。 */
public static int progressLevel(DeployStages.View view) {
return progressLevel(
view.completed(),
view.total(),
view.phase == DeployStages.Phase.RUNNING && view.current >= 0);
}

/** 主操作并排:宽度达到 600dp,或高度不足 480dp(手机横屏)。 */
public static boolean sideBySide(int screenWidthDp, int screenHeightDp) {
return screenWidthDp >= 600 || (screenHeightDp > 0 && screenHeightDp <= 480);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,112 @@
import static org.junit.Assert.*;

public class BackupTaskStateTest {
@org.junit.After
public void restoreLanguage() {
UiText.setLanguage("zh");
}

@Test
public void genericMaintenanceProgressKeepsTheLastRecognizedStageOnFailure() {
for (String language : new String[] {"zh", "en"}) {
UiText.setLanguage(language);
BackupTaskState state = new BackupTaskState();
long id = state.start(BackupTaskKinds.REBUILD);
String protecting =
UiText.choose(
"正在由 Android 保护应用数据与个人目录;不执行旧环境…",
"Android is protecting application data and personal folders without executing the old environment…");
state.update(id, BackupTaskState.Status.RUNNING, protecting);
state.update(id, BackupTaskState.Status.RUNNING, "正在核验受管文件… 3 MB");
state.update(id, BackupTaskState.Status.FAILED, "PERMISSION_DENIED");
var snapshot = state.snapshot();
assertEquals(protecting, snapshot.lastStage);
assertEquals("PERMISSION_DENIED", snapshot.detail);
var view =
DeployStages.view(
DeployStages.Mode.REBUILD,
DeployStages.Phase.FAILED,
-1,
snapshot.detail,
snapshot.lastStage,
id);
assertEquals(1, view.current);
assertEquals(DeployStages.Step.DONE, view.steps[0]);
assertEquals(DeployStages.Step.FAILED, view.steps[1]);
assertEquals(DeployStages.Step.TODO, view.steps[2]);
}
}

@Test
public void restoredInterruptedTaskRetainsItsRecognizedSnapshotStage() {
UiText.setLanguage("zh");
BackupTaskState state = new BackupTaskState();
long id = state.start(BackupTaskKinds.REBUILD);
String protecting = "正在由 Android 保护应用数据与个人目录;不执行旧环境…";
state.update(id, BackupTaskState.Status.RUNNING, protecting);
state.update(id, BackupTaskState.Status.RUNNING, "正在核验受管文件…");
var saved = state.snapshot();
BackupTaskState restored = new BackupTaskState();
restored.restore(saved.id, saved.kind, saved.status, saved.detail);
restored.restoreStage(saved.lastStage);
var snapshot = restored.snapshot();
assertEquals(BackupTaskState.Status.INTERRUPTED, snapshot.status);
var view =
DeployStages.view(
DeployStages.Mode.REBUILD,
DeployStages.Phase.FAILED,
-1,
snapshot.detail,
snapshot.lastStage,
snapshot.id);
assertEquals(1, view.current);
assertFalse(restored.busy());
}

@Test
public void lateEarlierMaintenanceProgressCannotReplaceALaterStage() {
UiText.setLanguage("zh");
BackupTaskState state = new BackupTaskState();
long id = state.start(BackupTaskKinds.REBUILD);
String restoring = "正在恢复本机数据并逐文件校验…";
state.update(id, BackupTaskState.Status.RUNNING, restoring);
state.update(id, BackupTaskState.Status.RUNNING, "正在停止 Web,等待运行队列完成…");
assertEquals(restoring, state.snapshot().lastStage);
}

@Test
public void updateTaskRetainsColdInstallStageAcrossUnrecognizedProgress() {
UiText.setLanguage("zh");
BackupTaskState state = new BackupTaskState();
long id = state.start(BackupTaskKinds.UPDATE);
state.update(id, BackupTaskState.Status.RUNNING, "解压 Ubuntu 与 Node");
state.update(id, BackupTaskState.Status.RUNNING, "未知的细粒度进度");
var snapshot = state.snapshot();
var view =
DeployStages.view(
DeployStages.Mode.UPDATE,
DeployStages.Phase.FAILED,
-1,
"FAILED",
snapshot.lastStage,
id);
assertEquals(DeployStages.Mode.INSTALL, view.mode);
assertEquals(1, view.current);
}

@Test
public void ordinaryBackupStillRecordsEveryProgressMessage() {
for (String kind :
new String[] {BackupTaskKinds.BACKUP, BackupTaskKinds.RECOVER, BackupTaskKinds.ROLLBACK}) {
BackupTaskState state = new BackupTaskState();
long id = state.start(kind);
state.update(id, BackupTaskState.Status.RUNNING, "first");
state.update(id, BackupTaskState.Status.RUNNING, "second");
assertEquals("second", state.snapshot().lastStage);
assertEquals("second", state.snapshot().detail);
}
}

@Test
public void previewKeepsGlobalSlotAndOldPageCannotConfirmAnotherTask() {
BackupTaskState state = new BackupTaskState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ public void restoreLanguage() {
UiText.setLanguage("zh");
}

@Test
public void unknownFailedStageDoesNotInventAStopFailure() {
var view =
DeployStages.view(
DeployStages.Mode.REBUILD,
DeployStages.Phase.FAILED,
-1,
"PERMISSION_DENIED",
"unrecognized legacy progress",
42);
assertEquals(-1, view.current);
assertEquals(0, view.completed());
for (var step : view.steps) assertEquals(DeployStages.Step.TODO, step);
}

@Test
public void unknownRunningStageHasNoInventedFirstStepCounter() {
var view =
DeployStages.view(
DeployStages.Mode.REBUILD,
DeployStages.Phase.RUNNING,
-1,
"unrecognized progress",
"",
42);
assertEquals(-1, view.current);
assertEquals("", view.counter());
assertEquals("", view.sub());
}

@Test
public void coldInstallStagesMapInOrderWithoutPercentages() {
UiText.setLanguage("zh");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
import org.junit.Test;

public class MaintenanceFooterPolicyTest {
@Test
public void unknownRunningStageHasNoInventedHalfStepFill() {
var view =
DeployStages.view(
DeployStages.Mode.REBUILD,
DeployStages.Phase.RUNNING,
-1,
"unrecognized progress",
"",
42);
assertEquals(0, MaintenanceFooterPolicy.progressLevel(view));
}

@Test
public void knownRunningAndCompletedStagesKeepTheirExistingFill() {
var running =
DeployStages.view(DeployStages.Mode.REBUILD, DeployStages.Phase.RUNNING, 1, "", "", 42);
assertEquals(3000, MaintenanceFooterPolicy.progressLevel(running));
var done =
DeployStages.view(DeployStages.Mode.REBUILD, DeployStages.Phase.SUCCEEDED, -1, "", "", 42);
assertEquals(10000, MaintenanceFooterPolicy.progressLevel(done));
}

@Test
public void busyShowsProgressEvenIfOtherFlagsAreStale() {
Model m = MaintenanceFooterPolicy.decide(true, true, true, true, true);
Expand Down