Skip to content

Commit

Permalink
Merge pull request #1307 from berryzplus/feature/add_try_select_profile
Browse files Browse the repository at this point in the history
プロファイルマネージャを表示するかどうかの判定をテスト可能にする
  • Loading branch information
berryzplus committed Jun 11, 2020
2 parents 96cf722 + d774d8f commit 74a0675
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 24 deletions.
28 changes: 4 additions & 24 deletions sakura_core/_main/CProcessFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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{
Expand Down
32 changes: 32 additions & 0 deletions sakura_core/dlg/CDlgProfileMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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)
{
Expand Down
4 changes: 4 additions & 0 deletions sakura_core/dlg/CDlgProfileMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#pragma once

#include "dlg/CDialog.h"
#include "_main/CCommandLine.h"
#include <string>
#include <vector>

Expand All @@ -44,6 +45,9 @@ struct SProfileSettings
class CDlgProfileMgr final : public CDialog
{
public:
//! コマンドラインだけでプロファイルが確定するか調べる
static bool TrySelectProfile( CCommandLine* pcCommandLine ) noexcept;

/*
|| Constructors
*/
Expand Down
166 changes: 166 additions & 0 deletions tests/unittests/test-cdlgprofilemgr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*! @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 <gtest/gtest.h>

#ifndef NOMINMAX
#define NOMINMAX
#endif /* #ifndef NOMINMAX */

#include <tchar.h>
#include <Windows.h>

#include "dlg/CDlgProfileMgr.h"

#include <cstdlib>
#include <filesystem>
#include <fstream>

/*!
* テスト用の極薄ラッパークラス
*/
class CCommandLineWrapper : public CCommandLine
{
public:
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::filesystem::remove( szProfileMgrIniName );
}

/*!
* テストが実行された直後に毎回呼ばれる関数
*/
virtual void TearDown() {
// プロファイル設定を削除する
std::filesystem::remove( szProfileMgrIniName );
}
};

/*!
* @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_F( CDlgProfileMgrIniTest, TrySelectProfile_003 )
{
// プロファイル設定がなかったらプロファイルは確定する
CCommandLineWrapper cCommandLine;
ASSERT_TRUE( CDlgProfileMgr::TrySelectProfile( &cCommandLine ) );
}

/*!
* @brief TrySelectProfileのテスト
*/
TEST_F( CDlgProfileMgrIniTest, 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_F( CDlgProfileMgrIniTest, 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_F( CDlgProfileMgrIniTest, 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 ) );
}
1 change: 1 addition & 0 deletions tests/unittests/tests1.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="test-ccommandline.cpp" />
<ClCompile Include="test-cdlgprofilemgr.cpp" />
<ClCompile Include="test-clayoutint.cpp" />
<ClCompile Include="test-cmemory.cpp" />
<ClCompile Include="test-cnative.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions tests/unittests/tests1.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@
<ClCompile Include="test-clayoutint.cpp">
<Filter>Test Files</Filter>
</ClCompile>
<ClCompile Include="test-cdlgprofilemgr.cpp">
<Filter>Test Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

0 comments on commit 74a0675

Please sign in to comment.