"Fossies" - the Fresh Open Source Software Archive

Member "incubator-pagespeed-mod-1.14.36.1/net/instaweb/rewriter/add_ids_filter_test.cc" (28 Feb 2020, 9332 Bytes) of package /linux/www/apache_httpd_modules/incubator-pagespeed-mod-1.14.36.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "add_ids_filter_test.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.13.35.2_vs_1.14.36.1.

    1 /*
    2  * Licensed to the Apache Software Foundation (ASF) under one
    3  * or more contributor license agreements.  See the NOTICE file
    4  * distributed with this work for additional information
    5  * regarding copyright ownership.  The ASF licenses this file
    6  * to you under the Apache License, Version 2.0 (the
    7  * "License"); you may not use this file except in compliance
    8  * with the License.  You may obtain a copy of the License at
    9  * 
   10  *   http://www.apache.org/licenses/LICENSE-2.0
   11  * 
   12  * Unless required by applicable law or agreed to in writing,
   13  * software distributed under the License is distributed on an
   14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15  * KIND, either express or implied.  See the License for the
   16  * specific language governing permissions and limitations
   17  * under the License.
   18  */
   19 
   20 
   21 #include "net/instaweb/rewriter/public/add_ids_filter.h"
   22 
   23 #include "net/instaweb/rewriter/public/rewrite_test_base.h"
   24 #include "pagespeed/kernel/base/gtest.h"
   25 #include "pagespeed/kernel/base/string_util.h"
   26 #include "pagespeed/kernel/html/html_parse_test_base.h"
   27 
   28 namespace net_instaweb {
   29 
   30 namespace {
   31 
   32 class AddIdsFilterTest : public RewriteTestBase {
   33  protected:
   34   AddIdsFilterTest()
   35       : add_ids_filter_(rewrite_driver()) {
   36   }
   37 
   38   virtual void SetUp() {
   39     RewriteTestBase::SetUp();
   40     html_parse()->AddFilter(&add_ids_filter_);
   41   }
   42 
   43   virtual bool AddBody() const { return false; }
   44 
   45   // Remove PageSpeed- ids
   46   GoogleString Unlabel(StringPiece labeled) {
   47     GoogleString result;
   48     labeled.CopyToString(&result);
   49     GlobalEraseBracketedSubstring(" id=\"PageSpeed-", "\"", &result);
   50     return result;
   51   }
   52 
   53  private:
   54   AddIdsFilter add_ids_filter_;
   55 };
   56 
   57 TEST_F(AddIdsFilterTest, NoDivtest) {
   58   static const char kExpected[] =
   59       "<html><body>\n"
   60       "  <p>Today's top stories are:</p>\n"
   61       "  <ol>\n"
   62       "    <li><a href='http://www.example1.com/'>"
   63                "Website wins award for most boring URL.</a></li>\n"
   64       "    <li><a href='http://www.example2.com/'>"
   65                "Copycats quickly try to steal some spotlight.</a></li>\n"
   66       "    <li><a href='http://www.example3.com/'>Internet "
   67                "proves itself capable of spawning copycat copycats.</a></li>\n"
   68       "    <li><a href='http://www.example5.com/'>Embarrassed "
   69                "imitator ruins trend.</a></li>\n"
   70       "  </ol>\n"
   71       "</body></html>\n";
   72   ValidateNoChanges("no_div_test", kExpected);
   73 }
   74 
   75 TEST_F(AddIdsFilterTest, WithDivsTest) {
   76   static const char kExpected[] =
   77       "<html><body>\n"
   78       "  <div id='menu'>\n"
   79       "   <ul id=\"PageSpeed-menu-0\">\n"
   80       "    <li><a href='http://www.example.com/home'>"
   81                "HOME</a></li>\n"
   82       "    <li><a href='http://www.example.com/contact_us'>"
   83                "CONTACT US</a></li>\n"
   84       "    <li><a href='http://www.example.com/about'>"
   85                "ABOUT</a></li>\n"
   86       "    <li><div id=\"PageSpeed-menu-0-3-0\">"
   87                "Share this</div></li>\n"
   88       "   </ul>\n"
   89       "  </div>\n"
   90       "  <div id=\"PageSpeed-1\"></div>\n"
   91       "  <div id='content'>\n"
   92       "    <div class='top_story' id=\"PageSpeed-content-0\">\n"
   93       "      <div id=\"PageSpeed-content-0-0\">TOP STORY</div>\n"
   94       "    </div>\n"
   95       "    <div class='stories' id=\"PageSpeed-content-1\">\n"
   96       "      <div id=\"PageSpeed-content-1-0\">STORY ONE</div>\n"
   97       "      <div id=\"PageSpeed-content-1-1\">STORY TWO</div>\n"
   98       "      <div id=\"PageSpeed-content-1-2\">STORY THREE</div>\n"
   99       "    </div>\n"
  100       "  </div>\n"
  101       "</body>\n"
  102       "<div id=\"Pagespeed-2\">"
  103       "   Post-BODY content"
  104       "</div></html>\n";
  105   ValidateExpected("with_divs_test", Unlabel(kExpected), kExpected);
  106 }
  107 
  108 TEST_F(AddIdsFilterTest, BodyHasId) {
  109   static const char kExpected[] =
  110       "<html><body id='body'>\n"
  111       "  <div id='menu'>\n"
  112       "   <ul id=\"PageSpeed-menu-0\">\n"
  113       "    <li><a href='http://www.example.com/home'>"
  114                "HOME</a></li>\n"
  115       "    <li><a href='http://www.example.com/contact_us'>"
  116                "CONTACT US</a></li>\n"
  117       "    <li><a href='http://www.example.com/about'>"
  118                "ABOUT</a></li>\n"
  119       "    <li><div id=\"PageSpeed-menu-0-3-0\">"
  120                "Share this</div></li>\n"
  121       "   </ul>\n"
  122       "  </div>\n"
  123       "  <div id=\"PageSpeed-body-1\"></div>\n"
  124       "  <div id='content'>\n"
  125       "    <div class='top_story' id=\"PageSpeed-content-0\">\n"
  126       "      <div id=\"PageSpeed-content-0-0\">TOP STORY</div>\n"
  127       "    </div>\n"
  128       "    <div class='stories' id=\"PageSpeed-content-1\">\n"
  129       "      <div id=\"PageSpeed-content-1-0\">STORY ONE</div>\n"
  130       "      <div id=\"PageSpeed-content-1-1\">STORY TWO</div>\n"
  131       "      <div id=\"PageSpeed-content-1-2\">STORY THREE</div>\n"
  132       "    </div>\n"
  133       "  </div>\n"
  134       "</body>\n"
  135       "<div id=\"Pagespeed-2\">"
  136       "   Post-BODY content"
  137       "</div></html>\n";
  138   ValidateExpected("body_has_id", Unlabel(kExpected), kExpected);
  139 }
  140 
  141 TEST_F(AddIdsFilterTest, TwoDigitDivCountTest) {
  142   static const char kExpected[] =
  143       "<html><body>\n"
  144       "  <menu id='menu'>\n"
  145       "    <div id=\"PageSpeed-menu-0\">Link 1</div>\n"
  146       "    <div id=\"PageSpeed-menu-1\">Link 2</div>\n"
  147       "    <div id=\"PageSpeed-menu-2\">Link 3</div>\n"
  148       "    <div id=\"PageSpeed-menu-3\">Link 4</div>\n"
  149       "    <div id=\"PageSpeed-menu-4\">Link 5</div>\n"
  150       "    <div id=\"PageSpeed-menu-5\">Link 6</div>\n"
  151       "    <div id=\"PageSpeed-menu-6\">Link 7</div>\n"
  152       "    <div id=\"PageSpeed-menu-7\">Link 8</div>\n"
  153       "    <div id=\"PageSpeed-menu-8\">Link 9</div>\n"
  154       "    <div id=\"PageSpeed-menu-9\">Link 10</div>\n"
  155       "    <div id=\"PageSpeed-menu-10\">Submenu 11\n"
  156       "      <div id=\"PageSpeed-menu-10-0\">Nested 0</div>\n"
  157       "      <div id=\"PageSpeed-menu-10-1\">Nested 1</div>\n"
  158       "      <div id=\"PageSpeed-menu-10-2\">Nested 2</div>\n"
  159       "      <div id=\"PageSpeed-menu-10-3\">Nested 3</div>\n"
  160       "      <div id=\"PageSpeed-menu-10-4\">Nested 4</div>\n"
  161       "      <div id=\"PageSpeed-menu-10-5\">Nested 5</div>\n"
  162       "      <div id=\"PageSpeed-menu-10-6\">Nested 6</div>\n"
  163       "      <div id=\"PageSpeed-menu-10-7\">Nested 7</div>\n"
  164       "      <div id=\"PageSpeed-menu-10-8\">Nested 8</div>\n"
  165       "      <div id=\"PageSpeed-menu-10-9\">Nested 9</div>\n"
  166       "      <div id=\"PageSpeed-menu-10-10\">Nested 10</div>\n"
  167       "    </div>\n"
  168       "  </menu>\n"
  169       "  <div id=\"PageSpeed-1\">\n"
  170       "    This page contains a large menu of links.\n"
  171       "  </div>\n"
  172       "</body></html>\n";
  173   ValidateExpected("with_divs_test", Unlabel(kExpected), kExpected);
  174 }
  175 
  176 TEST_F(AddIdsFilterTest, MidTagFlushTest) {
  177   // The filter relies on the fact that the attributes of a tag stay alive
  178   // across a flush window if the tag is still unclosed (but can safely
  179   // disappear immediately thereafter).
  180   // So we start with some unclosed divs with explicit ids...
  181   static const char kExpected1[] =
  182       "<html><body>\n"
  183       "  <div id='a'>\n"
  184       "    <div id='b'>\n";
  185   // Then after the flush we use those ids to label contained divs.
  186   static const char kExpected2[] =
  187       "      <div id=\"PageSpeed-b-0\">\n"
  188       "        <div id=\"PageSpeed-b-0-0\">\n"
  189       "        </div>\n"
  190       "      </div>\n"
  191       "    </div>\n"
  192       "    <div id=\"Pagespeed-a-0\">\n"
  193       "      <div id=\"Pagespeed-a-0-0\">\n"
  194       "      </div>\n"
  195       "    </div>\n"
  196       "  </div>\n"
  197       "</body></html>\n";
  198   SetupWriter();
  199   rewrite_driver()->StartParse(kTestDomain);
  200   rewrite_driver()->ParseText(kExpected1);
  201   rewrite_driver()->Flush();
  202   rewrite_driver()->ParseText(Unlabel(kExpected2));
  203   rewrite_driver()->FinishParse();
  204   EXPECT_EQ(StrCat(kExpected1, kExpected2), output_buffer_);
  205 }
  206 
  207 TEST_F(AddIdsFilterTest, TableIds) {
  208   // For tables, id assignment still reflects DOM order; this may differ from
  209   // semantic order if the table footer is above the body (as here) or if the
  210   // head is below the body.
  211   static const char kExpected[] =
  212       "<table id=\"PageSpeed-0\">\n"
  213       "  <thead>\n"
  214       "    <tr id=\"PageSpeed-0-0-0\">\n"
  215       "      <th>Header content 1</th>\n"
  216       "      <th>Header content 2</th>\n"
  217       "    </tr>\n"
  218       "  </thead>\n"
  219       "  <tfoot>\n"
  220       "    <tr id=\"PageSpeed-0-1-0\">\n"
  221       "      <td>Footer content 1</td>\n"
  222       "      <td>Footer content 2</td>\n"
  223       "    </tr>\n"
  224       "  </tfoot>\n"
  225       "  <tbody>\n"
  226       "    <tr id=\"PageSpeed-0-2-0\">\n"
  227       "      <td>Body content 1</td>\n"
  228       "      <td>Body content 2</td>\n"
  229       "    </tr>\n"
  230       "  </tbody>\n"
  231       "</table>";
  232   ValidateExpected("table_ids", Unlabel(kExpected), kExpected);
  233 }
  234 
  235 TEST_F(AddIdsFilterTest, UnicodeInIdTest) {
  236   // If there's already an id, don't add another one -- even if the existing id
  237   // uses characters we don't like!
  238   const char kExpected[] =
  239       "<header id='g\xc5\x82\xc3\xb3wna'>Header</header>\n";
  240   ValidateNoChanges("Unicode id", kExpected);
  241 }
  242 
  243 TEST_F(AddIdsFilterTest, EmptyIdTest) {
  244   // Don't touch an empty or bare id.
  245   const char kExpected[] =
  246       "<header id=''>Header</header>\n"
  247       "<content id>Content</content>\n";
  248   ValidateNoChanges("Empty id", kExpected);
  249 }
  250 
  251 }  // namespace
  252 
  253 }  // namespace net_instaweb