forked from beyhan/rap-ckeditor
/
CKEditor_Test.java
151 lines (117 loc) · 4.34 KB
/
CKEditor_Test.java
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
package com.eclipsesource.widgets.ckeditor;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import junit.framework.TestCase;
import org.eclipse.rap.json.JsonObject;
import org.eclipse.rap.json.JsonValue;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.client.WebClient;
import org.eclipse.rap.rwt.client.service.JavaScriptLoader;
import org.eclipse.rap.rwt.lifecycle.WidgetUtil;
import org.eclipse.rap.rwt.remote.Connection;
import org.eclipse.rap.rwt.remote.OperationHandler;
import org.eclipse.rap.rwt.remote.RemoteObject;
import org.eclipse.rap.rwt.service.ResourceManager;
import org.eclipse.rap.rwt.testfixture.Fixture;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.mockito.ArgumentCaptor;
public class CKEditor_Test extends TestCase {
private Display display;
private Shell shell;
private CKEditor editor;
private Connection connection;
private RemoteObject remoteObject;
@Override
protected void setUp() throws Exception {
Fixture.setUp();
display = new Display();
shell = new Shell( display );
Fixture.fakeNewRequest();
remoteObject = mock( RemoteObject.class );
connection = mock( Connection.class );
when( connection.createRemoteObject( anyString() ) ).thenReturn( remoteObject );
Fixture.fakeConnection( connection );
editor = new CKEditor( shell, SWT.BORDER );
}
@Override
protected void tearDown() throws Exception {
Fixture.tearDown();
}
public void testSetLayout() {
try {
editor.setLayout( new FillLayout() );
fail();
} catch( UnsupportedOperationException ex ) {
// expected
}
}
public void testContructor_CreatesRemoteObjectWithCorrectType() {
verify( connection ).createRemoteObject( eq( "eclipsesource.CKEditor" ) );
}
public void testContructor_SetsParent() {
verify( remoteObject ).set( "parent", WidgetUtil.getId( editor ) );
}
public void testContructor_LoadsJavaScriptFiles() {
JavaScriptLoader loader = mockJavaScriptLoader();
ResourceManager resourceManager = RWT.getResourceManager();
new CKEditor( shell, SWT.BORDER );
verify( loader ).require( resourceManager.getLocation( "ckeditor/ckeditor.js" ) );
verify( loader ).require( resourceManager.getLocation( "ckeditor/config.js" ) );
verify( loader ).require( resourceManager.getLocation( "ckeditor/handler.js" ) );
}
public void testSetText_GetText() {
String text = "foo<span>bar</span>";
editor.setText( text );
assertEquals( text, editor.getText() );
}
public void testSetText_RendersToClient() {
String text = "foo<span>bar</span>";
editor.setText( text );
verify( remoteObject ).set( "text", text );
}
public void testSetTextNull() {
try {
editor.setText( null );
fail();
} catch( IllegalArgumentException ex ) {
// expected
}
}
public void testSetTextFromClient() {
String text = "foo<span>bar</span>";
remoteSet( remoteObject, "text", JsonValue.valueOf( text ) );
assertEquals( text, editor.getText() );
}
public void testSetFont_RendersToClient() {
editor.setFont( new Font( display, "fantasy", 13, 0 ) );
verify( remoteObject ).set( "font", "13px fantasy" );
}
public void testDispose_RendersDestroyToClient() {
editor.dispose();
verify( remoteObject ).destroy();
}
/////////
// Helper
private JavaScriptLoader mockJavaScriptLoader() {
WebClient client = mock( WebClient.class );
Fixture.fakeClient( client );
JavaScriptLoader loader = mock( JavaScriptLoader.class );
when( client.getService( JavaScriptLoader.class ) ).thenReturn( loader );
return loader;
}
private static void remoteSet( RemoteObject remoteObjectMock, String proprety, JsonValue value ) {
getHandler( remoteObjectMock ).handleSet( new JsonObject().add( proprety, value ) );
}
private static OperationHandler getHandler( RemoteObject remoteObjectMock ) {
ArgumentCaptor<OperationHandler> captor = ArgumentCaptor.forClass( OperationHandler.class );
verify( remoteObjectMock ).setHandler( captor.capture() );
return captor.getValue();
}
}