GODT-2213: Don't unnecessarily enable/disable autostart

This commit is contained in:
James Houlahan
2022-12-19 08:29:57 +00:00
committed by Romain Le Jeune
parent 3499fbd758
commit e4b81063cb
8 changed files with 43 additions and 12 deletions

View File

@ -144,3 +144,17 @@ func (mr *MockAutostarterMockRecorder) Enable() *gomock.Call {
mr.mock.ctrl.T.Helper() mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Enable", reflect.TypeOf((*MockAutostarter)(nil).Enable)) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Enable", reflect.TypeOf((*MockAutostarter)(nil).Enable))
} }
// IsEnabled mocks base method.
func (m *MockAutostarter) IsEnabled() bool {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "IsEnabled")
ret0, _ := ret[0].(bool)
return ret0
}
// IsEnabled indicates an expected call of IsEnabled.
func (mr *MockAutostarterMockRecorder) IsEnabled() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsEnabled", reflect.TypeOf((*MockAutostarter)(nil).IsEnabled))
}

View File

@ -207,15 +207,24 @@ func (bridge *Bridge) GetAutostart() bool {
} }
func (bridge *Bridge) SetAutostart(autostart bool) error { func (bridge *Bridge) SetAutostart(autostart bool) error {
if err := bridge.vault.SetAutostart(autostart); err != nil { if autostart != bridge.vault.GetAutostart() {
return err if err := bridge.vault.SetAutostart(autostart); err != nil {
return err
}
} }
var err error var err error
if autostart { if autostart {
// do nothing if already enabled
if bridge.autostarter.IsEnabled() {
return nil
}
err = bridge.autostarter.Enable() err = bridge.autostarter.Enable()
} else { } else {
// do nothing if already disabled
if !bridge.autostarter.IsEnabled() {
return nil
}
err = bridge.autostarter.Disable() err = bridge.autostarter.Disable()
} }

View File

@ -134,10 +134,19 @@ func TestBridge_Settings_Proxy(t *testing.T) {
func TestBridge_Settings_Autostart(t *testing.T) { func TestBridge_Settings_Autostart(t *testing.T) {
withEnv(t, func(ctx context.Context, s *server.Server, netCtl *proton.NetCtl, locator bridge.Locator, storeKey []byte) { withEnv(t, func(ctx context.Context, s *server.Server, netCtl *proton.NetCtl, locator bridge.Locator, storeKey []byte) {
withBridge(ctx, t, s.GetHostURL(), netCtl, locator, storeKey, func(bridge *bridge.Bridge, mocks *bridge.Mocks) { withBridge(ctx, t, s.GetHostURL(), netCtl, locator, storeKey, func(bridge *bridge.Bridge, mocks *bridge.Mocks) {
// By default, autostart is disabled. // By default, autostart is enabled.
require.True(t, bridge.GetAutostart())
// Disable autostart.
mocks.Autostarter.EXPECT().IsEnabled().Return(true)
mocks.Autostarter.EXPECT().Disable().Return(nil)
require.NoError(t, bridge.SetAutostart(false))
// Get the new setting.
require.False(t, bridge.GetAutostart()) require.False(t, bridge.GetAutostart())
// Enable autostart. // Re Enable autostart.
mocks.Autostarter.EXPECT().IsEnabled().Return(false)
mocks.Autostarter.EXPECT().Enable().Return(nil) mocks.Autostarter.EXPECT().Enable().Return(nil)
require.NoError(t, bridge.SetAutostart(true)) require.NoError(t, bridge.SetAutostart(true))

View File

@ -51,6 +51,7 @@ type TLSReporter interface {
type Autostarter interface { type Autostarter interface {
Enable() error Enable() error
Disable() error Disable() error
IsEnabled() bool
} }
type Updater interface { type Updater interface {

View File

@ -191,9 +191,6 @@ func NewService(
return s, nil return s, nil
} }
// GODT-1507 Windows: autostart needs to be created after Qt is initialized.
// GODT-1206: if preferences file says it should be on enable it here.
// TO-DO GODT-1681 Autostart needs to be properly implement for gRPC approach.
func (s *Service) initAutostart() { func (s *Service) initAutostart() {
s.firstTimeAutostart.Do(func() { s.firstTimeAutostart.Do(func() {
shouldAutostartBeOn := s.bridge.GetAutostart() shouldAutostartBeOn := s.bridge.GetAutostart()

View File

@ -152,13 +152,13 @@ func TestVault_Settings_Autostart(t *testing.T) {
s := newVault(t) s := newVault(t)
// Check the default autostart setting. // Check the default autostart setting.
require.Equal(t, false, s.GetAutostart()) require.Equal(t, true, s.GetAutostart())
// Modify the autostart setting. // Modify the autostart setting.
require.NoError(t, s.SetAutostart(true)) require.NoError(t, s.SetAutostart(false))
// Check the new autostart setting. // Check the new autostart setting.
require.Equal(t, true, s.GetAutostart()) require.Equal(t, false, s.GetAutostart())
} }
func TestVault_Settings_AutoUpdate(t *testing.T) { func TestVault_Settings_AutoUpdate(t *testing.T) {

View File

@ -78,7 +78,7 @@ func newDefaultSettings(gluonDir string) Settings {
ColorScheme: "", ColorScheme: "",
ProxyAllowed: true, ProxyAllowed: true,
ShowAllMail: true, ShowAllMail: true,
Autostart: false, Autostart: true,
AutoUpdate: true, AutoUpdate: true,
LastVersion: "0.0.0", LastVersion: "0.0.0",

View File

@ -194,6 +194,7 @@ func (t *testCtx) initFrontendService(eventCh <-chan events.Event) error {
// When starting the frontend, we might enable autostart on bridge if it isn't already. // When starting the frontend, we might enable autostart on bridge if it isn't already.
t.mocks.Autostarter.EXPECT().Enable().AnyTimes() t.mocks.Autostarter.EXPECT().Enable().AnyTimes()
t.mocks.Autostarter.EXPECT().IsEnabled().AnyTimes()
service, err := frontend.NewService( service, err := frontend.NewService(
new(mockCrashHandler), new(mockCrashHandler),