diff --git a/src/Makefile b/src/Makefile
index 52c74f20e..adeec636e 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -2132,6 +2132,7 @@ test_arglist \
 	test_gn \
 	test_goto \
 	test_gui \
+	test_gui_init \
 	test_hardcopy \
 	test_help \
 	test_help_tagjump \
diff --git a/src/testdir/Make_all.mak b/src/testdir/Make_all.mak
index 3ff7247c3..7ea56786f 100644
--- a/src/testdir/Make_all.mak
+++ b/src/testdir/Make_all.mak
@@ -3,6 +3,7 @@
 #
 
 NO_PLUGIN = -U NONE --noplugin --not-a-term
+NO_PLUGIN_FOR_GUI_TEST = --noplugin --not-a-term
 
 # The first script creates small.vim.
 SCRIPTS_FIRST = \
@@ -153,6 +154,7 @@ NEW_TESTS = test_arglist.res \
 	    test_gf.res \
 	    test_gn.res \
 	    test_gui.res \
+	    test_gui_init.res \
 	    test_hardcopy.res \
 	    test_help.res \
 	    test_hide.res \
diff --git a/src/testdir/Makefile b/src/testdir/Makefile
index bb0249ac7..2f0259dfc 100644
--- a/src/testdir/Makefile
+++ b/src/testdir/Makefile
@@ -118,7 +118,10 @@ nolog:
 # New style of tests uses Vim script with assert calls.  These are easier
 # to write and a lot easier to read and debug.
 # Limitation: Only works with the +eval feature.
-RUN_VIMTEST = VIMRUNTIME=$(SCRIPTSOURCE); export VIMRUNTIME; $(VALGRIND) $(VIMPROG) -f $(GUI_FLAG) -u unix.vim $(NO_PLUGIN)
+RUN_VIMTEST_COMMON = VIMRUNTIME=$(SCRIPTSOURCE); export VIMRUNTIME; $(VALGRIND) $(VIMPROG) -f $(GUI_FLAG) -u unix.vim
+RUN_VIMTEST = $(RUN_VIMTEST_COMMON) $(NO_PLUGIN)
+RUN_GVIMTEST = $(RUN_VIMTEST_COMMON) -U NONE $(NO_PLUGIN_FOR_GUI_TEST)
+RUN_GVIMTEST_WITH_GVIMRC = $(RUN_VIMTEST_COMMON) -U gui_init.vim $(NO_PLUGIN_FOR_GUI_TEST)
 
 newtests: newtestssilent
 	@/bin/sh -c "if test -f messages && grep -q 'SKIPPED\|FAILED' messages; then cat messages && if test -f test.log; then cat test.log; fi ; fi"
@@ -130,3 +133,13 @@ newtestssilent: $(NEW_TESTS)
 	@echo "$(RUN_VIMTEST)" > vimcmd
 	$(RUN_VIMTEST) -U NONE -S runtest.vim $*.vim
 	@rm vimcmd
