| Index: chrome/browser/extensions/active_tab_unittest.cc
|
| diff --git a/chrome/browser/extensions/active_tab_unittest.cc b/chrome/browser/extensions/active_tab_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..360661496f57a13867e5de282ca9d7a980db5b4d
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/active_tab_unittest.cc
|
| @@ -0,0 +1,192 @@
|
| +// Copyright (c) 2012 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 <string>
|
| +
|
| +#include "base/compiler_specific.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/message_loop.h"
|
| +#include "base/values.h"
|
| +#include "chrome/browser/extensions/active_tab_permission_manager.h"
|
| +#include "chrome/browser/extensions/extension_tab_helper.h"
|
| +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
|
| +#include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h"
|
| +#include "chrome/common/extensions/extension.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/browser/notification_types.h"
|
| +#include "content/public/browser/web_contents.h"
|
| +#include "content/public/common/page_transition_types.h"
|
| +#include "content/public/test/test_browser_thread.h"
|
| +
|
| +using base::DictionaryValue;
|
| +using base::ListValue;
|
| +using content::BrowserThread;
|
| +using content::NavigationController;
|
| +
|
| +namespace extensions {
|
| +namespace {
|
| +
|
| +class ActiveTabTest : public TabContentsWrapperTestHarness {
|
| + public:
|
| + ActiveTabTest() : ui_thread_(BrowserThread::UI, MessageLoop::current()) {
|
| + {
|
| + scoped_ptr<ListValue> permissions(new ListValue());
|
| + permissions->Append(Value::CreateStringValue("activeTab"));
|
| + DictionaryValue manifest;
|
| + manifest.SetString("name", "Extension with activeTab.");
|
| + manifest.SetString("version", "1.0.0");
|
| + manifest.SetInteger("manifest_version", 2);
|
| + manifest.Set("permissions", permissions.release());
|
| + std::string error;
|
| + extension_ = Extension::Create(
|
| + FilePath(),
|
| + Extension::EXTERNAL_PREF,
|
| + manifest,
|
| + 0,
|
| + &error);
|
| + CHECK_EQ("", error);
|
| + CHECK(extension_->HasAPIPermission(ExtensionAPIPermission::kActiveTab));
|
| + }
|
| + {
|
| + scoped_ptr<ListValue> permissions(new ListValue());
|
| + permissions->Append(Value::CreateStringValue("activeTab"));
|
| + DictionaryValue manifest;
|
| + manifest.SetString("name", "Another extension with activeTab.");
|
| + manifest.SetString("version", "1.0.0");
|
| + manifest.SetInteger("manifest_version", 2);
|
| + manifest.Set("permissions", permissions.release());
|
| + std::string error;
|
| + another_extension_ = Extension::Create(
|
| + FilePath(),
|
| + Extension::EXTERNAL_PREF,
|
| + manifest,
|
| + 0,
|
| + &error);
|
| + CHECK_EQ("", error);
|
| + CHECK(another_extension_->HasAPIPermission(
|
| + ExtensionAPIPermission::kActiveTab));
|
| + }
|
| + {
|
| + scoped_ptr<ListValue> permissions(new ListValue());
|
| + DictionaryValue manifest;
|
| + manifest.SetString("name", "Extension without activeTab.");
|
| + manifest.SetString("version", "1.0.0");
|
| + manifest.SetInteger("manifest_version", 2);
|
| + manifest.Set("permissions", permissions.release());
|
| + std::string error;
|
| + extension_without_active_tab_ = Extension::Create(
|
| + FilePath(),
|
| + Extension::EXTERNAL_PREF,
|
| + manifest,
|
| + 0,
|
| + &error);
|
| + CHECK_EQ("", error);
|
| + CHECK(!extension_without_active_tab_->HasAPIPermission(
|
| + ExtensionAPIPermission::kActiveTab));
|
| + }
|
| + }
|
| +
|
| + protected:
|
| + const Extension* extension() {
|
| + return extension_.get();
|
| + }
|
| +
|
| + const Extension* another_extension() {
|
| + return another_extension_.get();
|
| + }
|
| +
|
| + const Extension* extension_without_active_tab() {
|
| + return extension_without_active_tab_.get();
|
| + }
|
| +
|
| + int tab_id() {
|
| + return contents_wrapper()->extension_tab_helper()->GetTabId();
|
| + }
|
| +
|
| + ActiveTabPermissionManager* manager() {
|
| + return contents_wrapper()->extension_tab_helper()->
|
| + active_tab_permission_manager();
|
| + }
|
| +
|
| + bool Allowed(const Extension* extension, const GURL& url) {
|
| + return Allowed(extension, url, tab_id());
|
| + }
|
| +
|
| + bool Allowed(const Extension* extension, const GURL& url, int tab_id) {
|
| + return (extension->CanExecuteScriptOnPage(url, tab_id, NULL, NULL) &&
|
| + extension->CanCaptureVisiblePage(url, tab_id, NULL));
|
| + }
|
| +
|
| + // Fakes loading a new frame on the page using the WebContentsObserver
|
| + // interface.
|
| + // TODO(kalman): if somebody can tell me a way to do this from the
|
| + // TabContentsWrapperTestHarness (or any other test harness) then pray tell.
|
| + void AddFrame(const GURL& url) {
|
| + manager()->DidCommitProvisionalLoadForFrame(
|
| + 0, // frame_id
|
| + false, // is_main_frame
|
| + url,
|
| + content::PAGE_TRANSITION_AUTO_SUBFRAME,
|
| + NULL); // render_view_host
|
| + }
|
| +
|
| + private:
|
| + // An extension with the activeTab permission.
|
| + scoped_refptr<const Extension> extension_;
|
| +
|
| + // Another extension with activeTab (for good measure).
|
| + scoped_refptr<const Extension> another_extension_;
|
| +
|
| + // An extension without the activeTab permission.
|
| + scoped_refptr<const Extension> extension_without_active_tab_;
|
| +
|
| + content::TestBrowserThread ui_thread_;
|
| +};
|
| +
|
| +// TODO:
|
| +// - multiple frames on the same tab
|
| +// - uninstalling extensions
|
| +// - ...
|
| +
|
| +TEST_F(ActiveTabTest, GrantToSinglePage) {
|
| + GURL google("http://www.google.com");
|
| + NavigateAndCommit(google);
|
| +
|
| + EXPECT_FALSE(Allowed(extension(), google));
|
| + EXPECT_FALSE(Allowed(another_extension(), google));
|
| + EXPECT_FALSE(Allowed(extension_without_active_tab(), google));
|
| +
|
| + manager()->MaybeGrant(extension());
|
| + manager()->MaybeGrant(extension_without_active_tab());
|
| +
|
| + EXPECT_TRUE(Allowed(extension(), google));
|
| + EXPECT_FALSE(Allowed(another_extension(), google));
|
| + EXPECT_FALSE(Allowed(extension_without_active_tab(), google));
|
| +
|
| + GURL chromium("http://www.chromium.org");
|
| + rvh_tester()->SendNavigate(1, chromium);
|
| +
|
| + EXPECT_FALSE(Allowed(extension(), google));
|
| + EXPECT_FALSE(Allowed(another_extension(), google));
|
| + EXPECT_FALSE(Allowed(extension_without_active_tab(), google));
|
| +
|
| + EXPECT_FALSE(Allowed(extension(), chromium));
|
| + EXPECT_FALSE(Allowed(another_extension(), chromium));
|
| + EXPECT_FALSE(Allowed(extension_without_active_tab(), chromium));
|
| +
|
| + manager()->MaybeGrant(extension());
|
| + manager()->MaybeGrant(another_extension());
|
| + manager()->MaybeGrant(extension_without_active_tab());
|
| +
|
| + EXPECT_FALSE(Allowed(extension(), google));
|
| + EXPECT_FALSE(Allowed(another_extension(), google));
|
| + EXPECT_FALSE(Allowed(extension_without_active_tab(), google));
|
| +
|
| + EXPECT_TRUE(Allowed(extension(), chromium));
|
| + EXPECT_TRUE(Allowed(another_extension(), chromium));
|
| + EXPECT_FALSE(Allowed(extension_without_active_tab(), chromium));
|
| +};
|
| +
|
| +} // namespace
|
| +} // namespace extensions
|
|
|