This is the cherry pick merge for the M73 branch which was approved here: http://crbug.com/926895

[Autofill Assistant] Fix recovery of status message after interrupt.

Before this patch, recovery of status message after an interrupt
depended on an uninitialized boolean, so sometimes cleared the status
instead of restoring it.

This patch initializes the boolean and adds a unit test.

Bug: 806868
Change-Id: I9b8c7875a6614c9e855a98ed14e70bc925cacdb3
Reviewed-on: https://chromium-review.googlesource.com/c/1437629
Reviewed-by: Mathias Carlen <mcarlen@chromium.org>
Commit-Queue: Stephane Zermatten <szermatt@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#626508}(cherry picked from commit 4e5a2a38a1125819e759b838734d1b213ad5937f)
Reviewed-on: https://chromium-review.googlesource.com/c/1448167
Reviewed-by: Stephane Zermatten <szermatt@chromium.org>
Cr-Commit-Position: refs/branch-heads/3683@{#91}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/components/autofill_assistant/browser/script_executor.cc b/components/autofill_assistant/browser/script_executor.cc
index 9e47d898..ac4bdd5 100644
--- a/components/autofill_assistant/browser/script_executor.cc
+++ b/components/autofill_assistant/browser/script_executor.cc
@@ -540,7 +540,6 @@
       check_type_(check_type),
       selector_(selector),
       callback_(std::move(callback)),
-      element_found_(false),
       weak_ptr_factory_(this) {}
 
 ScriptExecutor::WaitWithInterrupts::~WaitWithInterrupts() = default;
@@ -652,7 +651,7 @@
     RunCallback(false, &result);
     return;
   }
-  RestorePreInterruptUiState();
+  RestoreStatusMessage();
 
   // Restart. We use the original wait time since the interruption could have
   // triggered any kind of actions, including actions that wait on the user. We
@@ -680,7 +679,7 @@
   saved_pre_interrupt_state_ = true;
 }
 
-void ScriptExecutor::WaitWithInterrupts::RestorePreInterruptUiState() {
+void ScriptExecutor::WaitWithInterrupts::RestoreStatusMessage() {
   if (!saved_pre_interrupt_state_)
     return;
 
diff --git a/components/autofill_assistant/browser/script_executor.h b/components/autofill_assistant/browser/script_executor.h
index 4f1319a..3a055d9 100644
--- a/components/autofill_assistant/browser/script_executor.h
+++ b/components/autofill_assistant/browser/script_executor.h
@@ -209,7 +209,7 @@
     void SavePreInterruptState();
 
     // Restores the UI states as found by SavePreInterruptState.
-    void RestorePreInterruptUiState();
+    void RestoreStatusMessage();
 
     // if save_pre_interrupt_state_ is set, attempt to scroll the page back to
     // the original area.
@@ -223,7 +223,7 @@
 
     std::unique_ptr<BatchElementChecker> batch_element_checker_;
     std::set<const Script*> runnable_interrupts_;
-    bool element_found_;
+    bool element_found_ = false;
 
     // An empty vector of interrupts that can be passed to interrupt_executor_
     // and outlives it. Interrupts must not run interrupts.
@@ -234,7 +234,7 @@
 
     // If true, pre-interrupt state was saved already. This happens just before
     // the first interrupt.
-    bool saved_pre_interrupt_state_;
+    bool saved_pre_interrupt_state_ = false;
 
     // The status message that was displayed when the interrupt started.
     std::string pre_interrupt_status_;
diff --git a/components/autofill_assistant/browser/script_executor_unittest.cc b/components/autofill_assistant/browser/script_executor_unittest.cc
index c03005b..9a4b391 100644
--- a/components/autofill_assistant/browser/script_executor_unittest.cc
+++ b/components/autofill_assistant/browser/script_executor_unittest.cc
@@ -894,5 +894,54 @@
   EXPECT_THAT("update_from_interrupt", scripts_update_[0]->handle.name);
 }
 
+TEST_F(ScriptExecutorTest, RestorePreInterruptStatusMessage) {
+  ActionsResponseProto interruptible;
+  interruptible.add_actions()->mutable_tell()->set_message(
+      "pre-interrupt status");
+  auto* wait_action = interruptible.add_actions()->mutable_wait_for_dom();
+  wait_action->add_selectors("element");
+  wait_action->set_allow_interrupt(true);
+  EXPECT_CALL(mock_service_, OnGetActions(kScriptPath, _, _, _, _, _))
+      .WillRepeatedly(RunOnceCallback<5>(true, Serialize(interruptible)));
+
+  RegisterInterrupt("interrupt", "interrupt_trigger");
+  ActionsResponseProto interrupt_actions;
+  interrupt_actions.add_actions()->mutable_tell()->set_message(
+      "interrupt status");
+  EXPECT_CALL(mock_service_, OnGetActions(StrEq("interrupt"), _, _, _, _, _))
+      .WillRepeatedly(RunOnceCallback<5>(true, Serialize(interrupt_actions)));
+
+  EXPECT_CALL(mock_service_, OnGetNextActions(_, _, _, _))
+      .WillRepeatedly(RunOnceCallback<3>(true, ""));
+
+  EXPECT_CALL(executor_callback_,
+              Run(Field(&ScriptExecutor::Result::success, true)));
+
+  status_message_ = "pre-run status";
+  executor_->Run(executor_callback_.Get());
+  EXPECT_EQ("pre-interrupt status", status_message_);
+}
+
+TEST_F(ScriptExecutorTest, KeepStatusMessageWhenNotInterrupted) {
+  ActionsResponseProto interruptible;
+  interruptible.add_actions()->mutable_tell()->set_message(
+      "pre-interrupt status");
+  auto* wait_action = interruptible.add_actions()->mutable_wait_for_dom();
+  wait_action->add_selectors("element");
+  wait_action->set_allow_interrupt(true);
+  EXPECT_CALL(mock_service_, OnGetActions(kScriptPath, _, _, _, _, _))
+      .WillRepeatedly(RunOnceCallback<5>(true, Serialize(interruptible)));
+
+  EXPECT_CALL(mock_service_, OnGetNextActions(_, _, _, _))
+      .WillRepeatedly(RunOnceCallback<3>(true, ""));
+
+  EXPECT_CALL(executor_callback_,
+              Run(Field(&ScriptExecutor::Result::success, true)));
+
+  status_message_ = "pre-run status";
+  executor_->Run(executor_callback_.Get());
+  EXPECT_EQ("pre-interrupt status", status_message_);
+}
+
 }  // namespace
 }  // namespace autofill_assistant