Page Speed Optimization Libraries
1.5.27.2
|
00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 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/htmlparse/public/html_parse.h" 00025 #include "net/instaweb/htmlparse/public/html_writer_filter.h" 00026 #include "net/instaweb/util/public/basictypes.h" 00027 #include "net/instaweb/util/public/gtest.h" 00028 #include "net/instaweb/util/public/mock_message_handler.h" 00029 #include "net/instaweb/util/public/scoped_ptr.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 00037 class HtmlParseTestBaseNoAlloc : public testing::Test { 00038 protected: 00039 static const char kTestDomain[]; 00040 static const char kXhtmlDtd[]; 00041 00042 HtmlParseTestBaseNoAlloc() 00043 : write_to_string_(&output_buffer_), 00044 added_filter_(false) { 00045 } 00046 virtual ~HtmlParseTestBaseNoAlloc(); 00047 00060 virtual bool AddBody() const = 0; 00061 00067 virtual bool AddHtmlTags() const { return true; } 00068 00072 void SetDoctype(const StringPiece& directive) { 00073 directive.CopyToString(&doctype_string_); 00074 } 00075 00076 virtual GoogleString AddHtmlBody(const StringPiece& html) { 00077 GoogleString ret; 00078 if (AddHtmlTags()) { 00079 ret = AddBody() ? "<html><body>\n" : "<html>\n"; 00080 StrAppend(&ret, html, (AddBody() ? "\n</body></html>\n" : "\n</html>")); 00081 } else { 00082 html.CopyToString(&ret); 00083 } 00084 return ret; 00085 } 00086 00089 void ValidateNoChanges(const StringPiece& case_id, 00090 const GoogleString& html_input) { 00091 ValidateExpected(case_id, html_input, html_input); 00092 } 00093 00095 void ValidateNoChangesFail(const StringPiece& case_id, 00096 const GoogleString& html_input) { 00097 ValidateExpectedFail(case_id, html_input, html_input); 00098 } 00099 00100 void SetupWriter() { 00101 SetupWriter(&html_writer_filter_); 00102 } 00103 00104 void SetupWriter(scoped_ptr<HtmlWriterFilter>* html_writer_filter) { 00105 output_buffer_.clear(); 00106 if (html_writer_filter->get() == NULL) { 00107 html_writer_filter->reset(new HtmlWriterFilter(html_parse())); 00108 (*html_writer_filter)->set_writer(&write_to_string_); 00109 html_parse()->AddFilter(html_writer_filter->get()); 00110 } 00111 } 00112 00114 void Parse(const StringPiece& case_id, const GoogleString& html_input) { 00117 GoogleString dummy_url = StrCat(kTestDomain, case_id, ".html"); 00118 ParseUrl(dummy_url, html_input); 00119 } 00120 00122 virtual void ParseUrl(const StringPiece& url, const StringPiece& html_input); 00123 00127 bool ValidateExpected(const StringPiece& case_id, 00128 const GoogleString& html_input, 00129 const GoogleString& expected); 00130 00132 bool ValidateExpectedUrl(const StringPiece& url, 00133 const GoogleString& html_input, 00134 const GoogleString& expected); 00135 00137 void ValidateExpectedFail(const StringPiece& case_id, 00138 const GoogleString& html_input, 00139 const GoogleString& expected); 00140 00141 virtual HtmlParse* html_parse() = 0; 00142 00143 MockMessageHandler message_handler_; 00144 StringWriter write_to_string_; 00145 GoogleString output_buffer_; 00146 bool added_filter_; 00147 scoped_ptr<HtmlWriterFilter> html_writer_filter_; 00148 GoogleString doctype_string_; 00149 00150 private: 00151 DISALLOW_COPY_AND_ASSIGN(HtmlParseTestBaseNoAlloc); 00152 }; 00153 00154 class HtmlParseTestBase : public HtmlParseTestBaseNoAlloc { 00155 public: 00156 HtmlParseTestBase() : html_parse_(&message_handler_) { 00157 }; 00158 protected: 00159 virtual HtmlParse* html_parse() { return &html_parse_; } 00160 00161 HtmlParse html_parse_; 00162 00163 private: 00164 DISALLOW_COPY_AND_ASSIGN(HtmlParseTestBase); 00165 }; 00166 00167 } 00168 00169 #endif ///< NET_INSTAWEB_HTMLPARSE_PUBLIC_HTML_PARSE_TEST_BASE_H_