00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00018
00020
00021 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_CSS_REWRITE_TEST_BASE_H_
00022 #define NET_INSTAWEB_REWRITER_PUBLIC_CSS_REWRITE_TEST_BASE_H_
00023
00024 #include "base/logging.h"
00025 #include "net/instaweb/htmlparse/public/html_parse_test_base.h"
00026 #include "net/instaweb/rewriter/public/css_filter.h"
00027 #include "net/instaweb/rewriter/public/resource_manager_test_base.h"
00028 #include "net/instaweb/rewriter/public/rewrite_options.h"
00029 #include "net/instaweb/util/public/statistics.h"
00030 #include "net/instaweb/util/public/string.h"
00031 #include "net/instaweb/util/public/string_util.h"
00032
00033 namespace net_instaweb {
00034
00035 class ResourceNamer;
00036 struct ContentType;
00037
00038 class CssRewriteTestBase : public ResourceManagerTestBase {
00039 protected:
00040 CssRewriteTestBase() {
00041 num_blocks_rewritten_ =
00042 statistics()->GetVariable(CssFilter::kBlocksRewritten);
00043 num_parse_failures_ = statistics()->GetVariable(CssFilter::kParseFailures);
00044 num_rewrites_dropped_ =
00045 statistics()->GetVariable(CssFilter::kRewritesDropped);
00046 total_bytes_saved_ = statistics()->GetVariable(CssFilter::kTotalBytesSaved);
00047 total_original_bytes_ =
00048 statistics()->GetVariable(CssFilter::kTotalOriginalBytes);
00049 num_uses_ = statistics()->GetVariable(CssFilter::kUses);
00050 }
00051 ~CssRewriteTestBase();
00052
00053 virtual void SetUp() {
00054 ResourceManagerTestBase::SetUp();
00055 options()->set_always_rewrite_css(true);
00056 AddFilter(RewriteOptions::kRewriteCss);
00057 }
00058
00059 enum ValidationFlags {
00060 kExpectSuccess = 1,
00061 kExpectNoChange = 2,
00062
00063 kExpectFallback = 4,
00064 kExpectFailure = 8,
00065
00067 kNoStatCheck = 16,
00069 kNoClearFetcher = 32,
00071 kNoOtherContexts = 64,
00072
00073 kLinkCharsetIsUTF8 = 128,
00074 kLinkScreenMedia = 256,
00075 kLinkPrintMedia = 512,
00076
00077 kMetaCharsetUTF8 = 1024,
00078 kMetaCharsetISO88591 = 2048,
00079 kMetaHttpEquiv = 4096,
00080 kMetaHttpEquivUnquoted = 8192,
00081 };
00082
00083 static bool ExactlyOneTrue(bool a, bool b) {
00084 return a ^ b;
00085 }
00086 static bool ExactlyOneTrue(bool a, bool b, bool c) {
00087 return ExactlyOneTrue(a, ExactlyOneTrue(b, c));
00088 }
00089 static bool ExactlyOneTrue(bool a, bool b, bool c, bool d) {
00090 return ExactlyOneTrue(a, ExactlyOneTrue(b, c, d));
00091 }
00092
00093 bool FlagSet(int flags, ValidationFlags f) {
00094 return (flags & f) != 0;
00095 }
00096
00098 void CheckFlags(int flags) {
00099 CHECK(ExactlyOneTrue(FlagSet(flags, kExpectSuccess),
00100 FlagSet(flags, kExpectNoChange),
00101 FlagSet(flags, kExpectFallback),
00102 FlagSet(flags, kExpectFailure)));
00103 }
00104
00106 void ValidateRewriteInlineCss(const StringPiece& id,
00107 const StringPiece& css_input,
00108 const StringPiece& expected_css_output,
00109 int flags);
00110
00112 GoogleString ExpectedRewrittenUrl(const StringPiece& original_url,
00113 const StringPiece& expected_contents,
00114 const StringPiece& filter_id,
00115 const ContentType& content_type);
00116
00117 void GetNamerForCss(const StringPiece& id,
00118 const GoogleString& expected_css_output,
00119 ResourceNamer* namer);
00120
00121 GoogleString ExpectedUrlForNamer(const ResourceNamer& namer);
00122
00123 GoogleString ExpectedUrlForCss(const StringPiece& id,
00124 const GoogleString& expected_css_output);
00125
00127 void ValidateRewriteExternalCss(const StringPiece& id,
00128 const GoogleString& css_input,
00129 const GoogleString& expected_css_output,
00130 int flags) {
00131 ValidateRewriteExternalCssUrl(StrCat(kTestDomain, id, ".css"),
00132 css_input, expected_css_output, flags);
00133 }
00134
00135 void ValidateRewriteExternalCssUrl(const StringPiece& css_url,
00136 const GoogleString& css_input,
00137 const GoogleString& expected_css_output,
00138 int flags);
00139
00140
00141 void ValidateRewrite(const StringPiece& id,
00142 const GoogleString& css_input,
00143 const GoogleString& gold_output,
00144 int flags) {
00145 ValidateRewriteInlineCss(StrCat(id, "-inline"),
00146 css_input, gold_output, flags);
00147 ValidateRewriteExternalCss(StrCat(id, "-external"),
00148 css_input, gold_output, flags);
00149 }
00150
00151 void ValidateFailParse(const StringPiece& id, const GoogleString& css_input) {
00152 ValidateRewrite(id, css_input, css_input, kExpectFailure);
00153 }
00154
00156 void ResetStats();
00157
00159 void ValidateWithStats(
00160 const StringPiece& id,
00161 const GoogleString& html_input, const GoogleString& expected_html_output,
00162 const StringPiece& css_input, const StringPiece& expected_css_output,
00163 int flags);
00164
00166 void TestCorruptUrl(const char* junk);
00167
00168 Variable* num_blocks_rewritten_;
00169 Variable* num_parse_failures_;
00170 Variable* num_rewrites_dropped_;
00171 Variable* total_bytes_saved_;
00172 Variable* total_original_bytes_;
00173 Variable* num_uses_;
00174 };
00175
00176 }
00177
00178 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_CSS_REWRITE_TEST_BASE_H_