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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
// Copyright 2013 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 "chrome/browser/ui/views/toolbar/browser_actions_container.h"
#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
#include "chrome/browser/extensions/browser_action_test_util.h"
#include "chrome/browser/extensions/extension_toolbar_model.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/toolbar/browser_actions_bar_browsertest.h"
#include "chrome/browser/ui/views/extensions/browser_action_drag_data.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_action_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/common/extension.h"
#include "ui/base/dragdrop/drop_target_event.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/gfx/geometry/point.h"
#include "ui/views/view.h"
// TODO(devlin): Continue moving any tests that should be platform independent
// from this file to the crossplatform tests in
// chrome/browser/ui/toolbar/browser_actions_bar_browsertest.cc.
// Test that dragging browser actions works, and that dragging a browser action
// from the overflow menu results in it "popping" out (growing the container
// size by 1), rather than just reordering the extensions.
IN_PROC_BROWSER_TEST_F(BrowserActionsBarBrowserTest, DragBrowserActions) {
LoadExtensions();
// Sanity check: All extensions showing; order is A B C.
EXPECT_EQ(3, browser_actions_bar()->VisibleBrowserActions());
EXPECT_EQ(3, browser_actions_bar()->NumberOfBrowserActions());
EXPECT_EQ(extension_a()->id(), browser_actions_bar()->GetExtensionId(0));
EXPECT_EQ(extension_b()->id(), browser_actions_bar()->GetExtensionId(1));
EXPECT_EQ(extension_c()->id(), browser_actions_bar()->GetExtensionId(2));
BrowserActionsContainer* container =
BrowserView::GetBrowserViewForBrowser(browser())
->toolbar()->browser_actions();
// Simulate a drag and drop to the right.
ui::OSExchangeData drop_data;
// Drag extension A from index 0...
BrowserActionDragData browser_action_drag_data(extension_a()->id(), 0u);
browser_action_drag_data.Write(profile(), &drop_data);
ToolbarActionView* view = container->GetViewForId(extension_b()->id());
// ...to the right of extension B.
gfx::Point location(view->x() + view->width(), view->y());
ui::DropTargetEvent target_event(
drop_data, location, location, ui::DragDropTypes::DRAG_MOVE);
// Drag and drop.
container->OnDragUpdated(target_event);
container->OnPerformDrop(target_event);
// The order should now be B A C, since A was dragged to the right of B.
EXPECT_EQ(extension_b()->id(), browser_actions_bar()->GetExtensionId(0));
EXPECT_EQ(extension_a()->id(), browser_actions_bar()->GetExtensionId(1));
EXPECT_EQ(extension_c()->id(), browser_actions_bar()->GetExtensionId(2));
// This order should be reflected in the underlying model.
EXPECT_EQ(extension_b(), toolbar_model()->toolbar_items()[0].get());
EXPECT_EQ(extension_a(), toolbar_model()->toolbar_items()[1].get());
EXPECT_EQ(extension_c(), toolbar_model()->toolbar_items()[2].get());
// Simulate a drag and drop to the left.
ui::OSExchangeData drop_data2;
// Drag extension A from index 1...
BrowserActionDragData browser_action_drag_data2(extension_a()->id(), 1u);
browser_action_drag_data2.Write(profile(), &drop_data2);
// ...to the left of extension B (which is now at index 0).
location = gfx::Point(view->x(), view->y());
ui::DropTargetEvent target_event2(
drop_data2, location, location, ui::DragDropTypes::DRAG_MOVE);
// Drag and drop.
container->OnDragUpdated(target_event2);
container->OnPerformDrop(target_event2);
// Order should be restored to A B C.
EXPECT_EQ(extension_a()->id(), browser_actions_bar()->GetExtensionId(0));
EXPECT_EQ(extension_b()->id(), browser_actions_bar()->GetExtensionId(1));
EXPECT_EQ(extension_c()->id(), browser_actions_bar()->GetExtensionId(2));
// Shrink the size of the container so we have an overflow menu.
toolbar_model()->SetVisibleIconCount(2u);
EXPECT_EQ(2u, container->VisibleBrowserActions());
ASSERT_TRUE(container->chevron());
EXPECT_TRUE(container->chevron()->visible());
// Simulate a drag and drop from the overflow menu.
ui::OSExchangeData drop_data3;
// Drag extension C from index 2 (in the overflow menu)...
BrowserActionDragData browser_action_drag_data3(extension_c()->id(), 2u);
browser_action_drag_data3.Write(profile(), &drop_data3);
// ...to the left of extension B (which is back in index 1 on the main bar).
location = gfx::Point(view->x(), view->y());
ui::DropTargetEvent target_event3(
drop_data3, location, location, ui::DragDropTypes::DRAG_MOVE);
// Drag and drop.
container->OnDragUpdated(target_event3);
container->OnPerformDrop(target_event3);
// The order should have changed *and* the container should have grown to
// accommodate extension C. The new order should be A C B, and all three
// extensions should be visible, with no overflow menu.
EXPECT_EQ(extension_a()->id(), browser_actions_bar()->GetExtensionId(0));
EXPECT_EQ(extension_c()->id(), browser_actions_bar()->GetExtensionId(1));
EXPECT_EQ(extension_b()->id(), browser_actions_bar()->GetExtensionId(2));
EXPECT_EQ(3u, container->VisibleBrowserActions());
EXPECT_FALSE(container->chevron()->visible());
EXPECT_TRUE(toolbar_model()->all_icons_visible());
// TODO(devlin): Ideally, we'd also have tests for dragging from the legacy
// overflow menu (i.e., chevron) to the main bar, but this requires either
// having a fairly complicated interactive UI test or finding a good way to
// mock up the BrowserActionOverflowMenuController.
}
// Test that changes performed in one container affect containers in other
// windows so that it is consistent.
IN_PROC_BROWSER_TEST_F(BrowserActionsBarBrowserTest, MultipleWindows) {
LoadExtensions();
BrowserActionsContainer* first =
BrowserView::GetBrowserViewForBrowser(browser())->toolbar()->
browser_actions();
// Create a second browser.
Browser* second_browser = new Browser(
Browser::CreateParams(profile(), browser()->host_desktop_type()));
BrowserActionsContainer* second =
BrowserView::GetBrowserViewForBrowser(second_browser)->toolbar()->
browser_actions();
// Both containers should have the same order and visible actions, which
// is right now A B C.
EXPECT_EQ(3u, first->VisibleBrowserActions());
EXPECT_EQ(3u, second->VisibleBrowserActions());
EXPECT_EQ(extension_a()->id(), first->GetIdAt(0u));
EXPECT_EQ(extension_a()->id(), second->GetIdAt(0u));
EXPECT_EQ(extension_b()->id(), first->GetIdAt(1u));
EXPECT_EQ(extension_b()->id(), second->GetIdAt(1u));
EXPECT_EQ(extension_c()->id(), first->GetIdAt(2u));
EXPECT_EQ(extension_c()->id(), second->GetIdAt(2u));
// Simulate a drag and drop to the right.
ui::OSExchangeData drop_data;
// Drag extension A from index 0...
BrowserActionDragData browser_action_drag_data(extension_a()->id(), 0u);
browser_action_drag_data.Write(profile(), &drop_data);
ToolbarActionView* view = first->GetViewForId(extension_b()->id());
// ...to the right of extension B.
gfx::Point location(view->x() + view->width(), view->y());
ui::DropTargetEvent target_event(
drop_data, location, location, ui::DragDropTypes::DRAG_MOVE);
// Drag and drop.
first->OnDragUpdated(target_event);
first->OnPerformDrop(target_event);
// The new order, B A C, should be reflected in *both* containers, even
// though the drag only happened in the first one.
EXPECT_EQ(extension_b()->id(), first->GetIdAt(0u));
EXPECT_EQ(extension_b()->id(), second->GetIdAt(0u));
EXPECT_EQ(extension_a()->id(), first->GetIdAt(1u));
EXPECT_EQ(extension_a()->id(), second->GetIdAt(1u));
EXPECT_EQ(extension_c()->id(), first->GetIdAt(2u));
EXPECT_EQ(extension_c()->id(), second->GetIdAt(2u));
// Next, simulate a resize by shrinking the container.
first->OnResize(1, true);
// The first and second container should each have resized.
EXPECT_EQ(2u, first->VisibleBrowserActions());
EXPECT_EQ(2u, second->VisibleBrowserActions());
}
// Test that the BrowserActionsContainer responds correctly when the underlying
// model enters highlight mode, and that browser actions are undraggable in
// highlight mode. (Highlight mode itself it tested more thoroughly in the
// ExtensionToolbarModel browsertests).
IN_PROC_BROWSER_TEST_F(BrowserActionsBarBrowserTest, HighlightMode) {
LoadExtensions();
EXPECT_EQ(3, browser_actions_bar()->VisibleBrowserActions());
EXPECT_EQ(3, browser_actions_bar()->NumberOfBrowserActions());
BrowserActionsContainer* container =
BrowserView::GetBrowserViewForBrowser(browser())
->toolbar()->browser_actions();
// Currently, dragging should be enabled.
ToolbarActionView* action_view = container->GetToolbarActionViewAt(0);
ASSERT_TRUE(action_view);
gfx::Point point(action_view->x(), action_view->y());
EXPECT_TRUE(container->CanStartDragForView(action_view, point, point));
extensions::ExtensionIdList extension_ids;
extension_ids.push_back(extension_a()->id());
extension_ids.push_back(extension_b()->id());
toolbar_model()->HighlightExtensions(extension_ids);
// Only two browser actions should be visible.
EXPECT_EQ(2, browser_actions_bar()->VisibleBrowserActions());
EXPECT_EQ(2, browser_actions_bar()->NumberOfBrowserActions());
// We shouldn't be able to drag in highlight mode.
action_view = container->GetToolbarActionViewAt(0);
EXPECT_FALSE(container->CanStartDragForView(action_view, point, point));
// We should go back to normal after leaving highlight mode.
toolbar_model()->StopHighlighting();
EXPECT_EQ(3, browser_actions_bar()->VisibleBrowserActions());
EXPECT_EQ(3, browser_actions_bar()->NumberOfBrowserActions());
action_view = container->GetToolbarActionViewAt(0);
EXPECT_TRUE(container->CanStartDragForView(action_view, point, point));
}
// Test the behavior of the overflow container for Extension Actions.
class BrowserActionsContainerOverflowTest
: public BrowserActionsBarRedesignBrowserTest {
public:
BrowserActionsContainerOverflowTest() : main_bar_(nullptr),
overflow_bar_(nullptr) {
}
~BrowserActionsContainerOverflowTest() override {}
protected:
// Returns true if the order of the ToolbarActionViews in |main_bar_|
// and |overflow_bar_| match.
bool ViewOrdersMatch();
// Returns Success if the visible count matches |expected_visible|. This means
// that the number of visible browser actions in |main_bar_| is
// |expected_visible| and shows the first icons, and that the overflow bar
// shows all (and only) the remainder.
testing::AssertionResult VerifyVisibleCount(size_t expected_visible)
WARN_UNUSED_RESULT;
// Accessors.
BrowserActionsContainer* main_bar() { return main_bar_; }
BrowserActionsContainer* overflow_bar() { return overflow_bar_; }
private:
void SetUpOnMainThread() override;
void TearDownOnMainThread() override;
// The main BrowserActionsContainer (owned by the browser view).
BrowserActionsContainer* main_bar_;
// A parent view for the overflow menu.
scoped_ptr<views::View> overflow_parent_;
// The overflow BrowserActionsContainer. We manufacture this so that we don't
// have to open the wrench menu.
// Owned by the |overflow_parent_|.
BrowserActionsContainer* overflow_bar_;
// The associated toolbar model.
extensions::ExtensionToolbarModel* model_;
DISALLOW_COPY_AND_ASSIGN(BrowserActionsContainerOverflowTest);
};
void BrowserActionsContainerOverflowTest::SetUpOnMainThread() {
BrowserActionsBarBrowserTest::SetUpOnMainThread();
main_bar_ = BrowserView::GetBrowserViewForBrowser(browser())
->toolbar()->browser_actions();
overflow_parent_.reset(new views::View());
overflow_parent_->set_owned_by_client();
overflow_bar_ = new BrowserActionsContainer(browser(), main_bar_);
overflow_parent_->AddChildView(overflow_bar_);
}
void BrowserActionsContainerOverflowTest::TearDownOnMainThread() {
overflow_parent_.reset();
BrowserActionsBarBrowserTest::TearDownOnMainThread();
}
bool BrowserActionsContainerOverflowTest::ViewOrdersMatch() {
if (main_bar_->num_toolbar_actions() !=
overflow_bar_->num_toolbar_actions())
return false;
for (size_t i = 0; i < main_bar_->num_toolbar_actions(); ++i) {
if (main_bar_->GetIdAt(i) != overflow_bar_->GetIdAt(i))
return false;
}
return true;
}
testing::AssertionResult
BrowserActionsContainerOverflowTest::VerifyVisibleCount(
size_t expected_visible) {
// Views order should always match (as it is based directly off the model).
if (!ViewOrdersMatch())
return testing::AssertionFailure() << "View orders don't match";
// Loop through and check each browser action for proper visibility (which
// implicitly also guarantees that the proper number are visible).
for (size_t i = 0; i < overflow_bar_->num_toolbar_actions(); ++i) {
bool visible = i < expected_visible;
if (main_bar_->GetToolbarActionViewAt(i)->visible() != visible) {
return testing::AssertionFailure() << "Index " << i <<
" has improper visibility in main: " << !visible;
}
if (overflow_bar_->GetToolbarActionViewAt(i)->visible() == visible) {
return testing::AssertionFailure() << "Index " << i <<
" has improper visibility in overflow: " << visible;
}
}
return testing::AssertionSuccess();
}
// Test the basic functionality of the BrowserActionsContainer in overflow mode.
IN_PROC_BROWSER_TEST_F(BrowserActionsContainerOverflowTest,
TestBasicActionOverflow) {
LoadExtensions();
// Since the overflow bar isn't attached to a view, we have to kick it in
// order to retrigger layout each time we change the number of icons in the
// bar.
overflow_bar()->Layout();
// All actions are showing, and are in the installation order.
EXPECT_TRUE(toolbar_model()->all_icons_visible());
EXPECT_EQ(3u, toolbar_model()->visible_icon_count());
ASSERT_EQ(3u, main_bar()->num_toolbar_actions());
EXPECT_EQ(extension_a()->id(), main_bar()->GetIdAt(0u));
EXPECT_EQ(extension_b()->id(), main_bar()->GetIdAt(1u));
EXPECT_EQ(extension_c()->id(), main_bar()->GetIdAt(2u));
EXPECT_TRUE(VerifyVisibleCount(3u));
// Reduce the visible count to 2. Order should be unchanged (A B C), but
// only A and B should be visible on the main bar.
toolbar_model()->SetVisibleIconCount(2u);
overflow_bar()->Layout(); // Kick.
EXPECT_EQ(extension_a()->id(), main_bar()->GetIdAt(0u));
EXPECT_EQ(extension_b()->id(), main_bar()->GetIdAt(1u));
EXPECT_EQ(extension_c()->id(), main_bar()->GetIdAt(2u));
EXPECT_TRUE(VerifyVisibleCount(2u));
// Move extension C to the first position. Order should now be C A B, with
// C and A visible in the main bar.
toolbar_model()->MoveExtensionIcon(extension_c()->id(), 0);
overflow_bar()->Layout(); // Kick.
EXPECT_EQ(extension_c()->id(), main_bar()->GetIdAt(0u));
EXPECT_EQ(extension_a()->id(), main_bar()->GetIdAt(1u));
EXPECT_EQ(extension_b()->id(), main_bar()->GetIdAt(2u));
EXPECT_TRUE(VerifyVisibleCount(2u));
// Hide action A. This results in it being sent to overflow, and reducing the
// visible size to 1, so the order should be C A B, with only C visible in the
// main bar.
extensions::ExtensionActionAPI::SetBrowserActionVisibility(
extensions::ExtensionPrefs::Get(profile()),
extension_a()->id(),
false);
overflow_bar()->Layout(); // Kick.
EXPECT_EQ(extension_c()->id(), main_bar()->GetIdAt(0u));
EXPECT_EQ(extension_a()->id(), main_bar()->GetIdAt(1u));
EXPECT_EQ(extension_b()->id(), main_bar()->GetIdAt(2u));
EXPECT_TRUE(VerifyVisibleCount(1u));
}
// Test drag and drop between the overflow container and the main container.
IN_PROC_BROWSER_TEST_F(BrowserActionsContainerOverflowTest,
TestOverflowDragging) {
LoadExtensions();
// Start with one extension in overflow.
toolbar_model()->SetVisibleIconCount(2u);
overflow_bar()->Layout();
// Verify starting state is A B [C].
ASSERT_EQ(3u, main_bar()->num_toolbar_actions());
EXPECT_EQ(extension_a()->id(), main_bar()->GetIdAt(0u));
EXPECT_EQ(extension_b()->id(), main_bar()->GetIdAt(1u));
EXPECT_EQ(extension_c()->id(), main_bar()->GetIdAt(2u));
EXPECT_TRUE(VerifyVisibleCount(2u));
// Drag extension A (on the main bar) to the left of extension C (in
// overflow).
ui::OSExchangeData drop_data;
BrowserActionDragData browser_action_drag_data(extension_a()->id(), 0u);
browser_action_drag_data.Write(profile(), &drop_data);
ToolbarActionView* view = overflow_bar()->GetViewForId(extension_c()->id());
gfx::Point location(view->x(), view->y());
ui::DropTargetEvent target_event(
drop_data, location, location, ui::DragDropTypes::DRAG_MOVE);
overflow_bar()->OnDragUpdated(target_event);
overflow_bar()->OnPerformDrop(target_event);
overflow_bar()->Layout();
// Order should now be B [A C].
EXPECT_EQ(extension_b()->id(), main_bar()->GetIdAt(0u));
EXPECT_EQ(extension_a()->id(), main_bar()->GetIdAt(1u));
EXPECT_EQ(extension_c()->id(), main_bar()->GetIdAt(2u));
EXPECT_TRUE(VerifyVisibleCount(1u));
// Drag extension A back from overflow to the main bar.
ui::OSExchangeData drop_data2;
BrowserActionDragData browser_action_drag_data2(extension_a()->id(), 1u);
browser_action_drag_data2.Write(profile(), &drop_data2);
view = main_bar()->GetViewForId(extension_b()->id());
location = gfx::Point(view->x(), view->y());
ui::DropTargetEvent target_event2(
drop_data2, location, location, ui::DragDropTypes::DRAG_MOVE);
main_bar()->OnDragUpdated(target_event2);
main_bar()->OnPerformDrop(target_event2);
// Order should be A B [C] again.
EXPECT_EQ(extension_a()->id(), main_bar()->GetIdAt(0u));
EXPECT_EQ(extension_b()->id(), main_bar()->GetIdAt(1u));
EXPECT_EQ(extension_c()->id(), main_bar()->GetIdAt(2u));
EXPECT_TRUE(VerifyVisibleCount(2u));
// Drag extension C from overflow to the main bar (before extension B).
ui::OSExchangeData drop_data3;
BrowserActionDragData browser_action_drag_data3(extension_c()->id(), 2u);
browser_action_drag_data3.Write(profile(), &drop_data3);
location = gfx::Point(view->x(), view->y());
ui::DropTargetEvent target_event3(
drop_data3, location, location, ui::DragDropTypes::DRAG_MOVE);
main_bar()->OnDragUpdated(target_event3);
main_bar()->OnPerformDrop(target_event3);
// Order should be A C B, and there should be no extensions in overflow.
EXPECT_EQ(extension_a()->id(), main_bar()->GetIdAt(0u));
EXPECT_EQ(extension_c()->id(), main_bar()->GetIdAt(1u));
EXPECT_EQ(extension_b()->id(), main_bar()->GetIdAt(2u));
EXPECT_TRUE(VerifyVisibleCount(3u));
}
|