1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/cocoa/tab_menu_bridge.h"
#import <Cocoa/Cocoa.h>
#include <memory>
#include "base/functional/callback_helpers.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "chrome/browser/favicon/favicon_utils.h"
#include "chrome/browser/ui/tab_ui_helper.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
#include "chrome/browser/ui/tabs/test_util.h"
#include "chrome/test/base/testing_profile.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
namespace {
constexpr int kStaticItemCount = 4;
class TabStripModelUiHelperDelegate : public TestTabStripModelDelegate {
public:
void WillAddWebContents(content::WebContents* contents) override {
TestTabStripModelDelegate::WillAddWebContents(contents);
favicon::CreateContentFaviconDriverForWebContents(contents);
}
};
} // namespace
class TabMenuBridgeTest : public ::testing::Test {
public:
void SetUp() override {
profile_ = std::make_unique<TestingProfile>();
rvh_test_enabler_ = std::make_unique<content::RenderViewHostTestEnabler>();
delegate_ = std::make_unique<TabStripModelUiHelperDelegate>();
model_ = std::make_unique<TabStripModel>(delegate_.get(), profile_.get());
menu_root_ = ItemWithTitle(@"Tab");
menu_ = [[NSMenu alloc] initWithTitle:@"Tab"];
menu_root_.submenu = menu_;
AddStaticItems(menu_);
}
void TearDown() override { model_->CloseAllTabs(); }
NSMenuItem* menu_root() { return menu_root_; }
NSMenu* menu() { return menu_; }
TabStripModel* model() { return model_.get(); }
TabStripModelDelegate* delegate() { return delegate_.get(); }
void AddStaticItems(NSMenu* menu) {
[menu_ addItem:ItemWithTitle(@"Static 1")];
[menu_ addItem:ItemWithTitle(@"Static 2")];
[menu_ addItem:ItemWithTitle(@"Static 3")];
[menu_ addItem:SeparatorItem()];
}
std::unique_ptr<content::WebContents> CreateWebContents(
const std::string& title) {
std::unique_ptr<content::WebContents> contents =
content::WebContentsTester::CreateTestWebContents(profile_.get(),
nullptr);
content::WebContentsTester::For(contents.get())
->SetTitle(base::UTF8ToUTF16(title));
return contents;
}
content::WebContents* GetModelWebContentsAt(int index) {
return model()->GetWebContentsAt(index);
}
void AddModelTabNamed(const std::string& name,
TabStripModel* tab_strip_model) {
std::unique_ptr<tabs::TabModel> tab_model =
std::make_unique<tabs::TabModel>(CreateWebContents(name),
tab_strip_model);
tabs::TabFeatures* const tab_features = tab_model->GetTabFeatures();
tab_features->SetTabUIHelperForTesting(
std::make_unique<TabUIHelper>(*tab_model));
tab_strip_model->AppendTab(std::move(tab_model), true);
}
int ModelIndexForTabNamed(const std::string& name) {
std::u16string title16 = base::UTF8ToUTF16(name);
for (int i = 0; i < model()->count(); ++i) {
if (model()->GetWebContentsAt(i)->GetTitle() == title16) {
return i;
}
}
return -1;
}
void RemoveModelTabNamed(const std::string& name) {
int index = ModelIndexForTabNamed(name);
DCHECK(index >= 0);
model()->CloseWebContentsAt(index, TabCloseTypes::CLOSE_NONE);
}
void RenameModelTabNamed(const std::string& old_name,
const std::string& new_name) {
int index = ModelIndexForTabNamed(old_name);
if (index >= 0) {
content::WebContents* contents = model()->GetWebContentsAt(index);
content::WebContentsTester::For(contents)->SetTitle(
base::UTF8ToUTF16(new_name));
// The way WebContentsTester updates the title avoids the usual
// notification mechanism for TabStripModel, so manually synthesize the
// update notification here.
model()->UpdateWebContentsStateAt(index, TabChangeType::kAll);
}
}
void ReplaceModelTabNamed(const std::string& old_name,
const std::string& new_name) {
int index = ModelIndexForTabNamed(old_name);
if (index >= 0) {
std::unique_ptr<content::WebContents> old_contents =
model()->DiscardWebContentsAt(index, CreateWebContents(new_name));
// Let the old WebContents be destroyed here.
}
}
void ActivateModelTabNamed(const std::string& name) {
int index = ModelIndexForTabNamed(name);
DCHECK(index >= 0);
model()->ActivateTabAt(index);
}
NSMenuItem* MenuItemForTabNamed(const std::string& name) {
return [menu() itemWithTitle:base::SysUTF8ToNSString(name)];
}
void ExpectDynamicTabsInMenuAre(const std::vector<std::string>& titles) {
std::vector<std::string> actual_titles;
for (int i = kStaticItemCount; i < menu().numberOfItems; ++i) {
actual_titles.push_back(
base::SysNSStringToUTF8([menu() itemAtIndex:i].title));
}
ASSERT_EQ(actual_titles.size(), titles.size());
for (int i = 0; i < static_cast<int>(titles.size()); ++i) {
EXPECT_EQ(actual_titles[i], titles[i]);
}
}
std::string ActiveTabName() {
return base::UTF16ToUTF8(model()->GetActiveWebContents()->GetTitle());
}
void ExpectActiveMenuItemNameIs(const std::string& name) {
std::vector<std::string> active_items;
// Check the static items too, to make sure none of them are checked.
for (int i = 0; i < menu().numberOfItems; ++i) {
NSMenuItem* item = [menu() itemAtIndex:i];
if (item.state == NSControlStateValueOn) {
active_items.push_back(base::SysNSStringToUTF8(item.title));
}
}
ASSERT_EQ(active_items.size(), 1u);
EXPECT_EQ(name, active_items[0]);
}
TestingProfile* profile() { return profile_.get(); }
private:
NSMenuItem* ItemWithTitle(NSString* title) {
return [[NSMenuItem alloc] initWithTitle:title
action:nil
keyEquivalent:@""];
}
NSMenuItem* SeparatorItem() { return [NSMenuItem separatorItem]; }
content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<TestingProfile> profile_;
std::unique_ptr<content::RenderViewHostTestEnabler> rvh_test_enabler_;
std::unique_ptr<TabStripModelUiHelperDelegate> delegate_;
std::unique_ptr<TabStripModel> model_;
NSMenuItem* __strong menu_root_;
NSMenu* __strong menu_;
tabs::PreventTabFeatureInitialization prevent_;
};
TEST_F(TabMenuBridgeTest, CreatesBlankMenu) {
TabMenuBridge bridge(model(), menu_root());
bridge.BuildMenu();
EXPECT_EQ(menu().numberOfItems, kStaticItemCount);
ExpectDynamicTabsInMenuAre({});
}
TEST_F(TabMenuBridgeTest, TracksModelUpdates) {
TabStripModel* const tab_strip_model = model();
TabMenuBridge bridge(model(), menu_root());
bridge.BuildMenu();
AddModelTabNamed("Tab 1", tab_strip_model);
AddModelTabNamed("Tab 2", tab_strip_model);
AddModelTabNamed("Tab 3", tab_strip_model);
ExpectDynamicTabsInMenuAre({"Tab 1", "Tab 2", "Tab 3"});
RemoveModelTabNamed("Tab 2");
ExpectDynamicTabsInMenuAre({"Tab 1", "Tab 3"});
AddModelTabNamed("Tab 2", tab_strip_model);
ExpectDynamicTabsInMenuAre({"Tab 1", "Tab 3", "Tab 2"});
RenameModelTabNamed("Tab 1", "Tab 4");
ExpectDynamicTabsInMenuAre({"Tab 4", "Tab 3", "Tab 2"});
content::WebContents* old_contents = GetModelWebContentsAt(0);
ReplaceModelTabNamed("Tab 4", "Tab 5");
ASSERT_NE(GetModelWebContentsAt(0), old_contents);
ExpectDynamicTabsInMenuAre({"Tab 5", "Tab 3", "Tab 2"});
}
// Tests that dynamic menu items added by the bridge are removed on
// bridge destruction.
TEST_F(TabMenuBridgeTest, RemoveDynamicMenuItemsOnDestruct) {
TabStripModel* const tab_strip_model = model();
std::unique_ptr<TabMenuBridge> bridge =
std::make_unique<TabMenuBridge>(tab_strip_model, menu_root());
bridge->BuildMenu();
AddModelTabNamed("Tab 1", tab_strip_model);
AddModelTabNamed("Tab 2", tab_strip_model);
AddModelTabNamed("Tab 3", tab_strip_model);
ExpectDynamicTabsInMenuAre({"Tab 1", "Tab 2", "Tab 3"});
bridge.reset();
ExpectDynamicTabsInMenuAre({});
}
TEST_F(TabMenuBridgeTest, ClickingMenuActivatesTab) {
TabStripModel* const tab_strip_model = model();
TabMenuBridge bridge(tab_strip_model, menu_root());
bridge.BuildMenu();
AddModelTabNamed("Tab 1", tab_strip_model);
AddModelTabNamed("Tab 2", tab_strip_model);
EXPECT_EQ(ActiveTabName(), "Tab 2");
NSMenuItem* tab1_item = MenuItemForTabNamed("Tab 1");
// Don't go through NSMenuItem's normal click-dispatching machinery here - it
// would add flake potential to this test without improving coverage. Instead,
// call straight through to the C++-side callback:
bridge.OnDynamicItemChosen(tab1_item);
EXPECT_EQ(ActiveTabName(), "Tab 1");
// Activation does not re-order the menu.
ExpectDynamicTabsInMenuAre({"Tab 1", "Tab 2"});
}
// This is a regression test for a bug found during development. Previous
// versions of TabMenuBridge had an RAII-like API where creating a TabMenuBridge
// would fill in the dynamic menu during construction. Combining this with the
// common pattern of:
// tab_menu_bridge_ = std::make_unique<TabMenuBridge>(...);
// in the presence of an existing tab_menu_bridge_ led to there temporarily
// being two TabMenuBridge instances at a time, meaning both of them had their
// dynamic menu items installed. This, in turn, confused the menu index logic in
// the new TabMenuBridge - it counted the old TabMenuBridge's dynamic items as
// static items, and ended up with incorrect indexes. This test exercises that
// behavior.
TEST_F(TabMenuBridgeTest, SwappingBridgeRecreatesMenu) {
TabStripModel* const tab_strip_model = model();
auto bridge = std::make_unique<TabMenuBridge>(tab_strip_model, menu_root());
bridge->BuildMenu();
AddModelTabNamed("Tab 1", tab_strip_model);
auto model2 = std::make_unique<TabStripModel>(delegate(), profile());
AddModelTabNamed("Tab 2", model2.get());
bridge = std::make_unique<TabMenuBridge>(model2.get(), menu_root());
bridge->BuildMenu();
ExpectDynamicTabsInMenuAre({"Tab 2"});
// Simulate one of the tabs in the model being updated - if the computed
// indexes are wrong, this call will DCHECK.
model2->UpdateWebContentsStateAt(0, TabChangeType::kAll);
model2->CloseAllTabs();
// model2 gets torn down before bridge here, which exercises the code in
// TabMenuBridge for handling the TabStripModel being torn down (eg via
// browser window close) while the TabMenuBridge still exists. If that code
// does not correctly forget about the TabStripModel, this test will crash
// here in ASAN builds.
}
TEST_F(TabMenuBridgeTest, ActiveItemTracksChanges) {
TabStripModel* const tab_strip_model = model();
TabMenuBridge bridge(tab_strip_model, menu_root());
bridge.BuildMenu();
AddModelTabNamed("Tab 1", tab_strip_model);
AddModelTabNamed("Tab 2", tab_strip_model);
AddModelTabNamed("Tab 3", tab_strip_model);
ExpectActiveMenuItemNameIs("Tab 3");
ActivateModelTabNamed("Tab 2");
ExpectActiveMenuItemNameIs("Tab 2");
ActivateModelTabNamed("Tab 3");
ExpectActiveMenuItemNameIs("Tab 3");
RemoveModelTabNamed("Tab 1");
ExpectActiveMenuItemNameIs("Tab 3");
}
|