summaryrefslogtreecommitdiff
path: root/src/libs/dutil/test/DUtilUnitTest/LocUtilTests.cpp
blob: c30ae2e165b672b76fe887f94c12ee25eaa16217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

#include "precomp.h"

using namespace System;
using namespace Xunit;
using namespace WixBuildTools::TestSupport;

namespace DutilTests
{
    public ref class LocUtil
    {
    public:
        [Fact]
        void CanLoadStringsWxl()
        {
            HRESULT hr = S_OK;
            WIX_LOCALIZATION* pLoc = NULL;
            LOC_STRING* pLocString = NULL;
            LPWSTR sczValue = NULL;

            DutilInitialize(&DutilTestTraceError);

            try
            {
                hr = XmlInitialize();
                NativeAssert::Succeeded(hr, "Failed to initialize Xml.");

                pin_ptr<const wchar_t> wxlFilePath = PtrToStringChars(TestData::Get("TestData", "LocUtilTests", "strings.wxl"));
                hr = LocLoadFromFile(wxlFilePath, &pLoc);
                NativeAssert::Succeeded(hr, "Failed to parse strings.wxl: {0}", wxlFilePath);

                Assert::Equal(4ul, pLoc->cLocStrings);

                hr = LocGetString(pLoc, L"#(loc.Ex1)", &pLocString);
                NativeAssert::Succeeded(hr, "Failed to get loc string 'Ex1' from: {0}", wxlFilePath);
                NativeAssert::StringEqual(L"#(loc.Ex1)", pLocString->wzId);
                NativeAssert::StringEqual(L"This is example #1", pLocString->wzText);
                NativeAssert::True(pLocString->bOverridable);

                hr = LocGetString(pLoc, L"#(loc.Ex2)", &pLocString);
                NativeAssert::Succeeded(hr, "Failed to get loc string 'Ex2' from: {0}", wxlFilePath);
                NativeAssert::StringEqual(L"#(loc.Ex2)", pLocString->wzId);
                NativeAssert::StringEqual(L"This is example #2", pLocString->wzText);
                NativeAssert::False(pLocString->bOverridable);

                hr = LocGetString(pLoc, L"#(loc.Ex3)", &pLocString);
                NativeAssert::Succeeded(hr, "Failed to get loc string 'Ex3' from: {0}", wxlFilePath);
                NativeAssert::StringEqual(L"#(loc.Ex3)", pLocString->wzId);
                NativeAssert::StringEqual(L"This is example #3", pLocString->wzText);
                NativeAssert::False(pLocString->bOverridable);

                hr = LocGetString(pLoc, L"#(loc.Ex4)", &pLocString);
                NativeAssert::Succeeded(hr, "Failed to get loc string 'Ex4' from: {0}", wxlFilePath);
                NativeAssert::StringEqual(L"#(loc.Ex4)", pLocString->wzId);
                NativeAssert::StringEqual(L"", pLocString->wzText);
                NativeAssert::False(pLocString->bOverridable);

                hr = StrAllocString(&sczValue, L"Before #(loc.Ex1) After", 0);
                NativeAssert::Succeeded(hr, "Failed to create localizable Ex1 string");

                hr = LocLocalizeString(pLoc, &sczValue);
                NativeAssert::Succeeded(hr, "Failed to localize Ex1 string using: {0}", wxlFilePath);
                NativeAssert::StringEqual(L"Before This is example #1 After", sczValue);

                hr = StrAllocString(&sczValue, L"Xxx#(loc.Ex3)yyY", 0);
                NativeAssert::Succeeded(hr, "Failed to create localizable Ex3 string");

                hr = LocLocalizeString(pLoc, &sczValue);
                NativeAssert::Succeeded(hr, "Failed to localize Ex3 string using: {0}", wxlFilePath);
                NativeAssert::StringEqual(L"XxxThis is example #3yyY", sczValue);

                hr = StrAllocString(&sczValue, L"aaa#(loc.Ex4)bbb", 0);
                NativeAssert::Succeeded(hr, "Failed to create localizable Ex4 string");

                hr = LocLocalizeString(pLoc, &sczValue);
                NativeAssert::Succeeded(hr, "Failed to localize Ex4 string using: {0}", wxlFilePath);
                NativeAssert::StringEqual(L"aaabbb", sczValue);
            }
            finally
            {
                ReleaseStr(sczValue);

                if (pLoc)
                {
                    LocFree(pLoc);
                }

                DutilUninitialize();
            }
        }

        [Fact]
        void CanLoadControlsWxl()
        {
            HRESULT hr = S_OK;
            WIX_LOCALIZATION* pLoc = NULL;
            LOC_CONTROL* pLocControl = NULL;

            DutilInitialize(&DutilTestTraceError);

            try
            {
                hr = XmlInitialize();
                NativeAssert::Succeeded(hr, "Failed to initialize Xml.");

                pin_ptr<const wchar_t> wxlFilePath = PtrToStringChars(TestData::Get("TestData", "LocUtilTests", "controls.wxl"));
                hr = LocLoadFromFile(wxlFilePath, &pLoc);
                NativeAssert::Succeeded(hr, "Failed to parse controls.wxl: {0}", wxlFilePath);

                Assert::Equal(3ul, pLoc->cLocControls);

                hr = LocGetControl(pLoc, L"Control1", &pLocControl);
                NativeAssert::Succeeded(hr, "Failed to get loc control 'Control1' from: {0}", wxlFilePath);
                NativeAssert::StringEqual(L"Control1", pLocControl->wzControl);
                NativeAssert::Equal(1, pLocControl->nX);
                NativeAssert::Equal(2, pLocControl->nY);
                NativeAssert::Equal(LOC_CONTROL_NOT_SET, pLocControl->nWidth);
                NativeAssert::Equal(LOC_CONTROL_NOT_SET, pLocControl->nHeight);
                NativeAssert::StringEqual(L"This is control #1", pLocControl->wzText);

                hr = LocGetControl(pLoc, L"Control2", &pLocControl);
                NativeAssert::Succeeded(hr, "Failed to get loc control 'Control2' from: {0}", wxlFilePath);
                NativeAssert::StringEqual(L"Control2", pLocControl->wzControl);
                NativeAssert::Equal(LOC_CONTROL_NOT_SET, pLocControl->nX);
                NativeAssert::Equal(LOC_CONTROL_NOT_SET, pLocControl->nY);
                NativeAssert::Equal(50, pLocControl->nWidth);
                NativeAssert::Equal(LOC_CONTROL_NOT_SET, pLocControl->nHeight);
                NativeAssert::StringEqual(L"This is control #2", pLocControl->wzText);

                hr = LocGetControl(pLoc, L"Control3", &pLocControl);
                NativeAssert::Succeeded(hr, "Failed to get loc control 'Control3' from: {0}", wxlFilePath);
                NativeAssert::StringEqual(L"Control3", pLocControl->wzControl);
                NativeAssert::Equal(LOC_CONTROL_NOT_SET, pLocControl->nX);
                NativeAssert::Equal(LOC_CONTROL_NOT_SET, pLocControl->nY);
                NativeAssert::Equal(LOC_CONTROL_NOT_SET, pLocControl->nWidth);
                NativeAssert::Equal(150, pLocControl->nHeight);
                NativeAssert::StringEqual(L"", pLocControl->wzText);
            }
            finally
            {
                if (pLoc)
                {
                    LocFree(pLoc);
                }

                DutilUninitialize();
            }
        }
    };
}