/*------------------------------------------------------------------------------
  upload-direct.p            #12082: direct web upload (no Security Warning)

  Path:     WEB-FILE-UPLOAD REGISTER <button> IN FRAME f CHOOSE UPLOAD ... (FWD
            extension) sends the button to the web client as a direct-upload
            widget.  Clicking it (mouse down, or SPACE / ENTER) is caught by
            p2j.screen.processDirectUpload, which calls
            p2j.screen.chooseFiles(direct = true, ...): the browser's file
            chooser opens at once, WITHOUT the "Security Warning" dialog.
            chooseFiles still creates both p2j.ModalDialogs:
              selectUploadFilesDialog  "Security Warning" - created hidden,
                                       never shown; hide() and close() are
                                       still called on it
              actionResultsDialog      "Uploading...", then "Uploaded N
                                       file(s)." and closeAction 5 s later
            The result reaches 4GL as WEB-UPLOAD-COMPLETE on the button, with
            SESSION:WEB-UPLOAD-ERROR and SESSION:WEB-UPLOAD-FILES.
            The cases cover the common shapes of the statement.

  Run:      web GUI only, https://localhost:7443/gui?prog=upload_12082/upload-direct.p

  Cases:
    1 "Direct one"      REGISTER ... CHOOSE UPLOAD DIR TITLE
                        (button in a window)
    2 "Direct many"     ... UPLOAD MULTIPLE TITLE
    3 "Direct filter"   ... UPLOAD TITLE FILTERS
    4 "Open dialog"     opens a DIALOG-BOX whose button "Choose file" is
                        registered the same way, a file-selection dialog
                        box; OK / Cancel leave the dialog box
    5 "Toggle 2"        switches btnMany (case 2) SENSITIVE off / on: a state
                        change of one registered widget, with no new
                        registration; cases 1 and 3 must keep opening the
                        file chooser, and case 2 must open it again once on
  Steps:    Click a case button: the browser's file chooser must open with no
            "Security Warning".  Pick a file.  Wait for "Uploaded 1 file." and
            5 more seconds.  Click "Ping".  Run every case twice, and once
            closing the file chooser with Cancel.
  Expected: no "Security Warning" at any time; "Uploading..." is gone 5 s
            after the upload; the log shows WEB-UPLOAD-COMPLETE with the
            uploaded file and "Ping" is logged at once.
  Broken:   a dialog stays on screen, or "Security Warning" becomes visible
            during or after the upload, and the application stays blocked.

  Found (2026-11-20, 12082a AND trunk r16862 - not caused by 12082a):
            after case 4's dialog box has been open (even left with Cancel,
            no upload in it), the buttons of cases 1-3 lose their direct
            upload: the client's window 1 directUploads becomes null and a
            click opens no file chooser, it is a plain CHOOSE (logged as
            "case N CHOOSE reached 4GL").
            Steps: 1 Direct one (chooser opens, cancel it), 4 Open dialog,
            Cancel, 1 Direct one again - no file chooser.
            Cause: GuiWebDriver.getDirectUploadDefinitions() sent only the
            registered widgets whose state changed, while the JS client
            (resetDirectUploads + registerDirectUploads) takes every upload
            section as the complete list of all windows.  Case 5 hits the
            same cause without a dialog box.
------------------------------------------------------------------------------*/

DEFINE VARIABLE cDir  AS CHARACTER NO-UNDO.
DEFINE VARIABLE iSeq  AS INTEGER   NO-UNDO.
DEFINE VARIABLE cLog  AS CHARACTER NO-UNDO
   VIEW-AS EDITOR SCROLLBAR-VERTICAL SIZE 96 BY 12.

DEFINE BUTTON btnOne    LABEL "1 Direct one".
DEFINE BUTTON btnMany   LABEL "2 Direct many".
DEFINE BUTTON btnFilter LABEL "3 Direct filter".
DEFINE BUTTON btnDialog LABEL "4 Open dialog".
DEFINE BUTTON btnToggle LABEL "5 Toggle 2".
DEFINE BUTTON btnPing   LABEL "Ping".
DEFINE BUTTON btnQuit   LABEL "Quit".

DEFINE BUTTON btnDlgFile LABEL "Choose file".
DEFINE BUTTON btnDlgOk   LABEL "OK"     AUTO-GO.
DEFINE BUTTON btnDlgCanc LABEL "Cancel" AUTO-ENDKEY.

DEFINE FRAME fMain
   btnOne btnMany btnFilter btnDialog btnPing btnQuit SKIP(0.5)
   btnToggle SKIP(0.5)
   cLog NO-LABEL
   WITH SIDE-LABELS THREE-D SIZE 100 BY 17 TITLE "#12082 direct upload".

DEFINE FRAME D-Dialog
   btnDlgFile SKIP(1)
   btnDlgOk btnDlgCanc
   WITH VIEW-AS DIALOG-BOX THREE-D SIDE-LABELS TITLE "12082 dialog-box upload".

PROCEDURE log-step:
   DEFINE INPUT PARAMETER pText AS CHARACTER NO-UNDO.

   iSeq = iSeq + 1.
   cLog:INSERT-STRING(STRING(iSeq, "999") + " " + STRING(TIME, "HH:MM:SS") + " "
                      + pText + CHR(10)) IN FRAME fMain.
END PROCEDURE.

PROCEDURE log-upload:
   DEFINE INPUT PARAMETER pCase AS CHARACTER NO-UNDO.

   RUN log-step (pCase + " WEB-UPLOAD-COMPLETE, error " + STRING(SESSION:WEB-UPLOAD-ERROR)
                 + " [" + (IF SESSION:WEB-UPLOAD-FILES = ? THEN "?" ELSE SESSION:WEB-UPLOAD-FILES)
                 + "] - wait 5 s, no dialog may stay, then click Ping").
END PROCEDURE.

/* case 1: a button in a window, with UPLOAD DIR */
ON CHOOSE OF btnOne IN FRAME fMain
DO:
   RUN log-step ("case 1 CHOOSE reached 4GL (the direct upload consumes the click)").
END.
ON WEB-UPLOAD-COMPLETE OF btnOne IN FRAME fMain
DO:
   RUN log-upload ("case 1").
END.

/* case 2: MULTIPLE */
ON CHOOSE OF btnMany IN FRAME fMain
DO:
   RUN log-step ("case 2 CHOOSE reached 4GL (the direct upload consumes the click)").
END.
ON WEB-UPLOAD-COMPLETE OF btnMany IN FRAME fMain
DO:
   RUN log-upload ("case 2").
END.

/* case 3: FILTERS */
ON CHOOSE OF btnFilter IN FRAME fMain
DO:
   RUN log-step ("case 3 CHOOSE reached 4GL (the direct upload consumes the click)").
END.
ON WEB-UPLOAD-COMPLETE OF btnFilter IN FRAME fMain
DO:
   RUN log-upload ("case 3").
END.

/* case 4: the registered button is in a DIALOG-BOX */
ON WEB-UPLOAD-COMPLETE OF btnDlgFile IN FRAME D-Dialog
DO:
   RUN log-upload ("case 4").
END.

ON CHOOSE OF btnDialog IN FRAME fMain
DO:
   RUN log-step ("case 4: dialog box open - click Choose file, then OK").

   WEB-FILE-UPLOAD REGISTER btnDlgFile IN FRAME D-Dialog CHOOSE
      UPLOAD
      TITLE "12082 dialog-box file".

   ENABLE ALL WITH FRAME D-Dialog.
   DO ON ENDKEY UNDO, LEAVE:
      WAIT-FOR GO OF FRAME D-Dialog.
   END.
   HIDE FRAME D-Dialog.

   RUN log-step ("case 4: dialog box closed - click Ping").
END.

/* case 5: a state change of one registered widget, no new registration */
ON CHOOSE OF btnToggle IN FRAME fMain
DO:
   btnMany:SENSITIVE = NOT btnMany:SENSITIVE.
   RUN log-step ("case 5: btnMany SENSITIVE " + STRING(btnMany:SENSITIVE)
                 + " - 1 Direct one and 3 Direct filter must still open the chooser").
END.

ON CHOOSE OF btnPing IN FRAME fMain
DO:
   RUN log-step ("Ping - the application takes input").
END.

cDir = SESSION:TEMP-DIRECTORY.

WEB-FILE-UPLOAD REGISTER btnOne IN FRAME fMain CHOOSE
   UPLOAD DIR cDir
   TITLE "12082 direct one".

WEB-FILE-UPLOAD REGISTER btnMany IN FRAME fMain CHOOSE
   UPLOAD
   MULTIPLE
   TITLE "12082 direct many".

WEB-FILE-UPLOAD REGISTER btnFilter IN FRAME fMain CHOOSE
   UPLOAD
   TITLE "12082 direct filter"
   FILTERS "Text file" "*.txt".

ENABLE ALL WITH FRAME fMain.
RUN log-step ("upload dir " + cDir + " - click a case button").

WAIT-FOR CHOOSE OF btnQuit IN FRAME fMain.
