1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/renderer_context_menu/spelling_menu_observer.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/app/chrome_command_ids.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/spellchecker/spelling_service_client.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/testing_profile.h"
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/renderer_context_menu/render_view_context_menu_observer.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::RenderViewHost;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::WebContents;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A mock context menu used in this test. This class overrides virtual methods
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// derived from the RenderViewContextMenuProxy class to monitor calls from the
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SpellingMenuObserver class.
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockRenderViewContextMenu : public RenderViewContextMenuProxy {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A menu item used in this test. This test uses a vector of this struct to
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // hold menu items added by this test.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct MockMenuItem {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MockMenuItem()
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        : command_id(0),
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          enabled(false),
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          checked(false),
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          hidden(true) {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int command_id;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool enabled;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool checked;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool hidden;
42a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16 title;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  explicit MockRenderViewContextMenu(bool incognito);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~MockRenderViewContextMenu();
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // RenderViewContextMenuProxy implementation.
49a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void AddMenuItem(int command_id,
50a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                           const base::string16& title) OVERRIDE;
51a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void AddCheckItem(int command_id,
52a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                            const base::string16& title) OVERRIDE;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void AddSeparator() OVERRIDE;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void AddSubMenu(int command_id,
55a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                          const base::string16& label,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          ui::MenuModel* model) OVERRIDE;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void UpdateMenuItem(int command_id,
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              bool enabled,
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              bool hidden,
60a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                              const base::string16& title) OVERRIDE;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual RenderViewHost* GetRenderViewHost() const OVERRIDE;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual WebContents* GetWebContents() const OVERRIDE;
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual content::BrowserContext* GetBrowserContext() const OVERRIDE;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Attaches a RenderViewContextMenuObserver to be tested.
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetObserver(RenderViewContextMenuObserver* observer);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the number of items added by the test.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t GetMenuSize() const;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the i-th item.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetMenuItem(size_t i, MockMenuItem* item) const;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the writable profile used in this test.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefService* GetPrefs();
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // An observer used for initializing the status of menu items added in this
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // test. A test should delete this RenderViewContextMenuObserver object.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RenderViewContextMenuObserver* observer_;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A dummy profile used in this test. Call GetPrefs() when a test needs to
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // change this profile and use PrefService methods.
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_ptr<TestingProfile> original_profile_;
851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Either |original_profile_| or its incognito profile.
871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Profile* profile_;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A list of menu items added by the SpellingMenuObserver class.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<MockMenuItem> items_;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MockRenderViewContextMenu);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)MockRenderViewContextMenu::MockRenderViewContextMenu(bool incognito)
963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    : observer_(NULL) {
971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  original_profile_ = TestingProfile::Builder().Build();
981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  profile_ = incognito ? original_profile_->GetOffTheRecordProfile()
991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                       : original_profile_.get();
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MockRenderViewContextMenu::~MockRenderViewContextMenu() {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MockRenderViewContextMenu::AddMenuItem(int command_id,
106a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                            const base::string16& title) {
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockMenuItem item;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.command_id = command_id;
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.enabled = observer_->IsCommandIdEnabled(command_id);
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.checked = false;
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.hidden = false;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.title = title;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  items_.push_back(item);
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MockRenderViewContextMenu::AddCheckItem(int command_id,
117a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                             const base::string16& title) {
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockMenuItem item;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.command_id = command_id;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.enabled = observer_->IsCommandIdEnabled(command_id);
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.checked = observer_->IsCommandIdChecked(command_id);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.hidden = false;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.title = title;
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  items_.push_back(item);
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MockRenderViewContextMenu::AddSeparator() {
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockMenuItem item;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.command_id = -1;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.enabled = false;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.checked = false;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.hidden = false;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  items_.push_back(item);
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MockRenderViewContextMenu::AddSubMenu(int command_id,
137a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                           const base::string16& label,
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           ui::MenuModel* model) {
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockMenuItem item;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.command_id = -1;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.enabled = false;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.checked = false;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item.hidden = false;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  items_.push_back(item);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MockRenderViewContextMenu::UpdateMenuItem(int command_id,
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                               bool enabled,
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                               bool hidden,
150a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                               const base::string16& title) {
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (std::vector<MockMenuItem>::iterator it = items_.begin();
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       it != items_.end(); ++it) {
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (it->command_id == command_id) {
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      it->enabled = enabled;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      it->hidden = hidden;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      it->title = title;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The SpellingMenuObserver class tries to change a menu item not added by the
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // class. This is an unexpected behavior and we should stop now.
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FAIL();
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)RenderViewHost* MockRenderViewContextMenu::GetRenderViewHost() const {
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)WebContents* MockRenderViewContextMenu::GetWebContents() const {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)content::BrowserContext* MockRenderViewContextMenu::GetBrowserContext() const {
1751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return profile_;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)size_t MockRenderViewContextMenu::GetMenuSize() const {
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return items_.size();
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MockRenderViewContextMenu::GetMenuItem(size_t i,
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                            MockMenuItem* item) const {
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (i >= items_.size())
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item->command_id = items_[i].command_id;
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item->enabled = items_[i].enabled;
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item->checked = items_[i].checked;
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item->hidden = items_[i].hidden;
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  item->title = items_[i].title;
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MockRenderViewContextMenu::SetObserver(
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RenderViewContextMenuObserver* observer) {
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  observer_ = observer;
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PrefService* MockRenderViewContextMenu::GetPrefs() {
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return profile_->GetPrefs();
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A test class used in this file. This test should be a browser test because it
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// accesses resources.
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SpellingMenuObserverTest : public InProcessBrowserTest {
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SpellingMenuObserverTest();
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
209c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void SetUpOnMainThread() OVERRIDE {
2103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    Reset(false);
211c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
212c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void TearDownOnMainThread() OVERRIDE {
214c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    observer_.reset();
215c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    menu_.reset();
216c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
217c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  void Reset(bool incognito) {
219c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    observer_.reset();
2203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    menu_.reset(new MockRenderViewContextMenu(incognito));
221c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    observer_.reset(new SpellingMenuObserver(menu_.get()));
222c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    menu_->SetObserver(observer_.get());
223c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
224c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
225c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void InitMenu(const char* word, const char* suggestion) {
226c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::ContextMenuParams params;
227c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    params.is_editable = true;
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    params.misspelled_word = base::ASCIIToUTF16(word);
229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    params.dictionary_suggestions.clear();
230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (suggestion)
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      params.dictionary_suggestions.push_back(base::ASCIIToUTF16(suggestion));
232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    observer_->InitMenu(params);
233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
234c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
235010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void ForceSuggestMode() {
236010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
237010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // Force a non-empty and non-"en" locale so SUGGEST is available.
238010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    menu()->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "fr");
239010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    ASSERT_TRUE(SpellingServiceClient::IsAvailable(
2405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        menu()->GetBrowserContext(), SpellingServiceClient::SUGGEST));
241010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    ASSERT_FALSE(SpellingServiceClient::IsAvailable(
2425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK));
243010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  }
244010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual ~SpellingMenuObserverTest();
246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MockRenderViewContextMenu* menu() { return menu_.get(); }
247c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SpellingMenuObserver* observer() { return observer_.get(); }
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
249c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<SpellingMenuObserver> observer_;
250c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<MockRenderViewContextMenu> menu_;
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SpellingMenuObserverTest);
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SpellingMenuObserverTest::SpellingMenuObserverTest() {
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SpellingMenuObserverTest::~SpellingMenuObserverTest() {
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests that right-clicking a correct word does not add any items.
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithCorrectWord) {
264c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  InitMenu("", NULL);
265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(static_cast<size_t>(0), menu()->GetMenuSize());
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests that right-clicking a misspelled word adds four items:
269c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// "No spelling suggestions", "Add to dictionary", "Ask Google for suggestions",
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and a separator.
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, InitMenuWithMisspelledWord) {
272c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  InitMenu("wiimode", NULL);
273c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(static_cast<size_t>(4), menu()->GetMenuSize());
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Read all the context-menu items added by this test and verify they are
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // expected ones. We do not check the item titles to prevent resource changes
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from breaking this test. (I think it is not expected by those who change
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // resources.)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockRenderViewContextMenu::MockMenuItem item;
280c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(0, &item);
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, item.command_id);
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.enabled);
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.hidden);
284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(1, &item);
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(item.enabled);
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.hidden);
288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(2, &item);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(item.enabled);
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.checked);
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.hidden);
293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(3, &item);
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(-1, item.command_id);
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.enabled);
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.hidden);
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests that right-clicking a correct word when we enable spelling-service
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// integration to verify an item "Ask Google for suggestions" is checked. Even
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// though this meanu itself does not add this item, its sub-menu adds the item
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and calls SpellingMenuObserver::IsChecked() to check it.
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       EnableSpellingServiceWithCorrectWord) {
305c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
306c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  InitMenu("", NULL);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(
309c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      observer()->IsCommandIdChecked(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE));
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests that right-clicking a misspelled word when we enable spelling-service
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// integration to verify an item "Ask Google for suggestions" is checked. (This
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// test does not actually send JSON-RPC requests to the service because it makes
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this test flaky.)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, EnableSpellingService) {
317c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
318c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetPrefs()->SetString(prefs::kSpellCheckDictionary, std::string());
319c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
320c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  InitMenu("wiimode", NULL);
321c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(static_cast<size_t>(4), menu()->GetMenuSize());
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // To avoid duplicates, this test reads only the "Ask Google for suggestions"
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // item and verifies it is enabled and checked.
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockRenderViewContextMenu::MockMenuItem item;
326c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(2, &item);
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(item.enabled);
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(item.checked);
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.hidden);
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test that there will be a separator after "no suggestions" if
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SpellingServiceClient::SUGGEST is on.
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SeparatorAfterSuggestions) {
336010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ForceSuggestMode();
337c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  InitMenu("jhhj", NULL);
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The test should see a top separator, "No spelling suggestions",
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "No more Google suggestions" (from SpellingService) and a separator
341c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // as the first four items, then possibly more (not relevant here).
342c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_LT(4U, menu()->GetMenuSize());
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MockRenderViewContextMenu::MockMenuItem item;
345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(0, &item);
346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(-1, item.command_id);
347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(item.enabled);
348c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(item.hidden);
349c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
350c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(1, &item);
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id);
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.enabled);
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.hidden);
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(2, &item);
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, item.command_id);
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.enabled);
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.hidden);
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
360c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(3, &item);
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(-1, item.command_id);
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.enabled);
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(item.hidden);
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Test that we don't show "No more suggestions from Google" if the spelling
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// service is enabled and that there is only one suggestion.
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       NoMoreSuggestionsNotDisplayed) {
370c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Force a non-empty locale so SPELLCHECK is available.
373c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en");
3745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  EXPECT_TRUE(SpellingServiceClient::IsAvailable(
3755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK));
376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  InitMenu("asdfkj", "asdf");
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // The test should see a separator, a suggestion and another separator
3792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // as the first two items, then possibly more (not relevant here).
380c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_LT(3U, menu()->GetMenuSize());
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MockRenderViewContextMenu::MockMenuItem item;
383c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(0, &item);
384c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(-1, item.command_id);
385c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(item.enabled);
386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(item.hidden);
387c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(1, &item);
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(IDC_SPELLCHECK_SUGGESTION_0, item.command_id);
3902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(item.enabled);
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(item.hidden);
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
393c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(2, &item);
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(-1, item.command_id);
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(item.enabled);
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(item.hidden);
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Test that "Ask Google For Suggestions" is grayed out when using an
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// off the record profile.
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// TODO(rlp): Include graying out of autocorrect in this test when autocorrect
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// is functional.
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest,
4042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       NoSpellingServiceWhenOffTheRecord) {
4053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Create a menu in an incognito profile.
4063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  Reset(true);
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This means spellchecking is allowed. Default is that the service is
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // contacted but this test makes sure that if profile is incognito, that
4102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is not an option.
411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Force a non-empty locale so SUGGEST normally would be available.
414c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en");
4155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  EXPECT_FALSE(SpellingServiceClient::IsAvailable(
4165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      menu()->GetBrowserContext(), SpellingServiceClient::SUGGEST));
4175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  EXPECT_FALSE(SpellingServiceClient::IsAvailable(
4185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      menu()->GetBrowserContext(), SpellingServiceClient::SPELLCHECK));
4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
420c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  InitMenu("sjxdjiiiiii", NULL);
4212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The test should see "No spelling suggestions" (from system checker).
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // They should not see "No more Google suggestions" (from SpellingService) or
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // a separator. The next 2 items should be "Add to Dictionary" followed
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // by "Ask Google for suggestions" which should be disabled.
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(rlp): add autocorrect here when it is functional.
427c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_LT(3U, menu()->GetMenuSize());
4282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  MockRenderViewContextMenu::MockMenuItem item;
430c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(0, &item);
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(IDC_CONTENT_CONTEXT_NO_SPELLING_SUGGESTIONS, item.command_id);
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(item.enabled);
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(item.hidden);
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
435c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(1, &item);
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(IDC_SPELLCHECK_ADD_TO_DICTIONARY, item.command_id);
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(item.enabled);
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(item.hidden);
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
440c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(2, &item);
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, item.command_id);
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(item.enabled);
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_FALSE(item.hidden);
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
446c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test that the menu is preceeded by a separator if there are any suggestions,
447c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// or if the SpellingServiceClient is available
448c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(SpellingMenuObserverTest, SuggestionsForceTopSeparator) {
449c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false);
450c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
451c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // First case: Misspelled word, no suggestions, no spellcheck service.
452c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  InitMenu("asdfkj", NULL);
453c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // See SpellingMenuObserverTest.InitMenuWithMisspelledWord on why 4 items.
454c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(static_cast<size_t>(4), menu()->GetMenuSize());
455c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MockRenderViewContextMenu::MockMenuItem item;
456c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(0, &item);
457c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_NE(-1, item.command_id);
458c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
459c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Case #2. Misspelled word, suggestions, no spellcheck service.
4603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  Reset(false);
461c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetPrefs()->SetBoolean(prefs::kSpellCheckUseSpellingService, false);
462c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  InitMenu("asdfkj", "asdf");
463c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
464c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Expect at least separator and 4 default entries.
465c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_LT(static_cast<size_t>(5), menu()->GetMenuSize());
466c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // This test only cares that the first one is a separator.
467c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(0, &item);
468c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(-1, item.command_id);
469c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
470c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Case #3. Misspelled word, suggestion service is on.
4713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  Reset(false);
472010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ForceSuggestMode();
473c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  InitMenu("asdfkj", NULL);
474c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
475c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Should have at least 2 entries. Separator, suggestion.
476c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_LT(2U, menu()->GetMenuSize());
477c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(0, &item);
478c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(-1, item.command_id);
479c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  menu()->GetMenuItem(1, &item);
480c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(IDC_CONTENT_CONTEXT_SPELLING_SUGGESTION, item.command_id);
481c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
482