diff --git a/internal/bridge/mocks/mocks.go b/internal/bridge/mocks/mocks.go index 71e861dc..56d89368 100644 --- a/internal/bridge/mocks/mocks.go +++ b/internal/bridge/mocks/mocks.go @@ -144,3 +144,17 @@ func (mr *MockAutostarterMockRecorder) Enable() *gomock.Call { mr.mock.ctrl.T.Helper() 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)) +} diff --git a/internal/bridge/settings.go b/internal/bridge/settings.go index 137b4ee9..a459c0ae 100644 --- a/internal/bridge/settings.go +++ b/internal/bridge/settings.go @@ -207,15 +207,24 @@ func (bridge *Bridge) GetAutostart() bool { } func (bridge *Bridge) SetAutostart(autostart bool) error { - if err := bridge.vault.SetAutostart(autostart); err != nil { - return err + if autostart != bridge.vault.GetAutostart() { + if err := bridge.vault.SetAutostart(autostart); err != nil { + return err + } } var err error - if autostart { + // do nothing if already enabled + if bridge.autostarter.IsEnabled() { + return nil + } err = bridge.autostarter.Enable() } else { + // do nothing if already disabled + if !bridge.autostarter.IsEnabled() { + return nil + } err = bridge.autostarter.Disable() } diff --git a/internal/bridge/settings_test.go b/internal/bridge/settings_test.go index 7bd95911..d3e438a1 100644 --- a/internal/bridge/settings_test.go +++ b/internal/bridge/settings_test.go @@ -134,10 +134,19 @@ func TestBridge_Settings_Proxy(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) { 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()) - // Enable autostart. + // Re Enable autostart. + mocks.Autostarter.EXPECT().IsEnabled().Return(false) mocks.Autostarter.EXPECT().Enable().Return(nil) require.NoError(t, bridge.SetAutostart(true)) diff --git a/internal/bridge/types.go b/internal/bridge/types.go index f9aa85b2..6f177772 100644 --- a/internal/bridge/types.go +++ b/internal/bridge/types.go @@ -51,6 +51,7 @@ type TLSReporter interface { type Autostarter interface { Enable() error Disable() error + IsEnabled() bool } type Updater interface { diff --git a/internal/frontend/grpc/service.go b/internal/frontend/grpc/service.go index 43c54a12..7d8c23fe 100644 --- a/internal/frontend/grpc/service.go +++ b/internal/frontend/grpc/service.go @@ -191,9 +191,6 @@ func NewService( 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() { s.firstTimeAutostart.Do(func() { shouldAutostartBeOn := s.bridge.GetAutostart() diff --git a/internal/vault/settings_test.go b/internal/vault/settings_test.go index 365912b6..968400ce 100644 --- a/internal/vault/settings_test.go +++ b/internal/vault/settings_test.go @@ -152,13 +152,13 @@ func TestVault_Settings_Autostart(t *testing.T) { s := newVault(t) // Check the default autostart setting. - require.Equal(t, false, s.GetAutostart()) + require.Equal(t, true, s.GetAutostart()) // Modify the autostart setting. - require.NoError(t, s.SetAutostart(true)) + require.NoError(t, s.SetAutostart(false)) // Check the new autostart setting. - require.Equal(t, true, s.GetAutostart()) + require.Equal(t, false, s.GetAutostart()) } func TestVault_Settings_AutoUpdate(t *testing.T) { diff --git a/internal/vault/types_settings.go b/internal/vault/types_settings.go index 540a80b6..7e94af38 100644 --- a/internal/vault/types_settings.go +++ b/internal/vault/types_settings.go @@ -78,7 +78,7 @@ func newDefaultSettings(gluonDir string) Settings { ColorScheme: "", ProxyAllowed: true, ShowAllMail: true, - Autostart: false, + Autostart: true, AutoUpdate: true, LastVersion: "0.0.0", diff --git a/tests/ctx_bridge_test.go b/tests/ctx_bridge_test.go index e68ac08e..4ccc84b7 100644 --- a/tests/ctx_bridge_test.go +++ b/tests/ctx_bridge_test.go @@ -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. t.mocks.Autostarter.EXPECT().Enable().AnyTimes() + t.mocks.Autostarter.EXPECT().IsEnabled().AnyTimes() service, err := frontend.NewService( new(mockCrashHandler),