| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" | 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> |
| 10 | 11 |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "net/base/request_priority.h" | 14 #include "net/base/request_priority.h" |
| 14 #include "net/nqe/effective_connection_type.h" | 15 #include "net/nqe/effective_connection_type.h" |
| 15 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 16 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 16 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 37 EXPECT_TRUE(data->used_data_reduction_proxy()); | 38 EXPECT_TRUE(data->used_data_reduction_proxy()); |
| 38 data->set_used_data_reduction_proxy(false); | 39 data->set_used_data_reduction_proxy(false); |
| 39 EXPECT_FALSE(data->used_data_reduction_proxy()); | 40 EXPECT_FALSE(data->used_data_reduction_proxy()); |
| 40 | 41 |
| 41 EXPECT_FALSE(data->lofi_requested()); | 42 EXPECT_FALSE(data->lofi_requested()); |
| 42 data->set_lofi_requested(true); | 43 data->set_lofi_requested(true); |
| 43 EXPECT_TRUE(data->lofi_requested()); | 44 EXPECT_TRUE(data->lofi_requested()); |
| 44 data->set_lofi_requested(false); | 45 data->set_lofi_requested(false); |
| 45 EXPECT_FALSE(data->lofi_requested()); | 46 EXPECT_FALSE(data->lofi_requested()); |
| 46 | 47 |
| 48 EXPECT_FALSE(data->lite_page_received()); |
| 49 data->set_lite_page_received(true); |
| 50 EXPECT_TRUE(data->lite_page_received()); |
| 51 data->set_lite_page_received(false); |
| 52 EXPECT_FALSE(data->lite_page_received()); |
| 53 |
| 54 EXPECT_FALSE(data->lofi_received()); |
| 55 data->set_lofi_received(true); |
| 56 EXPECT_TRUE(data->lofi_received()); |
| 57 data->set_lofi_received(false); |
| 58 EXPECT_FALSE(data->lofi_received()); |
| 59 |
| 47 EXPECT_EQ(std::string(), data->session_key()); | 60 EXPECT_EQ(std::string(), data->session_key()); |
| 48 EXPECT_EQ(GURL(std::string()), data->request_url()); | |
| 49 std::string session_key = "test-key"; | 61 std::string session_key = "test-key"; |
| 50 data->set_session_key(session_key); | 62 data->set_session_key(session_key); |
| 51 EXPECT_EQ(session_key, data->session_key()); | 63 EXPECT_EQ(session_key, data->session_key()); |
| 64 |
| 65 EXPECT_EQ(GURL(std::string()), data->request_url()); |
| 52 GURL test_url("test-url"); | 66 GURL test_url("test-url"); |
| 53 data->set_request_url(test_url); | 67 data->set_request_url(test_url); |
| 54 EXPECT_EQ(test_url, data->request_url()); | 68 EXPECT_EQ(test_url, data->request_url()); |
| 69 |
| 55 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN, | 70 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN, |
| 56 data->effective_connection_type()); | 71 data->effective_connection_type()); |
| 57 data->set_effective_connection_type(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); | 72 data->set_effective_connection_type(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); |
| 58 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE, | 73 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE, |
| 59 data->effective_connection_type()); | 74 data->effective_connection_type()); |
| 75 |
| 76 EXPECT_FALSE(data->page_id()); |
| 60 uint64_t page_id = 1; | 77 uint64_t page_id = 1; |
| 61 EXPECT_FALSE(data->page_id()); | |
| 62 data->set_page_id(page_id); | 78 data->set_page_id(page_id); |
| 63 EXPECT_EQ(page_id, data->page_id().value()); | 79 EXPECT_EQ(page_id, data->page_id().value()); |
| 64 } | 80 } |
| 65 | 81 |
| 66 TEST_F(DataReductionProxyDataTest, AddToURLRequest) { | 82 TEST_F(DataReductionProxyDataTest, AddToURLRequest) { |
| 67 std::unique_ptr<net::URLRequestContext> context(new net::URLRequestContext()); | 83 std::unique_ptr<net::URLRequestContext> context(new net::URLRequestContext()); |
| 68 std::unique_ptr<net::URLRequest> fake_request(context->CreateRequest( | 84 std::unique_ptr<net::URLRequest> fake_request(context->CreateRequest( |
| 69 GURL("http://www.google.com"), net::RequestPriority::IDLE, nullptr, | 85 GURL("http://www.google.com"), net::RequestPriority::IDLE, nullptr, |
| 70 TRAFFIC_ANNOTATION_FOR_TESTS)); | 86 TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 71 DataReductionProxyData* data = | 87 DataReductionProxyData* data = |
| 72 DataReductionProxyData::GetData(*fake_request.get()); | 88 DataReductionProxyData::GetData(*fake_request.get()); |
| 73 EXPECT_FALSE(data); | 89 EXPECT_FALSE(data); |
| 74 data = | 90 data = |
| 75 DataReductionProxyData::GetDataAndCreateIfNecessary(fake_request.get()); | 91 DataReductionProxyData::GetDataAndCreateIfNecessary(fake_request.get()); |
| 76 EXPECT_TRUE(data); | 92 EXPECT_TRUE(data); |
| 77 data = DataReductionProxyData::GetData(*fake_request.get()); | 93 data = DataReductionProxyData::GetData(*fake_request.get()); |
| 78 EXPECT_TRUE(data); | 94 EXPECT_TRUE(data); |
| 79 DataReductionProxyData* data2 = | 95 DataReductionProxyData* data2 = |
| 80 DataReductionProxyData::GetDataAndCreateIfNecessary(fake_request.get()); | 96 DataReductionProxyData::GetDataAndCreateIfNecessary(fake_request.get()); |
| 81 EXPECT_EQ(data, data2); | 97 EXPECT_EQ(data, data2); |
| 82 } | 98 } |
| 83 | 99 |
| 84 TEST_F(DataReductionProxyDataTest, DeepCopy) { | 100 TEST_F(DataReductionProxyDataTest, DeepCopy) { |
| 85 const struct { | 101 const struct { |
| 86 bool data_reduction_used; | 102 bool data_reduction_used; |
| 87 bool lofi_on; | 103 bool lofi_test_value; |
| 88 } tests[] = { | 104 } tests[] = { |
| 89 { | 105 { |
| 90 false, true, | 106 false, true, |
| 91 }, | 107 }, |
| 92 { | 108 { |
| 93 false, false, | 109 false, false, |
| 94 }, | 110 }, |
| 95 { | 111 { |
| 96 true, false, | 112 true, false, |
| 97 }, | 113 }, |
| 98 { | 114 { |
| 99 true, true, | 115 true, true, |
| 100 }, | 116 }, |
| 101 }; | 117 }; |
| 102 | 118 |
| 103 for (size_t i = 0; i < arraysize(tests); ++i) { | 119 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 104 static const char kSessionKey[] = "test-key"; | 120 static const char kSessionKey[] = "test-key"; |
| 105 static const GURL kTestURL("test-url"); | 121 static const GURL kTestURL("test-url"); |
| 106 std::unique_ptr<DataReductionProxyData> data(new DataReductionProxyData()); | 122 std::unique_ptr<DataReductionProxyData> data(new DataReductionProxyData()); |
| 107 data->set_used_data_reduction_proxy(tests[i].data_reduction_used); | 123 data->set_used_data_reduction_proxy(tests[i].data_reduction_used); |
| 108 data->set_lofi_requested(tests[i].lofi_on); | 124 data->set_lofi_requested(tests[i].lofi_test_value); |
| 125 data->set_lite_page_received(tests[i].lofi_test_value); |
| 126 data->set_lofi_received(tests[i].lofi_test_value); |
| 109 data->set_session_key(kSessionKey); | 127 data->set_session_key(kSessionKey); |
| 110 data->set_request_url(kTestURL); | 128 data->set_request_url(kTestURL); |
| 111 data->set_effective_connection_type(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); | 129 data->set_effective_connection_type(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); |
| 112 data->set_page_id(2u); | 130 data->set_page_id(2u); |
| 113 std::unique_ptr<DataReductionProxyData> copy = data->DeepCopy(); | 131 std::unique_ptr<DataReductionProxyData> copy = data->DeepCopy(); |
| 114 EXPECT_EQ(tests[i].lofi_on, copy->lofi_requested()); | 132 EXPECT_EQ(tests[i].lofi_test_value, copy->lofi_requested()); |
| 133 EXPECT_EQ(tests[i].lofi_test_value, copy->lite_page_received()); |
| 134 EXPECT_EQ(tests[i].lofi_test_value, copy->lofi_received()); |
| 115 EXPECT_EQ(tests[i].data_reduction_used, copy->used_data_reduction_proxy()); | 135 EXPECT_EQ(tests[i].data_reduction_used, copy->used_data_reduction_proxy()); |
| 116 EXPECT_EQ(kSessionKey, copy->session_key()); | 136 EXPECT_EQ(kSessionKey, copy->session_key()); |
| 117 EXPECT_EQ(kTestURL, copy->request_url()); | 137 EXPECT_EQ(kTestURL, copy->request_url()); |
| 118 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE, | 138 EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE, |
| 119 copy->effective_connection_type()); | 139 copy->effective_connection_type()); |
| 120 EXPECT_EQ(2u, data->page_id().value()); | 140 EXPECT_EQ(2u, data->page_id().value()); |
| 121 } | 141 } |
| 122 } | 142 } |
| 123 | 143 |
| 124 TEST_F(DataReductionProxyDataTest, ClearData) { | 144 TEST_F(DataReductionProxyDataTest, ClearData) { |
| 125 std::unique_ptr<net::URLRequestContext> context(new net::URLRequestContext()); | 145 std::unique_ptr<net::URLRequestContext> context(new net::URLRequestContext()); |
| 126 std::unique_ptr<net::URLRequest> fake_request(context->CreateRequest( | 146 std::unique_ptr<net::URLRequest> fake_request(context->CreateRequest( |
| 127 GURL("http://www.google.com"), net::RequestPriority::IDLE, nullptr, | 147 GURL("http://www.google.com"), net::RequestPriority::IDLE, nullptr, |
| 128 TRAFFIC_ANNOTATION_FOR_TESTS)); | 148 TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 129 | 149 |
| 130 DataReductionProxyData* data = | 150 DataReductionProxyData* data = |
| 131 DataReductionProxyData::GetDataAndCreateIfNecessary(fake_request.get()); | 151 DataReductionProxyData::GetDataAndCreateIfNecessary(fake_request.get()); |
| 132 EXPECT_TRUE(data); | 152 EXPECT_TRUE(data); |
| 133 DataReductionProxyData::ClearData(fake_request.get()); | 153 DataReductionProxyData::ClearData(fake_request.get()); |
| 134 data = DataReductionProxyData::GetData(*fake_request.get()); | 154 data = DataReductionProxyData::GetData(*fake_request.get()); |
| 135 EXPECT_FALSE(data); | 155 EXPECT_FALSE(data); |
| 136 } | 156 } |
| 137 | 157 |
| 138 } // namespace | 158 } // namespace |
| 139 | 159 |
| 140 } // namespace data_reduction_proxy | 160 } // namespace data_reduction_proxy |
| OLD | NEW |