Index: content/browser/browsing_data/clear_site_data_header_observer_unittest.cc |
diff --git a/content/browser/browsing_data/clear_site_data_header_observer_unittest.cc b/content/browser/browsing_data/clear_site_data_header_observer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..daa81e450bb18b5f974aef92eb9e56e220d1da63 |
--- /dev/null |
+++ b/content/browser/browsing_data/clear_site_data_header_observer_unittest.cc |
@@ -0,0 +1,140 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/browsing_data/clear_site_data_header_observer.h" |
+ |
+#include <memory> |
+ |
+#include "base/memory/ref_counted.h" |
+#include "content/public/browser/site_instance.h" |
+#include "content/public/test/test_browser_context.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
+#include "content/test/test_web_contents.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace content { |
+ |
+class ClearSiteDataHeaderObserverTest : public testing::Test { |
+ public: |
+ void SetUp() override { |
+ site_instance_ = SiteInstance::Create(&browser_context_); |
+ web_contents_.reset( |
+ TestWebContents::Create(&browser_context_, site_instance_.get())); |
+ observer_.reset(new ClearSiteDataHeaderObserver(web_contents_.get())); |
+ } |
+ |
+ ClearSiteDataHeaderObserver* GetObserver() { return observer_.get(); } |
+ |
+ private: |
+ TestBrowserThreadBundle browser_thread_bundle_; |
+ TestBrowserContext browser_context_; |
+ scoped_refptr<SiteInstance> site_instance_; |
+ std::unique_ptr<TestWebContents> web_contents_; |
+ std::unique_ptr<ClearSiteDataHeaderObserver> observer_; |
+}; |
+ |
+TEST_F(ClearSiteDataHeaderObserverTest, ParseHeader) { |
+ struct TestCase { |
+ const char* header; |
+ bool cookies; |
+ bool storage; |
+ bool cache; |
+ } test_cases[] = { |
+ // One data type. |
+ {"{ \"types\": [\"cookies\"] }", true, false, false}, |
+ {"{ \"types\": [\"storage\"] }", false, true, false}, |
+ {"{ \"types\": [\"cache\"] }", false, false, true}, |
+ |
+ // Two data types. |
+ {"{ \"types\": [\"cookies\", \"storage\"] }", true, true, false}, |
+ {"{ \"types\": [\"cookies\", \"cache\"] }", true, false, true}, |
+ {"{ \"types\": [\"storage\", \"cache\"] }", false, true, true}, |
+ |
+ // Three data types. |
+ {"{ \"types\": [\"storage\", \"cache\", \"cookies\"] }", true, true, |
+ true}, |
+ {"{ \"types\": [\"cache\", \"cookies\", \"storage\"] }", true, true, |
+ true}, |
+ {"{ \"types\": [\"cookies\", \"storage\", \"cache\"] }", true, true, |
+ true}, |
+ |
+ // Different formatting. |
+ {" { \"types\": [\"cookies\" ]}", true, false, false}, |
+ |
+ // Duplicates. |
+ {"{ \"types\": [\"cookies\", \"cookies\"] }", true, false, false}, |
+ |
+ // Other entries in the dictionary. |
+ {"{ \"types\": [\"storage\"], \"other_params\": {} }", false, true, |
+ false}, |
+ |
+ // Unknown types are ignored, but we still proceed with the deletion for |
+ // those that we recognize. |
+ {"{ \"types\": [\"cache\", \"foo\"] }", false, false, true}, |
+ }; |
+ |
+ for (const TestCase& test_case : test_cases) { |
+ SCOPED_TRACE(test_case.header); |
+ |
+ bool actual_cookies; |
+ bool actual_storage; |
+ bool actual_cache; |
+ |
+ std::vector<ClearSiteDataHeaderObserver::ConsoleMessage> messages; |
+ |
+ EXPECT_TRUE(GetObserver()->ParseHeader(test_case.header, &actual_cookies, |
+ &actual_storage, &actual_cache, |
+ &messages)); |
+ |
+ EXPECT_EQ(test_case.cookies, actual_cookies); |
+ EXPECT_EQ(test_case.storage, actual_storage); |
+ EXPECT_EQ(test_case.cache, actual_cache); |
+ } |
+} |
+ |
+TEST_F(ClearSiteDataHeaderObserverTest, InvalidHeader) { |
+ struct TestCase { |
+ const char* header; |
+ const char* console_message; |
+ } test_cases[] = { |
+ {"", " is not a valid JSON.\n"}, |
+ {"\"unclosed quote", "\"unclosed quote is not a valid JSON.\n"}, |
+ {"\"some text\"", |
+ "\"some text\" is not a JSON dictionary with a 'types' field.\n"}, |
+ {"{ \"field\" : {} }", |
+ "{ \"field\" : {} } is not a JSON dictionary with a 'types' field.\n"}, |
+ {"{ \"types\" : [ \"passwords\" ] }", |
+ "Invalid type: \"passwords\".\n" |
+ "No valid types specified in { \"types\" : [ \"passwords\" ] }.\n"}, |
+ {"{ \"types\" : [ [ \"list in a list\" ] ] }", |
+ "Invalid type: [\"list in a list\"].\n" |
+ "No valid types specified in " |
+ "{ \"types\" : [ [ \"list in a list\" ] ] }.\n"}, |
+ }; |
+ |
+ for (const TestCase& test_case : test_cases) { |
+ SCOPED_TRACE(test_case.header); |
+ |
+ bool actual_cookies; |
+ bool actual_storage; |
+ bool actual_cache; |
+ |
+ std::vector<ClearSiteDataHeaderObserver::ConsoleMessage> messages; |
+ |
+ EXPECT_FALSE(GetObserver()->ParseHeader(test_case.header, &actual_cookies, |
+ &actual_storage, &actual_cache, |
+ &messages)); |
+ |
+ std::string multiline_message; |
+ for (const auto& message : messages) { |
+ EXPECT_EQ(CONSOLE_MESSAGE_LEVEL_ERROR, message.level); |
+ multiline_message += message.text + "\n"; |
+ } |
+ |
+ EXPECT_EQ(test_case.console_message, multiline_message); |
+ } |
+} |
+ |
+} // namespace content |