Navigation Menu

Skip to content

Commit

Permalink
Add tests for settings code
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Jun 20, 2018
1 parent 0cfeffe commit f91eaff
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
8 changes: 4 additions & 4 deletions settings.go
Expand Up @@ -17,7 +17,7 @@ type settings struct {
changeListeners []chan Settings
}

var singleton *settings
var settingsCache *settings

func (s *settings) Theme() string {
return s.theme
Expand Down Expand Up @@ -55,9 +55,9 @@ func loadSettings() *settings {

// GetSettings returns the system wide settings currently configured
func GetSettings() Settings {
if singleton == nil {
singleton = loadSettings()
if settingsCache == nil {
settingsCache = loadSettings()
}

return singleton
return settingsCache
}
38 changes: 38 additions & 0 deletions settings_test.go
@@ -0,0 +1,38 @@
package fyne

import "os"
import "time"

import "testing"
import "github.com/stretchr/testify/assert"

func TestThemeDefault(t *testing.T) {
assert.Equal(t, "dark", GetSettings().Theme())
}

func TestThemeFromEnv(t *testing.T) {
settingsCache = nil
os.Setenv("FYNE_THEME", "light")

assert.Equal(t, "light", GetSettings().Theme())
}

func TestSetTheme(t *testing.T) {
GetSettings().SetTheme("light")

assert.Equal(t, "light", GetSettings().Theme())
}

func TestListenerCallback(t *testing.T) {
listener := make(chan Settings)
GetSettings().AddChangeListener(listener)
GetSettings().SetTheme("light")

func() {
select {
case <-listener:
case <-time.After(1 * time.Second):
assert.Fail(t, "Timed out waiting for callback")
}
}()
}

0 comments on commit f91eaff

Please sign in to comment.