1 // Copyright (c) 2013 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.
5 #include "content/browser/loader/offline_policy.h"
7 #include "base/command_line.h"
8 #include "content/public/common/content_switches.h"
9 #include "net/base/load_flags.h"
10 #include "net/http/http_response_info.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "webkit/common/resource_type.h"
16 class OfflinePolicyTest
: public testing::Test
{
18 virtual void SetUp() {
19 CommandLine::ForCurrentProcess()->AppendSwitch(
20 switches::kEnableOfflineCacheAccess
);
21 policy_
= new OfflinePolicy
;
24 virtual void TearDown() {
29 OfflinePolicy
* policy_
;
32 // Confirm that the initial state of an offline object is to return
33 // LOAD_FROM_CACHE_IF_OFFLINE until it gets changed.
34 TEST_F(OfflinePolicyTest
, InitialState
) {
35 // Two loads without any reset, no UpdateStateForSuccessfullyStartedRequest.
36 EXPECT_EQ(net::LOAD_FROM_CACHE_IF_OFFLINE
,
37 policy_
->GetAdditionalLoadFlags(0, true));
38 EXPECT_EQ(net::LOAD_FROM_CACHE_IF_OFFLINE
,
39 policy_
->GetAdditionalLoadFlags(0, false));
42 // Completion without any network probing doesn't change result value.
43 TEST_F(OfflinePolicyTest
, CompletedUncertain
) {
44 EXPECT_EQ(net::LOAD_FROM_CACHE_IF_OFFLINE
,
45 policy_
->GetAdditionalLoadFlags(0, true));
46 net::HttpResponseInfo response_info
;
47 policy_
->UpdateStateForSuccessfullyStartedRequest(response_info
);
48 EXPECT_EQ(net::LOAD_FROM_CACHE_IF_OFFLINE
,
49 policy_
->GetAdditionalLoadFlags(0, false));
52 // Completion with a failed network probe changes result value.
53 TEST_F(OfflinePolicyTest
, CompletedNoNetwork
) {
54 EXPECT_EQ(net::LOAD_FROM_CACHE_IF_OFFLINE
,
55 policy_
->GetAdditionalLoadFlags(0, true));
56 net::HttpResponseInfo response_info
;
57 response_info
.server_data_unavailable
= true;
58 policy_
->UpdateStateForSuccessfullyStartedRequest(response_info
);
59 EXPECT_EQ(net::LOAD_ONLY_FROM_CACHE
,
60 policy_
->GetAdditionalLoadFlags(0, false));
63 // Completion with a successful network probe changes result value.
64 TEST_F(OfflinePolicyTest
, CompletedNetwork
) {
65 EXPECT_EQ(net::LOAD_FROM_CACHE_IF_OFFLINE
,
66 policy_
->GetAdditionalLoadFlags(0, true));
67 net::HttpResponseInfo response_info
;
68 response_info
.network_accessed
= true;
69 policy_
->UpdateStateForSuccessfullyStartedRequest(response_info
);
70 EXPECT_EQ(0, policy_
->GetAdditionalLoadFlags(0, false));
73 // A new navigation resets a state change.
74 TEST_F(OfflinePolicyTest
, NewNavigationReset
) {
75 EXPECT_EQ(net::LOAD_FROM_CACHE_IF_OFFLINE
,
76 policy_
->GetAdditionalLoadFlags(0, true));
77 net::HttpResponseInfo response_info
;
78 response_info
.network_accessed
= true;
79 policy_
->UpdateStateForSuccessfullyStartedRequest(response_info
);
80 EXPECT_EQ(0, policy_
->GetAdditionalLoadFlags(0, false));
81 EXPECT_EQ(net::LOAD_FROM_CACHE_IF_OFFLINE
,
82 policy_
->GetAdditionalLoadFlags(0, true));
83 EXPECT_EQ(net::LOAD_FROM_CACHE_IF_OFFLINE
,
84 policy_
->GetAdditionalLoadFlags(0, false));
87 // Cache related flags inhibit the returning of any special flags.
88 TEST_F(OfflinePolicyTest
, ConsumerFlagOverride
) {
89 EXPECT_EQ(0, policy_
->GetAdditionalLoadFlags(net::LOAD_BYPASS_CACHE
, true));
90 net::HttpResponseInfo response_info
;
91 response_info
.server_data_unavailable
= true;
92 policy_
->UpdateStateForSuccessfullyStartedRequest(response_info
);
93 EXPECT_EQ(0, policy_
->GetAdditionalLoadFlags(net::LOAD_BYPASS_CACHE
, false));