blob: 0cf4fe81a2aba2cbc828d5e48db21451c097686f [file] [log] [blame]
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.user.cellview.client;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.user.cellview.client.ColumnSortEvent.AsyncHandler;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.ColumnSortList.ColumnSortInfo;
import com.google.gwt.view.client.MockHasData;
import com.google.gwt.view.client.Range;
import com.google.gwt.view.client.RangeChangeEvent;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* Tests for {@link ColumnSortEvent}.
*/
public class ColumnSortEventTest extends TestCase {
public void testAccessors() {
ColumnSortList sortList = new ColumnSortList();
IdentityColumn<String> col0 = new IdentityColumn<String>(new TextCell());
IdentityColumn<String> col1 = new IdentityColumn<String>(new TextCell());
sortList.push(new ColumnSortInfo(col0, true));
sortList.push(new ColumnSortInfo(col1, false));
ColumnSortEvent event = new ColumnSortEvent(sortList);
assertEquals(sortList, event.getColumnSortList());
assertEquals(col1, event.getColumn());
assertFalse(event.isSortAscending());
}
public void testAsyncHandler() {
MockHasData<String> hasData = new MockHasData<String>();
final List<Range> events = new ArrayList<Range>();
hasData.addRangeChangeHandler(new RangeChangeEvent.Handler() {
public void onRangeChange(RangeChangeEvent event) {
events.add(event.getNewRange());
}
});
AsyncHandler handler = new AsyncHandler(hasData);
assertEquals(0, events.size());
// Fire an event to the handler.
ColumnSortList sortList = new ColumnSortList();
handler.onColumnSort(new ColumnSortEvent(sortList));
assertEquals(1, events.size());
}
public void testListHandler() {
// Create some unsorted values.
List<String> values = new ArrayList<String>();
values.add("b");
values.add("a");
values.add("c");
// Create a handler for the list of values.
ListHandler<String> handler = new ListHandler<String>(values);
IdentityColumn<String> col0 = new IdentityColumn<String>(new TextCell());
handler.setComparator(col0, new Comparator<String>() {
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
IdentityColumn<String> col1 = new IdentityColumn<String>(new TextCell());
handler.setComparator(col1, null);
// Sort ascending.
ColumnSortList sortList = new ColumnSortList();
sortList.push(col0);
handler.onColumnSort(new ColumnSortEvent(sortList));
assertEquals("a", values.get(0));
assertEquals("b", values.get(1));
assertEquals("c", values.get(2));
// Sort descending.
sortList.push(col0); // Switches sort to descending.
handler.onColumnSort(new ColumnSortEvent(sortList));
assertEquals("c", values.get(0));
assertEquals("b", values.get(1));
assertEquals("a", values.get(2));
// Null comparator.
sortList.push(col1);
assertEquals("c", values.get(0));
assertEquals("b", values.get(1));
assertEquals("a", values.get(2));
}
}