refactor vk switch input modality function

Set local storage to SwitchToVoiceInput and SwitchToHandwriting to skip first-time prompt.

BUG=None
TEST=tast run <DUT> inputs.VirtualKeyboardHandwriting* inputs.VirtualKeyboardSpeech*

Change-Id: I33dbd36b180b34d1435267d7443e2825f27f04b0
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/tast-tests/+/3062539
Commit-Queue: Jun Sheng <shengjun@chromium.org>
Tested-by: Jun Sheng <shengjun@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
diff --git a/src/chromiumos/tast/local/bundles/cros/inputs/virtual_keyboard_handwriting.go b/src/chromiumos/tast/local/bundles/cros/inputs/virtual_keyboard_handwriting.go
index 7341b84..e5c2c73 100644
--- a/src/chromiumos/tast/local/bundles/cros/inputs/virtual_keyboard_handwriting.go
+++ b/src/chromiumos/tast/local/bundles/cros/inputs/virtual_keyboard_handwriting.go
@@ -135,7 +135,7 @@
 			}
 
 			// Switch to handwriting layout.
-			hwCtx, err := vkbCtx.SwitchToHandwritingAndCloseInfoDialogue(ctx)
+			hwCtx, err := vkbCtx.SwitchToHandwriting(ctx)
 			if err != nil {
 				s.Fatal("Failed to switch to handwriting: ", err)
 			}
diff --git a/src/chromiumos/tast/local/chrome/uiauto/vkb/vkb.go b/src/chromiumos/tast/local/chrome/uiauto/vkb/vkb.go
index 0e9d0fa..59fd827 100644
--- a/src/chromiumos/tast/local/chrome/uiauto/vkb/vkb.go
+++ b/src/chromiumos/tast/local/chrome/uiauto/vkb/vkb.go
@@ -42,6 +42,16 @@
 	}
 }
 
+// localStorageKey defines the key used in virtual keyboard local storage.
+type localStorageKey string
+
+const (
+	// voicePrivacyInfo key is defined in http://google3/i18n/input/javascript/chos/message/name.ts.
+	voicePrivacyInfo localStorageKey = "voice_privacy_info"
+	// showLongformEdu key is defined in http://google3/i18n/input/javascript/chos/ui/widget/longform_dialog_view.ts.
+	showLongformEdu localStorageKey = "shownLongformEdu"
+)
+
 // Finder of virtual keyboard root node.
 var vkRootFinder = nodewith.Role(role.RootWebArea).Name("Chrome OS Virtual Keyboard")
 
@@ -361,7 +371,10 @@
 		if err != nil {
 			return err
 		}
-		if err := bconn.Eval(ctx, `background.getTestOnlyApi().switchToVoiceInput()`, nil); err != nil {
+		if err := bconn.Call(ctx, nil, `(info) => {
+			window.localStorage.setItem(info, 'true');
+			background.getTestOnlyApi().switchToVoiceInput();
+		  }`, voicePrivacyInfo); err != nil {
 			return errors.Wrap(err, "failed to call switchToVoiceInput()")
 		}
 		return nil
@@ -378,35 +391,32 @@
 	)
 }
 
-// switchToHandwriting changes to handwriting layout and returns a handwriting context.
-func (vkbCtx *VirtualKeyboardContext) switchToHandwriting(ctx context.Context) (*HandwritingContext, error) {
+// SwitchToHandwriting changes to handwriting layout and returns a handwriting context.
+func (vkbCtx *VirtualKeyboardContext) SwitchToHandwriting(ctx context.Context) (*HandwritingContext, error) {
+	// Set local storage to override the LF first time tutorial prompt.
+	// It does not apply to legacy handwriting.
+	bconn, err := vkbCtx.BackgroundConn(ctx)
+	if err != nil {
+		return nil, err
+	}
+
+	if err := bconn.Call(ctx, nil, `(info) => {
+		window.localStorage.setItem(info, 'true');
+	  }`, showLongformEdu); err != nil {
+		return nil, errors.Wrap(err, "failed to set local storage")
+	}
+
 	if err := vkbCtx.leftClickIfExist(KeyFinder.NameStartingWith("switch to handwriting"))(ctx); err != nil {
 		return nil, err
 	}
 
+	if err := vkbCtx.ui.WaitUntilExists(NodeFinder.Role(role.Canvas))(ctx); err != nil {
+		return nil, err
+	}
+
 	return vkbCtx.NewHandwritingContext(ctx)
 }
 
-// SwitchToHandwritingAndCloseInfoDialogue changes to handwriting layout,
-// closes the information dialogue if one shows up and returns a handwriting context.
-func (vkbCtx *VirtualKeyboardContext) SwitchToHandwritingAndCloseInfoDialogue(ctx context.Context) (*HandwritingContext, error) {
-	hwCtx, err := vkbCtx.switchToHandwriting(ctx)
-	if err != nil {
-		return nil, err
-	}
-	// Close info dialogue if one shows up in longform VK.
-	if hwCtx.isLongForm {
-		if err := hwCtx.closeInfoDialogue("Got it")(ctx); err != nil {
-			return nil, err
-		}
-	}
-
-	if err = vkbCtx.ui.WaitUntilExists(NodeFinder.Role(role.Canvas))(ctx); err != nil {
-		return nil, err
-	}
-	return hwCtx, nil
-}
-
 // SwitchToMultipaste returns an action changing to multipaste layout.
 func (vkbCtx *VirtualKeyboardContext) SwitchToMultipaste() uiauto.Action {
 	return vkbCtx.ui.LeftClick(KeyFinder.Name("Multipaste clipboard"))