-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Expose `DefaultApplicationMenu`. Add `FindByLabel` and `ItemAt` for finding menu items in a menu * Add `Menu.RemoveMenuItem()`, `MneuItem.GetAccelerator()` and `MenuItem.RemoveAccelerator()` * Remove `Update` * Iterate when removing menu items * Add `GetSubmenu()`
- Loading branch information
1 parent
939d22d
commit e424a85
Showing
16 changed files
with
364 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package application_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/wailsapp/wails/v3/pkg/application" | ||
) | ||
|
||
func TestMenu_FindByLabel(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
menu *application.Menu | ||
label string | ||
shouldError bool | ||
}{ | ||
{ | ||
name: "Find top-level item", | ||
menu: application.NewMenuFromItems( | ||
application.NewMenuItem("Target"), | ||
), | ||
label: "Target", | ||
shouldError: false, | ||
}, | ||
{ | ||
name: "Find item in submenu", | ||
menu: application.NewMenuFromItems( | ||
application.NewMenuItem("Item 1"), | ||
application.NewSubmenu("Submenu", application.NewMenuFromItems( | ||
application.NewMenuItem("Subitem 1"), | ||
application.NewMenuItem("Target"), | ||
)), | ||
), | ||
label: "Target", | ||
shouldError: false, | ||
}, | ||
{ | ||
name: "Not find item", | ||
menu: application.NewMenuFromItems( | ||
application.NewMenuItem("Item 1"), | ||
application.NewSubmenu("Submenu", application.NewMenuFromItems( | ||
application.NewMenuItem("Subitem 1"), | ||
application.NewMenuItem("Target"), | ||
)), | ||
), | ||
label: "Random", | ||
shouldError: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
found := test.menu.FindByLabel(test.label) | ||
if test.shouldError && found != nil { | ||
t.Errorf("Expected error, but found %v", found) | ||
} | ||
if !test.shouldError && found == nil { | ||
t.Errorf("Expected item, but found none") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMenu_ItemAt(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
menu *application.Menu | ||
index int | ||
shouldError bool | ||
}{ | ||
{ | ||
name: "Valid index", | ||
menu: application.NewMenuFromItems( | ||
application.NewMenuItem("Item 1"), | ||
application.NewMenuItem("Item 2"), | ||
application.NewMenuItem("Target"), | ||
), | ||
index: 2, | ||
shouldError: false, | ||
}, | ||
{ | ||
name: "Index out of bounds (negative)", | ||
menu: application.NewMenuFromItems( | ||
application.NewMenuItem("Item 1"), | ||
application.NewMenuItem("Item 2"), | ||
), | ||
index: -1, | ||
shouldError: true, | ||
}, | ||
{ | ||
name: "Index out of bounds (too large)", | ||
menu: application.NewMenuFromItems( | ||
application.NewMenuItem("Item 1"), | ||
application.NewMenuItem("Item 2"), | ||
), | ||
index: 2, | ||
shouldError: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
item := test.menu.ItemAt(test.index) | ||
if test.shouldError && item != nil { | ||
t.Errorf("Expected error, but found %v", item) | ||
} | ||
if !test.shouldError && item == nil { | ||
t.Errorf("Expected item, but found none") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMenu_RemoveMenuItem(t *testing.T) { | ||
itemToRemove := application.NewMenuItem("Target") | ||
itemToKeep := application.NewMenuItem("Item 1") | ||
|
||
tests := []struct { | ||
name string | ||
menu *application.Menu | ||
item *application.MenuItem | ||
shouldFind bool | ||
}{ | ||
{ | ||
name: "Remove existing item", | ||
menu: application.NewMenuFromItems(itemToKeep, itemToRemove), | ||
item: itemToRemove, | ||
shouldFind: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
test.menu.RemoveMenuItem(test.item) | ||
found := test.menu.FindByLabel(test.item.Label()) | ||
if !test.shouldFind && found != nil { | ||
t.Errorf("Expected item to be removed, but found %v", found) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.