File: ui_thread_test.go

package info (click to toggle)
coyim 0.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,064 kB
  • ctags: 4,528
  • sloc: xml: 5,120; sh: 328; python: 286; makefile: 235; ruby: 51
file content (58 lines) | stat: -rw-r--r-- 1,185 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package gui

import (
	"github.com/twstrike/gotk3adapter/glib_mock"
	"github.com/twstrike/gotk3adapter/glibi"
	. "gopkg.in/check.v1"
)

type UIThreadSuite struct{}

var _ = Suite(&UIThreadSuite{})

type glibIdleAddMock struct {
	glib_mock.Mock
	f func(interface{}, ...interface{}) (glibi.SourceHandle, error)
}

func (v *glibIdleAddMock) IdleAdd(v1 interface{}, v2 ...interface{}) (glibi.SourceHandle, error) {
	return v.f(v1, v2...)
}

type glibMainDepthMock struct {
	glib_mock.Mock
	mainDepth int
}

func (v *glibMainDepthMock) MainDepth() int {
	return v.mainDepth
}

func (*UIThreadSuite) Test_assertInUIThread_panicsIfNotInUIThread(c *C) {
	m := &glibMainDepthMock{mainDepth: 1}
	g = Graphics{glib: m}

	assertInUIThread()

	m.mainDepth = 0
	c.Assert(func() {
		assertInUIThread()
	}, Panics, "This function has to be called from the UI thread")
}

func (*UIThreadSuite) Test_doInUIThread(c *C) {
	m := &glibIdleAddMock{}
	g = Graphics{glib: m}

	m.f = func(ff interface{}, vals ...interface{}) (glibi.SourceHandle, error) {
		ffx := ff.(func())
		ffx()
		return glibi.SourceHandle(0), nil
	}

	ran := false
	doInUIThread(func() {
		ran = true
	})
	c.Assert(ran, Equals, true)
}