Home | History | Annotate | Download | only in glue
      1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <string>
      6 
      7 #include "base/string_util.h"
      8 #include "net/url_request/url_request_test_util.h"
      9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
     10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
     11 #include "webkit/glue/unittest_test_server.h"
     12 #include "webkit/glue/webkit_glue.h"
     13 #include "webkit/tools/test_shell/test_shell_test.h"
     14 
     15 using WebKit::WebFrame;
     16 
     17 namespace {
     18 
     19 class MimeTypeTests : public TestShellTest {
     20  public:
     21   void LoadURL(const GURL& url) {
     22     test_shell_->LoadURL(url);
     23     test_shell_->WaitTestFinished();
     24   }
     25 
     26   void CheckMimeType(const char* mimetype, const std::string& expected) {
     27     std::string path("contenttype?");
     28     GURL url(test_server_.GetURL(path + mimetype));
     29     LoadURL(url);
     30     WebFrame* frame = test_shell_->webView()->mainFrame();
     31     EXPECT_EQ(expected, UTF16ToASCII(webkit_glue::DumpDocumentText(frame)));
     32   }
     33 
     34   UnittestTestServer test_server_;
     35 };
     36 
     37 TEST_F(MimeTypeTests, MimeTypeTests) {
     38   ASSERT_TRUE(test_server_.Start());
     39 
     40   std::string expected_src("<html>\n<body>\n"
     41       "<p>HTML text</p>\n</body>\n</html>\n");
     42 
     43   // These files should all be displayed as plain text.
     44   const char* plain_text[] = {
     45     // It is unclear whether to display text/css or download it.
     46     //   Firefox 3: Display
     47     //   Internet Explorer 7: Download
     48     //   Safari 3.2: Download
     49     // We choose to match Firefox due to the lot of complains
     50     // from the users if css files are downloaded:
     51     // http://code.google.com/p/chromium/issues/detail?id=7192
     52     "text/css",
     53     "text/javascript",
     54     "text/plain",
     55     "application/x-javascript",
     56   };
     57   for (size_t i = 0; i < arraysize(plain_text); ++i) {
     58     CheckMimeType(plain_text[i], expected_src);
     59   }
     60 
     61   // These should all be displayed as html content.
     62   const char* html_src[] = {
     63     "text/html",
     64     "text/xml",
     65     "text/xsl",
     66     "application/xhtml+xml",
     67   };
     68   for (size_t i = 0; i < arraysize(html_src); ++i) {
     69     CheckMimeType(html_src[i], "HTML text");
     70   }
     71 
     72   // These shouldn't be rendered as text or HTML, but shouldn't download
     73   // either.
     74   const char* not_text[] = {
     75     "image/png",
     76     "image/gif",
     77     "image/jpeg",
     78     "image/bmp",
     79   };
     80   for (size_t i = 0; i < arraysize(not_text); ++i) {
     81     CheckMimeType(not_text[i], "");
     82     test_shell_->webView()->mainFrame()->stopLoading();
     83   }
     84 
     85   // TODO(tc): make sure other mime types properly go to download (e.g.,
     86   // image/foo).
     87 
     88 }
     89 
     90 }  // namespace
     91