Page Speed Optimization Libraries  1.4.26.1
net/instaweb/htmlparse/public/html_parse_test_base.h
Go to the documentation of this file.
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 
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     SetupWriter(&html_writer_filter_);
00101   }
00102 
00103   void SetupWriter(scoped_ptr<HtmlWriterFilter>* html_writer_filter) {
00104     output_buffer_.clear();
00105     if (html_writer_filter->get() == NULL) {
00106       html_writer_filter->reset(new HtmlWriterFilter(html_parse()));
00107       (*html_writer_filter)->set_writer(&write_to_string_);
00108       html_parse()->AddFilter(html_writer_filter->get());
00109     }
00110   }
00111 
00113   void Parse(const StringPiece& case_id, const GoogleString& html_input) {
00116     GoogleString dummy_url = StrCat(kTestDomain, case_id, ".html");
00117     ParseUrl(dummy_url, html_input);
00118   }
00119 
00121   virtual void ParseUrl(const StringPiece& url, const StringPiece& html_input);
00122 
00126   bool ValidateExpected(const StringPiece& case_id,
00127                         const GoogleString& html_input,
00128                         const GoogleString& expected);
00129 
00131   bool ValidateExpectedUrl(const StringPiece& url,
00132                            const GoogleString& html_input,
00133                            const GoogleString& expected);
00134 
00136   void ValidateExpectedFail(const StringPiece& case_id,
00137                             const GoogleString& html_input,
00138                             const GoogleString& expected);
00139 
00140   virtual HtmlParse* html_parse() = 0;
00141 
00142   MockMessageHandler message_handler_;
00143   StringWriter write_to_string_;
00144   GoogleString output_buffer_;
00145   bool added_filter_;
00146   scoped_ptr<HtmlWriterFilter> html_writer_filter_;
00147   GoogleString doctype_string_;
00148 
00149  private:
00150   DISALLOW_COPY_AND_ASSIGN(HtmlParseTestBaseNoAlloc);
00151 };
00152 
00153 class HtmlParseTestBase : public HtmlParseTestBaseNoAlloc {
00154  public:
00155   HtmlParseTestBase() : html_parse_(&message_handler_) {
00156   };
00157  protected:
00158   virtual HtmlParse* html_parse() { return &html_parse_; }
00159 
00160   HtmlParse html_parse_;
00161 
00162  private:
00163   DISALLOW_COPY_AND_ASSIGN(HtmlParseTestBase);
00164 };
00165 
00166 }  
00167 
00168 #endif  ///< NET_INSTAWEB_HTMLPARSE_PUBLIC_HTML_PARSE_TEST_BASE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines