00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00018
00020
00021 #ifndef NET_INSTAWEB_HTMLPARSE_PUBLIC_HTML_PARSE_TEST_BASE_H_
00022 #define NET_INSTAWEB_HTMLPARSE_PUBLIC_HTML_PARSE_TEST_BASE_H_
00023
00024 #include "net/instaweb/util/public/basictypes.h"
00025 #include "base/scoped_ptr.h"
00026 #include "net/instaweb/htmlparse/public/html_parse.h"
00027 #include "net/instaweb/htmlparse/public/html_writer_filter.h"
00028 #include "net/instaweb/util/public/gtest.h"
00029 #include "net/instaweb/util/public/mock_message_handler.h"
00030 #include "net/instaweb/util/public/string.h"
00031 #include "net/instaweb/util/public/string_util.h"
00032 #include "net/instaweb/util/public/string_writer.h"
00033
00034 namespace net_instaweb {
00035
00036 class HtmlParseTestBaseNoAlloc : public testing::Test {
00037 protected:
00038 static const char kTestDomain[];
00039 static const char kXhtmlDtd[];
00040
00041 HtmlParseTestBaseNoAlloc()
00042 : write_to_string_(&output_buffer_),
00043 added_filter_(false) {
00044 }
00045 virtual ~HtmlParseTestBaseNoAlloc();
00046
00059 virtual bool AddBody() const = 0;
00060
00066 virtual bool AddHtmlTags() const { return true; }
00067
00071 void SetDoctype(const StringPiece& directive) {
00072 directive.CopyToString(&doctype_string_);
00073 }
00074
00075 virtual GoogleString AddHtmlBody(const StringPiece& html) {
00076 GoogleString ret;
00077 if (AddHtmlTags()) {
00078 ret = AddBody() ? "<html><body>\n" : "<html>\n";
00079 StrAppend(&ret, html, (AddBody() ? "\n</body></html>\n" : "\n</html>"));
00080 } else {
00081 html.CopyToString(&ret);
00082 }
00083 return ret;
00084 }
00085
00088 void ValidateNoChanges(const StringPiece& case_id,
00089 const GoogleString& html_input) {
00090 ValidateExpected(case_id, html_input, html_input);
00091 }
00092
00094 void ValidateNoChangesFail(const StringPiece& case_id,
00095 const GoogleString& html_input) {
00096 ValidateExpectedFail(case_id, html_input, html_input);
00097 }
00098
00099 void SetupWriter() {
00100 output_buffer_.clear();
00101 if (html_writer_filter_.get() == NULL) {
00102 html_writer_filter_.reset(new HtmlWriterFilter(html_parse()));
00103 html_writer_filter_->set_writer(&write_to_string_);
00104 html_parse()->AddFilter(html_writer_filter_.get());
00105 }
00106 }
00107
00109 void Parse(const StringPiece& case_id, const GoogleString& html_input) {
00112 GoogleString dummy_url = StrCat(kTestDomain, case_id, ".html");
00113 ParseUrl(dummy_url, html_input);
00114 }
00115
00117 virtual void ParseUrl(const StringPiece& url, const StringPiece& html_input);
00118
00121 void ValidateExpected(const StringPiece& case_id,
00122 const GoogleString& html_input,
00123 const GoogleString& expected);
00124
00126 void ValidateExpectedUrl(const StringPiece& url,
00127 const GoogleString& html_input,
00128 const GoogleString& expected);
00129
00131 void ValidateExpectedFail(const StringPiece& case_id,
00132 const GoogleString& html_input,
00133 const GoogleString& expected);
00134
00135 virtual HtmlParse* html_parse() = 0;
00136
00137 MockMessageHandler message_handler_;
00138 StringWriter write_to_string_;
00139 GoogleString output_buffer_;
00140 bool added_filter_;
00141 scoped_ptr<HtmlWriterFilter> html_writer_filter_;
00142 GoogleString doctype_string_;
00143
00144 private:
00145 DISALLOW_COPY_AND_ASSIGN(HtmlParseTestBaseNoAlloc);
00146 };
00147
00148 class HtmlParseTestBase : public HtmlParseTestBaseNoAlloc {
00149 public:
00150 HtmlParseTestBase() : html_parse_(&message_handler_) {
00151 };
00152 protected:
00153 virtual HtmlParse* html_parse() { return &html_parse_; }
00154
00155 HtmlParse html_parse_;
00156
00157 private:
00158 DISALLOW_COPY_AND_ASSIGN(HtmlParseTestBase);
00159 };
00160
00161 }
00162
00163 #endif ///< NET_INSTAWEB_HTMLPARSE_PUBLIC_HTML_PARSE_TEST_BASE_H_