Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove "Ask Brave for suggestions" from context menu spellcheck section #682

Merged
merged 1 commit into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ source_set("browser") {
"permissions",
"profiles",
"referrals",
"renderer_context_menu",
"renderer_host",
"tor",
"ui",
Expand Down
16 changes: 16 additions & 0 deletions browser/renderer_context_menu/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
source_set("renderer_context_menu") {
sources = []
if (is_win || is_linux) {
sources += [
"brave_spelling_options_submenu_observer.cc",
"brave_spelling_options_submenu_observer.h",
]
}

deps = []
if (is_win || is_linux) {
deps += [
"//chrome/browser",
]
}
}
251 changes: 251 additions & 0 deletions browser/renderer_context_menu/brave_mock_render_view_context_menu.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/renderer_context_menu/brave_mock_render_view_context_menu.h"

#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_service.h"
#include "components/renderer_context_menu/render_view_context_menu_observer.h"
#include "components/renderer_context_menu/render_view_context_menu_proxy.h"
#include "content/public/browser/browser_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

BraveMockRenderViewContextMenu::MockMenuItem::MockMenuItem()
: command_id(0),
enabled(false),
checked(false),
hidden(false),
is_submenu(false),
has_submenu(false) {}

BraveMockRenderViewContextMenu::MockMenuItem::MockMenuItem(
const MockMenuItem& other) = default;

BraveMockRenderViewContextMenu::MockMenuItem::~MockMenuItem() {}

BraveMockRenderViewContextMenu::MockMenuItem&
BraveMockRenderViewContextMenu::MockMenuItem::operator=(
const MockMenuItem& other) = default;

void BraveMockRenderViewContextMenu::MockMenuItem::PrintMockMenuItem(
unsigned int offset) const {
std::cout << std::setfill(' ');
if (offset)
std::cout << std::setfill(' ') << std::setw(offset) << ' ';
std::cout << (has_submenu ? "> " : " ");
if (command_id == -1) {
std::cout << std::setfill(' ') << std::setw(9) << ' ';
std::cout << std::setfill('-') << std::setw(15) << '-';
} else {
std::cout << std::setw(8) << command_id;
std::cout << " " << title;
if (!enabled || checked || hidden) {
std::cout << " (";
if (!enabled)
std::cout << " disabled ";
if (checked)
std::cout << " checked ";
if (hidden)
std::cout << " hidden ";
std::cout << ")";
}
}
std::cout << std::endl;
}

BraveMockRenderViewContextMenu::BraveMockRenderViewContextMenu(Profile* profile)
: observer_(nullptr), profile_(profile), enable_print_menu_(false) {}

BraveMockRenderViewContextMenu::~BraveMockRenderViewContextMenu() {}

// SimpleMenuModel::Delegate implementation.

bool BraveMockRenderViewContextMenu::IsCommandIdChecked(int command_id) const {
return observer_->IsCommandIdChecked(command_id);
}

bool BraveMockRenderViewContextMenu::IsCommandIdEnabled(int command_id) const {
return observer_->IsCommandIdEnabled(command_id);
}

void BraveMockRenderViewContextMenu::ExecuteCommand(int command_id,
int event_flags) {
observer_->ExecuteCommand(command_id);
}

// RenderViewContextMenuProxy implementation.

void BraveMockRenderViewContextMenu::AddMenuItem(int command_id,
const base::string16& title) {
MockMenuItem item;
item.command_id = command_id;
item.enabled = observer_->IsCommandIdEnabled(command_id);
item.checked = false;
item.hidden = false;
item.title = title;
items_.push_back(item);
}

void BraveMockRenderViewContextMenu::AddCheckItem(int command_id,
const base::string16& title) {
MockMenuItem item;
item.command_id = command_id;
item.enabled = observer_->IsCommandIdEnabled(command_id);
item.checked = observer_->IsCommandIdChecked(command_id);
item.hidden = false;
item.title = title;
items_.push_back(item);
}

void BraveMockRenderViewContextMenu::AddSeparator() {
MockMenuItem item;
item.command_id = -1;
item.enabled = false;
item.checked = false;
item.hidden = false;
items_.push_back(item);
}

void BraveMockRenderViewContextMenu::AddSubMenu(int command_id,
const base::string16& label,
ui::MenuModel* model) {
MockMenuItem item;
item.command_id = command_id;
item.enabled = observer_->IsCommandIdEnabled(command_id);
item.checked = observer_->IsCommandIdChecked(command_id);
item.hidden = false;
item.title = label;
item.has_submenu = true;
items_.push_back(item);

for (int i = 0; i < model->GetItemCount(); ++i) {
MockMenuItem sub_item;
sub_item.is_submenu = true;
if (model->GetTypeAt(i) != ui::MenuModel::TYPE_SEPARATOR) {
sub_item.command_id = model->GetCommandIdAt(i);
sub_item.enabled = observer_->IsCommandIdSupported(sub_item.command_id)
? model->IsEnabledAt(i)
: false;
sub_item.checked = observer_->IsCommandIdSupported(sub_item.command_id)
? model->IsItemCheckedAt(i)
: false;
sub_item.hidden = !model->IsVisibleAt(i);
sub_item.title = model->GetLabelAt(i);
} else {
sub_item.command_id = -1;
}
items_.push_back(sub_item);
}
}

void BraveMockRenderViewContextMenu::UpdateMenuItem(
int command_id,
bool enabled,
bool hidden,
const base::string16& title) {
for (auto& item : items_) {
if (item.command_id == command_id) {
item.enabled = enabled;
item.hidden = hidden;
item.title = title;
return;
}
}

FAIL() << "Menu observer is trying to change a menu item it doesn't own."
<< " command_id: " << command_id;
}

void BraveMockRenderViewContextMenu::UpdateMenuIcon(int command_id,
const gfx::Image& image) {
for (auto& item : items_) {
if (item.command_id == command_id) {
return;
}
}

FAIL() << "Menu observer is trying to change a menu item it doesn't own."
<< " command_id: " << command_id;
}

void BraveMockRenderViewContextMenu::RemoveMenuItem(int command_id) {
auto it = items_.begin();
while (it != items_.end()) {
if (it->command_id == command_id) {
bool submenu = it->has_submenu;
it = items_.erase(it);
if (submenu) {
while (it != items_.end() && it->is_submenu)
it = items_.erase(it);
}
break;
} else
++it;
}
}

void BraveMockRenderViewContextMenu::RemoveAdjacentSeparators() {}

void BraveMockRenderViewContextMenu::AddSpellCheckServiceItem(bool is_checked) {
// Call the static method of RenderViewContextMenu which should our override
// that doesn't add the item.
RenderViewContextMenu::AddSpellCheckServiceItem(nullptr, is_checked);
}

content::RenderViewHost* BraveMockRenderViewContextMenu::GetRenderViewHost()
const {
return nullptr;
}

content::BrowserContext* BraveMockRenderViewContextMenu::GetBrowserContext()
const {
return profile_;
}

content::WebContents* BraveMockRenderViewContextMenu::GetWebContents() const {
return nullptr;
}

// Methods that don't implement inherited interfaces.

void BraveMockRenderViewContextMenu::SetObserver(
RenderViewContextMenuObserver* observer) {
observer_ = observer;
}

size_t BraveMockRenderViewContextMenu::GetMenuSize() const {
return items_.size();
}

bool BraveMockRenderViewContextMenu::GetMenuItem(size_t index,
MockMenuItem* item) const {
if (index >= items_.size())
return false;
*item = items_[index];
return true;
}

PrefService* BraveMockRenderViewContextMenu::GetPrefs() {
return profile_->GetPrefs();
}

void BraveMockRenderViewContextMenu::PrintMenu(const std::string& title) const {
if (!enable_print_menu_)
return;

std::cout << title << std::endl;
std::cout << std::setfill('-') << std::setw(40) << '-' << std::endl;
for (const auto& item : items_)
item.PrintMockMenuItem(item.is_submenu ? 4 : 0);
std::cout << std::setfill('-') << std::setw(40) << '-' << std::endl;
}

void BraveMockRenderViewContextMenu::EnablePrintMenu(bool enable) {
enable_print_menu_ = enable;
}
109 changes: 109 additions & 0 deletions browser/renderer_context_menu/brave_mock_render_view_context_menu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_RENDERER_CONTEXT_MENU_BRAVE_MOCK_RENDER_VIEW_CONTEXT_MENU_H_
#define BRAVE_BROWSER_RENDERER_CONTEXT_MENU_BRAVE_MOCK_RENDER_VIEW_CONTEXT_MENU_H_

#include <cstddef>
#include <memory>
#include <vector>

#include "base/macros.h"
#include "base/strings/string16.h"
#include "components/renderer_context_menu/render_view_context_menu_proxy.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/gfx/image/image.h"

class BraveMockRenderViewContextMenu;
class PrefService;
class Profile;
class RenderViewContextMenuObserver;

// A mock context menu proxy used in tests. This class overrides virtual methods
// derived from the RenderViewContextMenuProxy class to monitor calls from a
// MenuObserver class.
class BraveMockRenderViewContextMenu : public ui::SimpleMenuModel::Delegate,
public RenderViewContextMenuProxy {
public:
// A menu item used in this test.
struct MockMenuItem {
MockMenuItem();
MockMenuItem(const MockMenuItem& other);
~MockMenuItem();

MockMenuItem& operator=(const MockMenuItem& other);

void PrintMockMenuItem(unsigned int offset = 0) const;

int command_id;
bool enabled;
bool checked;
bool hidden;
base::string16 title;
bool is_submenu; // This item lives in a submenu.
bool has_submenu; // This item is a submenu.
};

explicit BraveMockRenderViewContextMenu(Profile* profile);
~BraveMockRenderViewContextMenu() override;

// SimpleMenuModel::Delegate implementation.
bool IsCommandIdChecked(int command_id) const override;
bool IsCommandIdEnabled(int command_id) const override;
void ExecuteCommand(int command_id, int event_flags) override;

// RenderViewContextMenuProxy implementation.
void AddMenuItem(int command_id, const base::string16& title) override;
void AddCheckItem(int command_id, const base::string16& title) override;
void AddSeparator() override;
void AddSubMenu(int command_id,
const base::string16& label,
ui::MenuModel* model) override;
void UpdateMenuItem(int command_id,
bool enabled,
bool hidden,
const base::string16& title) override;
void UpdateMenuIcon(int command_id, const gfx::Image& image) override;
void RemoveMenuItem(int command_id) override;
void RemoveAdjacentSeparators() override;
void AddSpellCheckServiceItem(bool is_checked) override;
content::RenderViewHost* GetRenderViewHost() const override;
content::BrowserContext* GetBrowserContext() const override;
content::WebContents* GetWebContents() const override;

// Attaches a RenderViewContextMenuObserver to be tested.
void SetObserver(RenderViewContextMenuObserver* observer);

// Returns the number of items added by the test.
size_t GetMenuSize() const;

// Returns the item at |index|.
bool GetMenuItem(size_t index, MockMenuItem* item) const;

// Returns the writable profile used in this test.
PrefService* GetPrefs();

// Prints the menu to the standard output.
void PrintMenu(const std::string& title) const;
void EnablePrintMenu(bool enable = true);

private:
// An observer used for initializing the status of menu items added in this
// test. This is owned by our owner and the owner is responsible for its
// lifetime.
RenderViewContextMenuObserver* observer_;

// Either a regular profile or an incognito profile.
Profile* profile_;

// A list of menu items added.
std::vector<MockMenuItem> items_;

// Is menu printing enabled.
bool enable_print_menu_;

DISALLOW_COPY_AND_ASSIGN(BraveMockRenderViewContextMenu);
};

#endif // BRAVE_BROWSER_RENDERER_CONTEXT_MENU_BRAVE_MOCK_RENDER_VIEW_CONTEXT_MENU_H_
Loading