patch 8.2.3040: GUI: dropping files not tested

Problem:    GUI: dropping files not tested.
Solution:   Add test_gui_drop_files() and tests. (Yegappan Lakshmanan,
            closes #8434)
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 21d28af..3dd887b 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -1700,6 +1700,8 @@
 			ret_void,	    f_test_garbagecollect_soon},
     {"test_getvalue",	1, 1, FEARG_1,	    NULL,
 			ret_number,	    f_test_getvalue},
+    {"test_gui_drop_files",	4, 4, 0,	    NULL,
+			ret_void,	    f_test_gui_drop_files},
     {"test_gui_mouse_event",	5, 5, 0,	    NULL,
 			ret_void,	    f_test_gui_mouse_event},
     {"test_ignore_error", 1, 1, FEARG_1,    NULL,
diff --git a/src/gui.c b/src/gui.c
index b5ec007..c748fae 100644
--- a/src/gui.c
+++ b/src/gui.c
@@ -5567,6 +5567,7 @@
 	{
 	    vim_free(fnames[0]);
 	    vim_free(fnames);
+	    vim_free(p);
 	}
 	else
 	    handle_drop(count, fnames, (modifiers & MOUSE_CTRL) != 0,
diff --git a/src/proto/testing.pro b/src/proto/testing.pro
index 3e203f8..0d4ff49 100644
--- a/src/proto/testing.pro
+++ b/src/proto/testing.pro
@@ -36,4 +36,5 @@
 void f_test_setmouse(typval_T *argvars, typval_T *rettv);
 void f_test_gui_mouse_event(typval_T *argvars, typval_T *rettv);
 void f_test_settime(typval_T *argvars, typval_T *rettv);
+void f_test_gui_drop_files(typval_T *argvars, typval_T *rettv);
 /* vim: set ft=c : */
diff --git a/src/testdir/test_gui.vim b/src/testdir/test_gui.vim
index e935e5f..5c18d5d 100644
--- a/src/testdir/test_gui.vim
+++ b/src/testdir/test_gui.vim
@@ -1158,4 +1158,93 @@
   let &lines = save_lines
 endfunc
 
+" Test for dropping files into a window in GUI
+func DropFilesInCmdLine()
+  call feedkeys(":\"", 'L')
+  call test_gui_drop_files(['a.c', 'b.c'], &lines, 1, 0)
+  call feedkeys("\<CR>", 'L')
+endfunc
+
+func Test_gui_drop_files()
+  call assert_fails('call test_gui_drop_files(1, 1, 1, 0)', 'E474:')
+  call assert_fails('call test_gui_drop_files(["x"], "", 1, 0)', 'E474:')
+  call assert_fails('call test_gui_drop_files(["x"], 1, "", 0)', 'E474:')
+  call assert_fails('call test_gui_drop_files(["x"], 1, 1, "")', 'E474:')
+
+  %bw!
+  %argdelete
+  call test_gui_drop_files([], 1, 1, 0)
+  call assert_equal([], argv())
+  call test_gui_drop_files([1, 2], 1, 1, 0)
+  call assert_equal([], argv())
+
+  call test_gui_drop_files(['a.c', 'b.c'], 1, 1, 0)
+  call assert_equal(['a.c', 'b.c'], argv())
+  %bw!
+  %argdelete
+  call test_gui_drop_files([], 1, 1, 0)
+  call assert_equal([], argv())
+  %bw!
+  " if the buffer in the window is modified, then the file should be opened in
+  " a new window
+  set modified
+  call test_gui_drop_files(['x.c', 'y.c'], 1, 1, 0)
+  call assert_equal(['x.c', 'y.c'], argv())
+  call assert_equal(2, winnr('$'))
+  call assert_equal('x.c', bufname(winbufnr(1)))
+  %bw!
+  %argdelete
+  " if Ctrl is pressed, then the file should be opened in a new window
+  call test_gui_drop_files(['s.py', 't.py'], 1, 1, 0x10)
+  call assert_equal(['s.py', 't.py'], argv())
+  call assert_equal(2, winnr('$'))
+  call assert_equal('s.py', bufname(winbufnr(1)))
+  %bw!
+  %argdelete
+  " drop the files in a non-current window
+  belowright new
+  call test_gui_drop_files(['a.py', 'b.py'], 1, 1, 0)
+  call assert_equal(['a.py', 'b.py'], argv())
+  call assert_equal(2, winnr('$'))
+  call assert_equal(1, winnr())
+  call assert_equal('a.py', bufname(winbufnr(1)))
+  %bw!
+  %argdelete
+  " pressing shift when dropping files should change directory
+  let save_cwd = getcwd()
+  call mkdir('Xdir1')
+  call writefile([], 'Xdir1/Xfile1')
+  call writefile([], 'Xdir1/Xfile2')
+  call test_gui_drop_files(['Xdir1/Xfile1', 'Xdir1/Xfile2'], 1, 1, 0x4)
+  call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
+  call assert_equal('Xfile1', @%)
+  call chdir(save_cwd)
+  " pressing shift when dropping directory and files should change directory
+  call test_gui_drop_files(['Xdir1', 'Xdir1/Xfile2'], 1, 1, 0x4)
+  call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
+  call assert_equal('Xdir1', fnamemodify(@%, ':t'))
+  call chdir(save_cwd)
+  %bw!
+  %argdelete
+  " dropping a directory should edit it
+  call test_gui_drop_files(['Xdir1'], 1, 1, 0)
+  call assert_equal('Xdir1', @%)
+  %bw!
+  %argdelete
+  " dropping only a directory name with Shift should ignore it
+  call test_gui_drop_files(['Xdir1'], 1, 1, 0x4)
+  call assert_equal('', @%)
+  %bw!
+  %argdelete
+  call delete('Xdir1', 'rf')
+  " drop files in the command line. The GUI drop files adds the file names to
+  " the low level input buffer. So need to use a cmdline map and feedkeys()
+  " with 'Lx!' to process it in this function itself.
+  cnoremap <expr> <buffer> <F4> DropFilesInCmdLine()
+  call feedkeys(":\"\<F4>\<CR>", 'xt')
+  call feedkeys('k', 'Lx!')
+  call assert_equal('"a.c b.c', @:)
+  cunmap <buffer> <F4>
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testing.c b/src/testing.c
index 5ebcefb..b32eb1a 100644
--- a/src/testing.c
+++ b/src/testing.c
@@ -1224,7 +1224,7 @@
     void
 f_test_gui_mouse_event(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
 {
-#ifdef FEAT_GUI
+# ifdef FEAT_GUI
     int		button;
     int		row;
     int		col;
@@ -1248,7 +1248,7 @@
     mods = tv_get_number(&argvars[4]);
 
     gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1), repeated_click, mods);
-#endif
+# endif
 }
 
     void
@@ -1257,5 +1257,61 @@
     time_for_testing = (time_t)tv_get_number(&argvars[0]);
 }
 
