Adding null checks to all History methods to ensure that History is enabled.
http://gwt-code-reviews.appspot.com/138805


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@7542 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/History.java b/user/src/com/google/gwt/user/client/History.java
index 5224840..500acf3 100644
--- a/user/src/com/google/gwt/user/client/History.java
+++ b/user/src/com/google/gwt/user/client/History.java
@@ -81,7 +81,9 @@
    */
   @Deprecated
   public static void addHistoryListener(HistoryListener listener) {
-    BaseListenerWrapper.WrapHistory.add(listener);
+    if (impl != null) {
+      BaseListenerWrapper.WrapHistory.add(listener);
+    }
   }
 
   /**
@@ -93,7 +95,7 @@
    */
   public static HandlerRegistration addValueChangeHandler(
       ValueChangeHandler<String> handler) {
-    return impl.addValueChangeHandler(handler);
+    return impl != null ? impl.addValueChangeHandler(handler) : null;
   }
 
   /**
@@ -114,8 +116,10 @@
    * history handlers of the initial application state.
    */
   public static void fireCurrentHistoryState() {
-    String token = getToken();
-    impl.fireHistoryChangedImpl(token);
+    if (impl != null) {
+      String token = getToken();
+      impl.fireHistoryChangedImpl(token);
+    }
   }
 
   /**
@@ -183,7 +187,9 @@
    */
   @Deprecated
   public static void onHistoryChanged(String historyToken) {
-    impl.fireHistoryChangedImpl(historyToken);
+    if (impl != null) {
+      impl.fireHistoryChangedImpl(historyToken);
+    }
   }
 
   /**
@@ -193,6 +199,8 @@
    */
   @Deprecated
   public static void removeHistoryListener(HistoryListener listener) {
-    BaseListenerWrapper.WrapHistory.remove(impl.getHandlers(), listener);
+    if (impl != null) {
+      BaseListenerWrapper.WrapHistory.remove(impl.getHandlers(), listener);
+    }
   }
 }
diff --git a/user/test/com/google/gwt/user/HistoryDisabledTest.gwt.xml b/user/test/com/google/gwt/user/HistoryDisabledTest.gwt.xml
new file mode 100644
index 0000000..a0205d6
--- /dev/null
+++ b/user/test/com/google/gwt/user/HistoryDisabledTest.gwt.xml
@@ -0,0 +1,22 @@
+<!--                                                                        -->
+<!-- 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   -->
+<!-- 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. License for the specific language governing permissions and   -->
+<!-- limitations under the License.                                         -->
+<module>
+  <inherits name="com.google.gwt.user.User"/>
+
+  <!-- Replaces HistoryImpl with one that is disabled and does not -->
+  <!-- initialize.                                                 -->
+  <replace-with class="com.google.gwt.user.client.impl.HistoryImplDisabled">
+    <when-type-is class="com.google.gwt.user.client.impl.HistoryImpl"/>
+  </replace-with>
+</module>
diff --git a/user/test/com/google/gwt/user/UISuite.java b/user/test/com/google/gwt/user/UISuite.java
index af40081..a72917d 100644
--- a/user/test/com/google/gwt/user/UISuite.java
+++ b/user/test/com/google/gwt/user/UISuite.java
@@ -21,6 +21,7 @@
 import com.google.gwt.user.client.CommandExecutorTest;
 import com.google.gwt.user.client.CookieTest;
 import com.google.gwt.user.client.EventTest;
+import com.google.gwt.user.client.HistoryDisabledTest;
 import com.google.gwt.user.client.WindowTest;
 import com.google.gwt.user.client.ui.AbsolutePanelTest;
 import com.google.gwt.user.client.ui.AnchorTest;
@@ -144,6 +145,7 @@
     suite.addTestSuite(GridTest.class);
     suite.addTestSuite(HiddenTest.class);
     suite.addTestSuite(HistoryTest.class);
+    suite.addTestSuite(HistoryDisabledTest.class);
     suite.addTestSuite(HorizontalPanelTest.class);
     suite.addTestSuite(HorizontalSplitPanelTest.class);
     suite.addTestSuite(HTMLPanelTest.class);
diff --git a/user/test/com/google/gwt/user/client/HistoryDisabledTest.java b/user/test/com/google/gwt/user/client/HistoryDisabledTest.java
new file mode 100644
index 0000000..4b4b138
--- /dev/null
+++ b/user/test/com/google/gwt/user/client/HistoryDisabledTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.client;
+
+import com.google.gwt.event.logical.shared.ValueChangeEvent;
+import com.google.gwt.event.logical.shared.ValueChangeHandler;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.junit.client.GWTTestCase;
+
+/**
+ * Tests for {@link History} when History is disabled. Most of these tests are
+ * just assuring that we don't hit an NPE or JS error.
+ */
+public class HistoryDisabledTest extends GWTTestCase {
+
+  @Override
+  public String getModuleName() {
+    return "com.google.gwt.user.HistoryDisabledTest";
+  }
+
+  @SuppressWarnings("deprecation")
+  public void testAddHistoryListener() {
+    HistoryListener listener = new HistoryListener() {
+      public void onHistoryChanged(String historyToken) {
+      }
+    };
+    History.addHistoryListener(listener);
+    History.removeHistoryListener(listener);
+  }
+
+  public void testAddValueChangeHandler() {
+    HandlerRegistration reg = History.addValueChangeHandler(new ValueChangeHandler<String>() {
+      public void onValueChange(ValueChangeEvent<String> event) {
+      }
+    });
+    assertNull(reg);
+  }
+
+  public void testFireCurrentHistoryState() {
+    HandlerRegistration reg = History.addValueChangeHandler(new ValueChangeHandler<String>() {
+      public void onValueChange(ValueChangeEvent<String> event) {
+        fail("Handler should not have been added.");
+      }
+    });
+    assertNull(reg);
+    History.fireCurrentHistoryState();
+  }
+
+  @SuppressWarnings("deprecation")
+  public void testOnHistoryChanged() {
+    HandlerRegistration reg = History.addValueChangeHandler(new ValueChangeHandler<String>() {
+      public void onValueChange(ValueChangeEvent<String> event) {
+        fail("Handler should not have been added.");
+      }
+    });
+    assertNull(reg);
+    History.onHistoryChanged("test");
+  }
+
+  public void testGetToken() {
+    assertEquals("", History.getToken());
+  }
+
+  public void testNewItem() {
+    HandlerRegistration reg = History.addValueChangeHandler(new ValueChangeHandler<String>() {
+      public void onValueChange(ValueChangeEvent<String> event) {
+        fail("Handler should not have been added.");
+      }
+    });
+    assertNull(reg);
+
+    History.newItem("test");
+    assertEquals("", History.getToken());
+    History.newItem("test", true);
+    assertEquals("", History.getToken());
+  }
+}
diff --git a/user/test/com/google/gwt/user/client/impl/HistoryImplDisabled.java b/user/test/com/google/gwt/user/client/impl/HistoryImplDisabled.java
new file mode 100644
index 0000000..d51a535
--- /dev/null
+++ b/user/test/com/google/gwt/user/client/impl/HistoryImplDisabled.java
@@ -0,0 +1,26 @@
+/*
+ * 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.client.impl;
+
+/**
+ * An implementation of history that is disabled.
+ */
+public class HistoryImplDisabled extends HistoryImpl {
+  @Override
+  public boolean init() {
+    return false;
+  }
+}