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
3 // found in the LICENSE file.
4 
5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data.h"
6 
7 #include <stdint.h>
8 
9 #include <memory>
10 #include <string>
11 
12 #include "base/stl_util.h"
13 #include "base/test/task_environment.h"
14 #include "net/base/request_priority.h"
15 #include "net/nqe/effective_connection_type.h"
16 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h"
19 
20 namespace data_reduction_proxy {
21 
22 namespace {
23 
24 class DataReductionProxyDataTest : public testing::Test {
25  public:
DataReductionProxyDataTest()26   DataReductionProxyDataTest() {}
27 
28  private:
29   base::test::SingleThreadTaskEnvironment task_environment_{
30       base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
31 };
32 
TEST_F(DataReductionProxyDataTest,BasicSettersAndGetters)33 TEST_F(DataReductionProxyDataTest, BasicSettersAndGetters) {
34   std::unique_ptr<DataReductionProxyData> data(new DataReductionProxyData());
35   EXPECT_FALSE(data->used_data_reduction_proxy());
36   data->set_used_data_reduction_proxy(true);
37   EXPECT_TRUE(data->used_data_reduction_proxy());
38   data->set_used_data_reduction_proxy(false);
39   EXPECT_FALSE(data->used_data_reduction_proxy());
40 
41   EXPECT_FALSE(data->lite_page_received());
42   data->set_lite_page_received(true);
43   EXPECT_TRUE(data->lite_page_received());
44   data->set_lite_page_received(false);
45   EXPECT_FALSE(data->lite_page_received());
46 
47   EXPECT_FALSE(data->black_listed());
48   data->set_black_listed(true);
49   EXPECT_TRUE(data->black_listed());
50   data->set_black_listed(false);
51   EXPECT_FALSE(data->black_listed());
52 
53   EXPECT_EQ(std::string(), data->session_key());
54   std::string session_key = "test-key";
55   data->set_session_key(session_key);
56   EXPECT_EQ(session_key, data->session_key());
57 
58   EXPECT_EQ(GURL(std::string()), data->request_url());
59   GURL test_url("test-url");
60   data->set_request_url(test_url);
61   EXPECT_EQ(test_url, data->request_url());
62 
63   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
64             data->effective_connection_type());
65   data->set_effective_connection_type(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE);
66   EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE,
67             data->effective_connection_type());
68 
69   EXPECT_EQ(net::NetworkChangeNotifier::CONNECTION_UNKNOWN,
70             data->connection_type());
71   data->set_connection_type(net::NetworkChangeNotifier::CONNECTION_WIFI);
72   EXPECT_EQ(net::NetworkChangeNotifier::CONNECTION_WIFI,
73             data->connection_type());
74 
75   EXPECT_FALSE(data->page_id());
76   uint64_t page_id = 1;
77   data->set_page_id(page_id);
78   EXPECT_EQ(page_id, data->page_id().value());
79 }
80 
TEST_F(DataReductionProxyDataTest,DeepCopy)81 TEST_F(DataReductionProxyDataTest, DeepCopy) {
82   const struct {
83     bool data_reduction_used;
84     bool lite_page_test_value;
85   } tests[] = {
86       {
87           false, true,
88       },
89       {
90           false, false,
91       },
92       {
93           true, false,
94       },
95       {
96           true, true,
97       },
98   };
99 
100   for (size_t i = 0; i < base::size(tests); ++i) {
101     static const char kSessionKey[] = "test-key";
102     static const GURL kTestURL("test-url");
103     std::unique_ptr<DataReductionProxyData> data(new DataReductionProxyData());
104     data->set_used_data_reduction_proxy(tests[i].data_reduction_used);
105     data->set_lite_page_received(tests[i].lite_page_test_value);
106     data->set_black_listed(tests[i].lite_page_test_value);
107     data->set_session_key(kSessionKey);
108     data->set_request_url(kTestURL);
109     data->set_effective_connection_type(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE);
110     data->set_connection_type(net::NetworkChangeNotifier::CONNECTION_WIFI);
111     data->set_page_id(2u);
112     std::unique_ptr<DataReductionProxyData> copy = data->DeepCopy();
113     EXPECT_EQ(tests[i].lite_page_test_value, copy->lite_page_received());
114     EXPECT_EQ(tests[i].lite_page_test_value, copy->black_listed());
115     EXPECT_EQ(tests[i].data_reduction_used, copy->used_data_reduction_proxy());
116     EXPECT_EQ(kSessionKey, copy->session_key());
117     EXPECT_EQ(kTestURL, copy->request_url());
118     EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_OFFLINE,
119               copy->effective_connection_type());
120     EXPECT_EQ(net::NetworkChangeNotifier::CONNECTION_WIFI,
121               copy->connection_type());
122     EXPECT_EQ(2u, data->page_id().value());
123   }
124 }
125 
126 }  // namespace
127 
128 }  // namespace data_reduction_proxy
129