+    void
+f_test_gui_drop_files(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
+{
+# ifdef FEAT_GUI
+    int		row;
+    int		col;
+    int_u	mods;
+    char_u	**fnames;
+    int		count = 0;
+    list_T	*l;
+    listitem_T	*li;
+
+    if (argvars[0].v_type != VAR_LIST
+	    || (argvars[1].v_type) != VAR_NUMBER
+	    || (argvars[2].v_type) != VAR_NUMBER
+	    || (argvars[3].v_type) != VAR_NUMBER)
+    {
+	emsg(_(e_invarg));
+	return;
+    }
+
+    row = tv_get_number(&argvars[1]);
+    col = tv_get_number(&argvars[2]);
+    mods = tv_get_number(&argvars[3]);
+
+    l = argvars[0].vval.v_list;
+    if (list_len(l) == 0)
+	return;
+
+    fnames = ALLOC_MULT(char_u *, list_len(l));
+    if (fnames == NULL)
+	return;
+
+    FOR_ALL_LIST_ITEMS(l, li)
+    {
+	// ignore non-string items
+	if (li->li_tv.v_type != VAR_STRING)
+	    continue;
+
+	fnames[count] = vim_strsave(li->li_tv.vval.v_string);
+	if (fnames[count] == NULL)
+	{
+	    while (--count >= 0)
+		vim_free(fnames[count]);
+	    vim_free(fnames);
+	    return;
+	}
+	count++;
+    }
+
+    if (count > 0)
+	gui_handle_drop(TEXT_X(col - 1), TEXT_Y(row - 1), mods, fnames, count);
+    else
+	vim_free(fnames);
+# endif
+}
 
 #endif // defined(FEAT_EVAL)
diff --git a/src/version.c b/src/version.c
index 83c7460..bf33312 100644
--- a/src/version.c
+++ b/src/version.c
@@ -756,6 +756,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    3040,
+/**/
     3039,
 /**/
     3038,