Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 69d4b1ca07e3650547b32807ae9674a029e43c7f (plain) (blame)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*******************************************************************************
* Copyright (c) 2018 Etienne Reichenbach and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
* 
* Contributors:
*     Etienne Reichenbach - initial implementation
*******************************************************************************/
package org.eclipse.ui.workbench.texteditor.tests;

import static org.eclipse.jface.text.DocumentRewriteSessionType.SEQUENTIAL;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;

import org.eclipse.ui.internal.texteditor.quickdiff.DocumentLineDiffer;

/**
 * Tests for the {@link DocumentLineDiffer}.
 */
public class DocumentLineDifferTest {

	/** The document to connect to the {@link #fLineDiffer}. */
	private Document fDocument= new Document();


	/** The {@link DocumentLineDiffer line differ} under test. */
	private DocumentLineDiffer fLineDiffer= new DocumentLineDiffer();


	/**
	 * Test that when a document is {@link DocumentLineDiffer#connect(IDocument) connected} the
	 * differ is neither {@link DocumentLineDiffer#isSuspended() suspended} nor
	 * {@link DocumentLineDiffer#isSynchronized() synchronized} (i.e. it is initializing).
	 *
	 * @throws Exception unexpected exception
	 */
	@Test
	public void testLineDifferStateAfterConnectingDocument() throws Exception {
		// when
		fLineDiffer.connect(fDocument);

		// then
		assertFalse(fLineDiffer.isSuspended());
		assertFalse(fLineDiffer.isSynchronized());
	}

	/**
	 * Test that when {@link DocumentLineDiffer#suspend()} is called the differ is
	 * {@link DocumentLineDiffer#isSuspended() suspended}.
	 *
	 * @throws Exception unexpected exception
	 */
	@Test
	public void suspendSuspendsLineDiffer() throws Exception {
		// given
		fLineDiffer.connect(fDocument);

		// when
		fLineDiffer.suspend();

		// then
		assertTrue(fLineDiffer.isSuspended());
	}

	/**
	 * Test that after a suspended {@link DocumentLineDiffer line differ} is
	 * {@link DocumentLineDiffer#resume() resumed} is not {@link DocumentLineDiffer#isSuspended()
	 * suspended} anymore.
	 *
	 * @throws Exception unexpected exception
	 */
	@Test
	public void lineDifferNotSuspendedAfterResumeIsCalled() throws Exception {
		// given
		fLineDiffer.connect(fDocument);
		fLineDiffer.suspend();

		// when
		fLineDiffer.resume();

		// then
		assertFalse(fLineDiffer.isSuspended());
		assertFalse(fLineDiffer.isSynchronized());
	}

	/**
	 * Test that when the document connected to a non suspended {@link DocumentLineDiffer line
	 * differ} starts a rewrite session the differ gets suspended.
	 *
	 * @throws Exception unexpected exception
	 */
	@Test
	public void nonSuspendedLineDifferNotSuspendedAfterStartRewriteSession() throws Exception {
		// given
		fLineDiffer.connect(fDocument);

		// when
		fDocument.startRewriteSession(SEQUENTIAL);

		// then
		assertTrue(fLineDiffer.isSuspended());
	}

	/**
	 * Test that when a document connected to a suspended {@link DocumentLineDiffer line differ}
	 * stops the rewrite session, the differ gets suspended.
	 *
	 * @throws Exception unexpected exception
	 */
	@Test
	public void suspendedLineDifferStillSuspendedAfterStopRewriteSession() throws Exception {
		// given
		fLineDiffer.connect(fDocument);
		fLineDiffer.suspend();

		// when
		fDocument.stopRewriteSession(fDocument.getActiveRewriteSession());

		// then
		assertTrue(fLineDiffer.isSuspended());
	}

	/**
	 * Test that when the document connected to a suspended {@link DocumentLineDiffer line differ}
	 * goes through a rewrite session, the differ stays suspended.
	 *
	 * @throws Exception unexpected exception
	 */
	@Test
	public void suspendedLineDifferStaysSuspendedAfterDocumentRewriteSession() throws Exception {
		// given
		fLineDiffer.connect(fDocument);
		fLineDiffer.suspend();

		// when
		fDocument.startRewriteSession(SEQUENTIAL);
		fDocument.stopRewriteSession(fDocument.getActiveRewriteSession());

		// then
		assertTrue(fLineDiffer.isSuspended());
	}

	/**
	 * Test that when a document connected to a non suspended {@link DocumentLineDiffer line differ}
	 * goes through a rewrite session, the differ stays in the non suspended state.
	 *
	 * @throws Exception unexpected exception
	 */
	@Test
	public void nonSuspendedLineDifferStaysNonSuspendedAfterDocumentRewriteSession() throws Exception {
		// given
		fLineDiffer.connect(fDocument);

		// when
		fDocument.startRewriteSession(SEQUENTIAL);
		fDocument.stopRewriteSession(fDocument.getActiveRewriteSession());

		// then
		assertFalse(fLineDiffer.isSuspended());
	}

}

Back to the top