wineps.drv: Slightly simplify a PPD parser code snippet.
[wine] / dlls / dwrite / tests / layout.c
1 /*
2  *    Text layout/format tests
3  *
4  * Copyright 2012 Nikolay Sivov for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22
23 #include "windows.h"
24 #include "dwrite.h"
25
26 #include "wine/test.h"
27
28 static IDWriteFactory *factory;
29
30 static const WCHAR tahomaW[] = {'T','a','h','o','m','a',0};
31 static const WCHAR enusW[] = {'e','n','-','u','s',0};
32
33 #define EXPECT_REF(obj,ref) _expect_ref((IUnknown*)obj, ref, __LINE__)
34 static void _expect_ref(IUnknown* obj, ULONG ref, int line)
35 {
36     ULONG rc = IUnknown_AddRef(obj);
37     IUnknown_Release(obj);
38     ok_(__FILE__,line)(rc-1 == ref, "expected refcount %d, got %d\n", ref, rc-1);
39 }
40
41 static void test_CreateTextLayout(void)
42 {
43     static const WCHAR strW[] = {'s','t','r','i','n','g',0};
44     IDWriteTextLayout *layout;
45     HRESULT hr;
46
47     hr = IDWriteFactory_CreateTextLayout(factory, NULL, 0, NULL, 0.0, 0.0, &layout);
48     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
49
50     hr = IDWriteFactory_CreateTextLayout(factory, strW, 6, NULL, 0.0, 0.0, &layout);
51     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
52
53     hr = IDWriteFactory_CreateTextLayout(factory, strW, 6, NULL, 1.0, 0.0, &layout);
54     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
55
56     hr = IDWriteFactory_CreateTextLayout(factory, strW, 6, NULL, 0.0, 1.0, &layout);
57     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
58
59     hr = IDWriteFactory_CreateTextLayout(factory, strW, 6, NULL, 1000.0, 1000.0, &layout);
60     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
61 }
62
63 static void test_CreateGdiCompatibleTextLayout(void)
64 {
65     static const WCHAR strW[] = {'s','t','r','i','n','g',0};
66     IDWriteTextLayout *layout;
67     IDWriteTextFormat *format;
68     HRESULT hr;
69
70     hr = IDWriteFactory_CreateGdiCompatibleTextLayout(factory, NULL, 0, NULL, 0.0, 0.0, 0.0, NULL, FALSE, &layout);
71     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
72
73     hr = IDWriteFactory_CreateGdiCompatibleTextLayout(factory, strW, 6, NULL, 0.0, 0.0, 0.0, NULL, FALSE, &layout);
74     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
75
76     hr = IDWriteFactory_CreateGdiCompatibleTextLayout(factory, strW, 6, NULL, 1.0, 0.0, 0.0, NULL, FALSE, &layout);
77     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
78
79     hr = IDWriteFactory_CreateGdiCompatibleTextLayout(factory, strW, 6, NULL, 1.0, 0.0, 1.0, NULL, FALSE, &layout);
80     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
81
82     hr = IDWriteFactory_CreateGdiCompatibleTextLayout(factory, strW, 6, NULL, 1000.0, 1000.0, 1.0, NULL, FALSE, &layout);
83     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
84
85     /* create with text format */
86     hr = IDWriteFactory_CreateTextFormat(factory, tahomaW, NULL, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL,
87         DWRITE_FONT_STRETCH_NORMAL, 10.0, enusW, &format);
88     ok(hr == S_OK, "got 0x%08x\n", hr);
89     EXPECT_REF(format, 1);
90
91     hr = IDWriteFactory_CreateGdiCompatibleTextLayout(factory, strW, 6, format, 100.0, 100.0, 1.0, NULL, FALSE, &layout);
92     ok(hr == S_OK, "got 0x%08x\n", hr);
93     EXPECT_REF(format, 1);
94     EXPECT_REF(layout, 1);
95
96     IDWriteTextLayout_AddRef(layout);
97     EXPECT_REF(format, 1);
98     EXPECT_REF(layout, 2);
99     IDWriteTextLayout_Release(layout);
100     IDWriteTextLayout_Release(layout);
101
102     /* zero length string is okay */
103     hr = IDWriteFactory_CreateGdiCompatibleTextLayout(factory, strW, 0, format, 100.0, 100.0, 1.0, NULL, FALSE, &layout);
104     ok(hr == S_OK, "got 0x%08x\n", hr);
105     IDWriteTextLayout_Release(layout);
106
107     IDWriteTextFormat_Release(format);
108 }
109
110 static void test_CreateTextFormat(void)
111 {
112     IDWriteFontCollection *collection, *syscoll;
113     IDWriteTextFormat *format;
114     HRESULT hr;
115
116     hr = IDWriteFactory_CreateTextFormat(factory, tahomaW, NULL, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL,
117         DWRITE_FONT_STRETCH_NORMAL, 10.0, enusW, &format);
118     ok(hr == S_OK, "got 0x%08x\n", hr);
119
120 if (0) /* crashes on native */
121     hr = IDWriteTextFormat_GetFontCollection(format, NULL);
122
123     collection = NULL;
124     hr = IDWriteTextFormat_GetFontCollection(format, &collection);
125     ok(hr == S_OK, "got 0x%08x\n", hr);
126     ok(collection != NULL, "got %p\n", collection);
127
128     hr = IDWriteFactory_GetSystemFontCollection(factory, &syscoll, FALSE);
129     ok(hr == S_OK, "got 0x%08x\n", hr);
130     ok(collection == syscoll, "got %p, was %p\n", syscoll, collection);
131     IDWriteFontCollection_Release(syscoll);
132     IDWriteFontCollection_Release(collection);
133
134     IDWriteTextFormat_Release(format);
135 }
136
137 static void test_GetLocaleName(void)
138 {
139     static const WCHAR strW[] = {'s','t','r','i','n','g',0};
140     static const WCHAR ruW[] = {'r','u',0};
141     IDWriteTextLayout *layout;
142     IDWriteTextFormat *format;
143     WCHAR buff[10];
144     UINT32 len;
145     HRESULT hr;
146
147     hr = IDWriteFactory_CreateTextFormat(factory, tahomaW, NULL, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL,
148         DWRITE_FONT_STRETCH_NORMAL, 10.0, ruW, &format);
149     ok(hr == S_OK, "got 0x%08x\n", hr);
150
151     hr = IDWriteFactory_CreateGdiCompatibleTextLayout(factory, strW, 0, format, 100.0, 100.0, 1.0, NULL, FALSE, &layout);
152     ok(hr == S_OK, "got 0x%08x\n", hr);
153     len = IDWriteTextLayout_GetLocaleNameLength(layout);
154     ok(len == 2, "got %u\n", len);
155     len = IDWriteTextFormat_GetLocaleNameLength(format);
156     ok(len == 2, "got %u\n", len);
157     hr = IDWriteTextLayout_GetLocaleName(layout, buff, len);
158     ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);
159     hr = IDWriteTextLayout_GetLocaleName(layout, buff, len+1);
160     ok(hr == S_OK, "got 0x%08x\n", hr);
161     ok(!lstrcmpW(buff, ruW), "got %s\n", wine_dbgstr_w(buff));
162     hr = IDWriteTextFormat_GetLocaleName(format, buff, len);
163     ok(hr == E_NOT_SUFFICIENT_BUFFER, "got 0x%08x\n", hr);
164     hr = IDWriteTextFormat_GetLocaleName(format, buff, len+1);
165     ok(hr == S_OK, "got 0x%08x\n", hr);
166     ok(!lstrcmpW(buff, ruW), "got %s\n", wine_dbgstr_w(buff));
167
168     IDWriteTextLayout_Release(layout);
169     IDWriteTextFormat_Release(format);
170 }
171
172 START_TEST(layout)
173 {
174     HRESULT hr;
175
176     hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_ISOLATED, &IID_IDWriteFactory, (IUnknown**)&factory);
177     ok(hr == S_OK, "got 0x%08x\n", hr);
178     if (hr != S_OK)
179     {
180         win_skip("failed to create factory\n");
181         return;
182     }
183
184     test_CreateTextLayout();
185     test_CreateGdiCompatibleTextLayout();
186     test_CreateTextFormat();
187     test_GetLocaleName();
188
189     IDWriteFactory_Release(factory);
190 }