Bug 668513 - make the w3c tests for navigationtiming pass
authorIgor Bazarny <igor.bazarny@gmail.com>
Mon, 04 Jul 2011 11:30:43 +0200
changeset 72526 df4ec91d0b0aea3801cc08b088f264f4b30f2c84
parent 72525 84f72e1d1fa292881c21cef63f989985f00f71f6
child 72527 06815ff84678e97420eca4890f28fe517396a548
push idunknown
push userunknown
push dateunknown
bugs668513
milestone7.0a1
Bug 668513 - make the w3c tests for navigationtiming pass r=smaug
docshell/base/nsDocShell.cpp
docshell/test/Makefile.in
docshell/test/bug668513_redirect.html
docshell/test/bug668513_redirect.html^headers^
docshell/test/test_bug668513.html
dom/base/nsDOMNavigationTiming.cpp
dom/base/nsDOMNavigationTiming.h
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -672,31 +672,31 @@ ConvertLoadTypeToNavigationType(PRUint32
 {
   nsDOMPerformanceNavigationType result = nsIDOMPerformanceNavigation::TYPE_RESERVED;
   switch (aLoadType) {
     case LOAD_NORMAL:
     case LOAD_NORMAL_EXTERNAL:
     case LOAD_NORMAL_BYPASS_CACHE:
     case LOAD_NORMAL_BYPASS_PROXY:
     case LOAD_NORMAL_BYPASS_PROXY_AND_CACHE:
+    case LOAD_NORMAL_REPLACE:
     case LOAD_LINK:
+    case LOAD_STOP_CONTENT:
         result = nsIDOMPerformanceNavigation::TYPE_NAVIGATE;
         break;
     case LOAD_HISTORY:
         result = nsIDOMPerformanceNavigation::TYPE_BACK_FORWARD;
         break;
     case LOAD_RELOAD_NORMAL:
     case LOAD_RELOAD_CHARSET_CHANGE:
     case LOAD_RELOAD_BYPASS_CACHE:
     case LOAD_RELOAD_BYPASS_PROXY:
     case LOAD_RELOAD_BYPASS_PROXY_AND_CACHE:
         result = nsIDOMPerformanceNavigation::TYPE_RELOAD;
         break;
-    case LOAD_NORMAL_REPLACE:
-    case LOAD_STOP_CONTENT:
     case LOAD_STOP_CONTENT_AND_REPLACE:
     case LOAD_REFRESH:
     case LOAD_BYPASS_HISTORY:
     case LOAD_ERROR_PAGE:
     case LOAD_PUSHSTATE:
         result = nsIDOMPerformanceNavigation::TYPE_RESERVED;
         break;
     default:
--- a/docshell/test/Makefile.in
+++ b/docshell/test/Makefile.in
@@ -108,16 +108,19 @@ include $(topsrcdir)/config/rules.mk
 		historyframes.html \
 		test_bug660404.html \
 		file_bug660404 \
 		file_bug660404^headers^ \
 		test_bug662170.html \
 		file_bug662170.html \
 		test_bug570341.html \
 		bug570341_recordevents.html \
+		test_bug668513.html \
+		bug668513_redirect.html \
+		bug668513_redirect.html^headers^ \
 		$(NULL)
 
 ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)
 _TEST_FILES += \
 		test_bug511449.html \
 		file_bug511449.html \
 		$(NULL)
 endif
new file mode 100644
--- /dev/null
+++ b/docshell/test/bug668513_redirect.html
@@ -0,0 +1,1 @@
+<html><body>This document is redirected to a blank document.</body></html>
new file mode 100644
--- /dev/null
+++ b/docshell/test/bug668513_redirect.html^headers^
@@ -0,0 +1,2 @@
+HTTP 302 Moved Temporarily
+Location: navigation/blank.html
new file mode 100644
--- /dev/null
+++ b/docshell/test/test_bug668513.html
@@ -0,0 +1,114 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=668513
+-->
+<head>
+  <title>Test for Bug 668513</title>
+  <script type="application/javascript" src="/MochiKit/packed.js"></script>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+<script>
+  function onload_test()
+  {
+    var win = frames[0];
+    ok(win.performance, 'Window.performance should be defined');
+    ok(win.performance.navigation, 'Window.performance.navigation should be defined');
+    var navigation = win.performance && win.performance.navigation;
+    if (navigation === undefined)
+    {
+      // avoid script errors
+      SimpleTest.finish();
+      return;
+    }
+
+    // do this with a timeout to see the visuals of the navigations.
+    setTimeout("nav_frame();", 100);
+  }
+
+  var step = 1;
+  function nav_frame()
+  {
+    var navigation_frame = frames[0];
+    var navigation = navigation_frame.performance.navigation;
+    switch (step)
+    {
+      case 1:
+      {
+        navigation_frame.location.href = 'bug570341_recordevents.html';
+        step++;
+        break;
+      }
+      case 2:
+      {
+        is(navigation.type, navigation.TYPE_NAVIGATE,
+           'Expected window.performance.navigation.type == TYPE_NAVIGATE');
+        navigation_frame.history.back();
+        step++;
+        break;
+      }
+      case 3:
+      {
+        is(navigation.type, navigation.TYPE_BACK_FORWARD,
+           'Expected window.performance.navigation.type == TYPE_BACK_FORWARD');
+        step++;
+        navigation_frame.history.forward();
+        break;
+      }
+      case 4:
+      {
+        is(navigation.type, navigation.TYPE_BACK_FORWARD,
+           'Expected window.performance.navigation.type == TYPE_BACK_FORWARD');
+        navigation_frame.location.href = 'bug668513_redirect.html';
+        step++;
+        break;
+      }
+      case 5:
+      {
+        is(navigation.type, navigation.TYPE_NAVIGATE,
+           'Expected timing.navigation.type as TYPE_NAVIGATE');
+        is(navigation.redirectCount, 1, 
+           'Expected navigation.redirectCount == 1 on an server redirected navigation');
+
+        var timing = navigation_frame.performance && navigation_frame.performance.timing;
+        if (timing === undefined)
+        {
+          // avoid script errors
+          SimpleTest.finish();
+          break;
+        }
+        ok(timing.navigationStart > 0, 'navigationStart should be > 0');
+        sequence = ['navigationStart', 'redirectStart', 'redirectEnd', 'fetchStart'];
+        for (var j = 1; j < sequence.length; ++j) {
+          var prop = sequence[j];
+          var prevProp = sequence[j-1];
+          ok(timing[prevProp] <= timing[prop],
+             ['Expected ', prevProp, ' to happen before ', prop,
+              ', got ', prevProp, ' = ', timing[prevProp],
+              ', ', prop, ' = ', timing[prop]].join(''));
+        }
+        step++;
+        SimpleTest.finish();
+      }
+      default:
+        break;
+    }
+  }
+</script>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=668513">Mozilla Bug 668513</a>
+<div id="frames">
+<iframe name="child0" onload="onload_test();" src="navigation/blank.html"></iframe>
+</div>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script type="application/javascript">
+SimpleTest.waitForExplicitFinish();
+</script>
+</pre>
+</body>
+</html>
--- a/dom/base/nsDOMNavigationTiming.cpp
+++ b/dom/base/nsDOMNavigationTiming.cpp
@@ -59,16 +59,17 @@ nsDOMNavigationTiming::~nsDOMNavigationT
 void
 nsDOMNavigationTiming::Clear()
 {
   mNavigationType = nsIDOMPerformanceNavigation::TYPE_RESERVED;
   mNavigationStart = 0;
   mFetchStart = 0;
   mRedirectStart = 0;
   mRedirectEnd = 0;
+  mRedirectCount = 0;
   mBeforeUnloadStart = 0;
   mUnloadStart = 0;
   mUnloadEnd = 0;
   mLoadEventStart = 0;
   mLoadEventEnd = 0;
   mDOMLoading = 0;
   mDOMInteractive = 0;
   mDOMContentLoadedEventStart = 0;
@@ -151,26 +152,28 @@ nsDOMNavigationTiming::NotifyLoadEventEn
 {
   mLoadEventEnd = DurationFromStart();
 }
 
 PRBool
 nsDOMNavigationTiming::ReportRedirects()
 {
   if (mRedirectCheck == NOT_CHECKED) {
+    mRedirectCount = mRedirects.Count();
     if (mRedirects.Count() == 0) {
       mRedirectCheck = NO_REDIRECTS;
     } else {
       mRedirectCheck = CHECK_PASSED;
       nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
       for (int i = mRedirects.Count() - 1; i >= 0; --i) {
         nsIURI * curr = mRedirects[i];
         nsresult rv = ssm->CheckSameOriginURI(curr, mLoadedURI, PR_FALSE);
         if (!NS_SUCCEEDED(rv)) {
           mRedirectCheck = CHECK_FAILED;
+          mRedirectCount = 0;
           break;
         }
       }
       // All we need to know is in mRedirectCheck now. Clear history.
       mRedirects.Clear();
     }
   }
   return mRedirectCheck == CHECK_PASSED;
@@ -238,17 +241,17 @@ nsDOMNavigationTiming::GetType(
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDOMNavigationTiming::GetRedirectCount(PRUint16* aRedirectCount)
 {
   *aRedirectCount = 0;
   if (ReportRedirects()) {
-    *aRedirectCount = mRedirects.Count();
+    *aRedirectCount = mRedirectCount;
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDOMNavigationTiming::GetRedirectStart(DOMTimeMilliSec* aRedirectStart)
 {
   *aRedirectStart = 0;
--- a/dom/base/nsDOMNavigationTiming.h
+++ b/dom/base/nsDOMNavigationTiming.h
@@ -90,16 +90,17 @@ private:
   nsCOMPtr<nsIURI> mLoadedURI;
   nsCOMArray<nsIURI> mRedirects;
 
   typedef enum { NOT_CHECKED,
                  CHECK_PASSED,
                  NO_REDIRECTS,
                  CHECK_FAILED} RedirectCheckState;
   RedirectCheckState mRedirectCheck;
+  PRInt16 mRedirectCount;
 
   nsDOMPerformanceNavigationType mNavigationType;
   DOMTimeMilliSec mNavigationStart;
   mozilla::TimeStamp mNavigationStartTimeStamp;
   DOMTimeMilliSec DurationFromStart();
 
   DOMTimeMilliSec mFetchStart;
   DOMTimeMilliSec mRedirectStart;