urlmon: Don't create stgmed_obj for binding to object.
[wine] / dlls / riched32 / tests / editor.c
1 /*
2 * Unit test suite for rich edit control 1.0
3 *
4 * Copyright 2006 Google (Thomas Kho)
5 * Copyright 2007 Matt Finnicum
6 * Copyright 2007 Dmitry Timoshkov
7 * Copyright 2007 Alex VillacĂ­s Lasso
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 #include <stdarg.h>
25 #include <assert.h>
26 #include <windef.h>
27 #include <winbase.h>
28 #include <wingdi.h>
29 #include <winuser.h>
30 #include <winnls.h>
31 #include <ole2.h>
32 #include <richedit.h>
33 #include <time.h>
34 #include <wine/test.h>
35
36 static HMODULE hmoduleRichEdit;
37
38 static HWND new_window(LPCTSTR lpClassName, DWORD dwStyle, HWND parent) {
39   HWND hwnd;
40   hwnd = CreateWindow(lpClassName, NULL, dwStyle|WS_POPUP|WS_HSCROLL|WS_VSCROLL
41                       |WS_VISIBLE, 0, 0, 200, 60, parent, NULL,
42                       hmoduleRichEdit, NULL);
43   ok(hwnd != NULL, "class: %s, error: %d\n", lpClassName, (int) GetLastError());
44   return hwnd;
45 }
46
47 static HWND new_richedit(HWND parent) {
48   return new_window(RICHEDIT_CLASS10A, ES_MULTILINE, parent);
49 }
50
51 static void test_WM_SETTEXT()
52 {
53   HWND hwndRichEdit = new_richedit(NULL);
54   const char * TestItem1 = "TestSomeText";
55   const char * TestItem2 = "TestSomeText\r";
56   const char * TestItem3 = "TestSomeText\rSomeMoreText\r";
57   const char * TestItem4 = "TestSomeText\n\nTestSomeText";
58   const char * TestItem5 = "TestSomeText\r\r\nTestSomeText";
59   const char * TestItem6 = "TestSomeText\r\r\n\rTestSomeText";
60   const char * TestItem7 = "TestSomeText\r\n\r\r\n\rTestSomeText";
61   const char * TestItem8 = "TestSomeText\r\n";
62   const char * TestItem9 = "TestSomeText\r\nSomeMoreText\r\n";
63   const char * TestItem10 = "TestSomeText\r\n\r\nTestSomeText";
64   const char * TestItem11 = "TestSomeText TestSomeText";
65   const char * TestItem12 = "TestSomeText \r\nTestSomeText";
66   const char * TestItem13 = "TestSomeText\r\n \r\nTestSomeText";
67   char buf[1024] = {0};
68   LRESULT result;
69
70   /* This test attempts to show that WM_SETTEXT on a riched32 control does not
71      attempt to modify the text that is pasted into the control, and should
72      return it as is. In particular, \r\r\n is NOT converted, unlike riched20.
73      Currently, builtin riched32 mangles solitary \r or \n when not part of
74      a \r\n pair.
75    */
76
77 #define TEST_SETTEXT(a, b, is_todo) \
78   result = SendMessage(hwndRichEdit, WM_SETTEXT, 0, (LPARAM) a); \
79   ok (result == 1, "WM_SETTEXT returned %ld instead of 1\n", result); \
80   result = SendMessage(hwndRichEdit, WM_GETTEXT, 1024, (LPARAM) buf); \
81   ok (result == lstrlen(buf), \
82         "WM_GETTEXT returned %ld instead of expected %u\n", \
83         result, lstrlen(buf)); \
84   result = strcmp(b, buf); \
85   if (is_todo) todo_wine { \
86   ok(result == 0, \
87         "WM_SETTEXT round trip: strcmp = %ld\n", result); \
88   } else { \
89   ok(result == 0, \
90         "WM_SETTEXT round trip: strcmp = %ld\n", result); \
91   }
92
93   TEST_SETTEXT(TestItem1, TestItem1, 0)
94   TEST_SETTEXT(TestItem2, TestItem2, 1)
95   TEST_SETTEXT(TestItem3, TestItem3, 1)
96   TEST_SETTEXT(TestItem4, TestItem4, 1)
97   TEST_SETTEXT(TestItem5, TestItem5, 1)
98   TEST_SETTEXT(TestItem6, TestItem6, 1)
99   TEST_SETTEXT(TestItem7, TestItem7, 1)
100   TEST_SETTEXT(TestItem8, TestItem8, 0)
101   TEST_SETTEXT(TestItem9, TestItem9, 0)
102   TEST_SETTEXT(TestItem10, TestItem10, 0)
103   TEST_SETTEXT(TestItem11, TestItem11, 0)
104   TEST_SETTEXT(TestItem12, TestItem12, 0)
105   TEST_SETTEXT(TestItem13, TestItem13, 0)
106
107 #undef TEST_SETTEXT
108   DestroyWindow(hwndRichEdit);
109 }
110
111 static void test_WM_GETTEXTLENGTH(void)
112 {
113     HWND hwndRichEdit = new_richedit(NULL);
114     static const char text3[] = "aaa\r\nbbb\r\nccc\r\nddd\r\neee";
115     static const char text4[] = "aaa\r\nbbb\r\nccc\r\nddd\r\neee\r\n";
116     int result;
117
118     /* Test for WM_GETTEXTLENGTH */
119     SendMessage(hwndRichEdit, WM_SETTEXT, 0, (LPARAM) text3);
120     result = SendMessage(hwndRichEdit, WM_GETTEXTLENGTH, 0, 0);
121     ok(result == lstrlen(text3),
122         "WM_GETTEXTLENGTH reports incorrect length %d, expected %d\n",
123         result, lstrlen(text3));
124
125     SendMessage(hwndRichEdit, WM_SETTEXT, 0, (LPARAM) text4);
126     result = SendMessage(hwndRichEdit, WM_GETTEXTLENGTH, 0, 0);
127     ok(result == lstrlen(text4),
128         "WM_GETTEXTLENGTH reports incorrect length %d, expected %d\n",
129         result, lstrlen(text4));
130
131     DestroyWindow(hwndRichEdit);
132 }
133
134 START_TEST( editor )
135 {
136   MSG msg;
137   time_t end;
138
139   /* Must explicitly LoadLibrary(). The test has no references to functions in
140    * RICHED32.DLL, so the linker doesn't actually link to it. */
141   hmoduleRichEdit = LoadLibrary("RICHED32.DLL");
142   ok(hmoduleRichEdit != NULL, "error: %d\n", (int) GetLastError());
143
144   test_WM_SETTEXT();
145   test_WM_GETTEXTLENGTH();
146
147   /* Set the environment variable WINETEST_RICHED32 to keep windows
148    * responsive and open for 30 seconds. This is useful for debugging.
149    *
150    * The message pump uses PeekMessage() to empty the queue and then sleeps for
151    * 50ms before retrying the queue. */
152   end = time(NULL) + 30;
153   if (getenv( "WINETEST_RICHED32" )) {
154     while (time(NULL) < end) {
155       if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
156         TranslateMessage(&msg);
157         DispatchMessage(&msg);
158       } else {
159         Sleep(50);
160       }
161     }
162   }
163
164   OleFlushClipboard();
165   ok(FreeLibrary(hmoduleRichEdit) != 0, "error: %d\n", (int) GetLastError());
166 }