Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "content/browser/browsing_data/clear_site_data_throttle.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "testing/gmock/include/gmock/gmock.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 class ClearSiteDataThrottleTest : public testing::Test { | |
| 15 public: | |
| 16 void SetUp() override { | |
| 17 throttle_ = ClearSiteDataThrottle::CreateThrottleFor(nullptr); | |
| 18 } | |
| 19 | |
| 20 ClearSiteDataThrottle* GetThrottle() { | |
| 21 return static_cast<ClearSiteDataThrottle*>(throttle_.get()); | |
| 22 } | |
| 23 | |
| 24 private: | |
| 25 std::unique_ptr<NavigationThrottle> throttle_; | |
| 26 }; | |
| 27 | |
| 28 TEST_F(ClearSiteDataThrottleTest, ParseHeader) { | |
| 29 struct TestCase { | |
| 30 const char* header; | |
| 31 bool cookies; | |
| 32 bool storage; | |
| 33 bool cache; | |
| 34 } test_cases[] = { | |
| 35 // One data type. | |
| 36 {"{ \"types\": [\"cookies\"] }", true, false, false}, | |
| 37 {"{ \"types\": [\"storage\"] }", false, true, false}, | |
| 38 {"{ \"types\": [\"cache\"] }", false, false, true}, | |
| 39 | |
| 40 // Two data types. | |
| 41 {"{ \"types\": [\"cookies\", \"cache\"] }", true, false, true}, | |
|
Mike West
2016/06/20 07:57:38
You might as well do all of the options.
msramek
2016/07/15 16:47:39
Done.
| |
| 42 | |
| 43 // Three data types. | |
| 44 {"{ \"types\": [\"storage\", \"cache\", \"cookies\"] }", true, true, true}, | |
|
Mike West
2016/06/20 07:57:38
Might as well add a check that the order doesn't m
msramek
2016/07/15 16:47:39
Done.
| |
| 45 | |
| 46 // Different formatting. | |
| 47 {" { \"types\": [\"cookies\" ]}", true, false, false}, | |
| 48 | |
| 49 // Other entries in the dictionary. | |
| 50 {"{ \"types\": [\"storage\"], \"other_params\": {} }", false, true, false}, | |
| 51 | |
| 52 // Unknown types are ignored, but we still proceed with the deletion for | |
| 53 // those that we recognize. | |
| 54 {"{ \"types\": [\"cache\", \"foo\"] }", false, false, true}, | |
| 55 }; | |
| 56 | |
| 57 for (const TestCase& test_case : test_cases) { | |
| 58 SCOPED_TRACE(test_case.header); | |
| 59 | |
| 60 bool actual_cookies; | |
| 61 bool actual_storage; | |
| 62 bool actual_cache; | |
| 63 | |
| 64 std::vector<ClearSiteDataThrottle::ConsoleMessage> messages; | |
| 65 | |
| 66 EXPECT_TRUE(GetThrottle()->ParseHeader( | |
| 67 test_case.header, | |
| 68 &actual_cookies, &actual_storage, &actual_cache, &messages)); | |
| 69 | |
| 70 EXPECT_EQ(test_case.cookies, actual_cookies); | |
| 71 EXPECT_EQ(test_case.storage, actual_storage); | |
| 72 EXPECT_EQ(test_case.cache, actual_cache); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 TEST_F(ClearSiteDataThrottleTest, InvalidHeader) { | |
| 77 struct TestCase { | |
| 78 const char* header; | |
| 79 const char* console_message; | |
| 80 } test_cases[] = { | |
| 81 { "", " is not a valid JSON.\n" }, | |
| 82 { "\"unclosed quote", "\"unclosed quote is not a valid JSON.\n" }, | |
| 83 { "\"some text\"", "\"some text\" is not a dictionary.\n" }, | |
| 84 { "{ \"field\" : {} }", | |
| 85 "No \'types\' field present in { \"field\" : {} }.\n" }, | |
| 86 { "{ \"types\" : [ \"passwords\" ] }", | |
| 87 "Invalid type: 'passwords'.\n" | |
| 88 "No valid types specified in { \"types\" : [ \"passwords\" ] }.\n" }, | |
| 89 }; | |
| 90 | |
| 91 for (const TestCase& test_case : test_cases) { | |
| 92 SCOPED_TRACE(test_case.header); | |
| 93 | |
| 94 bool actual_cookies; | |
| 95 bool actual_storage; | |
| 96 bool actual_cache; | |
| 97 | |
| 98 std::vector<ClearSiteDataThrottle::ConsoleMessage> messages; | |
| 99 | |
| 100 EXPECT_FALSE(GetThrottle()->ParseHeader( | |
| 101 test_case.header, | |
| 102 &actual_cookies, &actual_storage, &actual_cache, &messages)); | |
| 103 | |
| 104 std::string multiline_message; | |
| 105 for (const ClearSiteDataThrottle::ConsoleMessage& message : messages) { | |
| 106 EXPECT_EQ(CONSOLE_MESSAGE_LEVEL_ERROR, message.level); | |
| 107 multiline_message += message.text + "\n"; | |
| 108 } | |
| 109 | |
| 110 EXPECT_EQ(test_case.console_message, multiline_message); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 } // namespace content | |
| OLD | NEW |