summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortapted <tapted@chromium.org>2016-03-13 19:00:42 -0700
committerCommit bot <commit-bot@chromium.org>2016-03-14 02:02:03 +0000
commit2a697015fafd0c99b169890a9a7ac0b2011a91c0 (patch)
tree506611ad19952977ccbb79623b8fe11089dd34eb
parenta9224ac0cc140b536c8e13099fcf3cfc4cc4a80d (diff)
downloadchromium_src-2a697015fafd0c99b169890a9a7ac0b2011a91c0.zip
chromium_src-2a697015fafd0c99b169890a9a7ac0b2011a91c0.tar.gz
chromium_src-2a697015fafd0c99b169890a9a7ac0b2011a91c0.tar.bz2
MacViews: Fix ViewTest.HandleAccelerator by faking window activation
In fact, fake activation for all tests that use ViewsTestHelper, except those that run as interactive_ui_tests. Currently for HandleAccelerator, `EXPECT_TRUE(widget->IsActive())` fails on Mac since desktop widgets are used rather than Aura widgets, so activation is asynchronous. Also TYPE_POPUP widgets are not activatable by default (on all platforms). For non-desktop Aura widgets, Show() on non-activatable widgets does not activate, but Activate() does allow activation. Make it easier for a unit test inheriting from ViewsTestBase to use fake window activation on Mac without using #ifdefs by just giving it to them by default. BUG=592890 Review URL: https://codereview.chromium.org/1782773002 Cr-Commit-Position: refs/heads/master@{#380917}
-rw-r--r--ui/base/test/ui_controls.h4
-rw-r--r--ui/base/test/ui_controls_mac.mm4
-rw-r--r--ui/views/BUILD.gn1
-rw-r--r--ui/views/accessible_pane_view_unittest.cc11
-rw-r--r--ui/views/bubble/bubble_delegate_unittest.cc9
-rw-r--r--ui/views/cocoa/bridged_native_widget_interactive_uitest.mm7
-rw-r--r--ui/views/controls/textfield/textfield_unittest.cc6
-rw-r--r--ui/views/test/views_test_helper_mac.h11
-rw-r--r--ui/views/test/views_test_helper_mac.mm12
-rw-r--r--ui/views/test/widget_test.h19
-rw-r--r--ui/views/test/widget_test_mac.mm27
-rw-r--r--ui/views/view_unittest.cc6
-rw-r--r--ui/views/views.gyp1
-rw-r--r--ui/views/widget/native_widget_mac_interactive_uitest.mm7
-rw-r--r--ui/views/window/dialog_delegate_unittest.cc10
15 files changed, 50 insertions, 85 deletions
diff --git a/ui/base/test/ui_controls.h b/ui/base/test/ui_controls.h
index e54f936..4fc54ab 100644
--- a/ui/base/test/ui_controls.h
+++ b/ui/base/test/ui_controls.h
@@ -44,6 +44,10 @@ namespace ui_controls {
// tests.
void EnableUIControls();
+#if defined(OS_MACOSX)
+bool IsUIControlsEnabled();
+#endif
+
bool SendKeyPress(gfx::NativeWindow window,
ui::KeyboardCode key,
bool control,
diff --git a/ui/base/test/ui_controls_mac.mm b/ui/base/test/ui_controls_mac.mm
index cd4103d..b7615b6 100644
--- a/ui/base/test/ui_controls_mac.mm
+++ b/ui/base/test/ui_controls_mac.mm
@@ -179,6 +179,10 @@ void EnableUIControls() {
g_ui_controls_enabled = true;
}
+bool IsUIControlsEnabled() {
+ return g_ui_controls_enabled;
+}
+
bool SendKeyPress(gfx::NativeWindow window,
ui::KeyboardCode key,
bool control,
diff --git a/ui/views/BUILD.gn b/ui/views/BUILD.gn
index 8ef6465..16bc746 100644
--- a/ui/views/BUILD.gn
+++ b/ui/views/BUILD.gn
@@ -160,6 +160,7 @@ source_set("test_support_internal") {
"//skia",
"//testing/gtest",
"//ui/base",
+ "//ui/base:test_support",
"//ui/base/ime",
"//ui/compositor",
"//ui/compositor:test_support",
diff --git a/ui/views/accessible_pane_view_unittest.cc b/ui/views/accessible_pane_view_unittest.cc
index 303d206..864481f 100644
--- a/ui/views/accessible_pane_view_unittest.cc
+++ b/ui/views/accessible_pane_view_unittest.cc
@@ -12,10 +12,6 @@
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget.h"
-#if defined(OS_MACOSX)
-#include "ui/base/test/scoped_fake_nswindow_focus.h"
-#endif
-
namespace views {
// TODO(alicet): bring pane rotation into views and add tests.
@@ -105,13 +101,6 @@ TEST_F(AccessiblePaneViewTest, SimpleSetPaneFocus) {
}
TEST_F(AccessiblePaneViewTest, SetPaneFocusAndRestore) {
-#if defined(OS_MACOSX)
- // On Aura platforms, this test creates Ash windows and only interacts with
- // the Ash window manager. On Mac, it creates native windows, but since unit
- // tests cannot gain key status, fake it out here.
- ui::test::ScopedFakeNSWindowFocus fake_focus;
-#endif
-
View* test_view_main = new View();
scoped_ptr<Widget> widget_main(new Widget());
Widget::InitParams params_main = CreateParams(Widget::InitParams::TYPE_POPUP);
diff --git a/ui/views/bubble/bubble_delegate_unittest.cc b/ui/views/bubble/bubble_delegate_unittest.cc
index 5998d14..f7656f0 100644
--- a/ui/views/bubble/bubble_delegate_unittest.cc
+++ b/ui/views/bubble/bubble_delegate_unittest.cc
@@ -15,10 +15,6 @@
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"
-#if defined(OS_MACOSX)
-#include "ui/base/test/scoped_fake_nswindow_focus.h"
-#endif
-
namespace views {
namespace {
@@ -70,11 +66,6 @@ class BubbleDelegateTest : public ViewsTestBase {
}
private:
-#if defined(OS_MACOSX)
- // Ensure tests running in parallel don't steal focus from the Widget on Mac.
- ui::test::ScopedFakeNSWindowFocus fake_focus_;
-#endif
-
DISALLOW_COPY_AND_ASSIGN(BubbleDelegateTest);
};
diff --git a/ui/views/cocoa/bridged_native_widget_interactive_uitest.mm b/ui/views/cocoa/bridged_native_widget_interactive_uitest.mm
index 81ef73f..686b2c1 100644
--- a/ui/views/cocoa/bridged_native_widget_interactive_uitest.mm
+++ b/ui/views/cocoa/bridged_native_widget_interactive_uitest.mm
@@ -11,6 +11,7 @@
#include "base/macros.h"
#include "ui/base/hit_test.h"
#import "ui/base/test/nswindow_fullscreen_notification_waiter.h"
+#include "ui/base/test/ui_controls.h"
#import "ui/base/test/windowed_nsnotification_observer.h"
#import "ui/events/test/cocoa_test_event_utils.h"
#include "ui/views/test/widget_test.h"
@@ -39,7 +40,11 @@ class ResizableDelegateView : public WidgetDelegateView {
class BridgedNativeWidgetUITest : public test::WidgetTest {
public:
- BridgedNativeWidgetUITest() {}
+ BridgedNativeWidgetUITest() {
+ // TODO(tapted): Remove this when these are absorbed into Chrome's
+ // interactive_ui_tests target. See http://crbug.com/403679.
+ ui_controls::EnableUIControls();
+ }
// testing::Test:
void SetUp() override {
diff --git a/ui/views/controls/textfield/textfield_unittest.cc b/ui/views/controls/textfield/textfield_unittest.cc
index b619b84..97595c5 100644
--- a/ui/views/controls/textfield/textfield_unittest.cc
+++ b/ui/views/controls/textfield/textfield_unittest.cc
@@ -436,11 +436,6 @@ class TextfieldTest : public ViewsTestBase, public TextfieldController {
widget_->Show();
textfield_->RequestFocus();
- // On Mac, activation is asynchronous since desktop widgets are used. We
- // don't want parallel tests to steal active status either, so fake it.
-#if defined(OS_MACOSX) && !defined(USE_AURA)
- fake_activation_ = test::WidgetTest::FakeWidgetIsActiveAlways();
-#endif
event_generator_.reset(
new ui::test::EventGenerator(GetContext(), widget_->GetNativeWindow()));
}
@@ -660,7 +655,6 @@ class TextfieldTest : public ViewsTestBase, public TextfieldController {
private:
ui::ClipboardType copied_to_clipboard_;
- scoped_ptr<test::WidgetTest::FakeActivation> fake_activation_;
scoped_ptr<ui::test::EventGenerator> event_generator_;
DISALLOW_COPY_AND_ASSIGN(TextfieldTest);
};
diff --git a/ui/views/test/views_test_helper_mac.h b/ui/views/test/views_test_helper_mac.h
index 31fc0d8..6dbb253 100644
--- a/ui/views/test/views_test_helper_mac.h
+++ b/ui/views/test/views_test_helper_mac.h
@@ -10,6 +10,9 @@
#include "ui/views/test/views_test_helper.h"
namespace ui {
+namespace test {
+class ScopedFakeNSWindowFocus;
+}
class ScopedAnimationDurationScaleMode;
}
@@ -21,12 +24,20 @@ class ViewsTestHelperMac : public ViewsTestHelper {
~ViewsTestHelperMac() override;
// ViewsTestHelper:
+ void SetUp() override;
void TearDown() override;
private:
// Disable animations during tests.
scoped_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
+ // When using desktop widgets on Mac, window activation is asynchronous
+ // because the window server is involved. A window may also be deactivated by
+ // a test running in parallel, making it flaky. In non-interactive/sharded
+ // tests, |faked_focus_| is initialized, permitting a unit test to "fake" this
+ // activation, causing it to be synchronous and per-process instead.
+ scoped_ptr<ui::test::ScopedFakeNSWindowFocus> faked_focus_;
+
DISALLOW_COPY_AND_ASSIGN(ViewsTestHelperMac);
};
diff --git a/ui/views/test/views_test_helper_mac.mm b/ui/views/test/views_test_helper_mac.mm
index 8ef73d2..529d5c6 100644
--- a/ui/views/test/views_test_helper_mac.mm
+++ b/ui/views/test/views_test_helper_mac.mm
@@ -7,6 +7,8 @@
#import <Cocoa/Cocoa.h>
#import "base/mac/scoped_nsautorelease_pool.h"
+#include "ui/base/test/scoped_fake_nswindow_focus.h"
+#include "ui/base/test/ui_controls.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/views/test/event_generator_delegate_mac.h"
#include "ui/views/widget/widget.h"
@@ -33,6 +35,16 @@ ViewsTestHelperMac::ViewsTestHelperMac()
ViewsTestHelperMac::~ViewsTestHelperMac() {
}
+void ViewsTestHelperMac::SetUp() {
+ ViewsTestHelper::SetUp();
+ // Assume that if the methods in the ui_controls.h test header are enabled
+ // then the test runner is in a non-sharded mode, and will use "real"
+ // activations. This allows interactive_ui_tests to test the actual OS window
+ // activation codepaths.
+ if (!ui_controls::IsUIControlsEnabled())
+ faked_focus_.reset(new ui::test::ScopedFakeNSWindowFocus);
+}
+
void ViewsTestHelperMac::TearDown() {
// Ensure all Widgets are closed explicitly in tests. The Widget may be
// hosting a Compositor. If that's torn down after the test ContextFactory
diff --git a/ui/views/test/widget_test.h b/ui/views/test/widget_test.h
index 3b7dfe1..a5f8782 100644
--- a/ui/views/test/widget_test.h
+++ b/ui/views/test/widget_test.h
@@ -33,21 +33,6 @@ namespace test {
class WidgetTest : public ViewsTestBase {
public:
- // Scoped handle that fakes all widgets into claiming they are active. This
- // allows a test to assume active status does not get stolen by a test that
- // may be running in parallel. It shouldn't be used in tests that create
- // multiple widgets.
- class FakeActivation {
- public:
- virtual ~FakeActivation() {}
-
- protected:
- FakeActivation() {}
-
- private:
- DISALLOW_COPY_AND_ASSIGN(FakeActivation);
- };
-
WidgetTest();
~WidgetTest() override;
@@ -104,10 +89,6 @@ class WidgetTest : public ViewsTestBase {
static ui::internal::InputMethodDelegate* GetInputMethodDelegateForWidget(
Widget* widget);
-#if defined(OS_MACOSX)
- static scoped_ptr<FakeActivation> FakeWidgetIsActiveAlways();
-#endif
-
private:
DISALLOW_COPY_AND_ASSIGN(WidgetTest);
};
diff --git a/ui/views/test/widget_test_mac.mm b/ui/views/test/widget_test_mac.mm
index 47a88e5..b63f41c 100644
--- a/ui/views/test/widget_test_mac.mm
+++ b/ui/views/test/widget_test_mac.mm
@@ -13,33 +13,11 @@
#include "ui/views/widget/native_widget_mac.h"
#include "ui/views/widget/root_view.h"
-@interface IsKeyWindowDonor : NSObject
-@end
-
-@implementation IsKeyWindowDonor
-- (BOOL)isKeyWindow {
- return YES;
-}
-@end
-
namespace views {
namespace test {
namespace {
-class FakeActivationMac : public WidgetTest::FakeActivation {
- public:
- FakeActivationMac()
- : swizzler_([NSWindow class],
- [IsKeyWindowDonor class],
- @selector(isKeyWindow)) {}
-
- private:
- base::mac::ScopedObjCClassSwizzler swizzler_;
-
- DISALLOW_COPY_AND_ASSIGN(FakeActivationMac);
-};
-
// The NSWindow last activated by SimulateNativeActivate(). It will have a
// simulated deactivate on a subsequent call.
NSWindow* g_simulated_active_window_ = nil;
@@ -107,11 +85,6 @@ ui::EventProcessor* WidgetTest::GetEventProcessor(Widget* widget) {
}
// static
-scoped_ptr<WidgetTest::FakeActivation> WidgetTest::FakeWidgetIsActiveAlways() {
- return make_scoped_ptr(new FakeActivationMac);
-}
-
-// static
ui::internal::InputMethodDelegate* WidgetTest::GetInputMethodDelegateForWidget(
Widget* widget) {
return NativeWidgetMac::GetBridgeForNativeWindow(widget->GetNativeWindow());
diff --git a/ui/views/view_unittest.cc b/ui/views/view_unittest.cc
index 7d70000..4b50b319 100644
--- a/ui/views/view_unittest.cc
+++ b/ui/views/view_unittest.cc
@@ -2115,7 +2115,7 @@ TEST_F(ViewTest, HandleAccelerator) {
// Create a window and add the view as its child.
scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
- params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
+ params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(0, 0, 100, 100);
widget->Init(params);
View* root = widget->GetRootView();
@@ -2132,6 +2132,10 @@ TEST_F(ViewTest, HandleAccelerator) {
EXPECT_EQ(0, view->accelerator_count_map_[return_accelerator]);
#endif
+ // TYPE_POPUP widgets default to non-activatable, so the Show() above wouldn't
+ // have activated the Widget. First, allow activation.
+ widget->widget_delegate()->set_can_activate(true);
+
// When a non-child view is active, it should handle accelerators.
view->accelerator_count_map_[return_accelerator] = 0;
widget->Activate();
diff --git a/ui/views/views.gyp b/ui/views/views.gyp
index 8c12de9..b792e30 100644
--- a/ui/views/views.gyp
+++ b/ui/views/views.gyp
@@ -812,6 +812,7 @@
'../../testing/gtest.gyp:gtest',
'../base/ime/ui_base_ime.gyp:ui_base_ime',
'../base/ui_base.gyp:ui_base',
+ '../base/ui_base.gyp:ui_base_test_support',
'../compositor/compositor.gyp:compositor',
'../compositor/compositor.gyp:compositor_test_support',
'../events/events.gyp:events',
diff --git a/ui/views/widget/native_widget_mac_interactive_uitest.mm b/ui/views/widget/native_widget_mac_interactive_uitest.mm
index 88d51e2..e8537ab 100644
--- a/ui/views/widget/native_widget_mac_interactive_uitest.mm
+++ b/ui/views/widget/native_widget_mac_interactive_uitest.mm
@@ -9,6 +9,7 @@
#import "base/mac/mac_util.h"
#import "base/mac/scoped_nsobject.h"
#include "base/macros.h"
+#include "ui/base/test/ui_controls.h"
#import "ui/base/test/windowed_nsnotification_observer.h"
#include "ui/views/test/test_widget_observer.h"
#include "ui/views/test/widget_test.h"
@@ -25,7 +26,11 @@ class NativeWidgetMacInteractiveUITest
class Observer;
NativeWidgetMacInteractiveUITest()
- : activationCount_(0), deactivationCount_(0) {}
+ : activationCount_(0), deactivationCount_(0) {
+ // TODO(tapted): Remove this when these are absorbed into Chrome's
+ // interactive_ui_tests target. See http://crbug.com/403679.
+ ui_controls::EnableUIControls();
+ }
Widget* MakeWidget() {
return GetParam() ? CreateTopLevelFramelessPlatformWidget()
diff --git a/ui/views/window/dialog_delegate_unittest.cc b/ui/views/window/dialog_delegate_unittest.cc
index df9d192..54099e6 100644
--- a/ui/views/window/dialog_delegate_unittest.cc
+++ b/ui/views/window/dialog_delegate_unittest.cc
@@ -18,10 +18,6 @@
#include "ui/views/window/dialog_client_view.h"
#include "ui/views/window/dialog_delegate.h"
-#if defined(OS_MACOSX)
-#include "ui/base/test/scoped_fake_nswindow_focus.h"
-#endif
-
namespace views {
namespace {
@@ -140,12 +136,6 @@ class DialogTest : public ViewsTestBase {
private:
TestDialog* dialog_;
-#if defined(OS_MACOSX)
- // Causes Widget::Show() to transfer focus synchronously and become immune to
- // losing focus to processes running in parallel.
- ui::test::ScopedFakeNSWindowFocus fake_focus;
-#endif
-
DISALLOW_COPY_AND_ASSIGN(DialogTest);
};