| 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 <memory> |
| 6 |
| 5 #include "base/bind.h" | 7 #include "base/bind.h" |
| 6 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 7 #include "components/web_restrictions/browser/mock_web_restrictions_client.h" | 9 #include "components/web_restrictions/browser/mock_web_restrictions_client.h" |
| 8 #include "components/web_restrictions/browser/web_restrictions_client.h" | 10 #include "components/web_restrictions/browser/web_restrictions_client.h" |
| 9 #include "content/public/test/test_browser_thread_bundle.h" | 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 13 |
| 12 using web_restrictions::WebRestrictionsClient; | 14 using web_restrictions::WebRestrictionsClient; |
| 15 using web_restrictions::WebRestrictionsClientResult; |
| 13 using web_restrictions::MockWebRestrictionsClient; | 16 using web_restrictions::MockWebRestrictionsClient; |
| 14 | 17 |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| 17 bool g_returned_result; | 20 bool g_returned_result; |
| 18 | 21 |
| 19 void ResultCallback(const base::Closure& quit_closure, bool result) { | 22 void ResultCallback(const base::Closure& quit_closure, bool result) { |
| 20 g_returned_result = result; | 23 g_returned_result = result; |
| 21 quit_closure.Run(); | 24 quit_closure.Run(); |
| 22 } | 25 } |
| 23 | 26 |
| 24 } // namespace | 27 } // namespace |
| 25 | 28 |
| 29 namespace web_restrictions { |
| 30 |
| 26 class WebRestrictionsClientTest : public testing::Test { | 31 class WebRestrictionsClientTest : public testing::Test { |
| 27 protected: | 32 protected: |
| 33 void SetAuthority(std::string authority) { |
| 34 client_.SetAuthorityTask(authority); |
| 35 } |
| 28 // Mock the Java WebRestrictionsClient. The real version | 36 // Mock the Java WebRestrictionsClient. The real version |
| 29 // would need a content provider to do anything. | 37 // would need a content provider to do anything. |
| 30 MockWebRestrictionsClient mock_; | 38 MockWebRestrictionsClient mock_; |
| 31 content::TestBrowserThreadBundle thread_bundle_; | 39 content::TestBrowserThreadBundle thread_bundle_; |
| 32 WebRestrictionsClient client_; | 40 WebRestrictionsClient client_; |
| 33 }; | 41 }; |
| 34 | 42 |
| 35 TEST_F(WebRestrictionsClientTest, ShouldProceed) { | 43 TEST_F(WebRestrictionsClientTest, ShouldProceed) { |
| 36 client_.SetAuthority("Good"); | 44 SetAuthority("Good"); |
| 37 // First call should go to Web Restrictions Content Provider, and return a | 45 // First call should go to Web Restrictions Content Provider, and return a |
| 38 // delayed result. | 46 // delayed result. |
| 39 { | 47 { |
| 40 g_returned_result = false; | 48 g_returned_result = false; |
| 41 base::RunLoop run_loop; | 49 base::RunLoop run_loop; |
| 42 EXPECT_EQ(web_restrictions::PENDING, | 50 ASSERT_EQ(web_restrictions::PENDING, |
| 43 client_.ShouldProceed( | 51 client_.ShouldProceed( |
| 44 true, GURL("http://example.com"), | 52 true, "http://example.com", |
| 45 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | 53 base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
| 46 run_loop.Run(); | 54 run_loop.Run(); |
| 47 EXPECT_TRUE(g_returned_result); | 55 EXPECT_TRUE(g_returned_result); |
| 48 EXPECT_EQ(1, client_.GetResultColumnCount(GURL("http://example.com"))); | |
| 49 } | 56 } |
| 50 // A repeated call should go to the cache and return a result immediately. | 57 // A repeated call should go to the cache and return a result immediately. |
| 51 { | 58 { |
| 52 base::RunLoop run_loop; | 59 base::RunLoop run_loop; |
| 53 EXPECT_EQ(web_restrictions::ALLOW, | 60 ASSERT_EQ(web_restrictions::ALLOW, |
| 54 client_.ShouldProceed( | 61 client_.ShouldProceed( |
| 55 true, GURL("http://example.com"), | 62 true, "http://example.com", |
| 56 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | 63 base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
| 57 } | 64 } |
| 58 // However a different url should miss the cache | 65 // However a different url should miss the cache |
| 59 { | 66 { |
| 60 g_returned_result = false; | 67 g_returned_result = false; |
| 61 base::RunLoop run_loop; | 68 base::RunLoop run_loop; |
| 62 EXPECT_EQ(web_restrictions::PENDING, | 69 ASSERT_EQ(web_restrictions::PENDING, |
| 63 client_.ShouldProceed( | 70 client_.ShouldProceed( |
| 64 true, GURL("http://example.com/2"), | 71 true, "http://example.com/2", |
| 65 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | 72 base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
| 66 run_loop.Run(); | 73 run_loop.Run(); |
| 67 EXPECT_TRUE(g_returned_result); | 74 EXPECT_TRUE(g_returned_result); |
| 68 } | 75 } |
| 69 // Switching the authority should clear the cache. | 76 // Switching the authority should clear the cache. |
| 70 { | 77 { |
| 71 client_.SetAuthority("Good2"); | 78 SetAuthority("Good2"); |
| 72 g_returned_result = false; | 79 g_returned_result = false; |
| 73 base::RunLoop run_loop; | 80 base::RunLoop run_loop; |
| 74 EXPECT_EQ(web_restrictions::PENDING, | 81 ASSERT_EQ(web_restrictions::PENDING, |
| 75 client_.ShouldProceed( | 82 client_.ShouldProceed( |
| 76 true, GURL("http://example.com/2"), | 83 true, "http://example.com/2", |
| 77 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | 84 base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
| 78 run_loop.Run(); | 85 run_loop.Run(); |
| 79 EXPECT_TRUE(g_returned_result); | 86 EXPECT_TRUE(g_returned_result); |
| 80 } | 87 } |
| 81 // Try getting a bad result | 88 // Try getting a bad result |
| 82 { | 89 { |
| 83 client_.SetAuthority("Bad"); | 90 SetAuthority("Bad"); |
| 84 g_returned_result = true; | 91 g_returned_result = true; |
| 85 base::RunLoop run_loop; | 92 base::RunLoop run_loop; |
| 86 EXPECT_EQ(web_restrictions::PENDING, | 93 ASSERT_EQ(web_restrictions::PENDING, |
| 87 client_.ShouldProceed( | 94 client_.ShouldProceed( |
| 88 true, GURL("http://example.com/2"), | 95 true, "http://example.com/2", |
| 89 base::Bind(&ResultCallback, run_loop.QuitClosure()))); | 96 base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
| 90 run_loop.Run(); | 97 run_loop.Run(); |
| 91 EXPECT_FALSE(g_returned_result); | 98 EXPECT_FALSE(g_returned_result); |
| 92 EXPECT_EQ(3, client_.GetResultColumnCount(GURL("http://example.com/2"))); | 99 std::unique_ptr<const WebRestrictionsClientResult> result = |
| 93 EXPECT_EQ(42, client_.GetResultIntValue(GURL("http://example.com/2"), 1)); | 100 client_.GetCachedWebRestrictionsResult("http://example.com/2"); |
| 94 EXPECT_EQ("Error string", | 101 ASSERT_NE(nullptr, result.get()); |
| 95 client_.GetResultColumnName(GURL("http://example.com/2"), 2)); | 102 EXPECT_EQ(42, result->GetInt(1)); |
| 96 EXPECT_EQ("http://example.com/2", | 103 EXPECT_EQ("http://example.com/2", result->GetString(2)); |
| 97 client_.GetResultStringValue(GURL("http://example.com/2"), 2)); | |
| 98 } | 104 } |
| 99 } | 105 } |
| 100 | 106 |
| 101 TEST_F(WebRestrictionsClientTest, RequestPermission) { | 107 TEST_F(WebRestrictionsClientTest, RequestPermission) { |
| 102 client_.SetAuthority("Good"); | |
| 103 { | 108 { |
| 109 SetAuthority("Good"); |
| 104 base::RunLoop run_loop; | 110 base::RunLoop run_loop; |
| 105 g_returned_result = false; | 111 g_returned_result = false; |
| 106 client_.RequestPermission( | 112 client_.RequestPermission( |
| 107 GURL("http://example.com"), | 113 "http://example.com", |
| 108 base::Bind(&ResultCallback, run_loop.QuitClosure())); | 114 base::Bind(&ResultCallback, run_loop.QuitClosure())); |
| 109 run_loop.Run(); | 115 run_loop.Run(); |
| 110 EXPECT_TRUE(g_returned_result); | 116 EXPECT_TRUE(g_returned_result); |
| 111 client_.SetAuthority("Bad"); | |
| 112 } | 117 } |
| 113 { | 118 { |
| 119 SetAuthority("Bad"); |
| 114 base::RunLoop run_loop; | 120 base::RunLoop run_loop; |
| 115 g_returned_result = true; | 121 g_returned_result = true; |
| 116 client_.RequestPermission( | 122 client_.RequestPermission( |
| 117 GURL("http://example.com"), | 123 "http://example.com", |
| 118 base::Bind(&ResultCallback, run_loop.QuitClosure())); | 124 base::Bind(&ResultCallback, run_loop.QuitClosure())); |
| 119 run_loop.Run(); | 125 run_loop.Run(); |
| 120 EXPECT_FALSE(g_returned_result); | 126 EXPECT_FALSE(g_returned_result); |
| 121 } | 127 } |
| 122 } | 128 } |
| 129 |
| 130 } // namespace web_restrictions |
| OLD | NEW |