1package gtk 2 3import ( 4 "reflect" 5 "testing" 6 "unsafe" 7 8 "github.com/gotk3/gotk3/glib" 9) 10 11func TestTestRegisterAllTypes(t *testing.T) { 12 TestRegisterAllTypes() 13 types := TestListAllTypes() 14 15 if len(types) == 0 { 16 t.Error("Expected at least one type to be registered") 17 } 18} 19 20func TestPointerAtOffset(t *testing.T) { 21 // Simulate a C array by using a pointer to the first element 22 intArray := []int{4, 8, 2, 5, 9} 23 arrayPointer := unsafe.Pointer(&intArray[0]) 24 elementSize := unsafe.Sizeof(intArray[0]) 25 26 for i, val := range intArray { 27 intAtOffset := (*int)(pointerAtOffset(arrayPointer, elementSize, uint(i))) 28 if val != *intAtOffset { 29 t.Errorf("Expected %d at offset %d, got %d", val, i, *intAtOffset) 30 } 31 } 32} 33 34func TestTestFindLabel(t *testing.T) { 35 // Build a dummy widget 36 box, _ := BoxNew(ORIENTATION_HORIZONTAL, 0) 37 label1, _ := LabelNew("First") 38 label2, _ := LabelNew("Second") 39 40 box.PackStart(label1, true, true, 0) 41 box.PackStart(label2, true, true, 0) 42 43 // Find a label in the box with text matching Fir* 44 found, err := TestFindLabel(box, "Fir*") 45 if err != nil { 46 t.Error("Unexpected error:", err.Error()) 47 } 48 49 // Should return the label1 50 if found == nil { 51 t.Error("Return value is nil") 52 } 53 foundAsLabel, ok := found.(*Label) 54 if !ok { 55 t.Error("Did not return a label. Received type:", reflect.TypeOf(found)) 56 } 57 58 text, _ := foundAsLabel.GetText() 59 if text != "First" { 60 t.Error("Expected: First, Got:", text) 61 } 62 63} 64 65func TestTestFindSibling(t *testing.T) { 66 // Build a dummy widget 67 box, _ := BoxNew(ORIENTATION_HORIZONTAL, 0) 68 label1, _ := LabelNew("First") 69 label2, _ := LabelNew("Second") 70 71 box.PackStart(label1, true, true, 0) 72 box.PackStart(label2, true, true, 0) 73 74 // Finx a sibling to label1, of type label 75 found, err := TestFindSibling(label1, glib.TypeFromName("GtkLabel")) 76 if err != nil { 77 t.Error("Unexpected error:", err.Error()) 78 } 79 80 // Should return the label2 81 if found == nil { 82 t.Error("Return value is nil") 83 } 84 foundAsLabel, ok := found.(*Label) 85 if !ok { 86 t.Error("Did not return a label. Received type:", reflect.TypeOf(found)) 87 } 88 89 text, _ := foundAsLabel.GetText() 90 if text != "Second" { 91 t.Error("Expected: First, Got:", text) 92 } 93 94} 95 96func TestTestFindWidget(t *testing.T) { 97 // Build a dummy widget 98 box, _ := BoxNew(ORIENTATION_HORIZONTAL, 0) 99 button1, _ := ButtonNewWithLabel("First") 100 button2, _ := ButtonNewWithLabel("Second") 101 102 box.PackStart(button1, true, true, 0) 103 box.PackStart(button2, true, true, 0) 104 105 // Find a label in the box with text matching Fir* 106 found, err := TestFindWidget(box, "Sec*", glib.TypeFromName("GtkButton")) 107 if err != nil { 108 t.Error("Unexpected error:", err.Error()) 109 } 110 111 // Should return the button2 112 if found == nil { 113 t.Error("Return value is nil") 114 } 115 foundAsButton, ok := found.(*Button) 116 if !ok { 117 t.Error("Did not return a button. Received type:", reflect.TypeOf(found)) 118 } 119 120 text, _ := foundAsButton.GetLabel() 121 if text != "Second" { 122 t.Error("Expected: Second, Got:", text) 123 } 124 125} 126