1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include <windows.h>
31 
32 #include <string>
33 
34 #include "base/util.h"
35 #include "protocol/renderer_command.pb.h"
36 #include "renderer/win32/win32_font_util.h"
37 #include "testing/base/public/googletest.h"
38 #include "testing/base/public/gunit.h"
39 
40 namespace mozc {
41 namespace win32 {
TEST(FontUtilTest,ToLOGFONTWithTooLongFaceName)42 TEST(FontUtilTest, ToLOGFONTWithTooLongFaceName) {
43   LOGFONT log_font = {
44     18,
45     11,
46     2700,
47     1800,
48     FW_NORMAL,
49     TRUE,
50     FALSE,
51     TRUE,
52     SYMBOL_CHARSET,
53     OUT_DEFAULT_PRECIS,
54     CLIP_DEFAULT_PRECIS,
55     ANTIALIASED_QUALITY,
56     FF_SCRIPT,
57     L"" };
58 
59   // make a string which is not null-terminated.
60   for (size_t i = 0; i < arraysize(log_font.lfFaceName); ++i) {
61     log_font.lfFaceName[i] = L' ';
62   }
63 
64   // Conversion should fail.
65   mozc::commands::RendererCommand::WinLogFont win_log_font;
66   EXPECT_FALSE(FontUtil::ToWinLogFont(log_font, &win_log_font));
67 }
68 
TEST(FontUtilTest,ToWinLogFontWithTooLongFaceName)69 TEST(FontUtilTest, ToWinLogFontWithTooLongFaceName) {
70   mozc::commands::RendererCommand::WinLogFont win_log_font;
71   win_log_font.set_height(18);
72   win_log_font.set_width(11);
73   win_log_font.set_escapement(2700);
74   win_log_font.set_orientation(1800);
75   win_log_font.set_weight(FW_NORMAL);
76   win_log_font.set_italic(true);
77   win_log_font.set_underline(false);
78   win_log_font.set_strike_out(true);
79   win_log_font.set_char_set(SYMBOL_CHARSET);
80   win_log_font.set_out_precision(OUT_DEFAULT_PRECIS);
81   win_log_font.set_clip_precision(CLIP_DEFAULT_PRECIS);
82   win_log_font.set_quality(ANTIALIASED_QUALITY);
83   win_log_font.set_pitch_and_family(FF_SCRIPT);
84 
85   LOGFONT log_font = { };
86   std::wstring too_long_face_name(L' ', arraysize(log_font.lfFaceName));
87   string face_name;
88   mozc::Util::WideToUTF8(too_long_face_name, &face_name);
89   win_log_font.set_face_name(face_name);
90 
91   // Conversion should fail.
92   EXPECT_FALSE(FontUtil::ToLOGFONT(win_log_font, &log_font));
93 }
94 
TEST(FontUtilTest,RoundtripToWinLogFont)95 TEST(FontUtilTest, RoundtripToWinLogFont) {
96   const LOGFONT original = {
97     18,
98     11,
99     2700,
100     1800,
101     FW_NORMAL,
102     TRUE,
103     FALSE,
104     TRUE,
105     SYMBOL_CHARSET,
106     OUT_DEFAULT_PRECIS,
107     CLIP_DEFAULT_PRECIS,
108     ANTIALIASED_QUALITY,
109     FF_SCRIPT,
110     L"MS Sans Serif" };
111 
112   mozc::commands::RendererCommand::WinLogFont win_log_font;
113   EXPECT_TRUE(FontUtil::ToWinLogFont(original, &win_log_font));
114 
115   LOGFONT result = { };
116   EXPECT_TRUE(FontUtil::ToLOGFONT(win_log_font, &result));
117 
118   EXPECT_EQ(original.lfHeight, result.lfHeight);
119   EXPECT_EQ(original.lfWidth, result.lfWidth);
120   EXPECT_EQ(original.lfEscapement, result.lfEscapement);
121   EXPECT_EQ(original.lfOrientation, result.lfOrientation);
122   EXPECT_EQ(original.lfWeight, result.lfWeight);
123   EXPECT_EQ(original.lfItalic, result.lfItalic);
124   EXPECT_EQ(original.lfUnderline, result.lfUnderline);
125   EXPECT_EQ(original.lfStrikeOut, result.lfStrikeOut);
126   EXPECT_EQ(original.lfCharSet, result.lfCharSet);
127   EXPECT_EQ(original.lfOutPrecision, result.lfOutPrecision);
128   EXPECT_EQ(original.lfClipPrecision, result.lfClipPrecision);
129   EXPECT_EQ(original.lfQuality, result.lfQuality);
130   EXPECT_EQ(original.lfPitchAndFamily, result.lfPitchAndFamily);
131   EXPECT_STREQ(original.lfFaceName, result.lfFaceName);
132 }
133 
TEST(FontUtilTest,RoundtripToLOGFONT)134 TEST(FontUtilTest, RoundtripToLOGFONT) {
135   mozc::commands::RendererCommand::WinLogFont original;
136   original.set_height(18);
137   original.set_width(11);
138   original.set_escapement(2700);
139   original.set_orientation(1800);
140   original.set_weight(FW_NORMAL);
141   original.set_italic(true);
142   original.set_underline(false);
143   original.set_strike_out(true);
144   original.set_char_set(SYMBOL_CHARSET);
145   original.set_out_precision(OUT_DEFAULT_PRECIS);
146   original.set_clip_precision(CLIP_DEFAULT_PRECIS);
147   original.set_quality(ANTIALIASED_QUALITY);
148   original.set_pitch_and_family(FF_SCRIPT);
149   string face_name;
150   mozc::Util::WideToUTF8(L"MS Sans Serif", &face_name);
151   original.set_face_name(face_name);
152 
153   LOGFONT log_font = { };
154   EXPECT_TRUE(FontUtil::ToLOGFONT(original, &log_font));
155 
156   mozc::commands::RendererCommand::WinLogFont result;
157   EXPECT_TRUE(FontUtil::ToWinLogFont(log_font, &result));
158 
159   EXPECT_EQ(original.height(), result.height());
160   EXPECT_EQ(original.width(), result.width());
161   EXPECT_EQ(original.escapement(), result.escapement());
162   EXPECT_EQ(original.orientation(), result.orientation());
163   EXPECT_EQ(original.weight(), result.weight());
164   EXPECT_EQ(original.italic(), result.italic());
165   EXPECT_EQ(original.underline(), result.underline());
166   EXPECT_EQ(original.strike_out(), result.strike_out());
167   EXPECT_EQ(original.char_set(), result.char_set());
168   EXPECT_EQ(original.out_precision(), result.out_precision());
169   EXPECT_EQ(original.clip_precision(), result.clip_precision());
170   EXPECT_EQ(original.quality(), result.quality());
171   EXPECT_EQ(original.pitch_and_family(), result.pitch_and_family());
172   EXPECT_EQ(original.face_name(), result.face_name());
173 }
174 }  // namespace win32
175 }  // namespace mozc
176