From d0850405e3230685be31ef9a10502bf787f6c9f8 Mon Sep 17 00:00:00 2001 From: berryplus Date: Sat, 16 May 2020 21:00:25 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E3=83=97=E3=83=AD=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=A4=E3=83=AB=E3=83=9E=E3=83=8D=E3=83=BC=E3=82=B8=E3=83=A3?= =?UTF-8?q?=E3=82=92=E8=A1=A8=E7=A4=BA=E3=81=99=E3=82=8B=E3=81=8B=E3=81=A9?= =?UTF-8?q?=E3=81=86=E3=81=8B=E3=81=AE=E5=88=A4=E5=AE=9A=E3=82=92=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E5=8F=AF=E8=83=BD=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/_main/CProcessFactory.cpp | 28 +---- sakura_core/dlg/CDlgProfileMgr.cpp | 32 ++++++ sakura_core/dlg/CDlgProfileMgr.h | 4 + tests/unittests/test-cdlgprofilemgr.cpp | 134 ++++++++++++++++++++++++ tests/unittests/tests1.vcxproj | 1 + tests/unittests/tests1.vcxproj.filters | 3 + 6 files changed, 178 insertions(+), 24 deletions(-) create mode 100644 tests/unittests/test-cdlgprofilemgr.cpp diff --git a/sakura_core/_main/CProcessFactory.cpp b/sakura_core/_main/CProcessFactory.cpp index 06861fb67d..b841c8e2f9 100644 --- a/sakura_core/_main/CProcessFactory.cpp +++ b/sakura_core/_main/CProcessFactory.cpp @@ -83,13 +83,6 @@ CProcess* CProcessFactory::Create( HINSTANCE hInstance, LPCWSTR lpCmdLine ) bool CProcessFactory::ProfileSelect( HINSTANCE hInstance, LPCWSTR lpCmdLine ) { - CDlgProfileMgr dlgProf; - SProfileSettings settings; - - CDlgProfileMgr::ReadProfSettings( settings ); - CSelectLang::InitializeLanguageEnvironment(); - CSelectLang::ChangeLang( settings.m_szDllLanguage ); - // May 30, 2000 genta // 実行ファイル名をもとに漢字コードを固定する. WCHAR szExeFileName[MAX_PATH]; @@ -98,23 +91,10 @@ bool CProcessFactory::ProfileSelect( HINSTANCE hInstance, LPCWSTR lpCmdLine ) CCommandLine::getInstance()->ParseCommandLine(lpCmdLine); - bool bDialog; - if( CCommandLine::getInstance()->IsProfileMgr() ){ - bDialog = true; - }else if( CCommandLine::getInstance()->IsSetProfile() ){ - bDialog = false; - }else if( settings.m_nDefaultIndex == -1 ){ - bDialog = true; - }else{ - assert( 0 <= settings.m_nDefaultIndex ); - if( 0 < settings.m_nDefaultIndex ){ - CCommandLine::getInstance()->SetProfileName( settings.m_vProfList[settings.m_nDefaultIndex - 1].c_str() ); - }else{ - CCommandLine::getInstance()->SetProfileName( L"" ); - } - bDialog = false; - } - if( bDialog ){ + // コマンドラインオプションから起動プロファイルを判定する + bool profileSelected = CDlgProfileMgr::TrySelectProfile( CCommandLine::getInstance() ); + if( !profileSelected ){ + CDlgProfileMgr dlgProf; if( dlgProf.DoModal( hInstance, NULL, 0 ) ){ CCommandLine::getInstance()->SetProfileName( dlgProf.m_strProfileName.c_str() ); }else{ diff --git a/sakura_core/dlg/CDlgProfileMgr.cpp b/sakura_core/dlg/CDlgProfileMgr.cpp index 4a4300ac54..22abeba97c 100644 --- a/sakura_core/dlg/CDlgProfileMgr.cpp +++ b/sakura_core/dlg/CDlgProfileMgr.cpp @@ -51,6 +51,38 @@ const DWORD p_helpids[] = { 0, 0 }; +//! コマンドラインだけでプロファイルが確定するか調べる +bool CDlgProfileMgr::TrySelectProfile( CCommandLine* pcCommandLine ) noexcept +{ + SProfileSettings settings; + bool bSettingLoaded = ReadProfSettings( settings ); + + bool bDialog; + if( pcCommandLine->IsProfileMgr() ){ // コマンドラインでプロファイルマネージャの表示が指定されている + bDialog = true; + }else if( pcCommandLine->IsSetProfile() ){ // コマンドラインでプロファイル名が指定されている + bDialog = false; + }else if( !bSettingLoaded ){ // プロファイル設定がなかった + bDialog = false; + }else if( 0 < settings.m_nDefaultIndex && settings.m_nDefaultIndex <= static_cast(settings.m_vProfList.size()) ){ + // プロファイル設定のデフォルトインデックス値から該当のプロファイル名が指定されたものとして動作する + pcCommandLine->SetProfileName( settings.m_vProfList[settings.m_nDefaultIndex - 1].c_str() ); + bDialog = false; + }else{ + // プロファイル設定のデフォルトインデックス値が不正なのでプロファイルマネージャを表示して設定更新を促す + bDialog = true; + } + if( bDialog ){ + // プロファイルマネージャを表示する場合、言語環境の初期化を済ませておく + CSelectLang::InitializeLanguageEnvironment(); + if( bSettingLoaded ){ + // 設定が読めた場合のみ、日本語以外の設定言語を適用する + CSelectLang::ChangeLang( settings.m_szDllLanguage ); + } + } + return !bDialog; +} + CDlgProfileMgr::CDlgProfileMgr() : CDialog(false, false) { diff --git a/sakura_core/dlg/CDlgProfileMgr.h b/sakura_core/dlg/CDlgProfileMgr.h index dd9f143708..b302fce149 100644 --- a/sakura_core/dlg/CDlgProfileMgr.h +++ b/sakura_core/dlg/CDlgProfileMgr.h @@ -30,6 +30,7 @@ #pragma once #include "dlg/CDialog.h" +#include "_main/CCommandLine.h" #include #include @@ -44,6 +45,9 @@ struct SProfileSettings class CDlgProfileMgr final : public CDialog { public: + //! コマンドラインだけでプロファイルが確定するか調べる + static bool TrySelectProfile( CCommandLine* pcCommandLine ) noexcept; + /* || Constructors */ diff --git a/tests/unittests/test-cdlgprofilemgr.cpp b/tests/unittests/test-cdlgprofilemgr.cpp new file mode 100644 index 0000000000..95f97c8834 --- /dev/null +++ b/tests/unittests/test-cdlgprofilemgr.cpp @@ -0,0 +1,134 @@ +/*! @file */ +/* + Copyright (C) 2018-2020 Sakura Editor Organization + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; + you must not claim that you wrote the original software. + If you use this software in a product, an acknowledgment + in the product documentation would be appreciated but is + not required. + + 2. Altered source versions must be plainly marked as such, + and must not be misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. +*/ +#include + +#ifndef NOMINMAX +#define NOMINMAX +#endif /* #ifndef NOMINMAX */ + +#include +#include + +#include "dlg/CDlgProfileMgr.h" + +#include +#include + +/*! + * テスト用の極薄ラッパークラス + */ +class CCommandLineWrapper : public CCommandLine +{ +public: + CCommandLineWrapper() = default; +}; + +/*! + * @brief TrySelectProfileのテスト + */ +TEST(CDlgProfileMgr, TrySelectProfile_001 ) +{ + // プロファイルマネージャ表示オプションが付いてたらプロファイルは確定しない + CCommandLineWrapper cCommandLine; + cCommandLine.ParseCommandLine( L"-PROFMGR", false ); + ASSERT_FALSE( CDlgProfileMgr::TrySelectProfile( &cCommandLine ) ); +} + +/*! + * @brief TrySelectProfileのテスト + */ +TEST( CDlgProfileMgr, TrySelectProfile_002 ) +{ + // プロファイル名が指定されていたらプロファイルは確定する + CCommandLineWrapper cCommandLine; + cCommandLine.ParseCommandLine( L"-PROF=執筆用", false ); + ASSERT_TRUE( CDlgProfileMgr::TrySelectProfile( &cCommandLine ) ); +} + +/*! + * @brief TrySelectProfileのテスト + */ +TEST( CDlgProfileMgr, TrySelectProfile_003 ) +{ + // プロファイル設定を削除する + std::remove( "tests1_prof.ini" ); + + // プロファイル設定がなかったらプロファイルは確定する + CCommandLineWrapper cCommandLine; + ASSERT_TRUE( CDlgProfileMgr::TrySelectProfile( &cCommandLine ) ); +} + +/*! + * @brief TrySelectProfileのテスト + */ +TEST( CDlgProfileMgr, TrySelectProfile_004 ) +{ + // プロファイル設定を作る + SProfileSettings settings; + settings.m_szDllLanguage[0] = L'\0'; + settings.m_nDefaultIndex = 3; + settings.m_vProfList = { L"保存用", L"鑑賞用", L"使用用" }; + settings.m_bDefaultSelect = true; + CDlgProfileMgr::WriteProfSettings( settings ); + + // プロファイル設定にデフォルト定義があればプロファイルは確定する + CCommandLineWrapper cCommandLine; + ASSERT_TRUE( CDlgProfileMgr::TrySelectProfile( &cCommandLine ) ); +} + +/*! + * @brief TrySelectProfileのテスト + */ +TEST( CDlgProfileMgr, TrySelectProfile_005 ) +{ + // プロファイル設定を作る + SProfileSettings settings; + settings.m_szDllLanguage[0] = L'\0'; + settings.m_nDefaultIndex = 4; + settings.m_vProfList = { L"保存用", L"鑑賞用", L"使用用" }; + settings.m_bDefaultSelect = true; + CDlgProfileMgr::WriteProfSettings( settings ); + + // プロファイル設定にデフォルト定義がおかしればプロファイルは確定しない + CCommandLineWrapper cCommandLine; + ASSERT_FALSE( CDlgProfileMgr::TrySelectProfile( &cCommandLine ) ); +} + +/*! + * @brief TrySelectProfileのテスト + */ +TEST( CDlgProfileMgr, TrySelectProfile_006 ) +{ + // 空のプロファイル設定を作る + SProfileSettings settings; + settings.m_szDllLanguage[0] = L'\0'; + settings.m_nDefaultIndex = -1; + settings.m_bDefaultSelect = false; + CDlgProfileMgr::WriteProfSettings( settings ); + + // プロファイル設定が空定義ならプロファイルは確定しない + CCommandLineWrapper cCommandLine; + ASSERT_FALSE( CDlgProfileMgr::TrySelectProfile( &cCommandLine ) ); +} diff --git a/tests/unittests/tests1.vcxproj b/tests/unittests/tests1.vcxproj index e2661f6600..295ca08889 100644 --- a/tests/unittests/tests1.vcxproj +++ b/tests/unittests/tests1.vcxproj @@ -106,6 +106,7 @@ + diff --git a/tests/unittests/tests1.vcxproj.filters b/tests/unittests/tests1.vcxproj.filters index 50805feae4..1c19c8427b 100644 --- a/tests/unittests/tests1.vcxproj.filters +++ b/tests/unittests/tests1.vcxproj.filters @@ -65,5 +65,8 @@ Test Files + + Test Files + \ No newline at end of file From a40a52c28ea3d510970dbac15f27414411d85cd2 Mon Sep 17 00:00:00 2001 From: berryplus Date: Mon, 18 May 2020 01:29:08 +0900 Subject: [PATCH 2/3] =?UTF-8?q?INI=E3=82=92=E4=BD=BF=E3=81=86=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=81=AESetUp/TearDown=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unittests/test-cdlgprofilemgr.cpp | 45 +++++++++++++++++++++---- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/tests/unittests/test-cdlgprofilemgr.cpp b/tests/unittests/test-cdlgprofilemgr.cpp index 95f97c8834..1ff1aed86b 100644 --- a/tests/unittests/test-cdlgprofilemgr.cpp +++ b/tests/unittests/test-cdlgprofilemgr.cpp @@ -45,6 +45,40 @@ class CCommandLineWrapper : public CCommandLine CCommandLineWrapper() = default; }; +/*! + * プロファイルマネージャ設定ファイルを使うテストのためのフィクスチャクラス + * + * 設定ファイルを使うテストは「設定ファイルがない状態」からの始動を想定しているので + * 始動前に設定ファイルを削除するようにしている。 + * テスト実行後に設定ファイルを残しておく意味はないので終了後も削除している。 + */ +class CDlgProfileMgrIniTest : public ::testing::Test { +protected: + /*! + * プロファイルマネージャ設定ファイルの名前 + * + * この名前は "%s_prof.ini" に 実行ファイル名 を埋め込んで生成される。 + * 実稼働環境では "sakura_prof.ini" となることに注意。 + */ + static constexpr const char szProfileMgrIniName[] = "tests1_prof.ini"; + + /*! + * テストが起動される直前に毎回呼ばれる関数 + */ + virtual void SetUp() { + // プロファイル設定を削除する + std::remove( szProfileMgrIniName ); + } + + /*! + * テストが実行された直後に毎回呼ばれる関数 + */ + virtual void TearDown() { + // プロファイル設定を削除する + std::remove( szProfileMgrIniName ); + } +}; + /*! * @brief TrySelectProfileのテスト */ @@ -70,11 +104,8 @@ TEST( CDlgProfileMgr, TrySelectProfile_002 ) /*! * @brief TrySelectProfileのテスト */ -TEST( CDlgProfileMgr, TrySelectProfile_003 ) +TEST_F( CDlgProfileMgrIniTest, TrySelectProfile_003 ) { - // プロファイル設定を削除する - std::remove( "tests1_prof.ini" ); - // プロファイル設定がなかったらプロファイルは確定する CCommandLineWrapper cCommandLine; ASSERT_TRUE( CDlgProfileMgr::TrySelectProfile( &cCommandLine ) ); @@ -83,7 +114,7 @@ TEST( CDlgProfileMgr, TrySelectProfile_003 ) /*! * @brief TrySelectProfileのテスト */ -TEST( CDlgProfileMgr, TrySelectProfile_004 ) +TEST_F( CDlgProfileMgrIniTest, TrySelectProfile_004 ) { // プロファイル設定を作る SProfileSettings settings; @@ -101,7 +132,7 @@ TEST( CDlgProfileMgr, TrySelectProfile_004 ) /*! * @brief TrySelectProfileのテスト */ -TEST( CDlgProfileMgr, TrySelectProfile_005 ) +TEST_F( CDlgProfileMgrIniTest, TrySelectProfile_005 ) { // プロファイル設定を作る SProfileSettings settings; @@ -119,7 +150,7 @@ TEST( CDlgProfileMgr, TrySelectProfile_005 ) /*! * @brief TrySelectProfileのテスト */ -TEST( CDlgProfileMgr, TrySelectProfile_006 ) +TEST_F( CDlgProfileMgrIniTest, TrySelectProfile_006 ) { // 空のプロファイル設定を作る SProfileSettings settings; From d774d8fcc6ad2dd5ae8e0335ece97a9743e010f0 Mon Sep 17 00:00:00 2001 From: berryplus Date: Mon, 18 May 2020 01:46:11 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E5=89=8A=E9=99=A4=E3=81=AB=E4=BD=BF=E3=81=86=E9=96=A2=E6=95=B0?= =?UTF-8?q?=E3=82=92=E5=89=8A=E9=99=A4=E4=BE=8B=E5=A4=96=E3=81=8C=E5=87=BA?= =?UTF-8?q?=E3=82=8B=E3=82=82=E3=81=AE=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/unittests/test-cdlgprofilemgr.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unittests/test-cdlgprofilemgr.cpp b/tests/unittests/test-cdlgprofilemgr.cpp index 1ff1aed86b..0e6e5c123e 100644 --- a/tests/unittests/test-cdlgprofilemgr.cpp +++ b/tests/unittests/test-cdlgprofilemgr.cpp @@ -34,6 +34,7 @@ #include "dlg/CDlgProfileMgr.h" #include +#include #include /*! @@ -67,7 +68,7 @@ class CDlgProfileMgrIniTest : public ::testing::Test { */ virtual void SetUp() { // プロファイル設定を削除する - std::remove( szProfileMgrIniName ); + std::filesystem::remove( szProfileMgrIniName ); } /*! @@ -75,7 +76,7 @@ class CDlgProfileMgrIniTest : public ::testing::Test { */ virtual void TearDown() { // プロファイル設定を削除する - std::remove( szProfileMgrIniName ); + std::filesystem::remove( szProfileMgrIniName ); } };