summaryrefslogtreecommitdiffstats
path: root/webkit/glue/mimetype_unittest.cc
blob: 1c2e5f4a3d7b6e51da93b987c2179ae707485d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/string_util.h"
#include "net/url_request/url_request_unittest.h"
#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/WebKit/chromium/public/WebView.h"
#include "webkit/glue/unittest_test_server.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/tools/test_shell/test_shell_test.h"

using WebKit::WebFrame;

namespace {

class MimeTypeTests : public TestShellTest {
 public:
  void LoadURL(const GURL& url) {
    test_shell_->LoadURL(url);
    test_shell_->WaitTestFinished();
  }

  void CheckMimeType(const char* mimetype, const std::wstring& expected) {
    std::string path("contenttype?");
    GURL url(test_server_.GetURL(path + mimetype));
    LoadURL(url);
    WebFrame* frame = test_shell_->webView()->mainFrame();
    EXPECT_EQ(expected, webkit_glue::DumpDocumentText(frame));
  }

  UnittestTestServer test_server_;
};

TEST_F(MimeTypeTests, MimeTypeTests) {
  ASSERT_TRUE(test_server_.Start());

  std::wstring expected_src(L"<html>\n<body>\n"
      L"<p>HTML text</p>\n</body>\n</html>\n");

  // These files should all be displayed as plain text.
  const char* plain_text[] = {
    // It is unclear whether to display text/css or download it.
    //   Firefox 3: Display
    //   Internet Explorer 7: Download
    //   Safari 3.2: Download
    // We choose to match Firefox due to the lot of complains
    // from the users if css files are downloaded:
    // http://code.google.com/p/chromium/issues/detail?id=7192
    "text/css",
    "text/javascript",
    "text/plain",
    "application/x-javascript",
  };
  for (size_t i = 0; i < arraysize(plain_text); ++i) {
    CheckMimeType(plain_text[i], expected_src);
  }

  // These should all be displayed as html content.
  const char* html_src[] = {
    "text/html",
    "text/xml",
    "text/xsl",
    "application/xhtml+xml",
  };
  for (size_t i = 0; i < arraysize(html_src); ++i) {
    CheckMimeType(html_src[i], L"HTML text");
  }

  // These shouldn't be rendered as text or HTML, but shouldn't download
  // either.
  const char* not_text[] = {
    "image/png",
    "image/gif",
    "image/jpeg",
    "image/bmp",
  };
  for (size_t i = 0; i < arraysize(not_text); ++i) {
    CheckMimeType(not_text[i], L"");
    test_shell_->webView()->mainFrame()->stopLoading();
  }

  // TODO(tc): make sure other mime types properly go to download (e.g.,
  // image/foo).

}

}  // namespace