| 1 |
/* |
| 2 |
Copyright 2023 Guido Berhoerster <[email protected]> |
| 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 focusinfo |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
|
| 22 |
. "gopkg.in/check.v1" |
| 23 |
|
| 24 |
testibus "gitlab.com/ubports/development/core/lomiri-push-service/bus/testing" |
| 25 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 26 |
clickhelp "gitlab.com/ubports/development/core/lomiri-push-service/click/testing" |
| 27 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 28 |
"gitlab.com/ubports/development/core/lomiri-push-service/testing/condition" |
| 29 |
) |
| 30 |
|
| 31 |
func TestFocusInfo(t *testing.T) { TestingT(t) } |
| 32 |
|
| 33 |
type focusSuite struct { |
| 34 |
log *helpers.TestLogger |
| 35 |
app *click.AppId |
| 36 |
} |
| 37 |
|
| 38 |
var _ = Suite(&focusSuite{}) |
| 39 |
|
| 40 |
func (hs *focusSuite) SetUpTest(c *C) { |
| 41 |
hs.log = helpers.NewTestLogger(c, "debug") |
| 42 |
hs.app = clickhelp.MustParseAppId("com.example.test_test-app_0") |
| 43 |
GetPrimaryPID = func (appId *click.AppId) int { |
| 44 |
return 42 |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// Checks that isPidFocused() actually calls isPidFocused |
| 49 |
func (fs *focusSuite) TestGetsFocusInfo(c *C) { |
| 50 |
endp := testibus.NewTestingEndpoint(nil, condition.Work(true), true) |
| 51 |
ec := New(endp, fs.log) |
| 52 |
c.Check(ec.isPIDFocused(42), DeepEquals, true) |
| 53 |
callArgs := testibus.GetCallArgs(endp) |
| 54 |
c.Assert(callArgs, HasLen, 1) |
| 55 |
c.Check(callArgs[0].Member, Equals, "isPidFocused") |
| 56 |
c.Check(callArgs[0].Args, DeepEquals, []interface{}{uint32(42)}) |
| 57 |
} |
| 58 |
|
| 59 |
var isFocusedTests = []struct { |
| 60 |
expected bool |
| 61 |
focused bool |
| 62 |
pid int |
| 63 |
}{ |
| 64 |
{ |
| 65 |
false, |
| 66 |
false, |
| 67 |
0, // no running app |
| 68 |
}, |
| 69 |
{ |
| 70 |
true, |
| 71 |
true, |
| 72 |
42, // running app, focused |
| 73 |
}, |
| 74 |
{ |
| 75 |
false, |
| 76 |
false, |
| 77 |
42, // running app, not focused |
| 78 |
}, |
| 79 |
{ |
| 80 |
false, |
| 81 |
true, |
| 82 |
0, // invalid: no running app, but focus true |
| 83 |
}, |
| 84 |
} |
| 85 |
|
| 86 |
// Check that if the app is focused, IsAppFocused returns true |
| 87 |
func (fs *focusSuite) TestIsAppFocused(c *C) { |
| 88 |
for _, t := range isFocusedTests { |
| 89 |
endp := testibus.NewTestingEndpoint(nil, condition.Work(true), t.focused) |
| 90 |
ec := New(endp, fs.log) |
| 91 |
GetPrimaryPID = func (appId *click.AppId) int { |
| 92 |
return t.pid |
| 93 |
} |
| 94 |
c.Check(ec.IsAppFocused(fs.app), Equals, t.expected) |
| 95 |
} |
| 96 |
} |