public void UserSettingsLoadedUsingDefaultWhenMissingFile()
        {
            // For this test, we need to use the actual object. Fortunately, the path is virtual, so we
            // can override that property and force it to use an non-existent path to prove that settings
            // will be still created using defaults without the file present.
            var persisterMock = new Mock <XmlPersistanceService <GeneralSettings> >();

            persisterMock.Setup(x => x.FilePath).Returns("C:\\some\\non\\existent\\path\\rubberduck");
            persisterMock.CallBase = true;
            var configProvider = new GeneralConfigProvider(persisterMock.Object);

            var settings        = configProvider.Create();
            var defaultSettings = configProvider.CreateDefaults();

            Assert.AreEqual(defaultSettings, settings);
        }
        public void UserSettingsLoadedUsingDefaultWhenMissingFile()
        {
            var pathProviderMock = new Mock <IPersistencePathProvider>();

            pathProviderMock.Setup(x => x.DataRootPath).Returns(@"C:\some\non\existent\path\rubberduck");
            pathProviderMock.Setup(x => x.DataFolderPath(It.IsAny <string>())).Returns <string>(x => x);
            // For this test, we need to use the actual object. Fortunately, the path is virtual, so we
            // can override that property and force it to use an non-existent path to prove that settings
            // will be still created using defaults without the file present.
            var persisterMock = new Mock <XmlPersistenceService <GeneralSettings> >(pathProviderMock.Object);

            persisterMock.CallBase = true;
            var configProvider = new GeneralConfigProvider(persisterMock.Object);

            var settings        = configProvider.Read();
            var defaultSettings = configProvider.ReadDefaults();

            Assert.AreEqual(defaultSettings, settings);
        }
Exemple #3
0
        private void InitializeAddIn()
        {
            Splash splash = null;

            try
            {
                if (_isInitialized)
                {
                    // The add-in is already initialized. See:
                    // The strange case of the add-in initialized twice
                    // http://msmvps.com/blogs/carlosq/archive/2013/02/14/the-strange-case-of-the-add-in-initialized-twice.aspx
                    return;
                }

                var configLoader = new XmlPersistanceService <GeneralSettings>
                {
                    FilePath =
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                     "Rubberduck", "rubberduck.config")
                };
                var configProvider = new GeneralConfigProvider(configLoader);

                _initialSettings = configProvider.Create();
                if (_initialSettings != null)
                {
                    try
                    {
                        var cultureInfo = CultureInfo.GetCultureInfo(_initialSettings.Language.Code);
                        Dispatcher.CurrentDispatcher.Thread.CurrentUICulture = cultureInfo;
                    }
                    catch (CultureNotFoundException)
                    {
                    }

                    try
                    {
                        if (_initialSettings.SetDpiUnaware)
                        {
                            SHCore.SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.Process_DPI_Unaware);
                        }
                    }
                    catch (Exception)
                    {
                        Debug.Assert(false, "Could not set DPI awareness.");
                    }
                }
                else
                {
                    Debug.Assert(false, "Settings could not be initialized.");
                }

                if (_initialSettings?.CanShowSplash ?? false)
                {
                    splash = new Splash
                    {
                        // note: IVersionCheck.CurrentVersion could return this string.
                        Version = $"version {Assembly.GetExecutingAssembly().GetName().Version}"
                    };
                    splash.Show();
                    splash.Refresh();
                }

                Startup();
            }
            catch (Win32Exception)
            {
                System.Windows.Forms.MessageBox.Show(Resources.RubberduckUI.RubberduckReloadFailure_Message,
                                                     RubberduckUI.RubberduckReloadFailure_Title,
                                                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception exception)
            {
                _logger.Fatal(exception);
                System.Windows.Forms.MessageBox.Show(
#if DEBUG
                    exception.ToString(),
#else
                    exception.Message.ToString(),
#endif
                    RubberduckUI.RubberduckLoadFailure, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                splash?.Dispose();
            }
        }
        private void InitializeAddIn()
        {
            Splash2021 splash = null;

            try
            {
                if (_isInitialized)
                {
                    // The add-in is already initialized. See:
                    // The strange case of the add-in initialized twice
                    // http://msmvps.com/blogs/carlosq/archive/2013/02/14/the-strange-case-of-the-add-in-initialized-twice.aspx
                    return;
                }

                var pathProvider   = PersistencePathProvider.Instance;
                var configLoader   = new XmlPersistenceService <GeneralSettings>(pathProvider, _fileSystem);
                var configProvider = new GeneralConfigProvider(configLoader);

                _initialSettings = configProvider.Read();
                if (_initialSettings != null)
                {
                    try
                    {
                        var cultureInfo = CultureInfo.GetCultureInfo(_initialSettings.Language.Code);
                        Dispatcher.CurrentDispatcher.Thread.CurrentUICulture = cultureInfo;
                    }
                    catch (CultureNotFoundException)
                    {
                    }

                    try
                    {
                        if (_initialSettings.SetDpiUnaware)
                        {
                            SHCore.SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.Process_DPI_Unaware);
                        }
                    }
                    catch (Exception)
                    {
                        Debug.Assert(false, "Could not set DPI awareness.");
                    }
                }
                else
                {
                    Debug.Assert(false, "Settings could not be initialized.");
                }

                if (_initialSettings?.CanShowSplash ?? false)
                {
                    splash = new Splash2021();
                    splash.Show();
                    splash.Refresh();
                }

                Startup();
            }
            catch (Win32Exception)
            {
                System.Windows.Forms.MessageBox.Show(Resources.RubberduckUI.RubberduckReloadFailure_Message,
                                                     RubberduckUI.RubberduckReloadFailure_Title,
                                                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception exception)
            {
                _logger.Fatal(exception);
                // TODO Use Rubberduck Interaction instead and provide exception stack trace as
                // an optional "more info" collapsible section to eliminate the conditional.
                MessageBox.Show(
#if DEBUG
                    exception.ToString(),
#else
                    exception.Message.ToString(),
#endif
                    RubberduckUI.RubberduckLoadFailure, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                splash?.Dispose();
            }
        }