summaryrefslogtreecommitdiffstats
path: root/chrome/common/net/cache_uitest.cc
blob: 99b41860de08fe1364887f503bd9a5b1533613a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/string_util.h"
#include "chrome/test/ui/ui_test.h"
#include "chrome/test/automation/automation_proxy.h"
#include "chrome/test/automation/browser_proxy.h"
#include "chrome/test/automation/tab_proxy.h"
#include "net/url_request/url_request_unittest.h"

// The CacheTest class extends the UITest class and provides functions to
// get a new tab and to run the tests on a particular path
//
// Typical usage:
//
//   1. Provide this class as the TestCase for TEST_F macro
//   2. Then run the cache test on a specific path using the function
//      RunCacheTest
//
// For example:
//
//  TEST_F(CacheTest, NoCacheMaxAge) {
//    RunCacheTest(L"nocachetime/maxage", false, false);
//  }
//
// Note that delays used in running the test is initialized to defaults
class CacheTest : public UITest {
  protected:

    // Runs the cache test for the specified path.
    // Can specify the test to check if a new tab is loaded from the cache
    // and also if a delayed reload is required. A true value passed to the
    // third parameter causes a delayed reload of the path in a new tab.
    // The amount of delay is set by a class constant.
    void RunCacheTest(const std::wstring &url,
                      bool expect_new_tab_cached,
                      bool expect_delayed_reload);

  private:
    // Class constants
    static const int kWaitForCacheUpdateMsec = 2000;
    static const int kCacheWaitMultiplier = 4;  // Used to increase delay

    // Appends a new tab to the test chrome window and loads the specified
    // URL. The new tab will try to get the URL from the cache before requesting
    // the server for it.
    void GetNewTab(AutomationProxy* automationProxy, const GURL& tab_url);
};

// Runs the cache test for the specified path.
void CacheTest::RunCacheTest(const std::wstring &url,
                             bool expect_new_tab_cached,
                             bool expect_delayed_reload) {
  scoped_refptr<HTTPTestServer> server =
      HTTPTestServer::CreateServer(L"chrome/test/data", NULL);
  ASSERT_TRUE(NULL != server.get());
  GURL test_page(server->TestServerPageW(url));

  NavigateToURL(test_page);
  std::wstring original_time = GetActiveTabTitle();

  Sleep(kWaitForCacheUpdateMsec);

  GetNewTab(automation(), test_page);
  std::wstring revisit_time = GetActiveTabTitle();

  if (expect_new_tab_cached) {
    EXPECT_EQ(original_time, revisit_time);
  }else {
    EXPECT_NE(original_time, revisit_time);
  }

  Sleep(kWaitForCacheUpdateMsec);

  // Force reload, overriding the caching behavior
  NavigateToURL(test_page);
  std::wstring reload_time = GetActiveTabTitle();

  EXPECT_NE(revisit_time, reload_time);

  if (expect_delayed_reload) {
    Sleep(kWaitForCacheUpdateMsec * kCacheWaitMultiplier);

    GetNewTab(automation(), test_page);
    revisit_time = GetActiveTabTitle();

    EXPECT_NE(reload_time, revisit_time);
  }
}

// Appends a new tab to the test chrome window and loads the specified URL.
void CacheTest::GetNewTab(AutomationProxy* automationProxy,
                          const GURL& tab_url) {
  scoped_ptr<BrowserProxy> window_proxy(automationProxy->GetBrowserWindow(0));
  ASSERT_TRUE(window_proxy.get());
  ASSERT_TRUE(window_proxy->AppendTab(tab_url));
}

// Tests that a cached copy of the page is not used when max-age=0 headers
// are specified.
TEST_F(CacheTest, NoCacheMaxAge) {
  RunCacheTest(L"nocachetime/maxage", false, false);
}

// Tests that a cached copy of the page is not used when no-cache header
// is specified.
TEST_F(CacheTest, NoCache) {
  RunCacheTest(L"nocachetime", false, false);
}

// Tests that a cached copy of a page is used when its headers specify
// that it should be cached for 60 seconds.
TEST_F(CacheTest, Cache) {
  RunCacheTest(L"cachetime", true, false);
}

// Tests that a cached copy of the page is used when expires header
// specifies that the page has not yet expired.
TEST_F(CacheTest, Expires) {
  RunCacheTest(L"cache/expires", true, false);
}

// Tests that a cached copy of the page is used when proxy-revalidate header
// is specified and the page has not yet expired.
TEST_F(CacheTest, ProxyRevalidate) {
  RunCacheTest(L"cache/proxy-revalidate", true, false);
}

// Tests that a cached copy of the page is used when private header
// is specified and the page has not yet expired.
TEST_F(CacheTest, Private) {
  RunCacheTest(L"cache/private", true, true);
}

// Tests that a cached copy of the page is used when public header
// is specified and the page has not yet expired.
TEST_F(CacheTest, Public) {
  RunCacheTest(L"cache/public", true, true);
}

// Tests that a cached copy of the page is not used when s-maxage header
// is specified.
TEST_F(CacheTest, SMaxAge) {
  RunCacheTest(L"cache/s-maxage", false, false);
}

// Tests that a cached copy of the page is not used when must-revalidate header
// is specified.
TEST_F(CacheTest, MustRevalidate) {
  RunCacheTest(L"cache/must-revalidate", false, false);
}

// Tests that a cached copy of the page is not used when must-revalidate header
// is specified, even though the page has not yet expired.
TEST_F(CacheTest, MustRevalidateMaxAge) {
  RunCacheTest(L"cache/must-revalidate/max-age", false, false);
}

// Tests that a cached copy of the page is not used when no-store header
// is specified.
TEST_F(CacheTest, NoStore) {
  RunCacheTest(L"cache/no-store", false, false);
}

// Tests that a cached copy of the page is not used when no-store header
// is specified, even though the page has not yet expired.
TEST_F(CacheTest, NoStoreMaxAge) {
  RunCacheTest(L"cache/no-store/max-age", false, false);
}

// Tests that a cached copy of the page is not transformed when no-transform
// header is specified.
TEST_F(CacheTest, NoTransform) {
  RunCacheTest(L"cache/no-transform", false, false);
}