
// Author: mmohabey@google.com (Megha Mohabey)

#include "net/instaweb/rewriter/public/deterministic_js_filter.h"

#include "net/instaweb/rewriter/public/server_context.h"
#include "net/instaweb/rewriter/public/rewrite_test_base.h"
#include "net/instaweb/rewriter/public/rewrite_driver.h"
#include "net/instaweb/rewriter/public/static_asset_manager.h"
#include "net/instaweb/util/public/gtest.h"
#include "net/instaweb/util/public/scoped_ptr.h"
#include "net/instaweb/util/public/string.h"
#include "net/instaweb/util/public/string_util.h"

namespace net_instaweb {

class DeterministicJsFilterTest : public RewriteTestBase {
 public:
  DeterministicJsFilterTest() {}
 protected:
  virtual void SetUp() {
    RewriteTestBase::SetUp();
    SetHtmlMimetype();  // Prevent insertion of CDATA tags to static JS.
    deterministic_js_filter_.reset(new DeterministicJsFilter(rewrite_driver()));
    rewrite_driver()->AddFilter(deterministic_js_filter_.get());
  }

 private:
  scoped_ptr<DeterministicJsFilter> deterministic_js_filter_;

  DISALLOW_COPY_AND_ASSIGN(DeterministicJsFilterTest);
};

TEST_F(DeterministicJsFilterTest, DeterministicJsInjection) {
  StringPiece deterministic_js_code =
      server_context()->static_asset_manager()->GetAsset(
          StaticAssetManager::kDeterministicJs, options());
  GoogleString expected_str = StrCat("<head><script type=\"text/javascript\" "
                                     "pagespeed_no_defer>",
                                     deterministic_js_code,
                                     "</script></head><body></body>");

  // Check if StaticAssetManager populated the script correctly.
  EXPECT_NE(GoogleString::npos, deterministic_js_code.find("Date"));
  EXPECT_NE(GoogleString::npos, deterministic_js_code.find("Math.random"));
  // Check if the deterministic js is inserted correctly.
  ValidateExpected("deterministicJs_injection",
                   "<head></head><body></body>",
                   expected_str);
}

TEST_F(DeterministicJsFilterTest, DeterministicJsInjectionWithSomeHeadContent) {
  StringPiece deterministic_js_code =
      server_context()->static_asset_manager()->GetAsset(
          StaticAssetManager::kDeterministicJs, options());
  GoogleString expected_str = StrCat("<head><script type=\"text/javascript\" "
                                     "pagespeed_no_defer>",
                                     deterministic_js_code,
                                     "</script>"
                                     "<link rel=\"stylesheet\" href=\"a.css\">"
                                     "</head><body></body>");

  // Check if StaticAssetManager populated the script correctly.
  EXPECT_NE(GoogleString::npos, deterministic_js_code.find("Date"));
  EXPECT_NE(GoogleString::npos, deterministic_js_code.find("Math.random"));
  // Check if the deterministic js is inserted correctly.
  ValidateExpected("deterministicJs_injection",
                   "<head><link rel=\"stylesheet\" href=\"a.css\">"
                   "</head><body></body>",
                   expected_str);
}
}  // namespace net_instaweb
