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 "content/browser/browsing_data/clear_site_data_throttle.h" | 5 #include "content/browser/browsing_data/clear_site_data_throttle.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/test/scoped_command_line.h" | |
12 #include "content/common/net/url_request_service_worker_data.h" | |
10 #include "content/public/common/content_switches.h" | 13 #include "content/public/common/content_switches.h" |
14 #include "net/base/load_flags.h" | |
15 #include "net/url_request/url_request_test_util.h" | |
11 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
13 | 18 |
14 namespace content { | 19 namespace content { |
15 | 20 |
16 class ClearSiteDataThrottleTest : public testing::Test { | 21 class ClearSiteDataThrottleTest : public testing::Test { |
17 public: | 22 public: |
18 void SetUp() override { | 23 void SetUp() override { throttle_.reset(new ClearSiteDataThrottle(nullptr)); } |
19 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
20 switches::kEnableExperimentalWebPlatformFeatures); | |
21 throttle_ = ClearSiteDataThrottle::CreateThrottleForNavigation(nullptr); | |
22 } | |
23 | 24 |
24 ClearSiteDataThrottle* GetThrottle() { | 25 ClearSiteDataThrottle* GetThrottle() { |
25 return static_cast<ClearSiteDataThrottle*>(throttle_.get()); | 26 return static_cast<ClearSiteDataThrottle*>(throttle_.get()); |
26 } | 27 } |
27 | 28 |
28 private: | 29 private: |
29 std::unique_ptr<NavigationThrottle> throttle_; | 30 base::MessageLoop message_loop_; |
31 std::unique_ptr<ResourceThrottle> throttle_; | |
30 }; | 32 }; |
31 | 33 |
34 TEST_F(ClearSiteDataThrottleTest, CreateThrottleForRequest) { | |
35 // Enable experimental features. | |
36 std::unique_ptr<base::test::ScopedCommandLine> command_line( | |
37 new base::test::ScopedCommandLine()); | |
38 command_line->GetProcessCommandLine()->AppendSwitch( | |
39 switches::kEnableExperimentalWebPlatformFeatures); | |
40 | |
41 // Create a URL request. | |
42 GURL url("https://www.example.com"); | |
43 net::TestURLRequestContext context; | |
44 std::unique_ptr<net::URLRequest> request( | |
45 context.CreateRequest(url, net::DEFAULT_PRIORITY, nullptr)); | |
46 | |
47 // We will not create the throttle for an empty ResourceRequestInfo. | |
48 EXPECT_FALSE(ClearSiteDataThrottle::CreateThrottleForRequest(request.get())); | |
49 | |
50 // We can create the throttle for a valid ResourceRequestInfo. | |
51 ResourceRequestInfo::AllocateForTesting(request.get(), RESOURCE_TYPE_IMAGE, | |
52 nullptr, 0, 0, 0, false, true, true, | |
53 true, false); | |
54 EXPECT_TRUE(ClearSiteDataThrottle::CreateThrottleForRequest(request.get())); | |
55 | |
56 // But not if experimental web features are disabled again. | |
57 request->SetLoadFlags(net::LOAD_NORMAL); | |
58 command_line.reset(); | |
59 EXPECT_FALSE(ClearSiteDataThrottle::CreateThrottleForRequest(request.get())); | |
60 } | |
61 | |
32 TEST_F(ClearSiteDataThrottleTest, ParseHeader) { | 62 TEST_F(ClearSiteDataThrottleTest, ParseHeader) { |
mmenke
2016/10/21 15:16:21
We don't have any tests of the deferring behavior,
mmenke
2016/10/21 15:16:21
I suggest making all these tests more integration-
msramek
2016/10/31 19:23:36
I disassembled ClearSiteDataThrottle a bit to make
mmenke
2016/11/08 18:23:59
Hrm...That seems a little weird. From a layering
| |
33 struct TestCase { | 63 struct TestCase { |
34 const char* header; | 64 const char* header; |
35 bool cookies; | 65 bool cookies; |
36 bool storage; | 66 bool storage; |
37 bool cache; | 67 bool cache; |
38 } test_cases[] = { | 68 } test_cases[] = { |
39 // One data type. | 69 // One data type. |
40 {"{ \"types\": [\"cookies\"] }", true, false, false}, | 70 {"{ \"types\": [\"cookies\"] }", true, false, false}, |
41 {"{ \"types\": [\"storage\"] }", false, true, false}, | 71 {"{ \"types\": [\"storage\"] }", false, true, false}, |
42 {"{ \"types\": [\"cache\"] }", false, false, true}, | 72 {"{ \"types\": [\"cache\"] }", false, false, true}, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 {"\"some text\"", "Expecting a JSON dictionary with a 'types' field.\n"}, | 128 {"\"some text\"", "Expecting a JSON dictionary with a 'types' field.\n"}, |
99 {"{ \"field\" : {} }", | 129 {"{ \"field\" : {} }", |
100 "Expecting a JSON dictionary with a 'types' field.\n"}, | 130 "Expecting a JSON dictionary with a 'types' field.\n"}, |
101 {"{ \"types\" : [ \"passwords\" ] }", | 131 {"{ \"types\" : [ \"passwords\" ] }", |
102 "Invalid type: \"passwords\".\n" | 132 "Invalid type: \"passwords\".\n" |
103 "No valid types specified in the 'types' field.\n"}, | 133 "No valid types specified in the 'types' field.\n"}, |
104 {"{ \"types\" : [ [ \"list in a list\" ] ] }", | 134 {"{ \"types\" : [ [ \"list in a list\" ] ] }", |
105 "Invalid type: [\"list in a list\"].\n" | 135 "Invalid type: [\"list in a list\"].\n" |
106 "No valid types specified in the 'types' field.\n"}, | 136 "No valid types specified in the 'types' field.\n"}, |
107 {"{ \"types\" : [ \"кукис\", \"сторидж\", \"кэш\" ]", | 137 {"{ \"types\" : [ \"кукис\", \"сторидж\", \"кэш\" ]", |
108 "Must only contain ASCII characters.\n"}}; | 138 "Must only contain ASCII characters.\n"}}; |
mmenke
2016/10/21 15:16:21
How hard would it be to write integration tests th
msramek
2016/10/31 19:23:36
The call goes to RenderFrameHostImpl. So far, I ca
| |
109 | 139 |
110 for (const TestCase& test_case : test_cases) { | 140 for (const TestCase& test_case : test_cases) { |
111 SCOPED_TRACE(test_case.header); | 141 SCOPED_TRACE(test_case.header); |
112 | 142 |
113 bool actual_cookies; | 143 bool actual_cookies; |
114 bool actual_storage; | 144 bool actual_storage; |
115 bool actual_cache; | 145 bool actual_cache; |
116 | 146 |
117 std::vector<ClearSiteDataThrottle::ConsoleMessage> messages; | 147 std::vector<ClearSiteDataThrottle::ConsoleMessage> messages; |
118 | 148 |
119 EXPECT_FALSE(GetThrottle()->ParseHeader(test_case.header, &actual_cookies, | 149 EXPECT_FALSE(GetThrottle()->ParseHeader(test_case.header, &actual_cookies, |
120 &actual_storage, &actual_cache, | 150 &actual_storage, &actual_cache, |
121 &messages)); | 151 &messages)); |
122 | 152 |
123 std::string multiline_message; | 153 std::string multiline_message; |
124 for (const auto& message : messages) { | 154 for (const auto& message : messages) { |
125 EXPECT_EQ(CONSOLE_MESSAGE_LEVEL_ERROR, message.level); | 155 EXPECT_EQ(CONSOLE_MESSAGE_LEVEL_ERROR, message.level); |
126 multiline_message += message.text + "\n"; | 156 multiline_message += message.text + "\n"; |
127 } | 157 } |
128 | 158 |
129 EXPECT_EQ(test_case.console_message, multiline_message); | 159 EXPECT_EQ(test_case.console_message, multiline_message); |
130 } | 160 } |
131 } | 161 } |
132 | 162 |
133 } // namespace content | 163 } // namespace content |
OLD | NEW |