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