Chromium Code Reviews| Index: components/web_restrictions/browser/web_restrictions_client_unittest.cc |
| diff --git a/components/web_restrictions/browser/web_restrictions_client_unittest.cc b/components/web_restrictions/browser/web_restrictions_client_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..63d19bc06a1cdd8a95086300d07ab0b31aafa965 |
| --- /dev/null |
| +++ b/components/web_restrictions/browser/web_restrictions_client_unittest.cc |
| @@ -0,0 +1,115 @@ |
| +// 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 "base/bind.h" |
| +#include "base/run_loop.h" |
| +#include "components/web_restrictions/browser/web_restrictions_client.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +/* |
| + * Tests of C++ WebRestrictionsClient. Note that these tests use a dummy |
| + * version of the Java WebRestrictionsClient which gives predefined |
| + * responses based on the authority and the URL. They will not work if |
| + * built with the real version of the Java WebRestrictionsClient. |
|
Bernhard Bauer
2016/02/19 13:46:26
This seems very fragile. How do you know that the
aberent
2016/02/19 19:40:09
Done.
|
| + */ |
| + |
| +using web_restrictions::WebRestrictionsClient; |
| + |
| +namespace { |
| + |
| +bool callback_called; |
|
Bernhard Bauer
2016/02/19 13:46:26
This one isn't really necessary -- the run loop wi
aberent
2016/02/19 19:40:08
Done.
|
| +bool returned_result; |
|
Bernhard Bauer
2016/02/19 13:46:26
|g_returned_result|?
aberent
2016/02/19 19:40:08
Done.
|
| + |
| +void ResultCallback(base::Closure quit_closure, bool result) { |
|
Bernhard Bauer
2016/02/19 13:46:26
Pass the closure by const reference.
aberent
2016/02/19 19:40:08
Done.
|
| + callback_called = true; |
| + returned_result = result; |
| + quit_closure.Run(); |
| +} |
| +} // namespace |
|
Bernhard Bauer
2016/02/19 13:46:26
Nit: Empty line before this one.
aberent
2016/02/19 19:40:08
Done.
|
| + |
| +TEST(WebRestrictionsClientTest, TestShouldProceed) { |
| + content::TestBrowserThreadBundle thread_bundle; |
| + WebRestrictionsClient client; |
| + client.SetAuthority("Good"); |
| + // First call should go to Web Restrictions Content Provider, and return a |
| + // delayed result. |
| + callback_called = false; |
| + returned_result = false; |
| + base::RunLoop run_loop; |
| + EXPECT_EQ(web_restrictions::PENDING, |
| + client.ShouldProceed( |
| + true, GURL("http://example.com"), |
| + base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
| + run_loop.Run(); |
| + EXPECT_TRUE(callback_called); |
| + EXPECT_TRUE(returned_result); |
| + // A repeated call should go to the cache and return a result immediately. |
| + EXPECT_EQ(web_restrictions::ALLOW, |
| + client.ShouldProceed( |
| + true, GURL("http://example.com"), |
| + base::Bind(&ResultCallback, run_loop.QuitClosure()))); |
| + // However a different url should miss the cache |
| + callback_called = false; |
| + returned_result = false; |
| + base::RunLoop run_loop2; |
| + EXPECT_EQ(web_restrictions::PENDING, |
| + client.ShouldProceed( |
| + true, GURL("http://example.com/2"), |
| + base::Bind(&ResultCallback, run_loop2.QuitClosure()))); |
| + run_loop2.Run(); |
| + EXPECT_TRUE(callback_called); |
| + EXPECT_TRUE(returned_result); |
| + // Switching the authority should clear the cache. |
| + client.SetAuthority("Good2"); |
| + callback_called = false; |
| + returned_result = false; |
| + base::RunLoop run_loop3; |
| + EXPECT_EQ(web_restrictions::PENDING, |
| + client.ShouldProceed( |
| + true, GURL("http://example.com/2"), |
| + base::Bind(&ResultCallback, run_loop3.QuitClosure()))); |
| + run_loop3.Run(); |
| + EXPECT_TRUE(callback_called); |
| + EXPECT_TRUE(returned_result); |
| + // Try getting a bad result |
| + client.SetAuthority("Bad"); |
| + callback_called = false; |
| + returned_result = true; |
| + base::RunLoop run_loop4; |
| + EXPECT_EQ(web_restrictions::PENDING, |
| + client.ShouldProceed( |
| + true, GURL("http://example.com/2"), |
| + base::Bind(&ResultCallback, run_loop4.QuitClosure()))); |
| + run_loop4.Run(); |
| + EXPECT_TRUE(callback_called); |
| + EXPECT_FALSE(returned_result); |
| + std::string error_html; |
| + EXPECT_TRUE(client.GetErrorHtml(GURL("http://example.com/2"), &error_html)); |
| + EXPECT_EQ("http://example.com/2", error_html); |
| +} |
| + |
| +TEST(WebRestrictionsClientTest, TestRequestPermission) { |
| + content::TestBrowserThreadBundle thread_bundle; |
| + WebRestrictionsClient client; |
| + client.SetAuthority("Good"); |
| + base::RunLoop run_loop; |
| + callback_called = false; |
| + returned_result = false; |
| + client.RequestPermission(GURL("http://example.com"), |
| + base::Bind(&ResultCallback, run_loop.QuitClosure())); |
| + run_loop.Run(); |
| + EXPECT_TRUE(callback_called); |
| + EXPECT_TRUE(returned_result); |
| + client.SetAuthority("Bad"); |
| + base::RunLoop run_loop2; |
| + callback_called = false; |
| + returned_result = true; |
| + client.RequestPermission( |
| + GURL("http://example.com"), |
| + base::Bind(&ResultCallback, run_loop2.QuitClosure())); |
| + run_loop2.Run(); |
| + EXPECT_TRUE(callback_called); |
| + EXPECT_FALSE(returned_result); |
| +} |