+
+test_gui.res: test_gui.vim
+	@echo "$(RUN_GVIMTEST)" > vimcmd
+	$(RUN_GVIMTEST) -S runtest.vim $<
+	@rm vimcmd
+
+test_gui_init.res: test_gui_init.vim
+	@echo "$(RUN_GVIMTEST_WITH_GVIMRC)" > vimcmd
+	$(RUN_GVIMTEST_WITH_GVIMRC) -S runtest.vim $<
+	@rm vimcmd
diff --git a/src/testdir/gui_init.vim b/src/testdir/gui_init.vim
new file mode 100644
index 000000000..272f12113
--- /dev/null
+++ b/src/testdir/gui_init.vim
@@ -0,0 +1,3 @@
+" gvimrc for test_gui_init.vim
+
+set guiheadroom=0
diff --git a/src/testdir/setup_gui.vim b/src/testdir/setup_gui.vim
new file mode 100644
index 000000000..88f538e0d
--- /dev/null
+++ b/src/testdir/setup_gui.vim
@@ -0,0 +1,29 @@
+" Common preparations for running GUI tests.
+
+let g:x11_based_gui = has('gui_athena') || has('gui_motif')
+	\ || has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
+
+" Reasons for 'skipped'.
+let g:not_supported   = "Skipped: Feature/Option not supported by this GUI: "
+let g:not_implemented = "Skipped: Test not implemented yet for this GUI"
+let g:not_hosted      = "Skipped: Test not hosted by the system/environment"
+
+" For KDE set a font, empty 'guifont' may cause a hang.
+func GUISetUpCommon()
+  if has("gui_kde")
+    set guifont=Courier\ 10\ Pitch/8/-1/5/50/0/0/0/0/0
+  endif
+
+  " Gnome insists on creating $HOME/.gnome2/, set $HOME to avoid changing the
+  " actual home directory.  But avoid triggering fontconfig by setting the
+  " cache directory.  Only needed for Unix.
+  if $XDG_CACHE_HOME == '' && exists('g:tester_HOME')
+    let $XDG_CACHE_HOME = g:tester_HOME . '/.cache'
+  endif
+  call mkdir('Xhome')
+  let $HOME = fnamemodify('Xhome', ':p')
+endfunc
+
+func GUITearDownCommon()
+  call delete('Xhome', 'rf')
+endfunc
diff --git a/src/testdir/test_gui.vim b/src/testdir/test_gui.vim
index aad569041..d9430747d 100644
--- a/src/testdir/test_gui.vim
+++ b/src/testdir/test_gui.vim
@@ -4,32 +4,14 @@ if !has('gui') || ($DISPLAY == "" && !has('gui_running'))
   finish
 endif
 
-let s:x11_based_gui = has('gui_athena') || has('gui_motif')
-	\ || has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
+source setup_gui.vim
 
-" Reasons for 'skipped'.
-let s:not_supported   = "Skipped: Feature/Option not supported by this GUI: "
-let s:not_implemented = "Skipped: Test not implemented yet for this GUI"
-let s:not_hosted      = "Skipped: Test not hosted by the system/environment"
-
-" For KDE set a font, empty 'guifont' may cause a hang.
-func SetUp()
-  if has("gui_kde")
-    set guifont=Courier\ 10\ Pitch/8/-1/5/50/0/0/0/0/0
-  endif
-
-  " Gnome insists on creating $HOME/.gnome2/, set $HOME to avoid changing the
-  " actual home directory.  But avoid triggering fontconfig by setting the
-  " cache directory.  Only needed for Unix.
-  if $XDG_CACHE_HOME == '' && exists('g:tester_HOME')
-    let $XDG_CACHE_HOME = g:tester_HOME . '/.cache'
-  endif
-  call mkdir('Xhome')
-  let $HOME = fnamemodify('Xhome', ':p')
+func Setup()
+  call GUISetUpCommon()
 endfunc
 
 func TearDown()
-  call delete('Xhome', 'rf')
+  call GUITearDownCommon()
 endfunc
 
 " Test for resetting "secure" flag after GUI has started.
@@ -43,8 +25,8 @@ endfunc
 func Test_getfontname_with_arg()
   let skipped = ''
 
-  if !s:x11_based_gui
-    let skipped = s:not_implemented
+  if !g:x11_based_gui
+    let skipped = g:not_implemented
   elseif has('gui_athena') || has('gui_motif')
     " Invalid font name. The result should be an empty string.
     call assert_equal('', getfontname('notexist'))
@@ -72,8 +54,8 @@ func Test_getfontname_without_arg()
 
   let fname = getfontname()
 
-  if !s:x11_based_gui
-    let skipped = s:not_implemented
+  if !g:x11_based_gui
+    let skipped = g:not_implemented
   elseif has('gui_kde')
     " 'expected' is the value specified by SetUp() above.
     call assert_equal('Courier 10 Pitch/8/-1/5/50/0/0/0/0/0', fname)
