Commit a4870221 authored by Ramin Halavati's avatar Ramin Halavati Committed by Commit Bot

Test Extensions incognito permission is respected by key binding.

A test is added to check if an extension with command key binding can
not be accessed when it is not allowed in incognito.

Bug: 862075
Change-Id: Id49fb16958a9913978f8b054740c0ef4d0977c25
Reviewed-on: https://chromium-review.googlesource.com/1141727Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Commit-Queue: Ramin Halavati <rhalavati@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579320}
parent fc2286bf
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/test/javascript_test_observer.h" #include "content/public/test/javascript_test_observer.h"
#include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_registry.h"
#include "extensions/browser/test_event_router_observer.h"
#include "extensions/common/extension.h" #include "extensions/common/extension.h"
#include "extensions/common/feature_switch.h" #include "extensions/common/feature_switch.h"
#include "extensions/common/manifest_constants.h" #include "extensions/common/manifest_constants.h"
...@@ -187,6 +188,9 @@ class CommandsApiTest : public ExtensionApiTest { ...@@ -187,6 +188,9 @@ class CommandsApiTest : public ExtensionApiTest {
#endif // OS_CHROMEOS #endif // OS_CHROMEOS
}; };
class IncognitoCommandsApiTest : public CommandsApiTest,
public testing::WithParamInterface<bool> {};
// Test the basic functionality of the Keybinding API: // Test the basic functionality of the Keybinding API:
// - That pressing the shortcut keys should perform actions (activate the // - That pressing the shortcut keys should perform actions (activate the
// browser action or send an event). // browser action or send an event).
...@@ -1054,4 +1058,48 @@ IN_PROC_BROWSER_TEST_F(CommandsApiTest, AddRemoveAddComponentExtension) { ...@@ -1054,4 +1058,48 @@ IN_PROC_BROWSER_TEST_F(CommandsApiTest, AddRemoveAddComponentExtension) {
ASSERT_TRUE(RunComponentExtensionTest("keybinding/component")) << message_; ASSERT_TRUE(RunComponentExtensionTest("keybinding/component")) << message_;
} }
// Test Keybinding in incognito mode.
IN_PROC_BROWSER_TEST_P(IncognitoCommandsApiTest, IncognitoMode) {
ASSERT_TRUE(embedded_test_server()->Start());
bool is_incognito_enabled = GetParam();
if (is_incognito_enabled)
ASSERT_TRUE(RunExtensionTestIncognito("keybinding/basics")) << message_;
else
ASSERT_TRUE(RunExtensionTest("keybinding/basics")) << message_;
// Open incognito window and navigate to test page.
Browser* incognito_browser = OpenURLOffTheRecord(
browser()->profile(),
embedded_test_server()->GetURL("/extensions/test_file.html"));
ui_test_utils::NavigateToURL(
incognito_browser,
embedded_test_server()->GetURL("/extensions/test_file.txt"));
TestEventRouterObserver test_observer(
EventRouter::Get(incognito_browser->profile()));
// Activate the browser action shortcut (Ctrl+Shift+F).
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(incognito_browser, ui::VKEY_F,
true, true, false, false));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(is_incognito_enabled,
base::ContainsKey(test_observer.dispatched_events(),
"browserAction.onClicked"));
test_observer.ClearEvents();
// Activate the command shortcut (Ctrl+Shift+Y).
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(incognito_browser, ui::VKEY_Y,
true, true, false, false));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(is_incognito_enabled,
base::ContainsKey(test_observer.dispatched_events(),
"commands.onCommand"));
}
INSTANTIATE_TEST_CASE_P(, IncognitoCommandsApiTest, testing::Bool());
} // namespace extensions } // namespace extensions
...@@ -648,6 +648,9 @@ void EventRouter::DispatchEventToProcess( ...@@ -648,6 +648,9 @@ void EventRouter::DispatchEventToProcess(
event->event_args.get(), event->user_gesture, event->event_args.get(), event->user_gesture,
event->filter_info); event->filter_info);
for (TestObserver& observer : test_observers_)
observer.OnDidDispatchEventToProcess(*event);
// TODO(lazyboy): This is wrong for extensions SW events. We need to: // TODO(lazyboy): This is wrong for extensions SW events. We need to:
// 1. Increment worker ref count // 1. Increment worker ref count
// 2. Add EventAck IPC to decrement that ref count. // 2. Add EventAck IPC to decrement that ref count.
......
...@@ -88,6 +88,7 @@ class EventRouter : public KeyedService, ...@@ -88,6 +88,7 @@ class EventRouter : public KeyedService,
public: public:
virtual ~TestObserver() = default; virtual ~TestObserver() = default;
virtual void OnWillDispatchEvent(const Event& event) = 0; virtual void OnWillDispatchEvent(const Event& event) = 0;
virtual void OnDidDispatchEventToProcess(const Event& event) = 0;
}; };
// Gets the EventRouter for |browser_context|. // Gets the EventRouter for |browser_context|.
......
...@@ -21,6 +21,7 @@ TestEventRouterObserver::~TestEventRouterObserver() { ...@@ -21,6 +21,7 @@ TestEventRouterObserver::~TestEventRouterObserver() {
void TestEventRouterObserver::ClearEvents() { void TestEventRouterObserver::ClearEvents() {
events_.clear(); events_.clear();
dispatched_events_.clear();
} }
void TestEventRouterObserver::OnWillDispatchEvent(const Event& event) { void TestEventRouterObserver::OnWillDispatchEvent(const Event& event) {
...@@ -28,4 +29,9 @@ void TestEventRouterObserver::OnWillDispatchEvent(const Event& event) { ...@@ -28,4 +29,9 @@ void TestEventRouterObserver::OnWillDispatchEvent(const Event& event) {
events_[event.event_name] = base::WrapUnique(event.DeepCopy()); events_[event.event_name] = base::WrapUnique(event.DeepCopy());
} }
void TestEventRouterObserver::OnDidDispatchEventToProcess(const Event& event) {
DCHECK(!event.event_name.empty());
dispatched_events_[event.event_name] = base::WrapUnique(event.DeepCopy());
}
} // namespace extensions } // namespace extensions
...@@ -24,12 +24,15 @@ class TestEventRouterObserver : public EventRouter::TestObserver { ...@@ -24,12 +24,15 @@ class TestEventRouterObserver : public EventRouter::TestObserver {
void ClearEvents(); void ClearEvents();
const EventMap& events() { return events_; } const EventMap& events() { return events_; }
const EventMap& dispatched_events() { return dispatched_events_; }
private: private:
// EventRouter::TestObserver: // EventRouter::TestObserver:
void OnWillDispatchEvent(const Event& event) override; void OnWillDispatchEvent(const Event& event) override;
void OnDidDispatchEventToProcess(const Event& event) override;
EventMap events_; EventMap events_;
EventMap dispatched_events_;
EventRouter* event_router_; EventRouter* event_router_;
DISALLOW_COPY_AND_ASSIGN(TestEventRouterObserver); DISALLOW_COPY_AND_ASSIGN(TestEventRouterObserver);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment