001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.junit.client.GWTTestCase;
019: import com.google.gwt.user.client.History;
020: import com.google.gwt.user.client.HistoryListener;
021:
022: /**
023: * Tests for the history system.
024: *
025: * TODO: find a way to test unescaping of the initial hash value.
026: */
027: public class HistoryTest extends GWTTestCase {
028:
029: public String getModuleName() {
030: return "com.google.gwt.user.User";
031: }
032:
033: /* Tests against issue #572: Double unescaping of history tokens. */
034: public void testTokenEscaping() {
035: final String escToken = "%24%24%24";
036: delayTestFinish(5000);
037: History.addHistoryListener(new HistoryListener() {
038: public void onHistoryChanged(String token) {
039: assertEquals(escToken, token);
040: finishTest();
041: History.removeHistoryListener(this );
042: }
043: });
044: History.newItem(escToken);
045: }
046:
047: /*
048: * Ensure that non-url-safe strings (such as those containing spaces) are
049: * encoded/decoded correctly, and that programmatic 'back' works.
050: */
051: public void testHistory() {
052: delayTestFinish(5000);
053: History.addHistoryListener(new HistoryListener() {
054: private int state = 0;
055:
056: public void onHistoryChanged(String historyToken) {
057: switch (state) {
058: case 0: {
059: if (!historyToken.equals("foo bar")) {
060: fail("Expecting token 'foo bar', but got: "
061: + historyToken);
062: }
063:
064: state = 1;
065: History.newItem("baz");
066: break;
067: }
068:
069: case 1: {
070: if (!historyToken.equals("baz")) {
071: fail("Expecting token 'baz', but got: "
072: + historyToken);
073: }
074:
075: state = 2;
076: History.back();
077: break;
078: }
079:
080: case 2: {
081: if (!historyToken.equals("foo bar")) {
082: fail("Expecting token 'foo bar', but got: "
083: + historyToken);
084: }
085: finishTest();
086: History.removeHistoryListener(this );
087: break;
088: }
089: }
090: }
091: });
092:
093: History.newItem("foo bar");
094: }
095:
096: /*
097: * Tests against issue #879: Ensure that empty history tokens do not add
098: * additional characters after the '#' symbol in the URL.
099: */
100: public void testEmptyHistoryTokens() {
101: delayTestFinish(5000);
102:
103: History.addHistoryListener(new HistoryListener() {
104: public void onHistoryChanged(String historyToken) {
105:
106: if (historyToken == null) {
107: fail("historyToken should not be null");
108: }
109:
110: if (historyToken.equals("foobar")) {
111: History.newItem("");
112: } else {
113: assertEquals(0, historyToken.length());
114: finishTest();
115: History.removeHistoryListener(this );
116: }
117: }
118: });
119:
120: // We must first start out with a non-blank history token. Adding a blank
121: // history token in the initial state will not cause an onHistoryChanged
122: // event to fire.
123: History.newItem("foobar");
124: }
125: }
|