22 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_TEST_BASE_H_
23 #define NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_TEST_BASE_H_
30 #include "net/instaweb/rewriter/cached_result.pb.h"
59 namespace net_instaweb {
66 static const char kFilterId[];
74 void ClearStats() { num_rewrites_ = 0; }
77 REFCOUNT_FRIEND_DECLARATION(TrimWhitespaceRewriter);
78 virtual ~TrimWhitespaceRewriter();
80 virtual bool RewriteText(
const StringPiece& url,
const StringPiece& in,
82 ServerContext* server_context);
83 virtual HtmlElement::Attribute* FindResourceAttribute(HtmlElement* element);
85 virtual const char* id()
const {
return kFilterId; }
86 virtual const char* name()
const {
return "TrimWhitespace"; }
102 static const char kFilterId[];
110 virtual void StartElementImpl(
HtmlElement* element);
111 virtual const char* id()
const {
return kFilterId; }
112 virtual const char* name()
const {
return "TrimWhitespaceSync"; }
121 static const char kFilterId[];
124 : kind_(kind), num_rewrites_(0) {}
132 int num_rewrites()
const {
return num_rewrites_; }
133 void ClearStats() { num_rewrites_ = 0; }
139 virtual bool RewriteText(
const StringPiece& url,
const StringPiece& in,
143 in.CopyToString(out);
148 if (element->
keyword() == HtmlName::kLink) {
154 virtual const char* id()
const {
return kFilterId; }
155 virtual const char* name()
const {
return "UpperCase"; }
170 static const bool kExpectNestedRewritesFail =
false;
172 static const char kFilterId[];
177 upper_rewriter_(upper_rewriter), chain_(false),
178 check_nested_rewrite_result_(true),
179 expected_nested_rewrite_result_(expected_nested_result) {
185 int num_sub_rewrites()
const {
return upper_rewriter_->num_rewrites(); }
188 num_top_rewrites_ = 0;
189 upper_rewriter_->ClearStats();
195 bool expected_nested_rewrite_result()
const {
196 return expected_nested_rewrite_result_;
199 void set_expected_nested_rewrite_result(
bool x) {
200 expected_nested_rewrite_result_ = x;
203 void set_check_nested_rewrite_result(
bool x) {
204 check_nested_rewrite_result_ = x;
208 virtual ~NestedFilter();
232 virtual const char*
id()
const {
return kFilterId; }
236 std::vector<GoogleString*> strings_;
239 ResourceSlotVector nested_slots_;
245 return new Context(driver(),
this, chain_);
248 virtual void StartElementImpl(
HtmlElement* element);
251 virtual const char* id()
const {
return kFilterId; }
252 virtual const char*
Name()
const {
return "NestedFilter"; }
254 virtual void EndElementImpl(
HtmlElement* element) {}
258 SimpleTextFilter* upper_filter_;
259 UpperCaseRewriter* upper_rewriter_;
263 bool check_nested_rewrite_result_;
265 bool expected_nested_rewrite_result_;
268 int num_top_rewrites_;
279 static const char kFilterId[];
283 int64 rewrite_delay_ms);
293 return Combine(rewrite_driver_->message_handler());
302 writer->Write(prefix_, handler);
304 index, num_pieces, input, combination, writer, handler);
307 void set_prefix(
const GoogleString& prefix) { prefix_ = prefix; }
310 virtual const ContentType* CombinationContentType() {
311 return &kContentTypeCss;
317 virtual const char* id()
const {
return kFilterId; }
331 virtual bool Partition(OutputPartitions* partitions,
332 OutputResourceVector* outputs);
334 virtual void Rewrite(
int partition_index,
335 CachedResult* partition,
338 return filter_->optimization_only();
341 void DoRewrite(
int partition_index,
342 CachedResult* partition,
348 void DisableRemovedSlots(
const CachedResult* partition);
350 virtual const char*
id()
const {
return kFilterId; }
359 int64 time_at_start_of_rewrite_us_;
364 virtual void StartElementImpl(
HtmlElement* element);
366 if (context_.get() != NULL) {
371 virtual void EndElementImpl(
HtmlElement* element) {}
372 virtual const char*
Name()
const {
return "Combining"; }
374 return new Context(driver(),
this, scheduler_);
380 int num_rewrites()
const {
return num_rewrites_; }
381 int num_render()
const {
return num_render_; }
382 int num_will_not_render()
const {
return num_will_not_render_; }
383 int num_cancel()
const {
return num_cancel_; }
385 void ClearStats() { num_rewrites_ = 0; }
386 int64 rewrite_delay_ms()
const {
return rewrite_delay_ms_; }
387 void set_rewrite_block_on(WorkerTestBase::SyncPoint* sync) {
388 rewrite_block_on_ = sync;
391 void set_rewrite_signal_on(WorkerTestBase::SyncPoint* sync) {
392 rewrite_signal_on_ = sync;
398 void set_on_the_fly(
bool v) { on_the_fly_ = v; }
400 void set_disable_successors(
bool v) { disable_successors_ = v; }
402 bool optimization_only()
const {
return optimization_only_; }
403 void set_optimization_only(
bool o) { optimization_only_ = o; }
406 friend class Context;
408 scoped_ptr<Context> context_;
409 UrlMultipartEncoder encoder_;
410 MockScheduler* scheduler_;
413 int num_will_not_render_;
415 int64 rewrite_delay_ms_;
419 WorkerTestBase::SyncPoint* rewrite_block_on_;
424 WorkerTestBase::SyncPoint* rewrite_signal_on_;
427 bool optimization_only_;
428 bool disable_successors_;
437 static const int64 kRewriteDeadlineMs = 20;
449 return StrCat(
"max-age=", Integer64ToString(
454 std::pair<TestRewriteDriverFactory*, TestRewriteDriverFactory*> factories)
456 RewriteContextTestBase() {}
457 virtual ~RewriteContextTestBase();
459 virtual void SetUp();
460 virtual void TearDown();
461 virtual bool AddBody()
const {
return false; }
462 virtual void ClearStats();
464 void InitResources() { InitResourcesToDomain(kTestDomain); }
465 void InitResourcesToDomain(
const char* domain);
468 void InitCombiningFilter(int64 rewrite_delay_ms);
469 void InitNestedFilter(
bool expected_nested_rewrite_result);
471 TrimWhitespaceRewriter* trim_filter_;
472 TrimWhitespaceRewriter* other_trim_filter_;
473 CombiningFilter* combining_filter_;
474 NestedFilter* nested_filter_;
virtual bool WritePiece(int index, int num_pieces, const Resource *input, OutputResource *combination, Writer *writer, MessageHandler *handler)
Definition: rewrite_context_test_base.h:299
virtual OutputResourceKind kind() const
Definition: rewrite_context_test_base.h:233
Definition: html_element.h:66
Definition: url_multipart_encoder.h:45
virtual const UrlSegmentEncoder * encoder() const
Definition: rewrite_context_test_base.h:349
Definition: mock_scheduler.h:39
Definition: rewrite_context_test_base.h:210
virtual bool WritePiece(int index, int num_pieces, const Resource *input, OutputResource *combination, Writer *writer, MessageHandler *handler)
virtual GoogleString LocationString() const
Definition: rewrite_context_test_base.h:215
virtual const char * Name() const
The name of this filter – used for logging and debugging.
Definition: rewrite_context_test_base.h:372
SingleRewriteContext(RewriteDriver *driver, RewriteContext *parent, ResourceContext *resource_context)
Definition: rewrite_context_test_base.h:286
const char * file_extension() const
Definition: content_type.h:64
ResourceCombiner(RewriteDriver *rewrite_driver, const StringPiece &extension, RewriteFilter *filter)
Note: extension should not include the leading dot here.
virtual bool ComputeOnTheFly() const
Definition: rewrite_context_test_base.h:378
Definition: rewrite_test_base.h:90
virtual RewriteContext * MakeRewriteContext()
Definition: rewrite_context_test_base.h:244
Definition: rewrite_context_test_base.h:277
RewriteDriver * rewrite_driver()
Definition: rewrite_test_base.h:517
bool PolicyPermitsRendering() const override
Definition: rewrite_context_test_base.h:228
virtual void StartDocumentImpl()
Definition: rewrite_context_test_base.h:363
static const int64 kSecondMs
Definition: timer.h:34
Definition: resource_slot.h:54
Definition: output_resource_kind.h:28
Definition: html_element.h:42
Definition: simple_text_filter.h:46
Definition: rewrite_context_test_base.h:100
virtual void Rewrite(int partition_index, CachedResult *partition, const OutputResourcePtr &output)
Definition: resource.h:57
const Attribute * FindAttribute(HtmlName::Keyword keyword) const
A similarly structured test-filter: this one just upper-cases its text.
Definition: rewrite_context_test_base.h:119
static const int64 kOriginTtlMs
Definition: rewrite_context_test_base.h:441
static const int64 kLowOriginTtlMs
Definition: rewrite_context_test_base.h:444
virtual const char * id() const
Returns the filter ID.
Definition: rewrite_context_test_base.h:350
virtual bool Partition(OutputPartitions *partitions, OutputResourceVector *outputs)
virtual void RewriteSingle(const ResourcePtr &input, const OutputResourcePtr &output)
Subclasses of SingleRewriteContext must override this:
virtual bool AddBody() const
Definition: rewrite_context_test_base.h:461
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
virtual void Render()
Definition: rewrite_context_test_base.h:214
GoogleString OriginTtlMaxAge()
Definition: rewrite_context_test_base.h:448
HtmlName::Keyword keyword() const
Definition: html_element.h:322
Definition: rewrite_context_test_base.h:166
void set_chain(bool x)
Set this to true to create a chain of nested rewrites on the same slot.
Definition: rewrite_context_test_base.h:193
int num_rewrites() const
Stats.
Definition: rewrite_context_test_base.h:73
virtual HtmlElement * element() const
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: rewrite_context_test_base.h:213
Definition: rewrite_filter.h:35
Definition: resource_combiner.h:57
virtual bool WriteCombination(const ResourceVector &combine_resources, const OutputResourcePtr &combination, MessageHandler *handler)
Interface for writing bytes to an output stream.
Definition: writer.h:29
Definition: rewrite_driver.h:100
void set_prefix(const GoogleString &prefix)
Each entry in combination will be prefixed with this.
Definition: rewrite_context_test_base.h:396
Definition: server_context.h:99
Definition: content_type.h:31
Definition: rewrite_context.h:155
virtual void Flush()
Definition: rewrite_context_test_base.h:365
virtual bool OptimizationOnly() const
Definition: rewrite_context_test_base.h:337
bool InitiateRewrite(RewriteContext *rewrite_context) LOCKS_EXCLUDED(rewrite_mutex())
Derived from some input resource URL or URLs.
Definition: output_resource_kind.h:27
static const bool kExpectNestedRewritesSucceed
For use with NestedFilter constructor.
Definition: rewrite_context_test_base.h:169
Definition: rewrite_context_test_base.h:218
virtual OutputResourceKind kind() const
Definition: rewrite_context_test_base.h:351
virtual const char * id() const
Returns the filter ID.
Definition: rewrite_context_test_base.h:232
virtual const char * Name() const
The name of this filter – used for logging and debugging.
Definition: rewrite_context_test_base.h:252
Definition: url_segment_encoder.h:33
Definition: single_rewrite_context.h:36
Definition: rewrite_context_test_base.h:319
RewriteContext * MakeRewriteContext()
Definition: rewrite_context_test_base.h:373
Definition: message_handler.h:39
Definition: rewrite_context_test_base.h:435
Definition: output_resource.h:44
OutputResourcePtr Combine(MessageHandler *handler)
virtual void WillNotRender()
Definition: simple_text_filter.h:48
virtual void StartDocumentImpl()
Definition: rewrite_context_test_base.h:253
int num_top_rewrites() const
Stats.
Definition: rewrite_context_test_base.h:184
virtual const UrlSegmentEncoder * encoder() const
Definition: rewrite_context_test_base.h:376
Simple test filter just trims whitespace from the input resource.
Definition: rewrite_context_test_base.h:64
void AddSlot(const ResourceSlotPtr &slot)
OutputResourceKind
Definition: output_resource_kind.h:26
bool PolicyPermitsRendering() const override
Definition: rewrite_context_test_base.h:344