Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
css_rewrite_test_base.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
20 
21 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_CSS_REWRITE_TEST_BASE_H_
22 #define NET_INSTAWEB_REWRITER_PUBLIC_CSS_REWRITE_TEST_BASE_H_
23 
24 #include "base/logging.h"
32 
33 namespace net_instaweb {
34 
35 class ResourceNamer;
36 
38  protected:
40  num_blocks_rewritten_ =
41  statistics()->GetVariable(CssFilter::kBlocksRewritten);
42  num_fallback_rewrites_ =
43  statistics()->GetVariable(CssFilter::kFallbackRewrites);
44  num_parse_failures_ = statistics()->GetVariable(CssFilter::kParseFailures);
45  num_rewrites_dropped_ =
46  statistics()->GetVariable(CssFilter::kRewritesDropped);
47  total_bytes_saved_ = statistics()->GetUpDownCounter(
48  CssFilter::kTotalBytesSaved);
49  total_original_bytes_ =
50  statistics()->GetVariable(CssFilter::kTotalOriginalBytes);
51  num_uses_ = statistics()->GetVariable(CssFilter::kUses);
52  num_flatten_imports_charset_mismatch_ =
53  statistics()->GetVariable(CssFilter::kCharsetMismatch);
54  num_flatten_imports_invalid_url_ =
55  statistics()->GetVariable(CssFilter::kInvalidUrl);
56  num_flatten_imports_limit_exceeded_ =
57  statistics()->GetVariable(CssFilter::kLimitExceeded);
58  num_flatten_imports_minify_failed_ =
59  statistics()->GetVariable(CssFilter::kMinifyFailed);
60  num_flatten_imports_recursion_ =
61  statistics()->GetVariable(CssFilter::kRecursion);
62  num_flatten_imports_complex_queries_ =
63  statistics()->GetVariable(CssFilter::kComplexQueries);
64  }
66 
67  virtual void SetUp() {
68  RewriteTestBase::SetUp();
69  options()->set_always_rewrite_css(true);
70  AddFilter(RewriteOptions::kRewriteCss);
71  }
72 
74  kNoFlags = 0,
75 
76  kExpectSuccess = 1<<0,
77  kExpectCached = 1<<1,
78  kExpectNoChange = 1<<2,
80  kExpectFallback = 1<<3,
82  kExpectFailure = 1<<4,
83 
85  kNoStatCheck = 1<<5,
90 
91  kLinkCharsetIsUTF8 = 1<<8,
92  kLinkScreenMedia = 1<<9,
93  kLinkPrintMedia = 1<<10,
94 
95  kMetaCharsetUTF8 = 1<<11,
96  kMetaCharsetISO88591 = 1<<12,
97  kMetaHttpEquiv = 1<<13,
98  kMetaHttpEquivUnquoted = 1<<14,
99 
102  kFlattenImportsInvalidUrl = 1<<16,
103  kFlattenImportsLimitExceeded = 1<<17,
104  kFlattenImportsMinifyFailed = 1<<18,
105  kFlattenImportsRecursion = 1<<19,
106  kFlattenImportsComplexQueries = 1<<20,
107 
109  kInputHtml = 1<<21,
110  kOutputHtml = 1<<22,
111  };
112 
113  static bool ExactlyOneTrue(bool a, bool b) {
114  return a ^ b;
115  }
116  static bool ExactlyOneTrue(bool a, bool b, bool c) {
117  return ExactlyOneTrue(a, ExactlyOneTrue(b, c));
118  }
119  static bool ExactlyOneTrue(bool a, bool b, bool c, bool d) {
120  return ExactlyOneTrue(a, ExactlyOneTrue(b, c, d));
121  }
122  static bool ExactlyOneTrue(bool a, bool b, bool c, bool d, bool e) {
123  return ExactlyOneTrue(a, ExactlyOneTrue(b, c, d, e));
124  }
125 
126  bool FlagSet(int flags, ValidationFlags f) const {
127  return (flags & f) != 0;
128  }
129 
131  void CheckFlags(int flags) {
132  CHECK(ExactlyOneTrue(FlagSet(flags, kExpectSuccess),
133  FlagSet(flags, kExpectCached),
134  FlagSet(flags, kExpectNoChange),
135  FlagSet(flags, kExpectFallback),
136  FlagSet(flags, kExpectFailure)));
137  }
138 
140  bool ValidateRewriteInlineCss(StringPiece id,
141  StringPiece css_input,
142  StringPiece expected_css_output,
143  int flags);
144 
145  void GetNamerForCss(StringPiece id,
146  StringPiece expected_css_output,
147  ResourceNamer* namer);
148 
149  GoogleString ExpectedUrlForNamer(const ResourceNamer& namer);
150 
151  GoogleString ExpectedUrlForCss(StringPiece id,
152  StringPiece expected_css_output);
153 
155  void ValidateRewriteExternalCss(StringPiece id,
156  StringPiece css_input,
157  StringPiece expected_css_output,
158  int flags) {
159  ValidateRewriteExternalCssUrl(id, StrCat(kTestDomain, id, ".css"),
160  css_input, expected_css_output, flags);
161  }
162 
163  void ValidateRewriteExternalCssUrl(StringPiece id,
164  StringPiece css_url,
165  StringPiece css_input,
166  StringPiece expected_css_output,
167  int flags);
168 
170  GoogleString MakeHtmlWithExternalCssLink(StringPiece css_url, int flags,
171  bool insert_debug_message);
172 
174  GoogleString MakeIndentedCssWithImage(StringPiece image_url);
175 
177  GoogleString MakeMinifiedCssWithImage(StringPiece image_url);
178 
180  GoogleString ExtractCssBackgroundImage(StringPiece in_css);
181 
182  void ValidateRewrite(StringPiece id,
183  StringPiece css_input,
184  StringPiece gold_output,
185  int flags) {
186  if (ValidateRewriteInlineCss(StrCat(id, "-inline"),
187  css_input, gold_output, flags)) {
189  ValidateRewriteExternalCss(StrCat(id, "-external"),
190  css_input, gold_output, flags);
191  }
192  }
193 
194  void ValidateFailParse(StringPiece id, StringPiece css_input) {
195  ValidateRewrite(id, css_input, css_input, kExpectFailure);
196  }
197 
199  void ResetStats();
200 
202  bool ValidateWithStats(
203  StringPiece id,
204  StringPiece html_input, StringPiece expected_html_output,
205  StringPiece css_input, StringPiece expected_css_output,
206  int flags);
207 
209  void TestCorruptUrl(const char* junk);
210 
211  Variable* num_blocks_rewritten_;
212  Variable* num_fallback_rewrites_;
213  Variable* num_parse_failures_;
214  Variable* num_rewrites_dropped_;
215  UpDownCounter* total_bytes_saved_;
216  Variable* total_original_bytes_;
217  Variable* num_uses_;
218  Variable* num_flatten_imports_charset_mismatch_;
219  Variable* num_flatten_imports_invalid_url_;
220  Variable* num_flatten_imports_limit_exceeded_;
221  Variable* num_flatten_imports_minify_failed_;
222  Variable* num_flatten_imports_recursion_;
223  Variable* num_flatten_imports_complex_queries_;
224 };
225 
226 }
227 
228 #endif
CSS parser succeeds and URL should be rewritten.
Definition: css_rewrite_test_base.h:76
CSS parser fails, fallback failed or disabled.
Definition: css_rewrite_test_base.h:82
Variable * GetVariable(const StringPiece &name) const
Find a variable from a name, aborting if not found.
Definition: statistics.h:379
bool ValidateRewriteInlineCss(StringPiece id, StringPiece css_input, StringPiece expected_css_output, int flags)
Check that inline CSS gets rewritten correctly.
Definition: css_rewrite_test_base.h:37
void TestCorruptUrl(const char *junk)
Helper to test for how we handle trailing junk.
void CheckFlags(int flags)
Sanity check on flags passed in.
Definition: css_rewrite_test_base.h:131
bool ValidateWithStats(StringPiece id, StringPiece html_input, StringPiece expected_html_output, StringPiece css_input, StringPiece expected_css_output, int flags)
Validate HTML rewrite as well as checking statistics.
Definition: rewrite_test_base.h:90
GoogleString MakeIndentedCssWithImage(StringPiece image_url)
Makes a CSS body with an external image link, with nice indentation.
Flags to the check various import flattening failure statistics.
Definition: css_rewrite_test_base.h:101
ValidationFlags
Definition: css_rewrite_test_base.h:73
Definition: css_rewrite_test_base.h:89
Definition: css_rewrite_test_base.h:87
CSS parser fails, fallback succeeds.
Definition: css_rewrite_test_base.h:81
UpDownCounter * GetUpDownCounter(const StringPiece &name) const
Find a variable from a name, aborting if not found.
Definition: statistics.h:364
void ValidateRewrite(StringPiece id, StringPiece css_input, StringPiece gold_output, int flags)
Definition: css_rewrite_test_base.h:182
GoogleString MakeHtmlWithExternalCssLink(StringPiece css_url, int flags, bool insert_debug_message)
Makes an HTML document with an external CSS link.
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
GoogleString MakeMinifiedCssWithImage(StringPiece image_url)
Makes a minified CSS body with an external image link.
Definition: css_rewrite_test_base.h:79
Definition: css_rewrite_test_base.h:85
void ResetStats()
Reset all Variables.
Flags to allow methods to know if the HTML is the test input or output.
Definition: css_rewrite_test_base.h:109
GoogleString ExtractCssBackgroundImage(StringPiece in_css)
Extract the background image from the css text.
Definition: css_rewrite_test_base.h:77
Definition: resource_namer.h:32
void ValidateRewriteExternalCss(StringPiece id, StringPiece css_input, StringPiece expected_css_output, int flags)
Check that external CSS gets rewritten correctly.
Definition: css_rewrite_test_base.h:155
void AddFilter(RewriteOptions::Filter filter)
Add a single rewrite filter to rewrite_driver_.