Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2545)

Unified Diff: chrome/browser/content_settings/permission_context_base_unittest.cc

Issue 371263003: Add tests for the permission context class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/content_settings/permission_context_base_unittest.cc
diff --git a/chrome/browser/content_settings/permission_context_base_unittest.cc b/chrome/browser/content_settings/permission_context_base_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e18d3657dfb59946b4ee05adfb5ac5e2da0e76df
--- /dev/null
+++ b/chrome/browser/content_settings/permission_context_base_unittest.cc
@@ -0,0 +1,139 @@
+// Copyright 2014 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 "chrome/browser/content_settings/permission_context_base.h"
+
+#include "base/bind.h"
+#include "chrome/browser/content_settings/host_content_settings_map.h"
+#include "chrome/browser/content_settings/permission_queue_controller.h"
+#include "chrome/browser/content_settings/permission_request_id.h"
+#include "chrome/browser/infobars/infobar_service.h"
+#include "chrome/common/content_settings.h"
+#include "chrome/common/content_settings_types.h"
+#include "chrome/test/base/chrome_render_view_host_test_harness.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/test/mock_render_process_host.h"
+#include "content/public/test/web_contents_tester.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+
Bernhard Bauer 2014/07/08 12:16:04 Nit: remove one empty line
Miguel Garcia 2014/07/08 15:26:38 Done.
+class PermissionContextBaseTests : public ChromeRenderViewHostTestHarness {
+ protected:
+ PermissionContextBaseTests() {}
+ virtual ~PermissionContextBaseTests() {}
+
+ private:
+ // ChromeRenderViewHostTestHarness:
+ virtual void SetUp() OVERRIDE {
+ ChromeRenderViewHostTestHarness::SetUp();
+ InfoBarService::CreateForWebContents(web_contents());
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(PermissionContextBaseTests);
+};
+
+class TestContextBase : public PermissionContextBase {
+ public:
+ TestContextBase(Profile* profile,
+ const ContentSettingsType permission_type)
+ : PermissionContextBase(profile, permission_type),
+ permission_set_(false),
+ permission_granted_(false),
+ update_tab_context_(false) {}
+
+ virtual ~TestContextBase() {}
+
+ PermissionQueueController* GetInfoBarController() {
+ return GetQueueController();
+ }
+
+ bool PermissionGranted() {
Bernhard Bauer 2014/07/08 12:16:04 Nit: very simple accessors are usually in unix_hac
Miguel Garcia 2014/07/08 15:27:02 Done.
+ return permission_granted_;
+ }
+
+ bool PermissionSet() {
+ return permission_set_;
+ }
+
+ bool TabContextUpdated() {
+ return update_tab_context_;
+ }
+
+ void PermissionCheck(bool granted) {
+ permission_set_ = true;
+ permission_granted_ = granted;
+ }
+
+ protected:
+ virtual void UpdateTabContext(const PermissionRequestID& id,
+ const GURL& requesting_origin,
+ bool allowed) {
+ update_tab_context_ = true;
Bernhard Bauer 2014/07/08 12:16:04 Nit: Name this |tab_context_updated_|?
Miguel Garcia 2014/07/08 15:26:38 Done.
+ }
+
+ private:
+ bool permission_set_;
+ bool permission_granted_;
+ bool update_tab_context_;
+};
+
+// Simulates clicking Accept. The permission should be granted and
+// saved for future use.
+TEST_F(PermissionContextBaseTests, TestAskAndGrant) {
+ TestContextBase sut(profile(),
Bernhard Bauer 2014/07/08 12:16:04 sut?
Miguel Garcia 2014/07/08 15:26:38 Subject Under Test, it is a common thing in other
+ CONTENT_SETTINGS_TYPE_PUSH_MESSAGING);
+ GURL url("http://www.google.com");
+ content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
+
+ const PermissionRequestID id(
+ web_contents()->GetRenderProcessHost()->GetID(),
+ web_contents()->GetRenderViewHost()->GetRoutingID(),
+ -1, GURL());
+ sut.RequestPermission(web_contents(),
+ id, url, true,
+ base::Bind(&TestContextBase::PermissionCheck,
+ base::Unretained(&sut)));
+
+ sut.GetInfoBarController()->OnPermissionSet(id, url, url, true, true);
+ EXPECT_TRUE(sut.PermissionSet());
+ EXPECT_TRUE(sut.PermissionGranted());
+ EXPECT_TRUE(sut.TabContextUpdated());
+
+ ContentSetting setting =
+ profile()->GetHostContentSettingsMap()->GetContentSetting(
+ url.GetOrigin(), url.GetOrigin(),
+ CONTENT_SETTINGS_TYPE_PUSH_MESSAGING, std::string());
+ EXPECT_EQ(CONTENT_SETTING_ALLOW , setting);
+};
+
+
Bernhard Bauer 2014/07/08 12:16:04 Nit: Remove one empty line.
Miguel Garcia 2014/07/08 15:26:38 Done.
+// Simulates clicking Dismiss (X in the infobar.
+// The permission should be denied but not saved for future use.
+TEST_F(PermissionContextBaseTests, TestAskAndDismiss) {
+ TestContextBase sut(profile(),
+ CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
+ GURL url("http://www.google.es");
+ content::WebContentsTester::For(web_contents())->NavigateAndCommit(url);
+
+ const PermissionRequestID id(
+ web_contents()->GetRenderProcessHost()->GetID(),
+ web_contents()->GetRenderViewHost()->GetRoutingID(),
+ -1, GURL());
+ sut.RequestPermission(web_contents(),
+ id, url, true,
+ base::Bind(&TestContextBase::PermissionCheck,
+ base::Unretained(&sut)));
+
+ sut.GetInfoBarController()->OnPermissionSet(id, url, url, false, false);
+ EXPECT_TRUE(sut.PermissionSet());
+ EXPECT_FALSE(sut.PermissionGranted());
+ EXPECT_TRUE(sut.TabContextUpdated());
+
+ ContentSetting setting =
+ profile()->GetHostContentSettingsMap()->GetContentSetting(
+ url.GetOrigin(), url.GetOrigin(),
+ CONTENT_SETTINGS_TYPE_MIDI_SYSEX, std::string());
+ EXPECT_EQ(CONTENT_SETTING_ASK , setting);
+};

Powered by Google App Engine
This is Rietveld 408576698