| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "base/platform_thread.h" | |
| 8 #include "base/string_util.h" | |
| 9 #include "chrome/test/ui/ui_test.h" | |
| 10 #include "chrome/test/automation/automation_proxy.h" | |
| 11 #include "chrome/test/automation/browser_proxy.h" | |
| 12 #include "chrome/test/automation/tab_proxy.h" | |
| 13 #include "net/url_request/url_request_unittest.h" | |
| 14 | |
| 15 // The CacheTest class extends the UITest class and provides functions to | |
| 16 // get a new tab and to run the tests on a particular path | |
| 17 // | |
| 18 // Typical usage: | |
| 19 // | |
| 20 // 1. Provide this class as the TestCase for TEST_F macro | |
| 21 // 2. Then run the cache test on a specific path using the function | |
| 22 // RunCacheTest | |
| 23 // | |
| 24 // For example: | |
| 25 // | |
| 26 // TEST_F(CacheTest, NoCacheMaxAge) { | |
| 27 // RunCacheTest(L"nocachetime/maxage", false, false); | |
| 28 // } | |
| 29 // | |
| 30 // Note that delays used in running the test is initialized to defaults | |
| 31 class CacheTest : public UITest { | |
| 32 protected: | |
| 33 | |
| 34 // Runs the cache test for the specified path. | |
| 35 // Can specify the test to check if a new tab is loaded from the cache | |
| 36 // and also if a delayed reload is required. A true value passed to the | |
| 37 // third parameter causes a delayed reload of the path in a new tab. | |
| 38 // The amount of delay is set by a class constant. | |
| 39 void RunCacheTest(const std::wstring &url, | |
| 40 bool expect_new_tab_cached, | |
| 41 bool expect_delayed_reload); | |
| 42 | |
| 43 private: | |
| 44 // Class constants | |
| 45 static const int kWaitForCacheUpdateMsec = 1000; | |
| 46 static const int kCacheWaitMultiplier = 4; // Used to increase delay | |
| 47 | |
| 48 // Appends a new tab to the test chrome window and loads the specified | |
| 49 // URL. The new tab will try to get the URL from the cache before requesting | |
| 50 // the server for it. | |
| 51 void GetNewTab(AutomationProxy* automationProxy, const GURL& tab_url); | |
| 52 }; | |
| 53 | |
| 54 // Runs the cache test for the specified path. | |
| 55 void CacheTest::RunCacheTest(const std::wstring &url, | |
| 56 bool expect_new_tab_cached, | |
| 57 bool expect_delayed_reload) { | |
| 58 scoped_refptr<HTTPTestServer> server = | |
| 59 HTTPTestServer::CreateServer(L"chrome/test/data", NULL); | |
| 60 ASSERT_TRUE(NULL != server.get()); | |
| 61 GURL test_page(server->TestServerPageW(url)); | |
| 62 | |
| 63 NavigateToURL(test_page); | |
| 64 std::wstring original_time = GetActiveTabTitle(); | |
| 65 | |
| 66 PlatformThread::Sleep(kWaitForCacheUpdateMsec); | |
| 67 | |
| 68 GetNewTab(automation(), test_page); | |
| 69 std::wstring revisit_time = GetActiveTabTitle(); | |
| 70 | |
| 71 if (expect_new_tab_cached) { | |
| 72 EXPECT_EQ(original_time, revisit_time); | |
| 73 }else { | |
| 74 EXPECT_NE(original_time, revisit_time); | |
| 75 } | |
| 76 | |
| 77 PlatformThread::Sleep(kWaitForCacheUpdateMsec); | |
| 78 | |
| 79 // Force reload, overriding the caching behavior | |
| 80 NavigateToURL(test_page); | |
| 81 std::wstring reload_time = GetActiveTabTitle(); | |
| 82 | |
| 83 EXPECT_NE(revisit_time, reload_time); | |
| 84 | |
| 85 if (expect_delayed_reload) { | |
| 86 PlatformThread::Sleep(kWaitForCacheUpdateMsec * kCacheWaitMultiplier); | |
| 87 | |
| 88 GetNewTab(automation(), test_page); | |
| 89 revisit_time = GetActiveTabTitle(); | |
| 90 | |
| 91 EXPECT_NE(reload_time, revisit_time); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 // Appends a new tab to the test chrome window and loads the specified URL. | |
| 96 void CacheTest::GetNewTab(AutomationProxy* automationProxy, | |
| 97 const GURL& tab_url) { | |
| 98 scoped_refptr<BrowserProxy> window_proxy(automationProxy->GetBrowserWindow(0))
; | |
| 99 ASSERT_TRUE(window_proxy.get()); | |
| 100 ASSERT_TRUE(window_proxy->AppendTab(tab_url)); | |
| 101 } | |
| 102 | |
| 103 // Tests that a cached copy of the page is not used when max-age=0 headers | |
| 104 // are specified. | |
| 105 TEST_F(CacheTest, NoCacheMaxAge) { | |
| 106 RunCacheTest(L"nocachetime/maxage", false, false); | |
| 107 } | |
| 108 | |
| 109 // Tests that a cached copy of the page is not used when no-cache header | |
| 110 // is specified. | |
| 111 TEST_F(CacheTest, NoCache) { | |
| 112 RunCacheTest(L"nocachetime", false, false); | |
| 113 } | |
| 114 | |
| 115 // Tests that a cached copy of a page is used when its headers specify | |
| 116 // that it should be cached for 60 seconds. | |
| 117 TEST_F(CacheTest, Cache) { | |
| 118 RunCacheTest(L"cachetime", true, false); | |
| 119 } | |
| 120 | |
| 121 // Tests that a cached copy of the page is used when expires header | |
| 122 // specifies that the page has not yet expired. | |
| 123 TEST_F(CacheTest, Expires) { | |
| 124 RunCacheTest(L"cache/expires", true, false); | |
| 125 } | |
| 126 | |
| 127 // Tests that a cached copy of the page is used when proxy-revalidate header | |
| 128 // is specified and the page has not yet expired. | |
| 129 TEST_F(CacheTest, ProxyRevalidate) { | |
| 130 RunCacheTest(L"cache/proxy-revalidate", true, false); | |
| 131 } | |
| 132 | |
| 133 // Tests that a cached copy of the page is used when private header | |
| 134 // is specified and the page has not yet expired. | |
| 135 TEST_F(CacheTest, Private) { | |
| 136 RunCacheTest(L"cache/private", true, true); | |
| 137 } | |
| 138 | |
| 139 // Tests that a cached copy of the page is used when public header | |
| 140 // is specified and the page has not yet expired. | |
| 141 TEST_F(CacheTest, Public) { | |
| 142 RunCacheTest(L"cache/public", true, true); | |
| 143 } | |
| 144 | |
| 145 // Tests that a cached copy of the page is not used when s-maxage header | |
| 146 // is specified. | |
| 147 TEST_F(CacheTest, SMaxAge) { | |
| 148 RunCacheTest(L"cache/s-maxage", false, false); | |
| 149 } | |
| 150 | |
| 151 // Tests that a cached copy of the page is not used when must-revalidate header | |
| 152 // is specified. | |
| 153 TEST_F(CacheTest, MustRevalidate) { | |
| 154 RunCacheTest(L"cache/must-revalidate", false, false); | |
| 155 } | |
| 156 | |
| 157 // Tests that a cached copy of the page is not used when must-revalidate header | |
| 158 // is specified, even though the page has not yet expired. | |
| 159 TEST_F(CacheTest, MustRevalidateMaxAge) { | |
| 160 RunCacheTest(L"cache/must-revalidate/max-age", false, false); | |
| 161 } | |
| 162 | |
| 163 // Tests that a cached copy of the page is not used when no-store header | |
| 164 // is specified. | |
| 165 TEST_F(CacheTest, NoStore) { | |
| 166 RunCacheTest(L"cache/no-store", false, false); | |
| 167 } | |
| 168 | |
| 169 // Tests that a cached copy of the page is not used when no-store header | |
| 170 // is specified, even though the page has not yet expired. | |
| 171 TEST_F(CacheTest, NoStoreMaxAge) { | |
| 172 RunCacheTest(L"cache/no-store/max-age", false, false); | |
| 173 } | |
| 174 | |
| 175 // Tests that a cached copy of the page is not transformed when no-transform | |
| 176 // header is specified. | |
| 177 TEST_F(CacheTest, NoTransform) { | |
| 178 RunCacheTest(L"cache/no-transform", false, false); | |
| 179 } | |
| OLD | NEW |