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 "base/string_util.h"
6#include "base/utf_string_conversions.h"
7#include "testing/gtest/include/gtest/gtest.h"
8#include "third_party/WebKit/Source/WebKit/chromium/public/WebData.h"
9#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
10#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
11#include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h"
12#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
13#include "webkit/tools/test_shell/test_shell_test.h"
14
15using WebKit::WebFrame;
16using WebKit::WebString;
17using WebKit::WebView;
18
19class WebFrameTest : public TestShellTest {
20};
21
22TEST_F(WebFrameTest, GetContentAsPlainText) {
23  WebView* view = test_shell_->webView();
24  WebFrame* frame = view->mainFrame();
25
26  // Generate a simple test case.
27  const char simple_source[] = "<div>Foo bar</div><div></div>baz";
28  GURL test_url("http://foo/");
29  frame->loadHTMLString(simple_source, test_url);
30  test_shell_->WaitTestFinished();
31
32  // Make sure it comes out OK.
33  const string16 expected(ASCIIToUTF16("Foo bar\nbaz"));
34  string16 text = frame->contentAsText(std::numeric_limits<size_t>::max());
35  EXPECT_EQ(expected, text);
36
37  // Try reading the same one with clipping of the text.
38  const int len = 5;
39  text = frame->contentAsText(len);
40  EXPECT_EQ(expected.substr(0, len), text);
41
42  // Now do a new test with a subframe.
43  const char outer_frame_source[] = "Hello<iframe></iframe> world";
44  frame->loadHTMLString(outer_frame_source, test_url);
45  test_shell_->WaitTestFinished();
46
47  // Load something into the subframe.
48  WebFrame* subframe = frame->findChildByExpression(
49      WebString::fromUTF8("/html/body/iframe"));
50  ASSERT_TRUE(subframe);
51  subframe->loadHTMLString("sub<p>text", test_url);
52  test_shell_->WaitTestFinished();
53
54  text = frame->contentAsText(std::numeric_limits<size_t>::max());
55  EXPECT_EQ("Hello world\n\nsub\ntext", UTF16ToUTF8(text));
56
57  // Get the frame text where the subframe separator falls on the boundary of
58  // what we'll take. There used to be a crash in this case.
59  text = frame->contentAsText(12);
60  EXPECT_EQ("Hello world", UTF16ToUTF8(text));
61}
62
63TEST_F(WebFrameTest, GetFullHtmlOfPage) {
64  WebView* view = test_shell_->webView();
65  WebFrame* frame = view->mainFrame();
66
67  // Generate a simple test case.
68  const char simple_source[] = "<p>Hello</p><p>World</p>";
69  GURL test_url("http://hello/");
70  frame->loadHTMLString(simple_source, test_url);
71  test_shell_->WaitTestFinished();
72
73  string16 text = frame->contentAsText(std::numeric_limits<size_t>::max());
74  EXPECT_EQ("Hello\n\nWorld", UTF16ToUTF8(text));
75
76  const std::string html = frame->contentAsMarkup().utf8();
77
78  // Load again with the output html.
79  frame->loadHTMLString(html, test_url);
80  test_shell_->WaitTestFinished();
81
82  EXPECT_EQ(html, UTF16ToUTF8(frame->contentAsMarkup()));
83
84  text = frame->contentAsText(std::numeric_limits<size_t>::max());
85  EXPECT_EQ("Hello\n\nWorld", UTF16ToUTF8(text));
86
87  // Test selection check
88  EXPECT_FALSE(frame->hasSelection());
89  frame->executeCommand(WebString::fromUTF8("SelectAll"));
90  EXPECT_TRUE(frame->hasSelection());
91  frame->executeCommand(WebString::fromUTF8("Unselect"));
92  EXPECT_FALSE(frame->hasSelection());
93  WebString selection_html = frame->selectionAsMarkup();
94  EXPECT_TRUE(selection_html.isEmpty());
95}
96