fork/gdu
git clone https://git.julia.blue/fork/gdu
54737b3
ignore
1package tui 2 3import ( 4"bytes" 5"testing" 6"time" 7 8"github.com/dundee/gdu/v5/internal/testanalyze" 9"github.com/dundee/gdu/v5/internal/testapp" 10"github.com/dundee/gdu/v5/internal/testdir" 11"github.com/dundee/gdu/v5/pkg/fs" 12"github.com/gdamore/tcell/v2" 13"github.com/rivo/tview" 14"github.com/stretchr/testify/assert" 15) 16 17func TestDoubleClick (t * testing.T ) { 18fin := testdir .CreateTestDir () 19defer fin () 20simScreen := testapp .CreateSimScreen () 21defer simScreen .Fini () 22 23app := testapp .CreateMockedApp (false ) 24ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,false ,false ) 25ui .done = make (chan struct {}) 26err := ui .AnalyzePath ("test_dir" ,nil ) 27assert .Nil (t ,err ) 28 29<- ui .done // wait for analyzer 30 31for _ ,f := range ui .app .(* testapp.MockedApp ).GetUpdateDraws () { 32f () 33 } 34 35ui .table .Select (0 ,0 ) 36assert .Equal (t ,"test_dir" ,ui .currentDir .GetName ()) 37 38ui .onMouse (tcell .NewEventMouse (0 ,0 ,0 ,0 ),tview .MouseLeftDoubleClick ) 39assert .Equal (t ,"nested" ,ui .currentDir .GetName ()) 40 41ui .onMouse (tcell .NewEventMouse (0 ,0 ,0 ,0 ),tview .MouseLeftDoubleClick ) 42assert .Equal (t ,"test_dir" ,ui .currentDir .GetName ()) 43 44// show file content 45ui .keyPressed (tcell .NewEventKey (tcell .KeyRight ,'l' ,0 )) 46ui .table .Select (2 ,0 ) 47selectedFile := ui .table .GetCell (2 ,0 ).GetReference ().(fs.Item ) 48assert .Equal (t ,selectedFile .GetName (),"file2" ) 49ui .onMouse (tcell .NewEventMouse (0 ,0 ,0 ,0 ),tview .MouseLeftDoubleClick ) 50assert .True (t ,ui .pages .HasPage ("file" )) 51} 52 53func TestDoubleClickNoViewFile (t * testing.T ) { 54fin := testdir .CreateTestDir () 55defer fin () 56simScreen := testapp .CreateSimScreen () 57defer simScreen .Fini () 58 59app := testapp .CreateMockedApp (false ) 60ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,false ,false ) 61ui .done = make (chan struct {}) 62err := ui .AnalyzePath ("test_dir" ,nil ) 63assert .Nil (t ,err ) 64 65<- ui .done // wait for analyzer 66 67for _ ,f := range ui .app .(* testapp.MockedApp ).GetUpdateDraws () { 68f () 69 } 70 71ui .keyPressed (tcell .NewEventKey (tcell .KeyRight ,'l' ,0 )) 72ui .table .Select (2 ,0 ) 73ui .SetNoViewFile () 74previousHeaderText := ui .header .GetText (false ) 75 76ui .onMouse (tcell .NewEventMouse (0 ,0 ,0 ,0 ),tview .MouseLeftDoubleClick ) 77assert .False (t ,ui .pages .HasPage ("file" )) 78assert .Equal (t ," Viewing files is disabled!" ,ui .header .GetText (false )) 79 80time .Sleep (2100 * time .Millisecond ) 81for _ ,f := range ui .app .(* testapp.MockedApp ).GetUpdateDraws () { 82f () 83 } 84 85assert .Equal (t ,previousHeaderText ,ui .header .GetText (false )) 86} 87 88func TestScroll (t * testing.T ) { 89simScreen := testapp .CreateSimScreen () 90defer simScreen .Fini () 91 92app := testapp .CreateMockedApp (true ) 93ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,false ,false ) 94ui .Analyzer = & testanalyze.MockedAnalyzer {} 95ui .done = make (chan struct {}) 96err := ui .AnalyzePath ("test_dir" ,nil ) 97assert .Nil (t ,err ) 98 99<- ui .done // wait for analyzer 100 101for _ ,f := range ui .app .(* testapp.MockedApp ).GetUpdateDraws () { 102f () 103 } 104 105ui .onMouse (tcell .NewEventMouse (0 ,0 ,0 ,0 ),tview .MouseScrollDown ) 106row ,_ := ui .table .GetSelection () 107assert .Equal (t ,row ,1 ) 108 109ui .onMouse (tcell .NewEventMouse (0 ,0 ,0 ,0 ),tview .MouseScrollUp ) 110row ,_ = ui .table .GetSelection () 111assert .Equal (t ,row ,0 ) 112} 113 114func TestScrollWhenPageOpened (t * testing.T ) { 115simScreen := testapp .CreateSimScreen () 116defer simScreen .Fini () 117 118app := testapp .CreateMockedApp (true ) 119ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,false ,false ) 120ui .Analyzer = & testanalyze.MockedAnalyzer {} 121ui .done = make (chan struct {}) 122err := ui .AnalyzePath ("test_dir" ,nil ) 123assert .Nil (t ,err ) 124 125<- ui .done // wait for analyzer 126 127for _ ,f := range ui .app .(* testapp.MockedApp ).GetUpdateDraws () { 128f () 129 } 130 131// open confirm dialog 132ui .keyPressed (tcell .NewEventKey (tcell .KeyRune ,'d' ,0 )) 133 134ui .onMouse (tcell .NewEventMouse (0 ,0 ,0 ,0 ),tview .MouseScrollDown ) 135row ,_ := ui .table .GetSelection () 136// scrolling does nothing 137assert .Equal (t ,0 ,row ) 138} 139 140func TestEmptyEvent (t * testing.T ) { 141simScreen := testapp .CreateSimScreen () 142defer simScreen .Fini () 143 144app := testapp .CreateMockedApp (true ) 145ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,false ,false ) 146 147event ,action := ui .onMouse (nil ,tview .MouseMove ) 148assert .True (t ,event == nil ) 149assert .Equal (t ,action ,tview .MouseMove ) 150} 151 152func TestMouseMove (t * testing.T ) { 153simScreen := testapp .CreateSimScreen () 154defer simScreen .Fini () 155 156app := testapp .CreateMockedApp (true ) 157ui := CreateUI (app ,simScreen ,& bytes.Buffer {},true ,true ,false ,false ) 158 159event ,action := ui .onMouse (tcell .NewEventMouse (0 ,0 ,0 ,0 ),tview .MouseMove ) 160assert .True (t ,event != nil ) 161assert .Equal (t ,action ,tview .MouseMove ) 162}