Debian Codegraph @

bus/powerd/powerd_test.go

raw
1 /*
2 Copyright 2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package powerd
18
19 import (
20 "testing"
21 "time"
22
23 . "gopkg.in/check.v1"
24
25 testibus "gitlab.com/ubports/development/core/lomiri-push-service/bus/testing"
26 helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing"
27 "gitlab.com/ubports/development/core/lomiri-push-service/testing/condition"
28 )
29
30 // hook up gocheck
31 func TestPowerd(t *testing.T) { TestingT(t) }
32
33 type PdSuite struct {
34 log *helpers.TestLogger
35 }
36
37 var _ = Suite(&PdSuite{})
38
39 func (s *PdSuite) SetUpTest(c *C) {
40 s.log = helpers.NewTestLogger(c, "debug")
41 }
42
43 func (s *PdSuite) TestRequestWakeupWorks(c *C) {
44 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), "cookie")
45 pd := New(endp, s.log)
46 t := time.Now().Add(5 * time.Minute)
47 ck, err := pd.RequestWakeup("name", t)
48 c.Assert(err, IsNil)
49 c.Check(ck, Equals, "cookie")
50 args := testibus.GetCallArgs(endp)
51 c.Assert(args, HasLen, 1)
52 c.Check(args[0].Member, Equals, "requestWakeup")
53 c.Check(args[0].Args, DeepEquals, []interface{}{"name", uint64(t.Unix())})
54 }
55
56 func (s *PdSuite) TestRequestWakeupUnconfigured(c *C) {
57 _, err := new(powerd).RequestWakeup("name", time.Now())
58 c.Assert(err, Equals, ErrUnconfigured)
59 }
60
61 func (s *PdSuite) TestRequestWakeupFails(c *C) {
62 endp := testibus.NewTestingEndpoint(nil, condition.Work(false))
63 pd := New(endp, s.log)
64 t := time.Now().Add(5 * time.Minute)
65 _, err := pd.RequestWakeup("name", t)
66 c.Assert(err, NotNil)
67 }
68
69 func (s *PdSuite) TestClearWakeupWorks(c *C) {
70 endp := testibus.NewTestingEndpoint(nil, condition.Work(true))
71 pd := New(endp, s.log)
72 err := pd.ClearWakeup("cookie")
73 c.Assert(err, IsNil)
74 args := testibus.GetCallArgs(endp)
75 c.Assert(args, HasLen, 1)
76 c.Check(args[0].Member, Equals, "clearWakeup")
77 c.Check(args[0].Args, DeepEquals, []interface{}{"cookie"})
78 }
79
80 func (s *PdSuite) TestClearWakeupUnconfigured(c *C) {
81 err := new(powerd).ClearWakeup("cookie")
82 c.Assert(err, Equals, ErrUnconfigured)
83 }
84
85 func (s *PdSuite) TestClearWakeupFails(c *C) {
86 endp := testibus.NewTestingEndpoint(nil, condition.Work(false))
87 pd := New(endp, s.log)
88 err := pd.ClearWakeup("cookie")
89 c.Assert(err, NotNil)
90 }
91
92 func (s *PdSuite) TestRequestWakelockWorks(c *C) {
93 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), "cookie")
94 pd := New(endp, s.log)
95 ck, err := pd.RequestWakelock("name")
96 c.Assert(err, IsNil)
97 c.Check(ck, Equals, "cookie")
98 args := testibus.GetCallArgs(endp)
99 c.Assert(args, HasLen, 1)
100 // wakelocks are documented on https://wiki.ubuntu.com/powerd#API
101 c.Check(args[0].Member, Equals, "requestSysState")
102 c.Check(args[0].Args, DeepEquals, []interface{}{"name", int32(1)})
103 }
104
105 func (s *PdSuite) TestRequestWakelockUnconfigured(c *C) {
106 _, err := new(powerd).RequestWakelock("name")
107 c.Assert(err, Equals, ErrUnconfigured)
108 }
109
110 func (s *PdSuite) TestRequestWakelockFails(c *C) {
111 endp := testibus.NewTestingEndpoint(nil, condition.Work(false))
112 pd := New(endp, s.log)
113 _, err := pd.RequestWakelock("name")
114 c.Assert(err, NotNil)
115 }
116
117 func (s *PdSuite) TestClearWakelockWorks(c *C) {
118 endp := testibus.NewTestingEndpoint(nil, condition.Work(true))
119 pd := New(endp, s.log)
120 err := pd.ClearWakelock("cookie")
121 c.Assert(err, IsNil)
122 args := testibus.GetCallArgs(endp)
123 c.Assert(args, HasLen, 1)
124 c.Check(args[0].Member, Equals, "clearSysState")
125 c.Check(args[0].Args, DeepEquals, []interface{}{"cookie"})
126 }
127
128 func (s *PdSuite) TestClearWakelockUnconfigured(c *C) {
129 c.Check(new(powerd).ClearWakelock("cookie"), NotNil)
130 }
131
132 func (s *PdSuite) TestClearWakelockFails(c *C) {
133 endp := testibus.NewTestingEndpoint(nil, condition.Work(false))
134 pd := New(endp, s.log)
135 err := pd.ClearWakelock("cookie")
136 c.Assert(err, NotNil)
137 }
138
139 func (s *PdSuite) TestWatchWakeupsWorks(c *C) {
140 endp := testibus.NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{})
141 pd := New(endp, s.log)
142 ch, err := pd.WatchWakeups()
143 c.Assert(err, IsNil)
144 select {
145 case b := <-ch:
146 c.Check(b, Equals, true)
147 case <-time.After(100 * time.Millisecond):
148 c.Error("timeout waiting for bool")
149 }
150 select {
151 case b := <-ch:
152 c.Check(b, Equals, false)
153 case <-time.After(100 * time.Millisecond):
154 c.Error("timeout waiting for close")
155 }
156 }
157
158 func (s *PdSuite) TestWatchWakeupsUnconfigured(c *C) {
159 _, err := new(powerd).WatchWakeups()
160 c.Check(err, Equals, ErrUnconfigured)
161 }