@@ -100,8 +82,8 @@ func Test_set_guifont()
     set guifontset=
   endif
 
-  if !s:x11_based_gui
-    let skipped = s:not_implemented
+  if !g:x11_based_gui
+    let skipped = g:not_implemented
   elseif has('gui_athena') || has('gui_motif')
     " Non-empty font list with invalid font names.
     "
@@ -153,7 +135,7 @@ func Test_set_guifontset()
   let skipped = ''
 
   if !has('xfontset')
-    let skipped = s:not_supported . 'xfontset'
+    let skipped = g:not_supported . 'xfontset'
   else
     let ctype_saved = v:ctype
 
@@ -202,7 +184,7 @@ func Test_set_guifontset()
 
     " Third, give a set of tests if it is found feasible.
     if !feasible
-      let skipped = s:not_hosted
+      let skipped = g:not_hosted
     else
       " N.B. 'v:ctype' has already been set to an appropriate value in the
       " previous loop.
@@ -224,15 +206,15 @@ endfunc
 func Test_set_guifontwide()
   let skipped = ''
 
-  if !s:x11_based_gui
-    let skipped = s:not_implemented
+  if !g:x11_based_gui
+    let skipped = g:not_implemented
   elseif has('gui_gtk')
     let guifont_saved = &guifont
     let guifontwide_saved = &guifontwide
 
     let fc_match = exepath('fc-match')
     if empty(fc_match)
-      let skipped = s:not_hosted
+      let skipped = g:not_hosted
     else
       let &guifont = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=en')
       let wide = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=ja')
@@ -246,7 +228,7 @@ func Test_set_guifontwide()
   elseif has('gui_athena') || has('gui_motif')
     " guifontwide is premised upon the xfontset feature.
     if !has('xfontset')
-      let skipped = s:not_supported . 'xfontset'
+      let skipped = g:not_supported . 'xfontset'
     else
       let encoding_saved    = &encoding
       let guifont_saved     = &guifont
@@ -303,6 +285,22 @@ func Test_set_guifontwide()
   endif
 endfunc
 
+func Test_set_guiheadroom()
+  let skipped = ''
+
+  if !g:x11_based_gui
+    let skipped = g:not_supported . 'guiheadroom'
+  else
+    " Since this script is to be read together with '-U NONE', the default
+    " value must be preserved.
+    call assert_equal(50, &guiheadroom)
+  endif
+
+  if !empty(skipped)
+    throw skipped
+  endif
+endfunc
+
 func Test_getwinpos()
   call assert_match('Window position: X \d\+, Y \d\+', execute('winpos'))
   call assert_true(getwinposx() >= 0)
@@ -317,7 +315,7 @@ func Test_shell_command()
 endfunc
 
 func Test_windowid_variable()
-  if s:x11_based_gui || has('win32')
+  if g:x11_based_gui || has('win32')
     call assert_true(v:windowid > 0)
   else
     call assert_equal(0, v:windowid)
diff --git a/src/testdir/test_gui_init.vim b/src/testdir/test_gui_init.vim
new file mode 100644
index 000000000..b26641039
--- /dev/null
+++ b/src/testdir/test_gui_init.vim
@@ -0,0 +1,34 @@
+" Tests specifically for the GUI features/options that are needed to be
+" set up at startup to take effec at runtime.
+
+if !has('gui') || ($DISPLAY == "" && !has('gui_running'))
+  finish
+endif
+
+source setup_gui.vim
+
+func Setup()
+  call GUISetUpCommon()
+endfunc
+
+func TearDown()
+  call GUITearDownCommon()
+endfunc
+
+gui f
+
+func Test_set_guiheadroom()
+  let skipped = ''
+
+  if !g:x11_based_gui
+    let skipped = g:not_supported . 'guiheadroom'
+  else
+    " The 'expected' value must be consistent with the value specified with
+    " gui_init.vim.
+    call assert_equal(0, &guiheadroom)
+  endif
+
+  if !empty(skipped)
+    throw skipped
+  endif
+endfunc
