Commit 5a1cee4f authored by tapted's avatar tapted Committed by Commit bot

Views: Give ScrollViewTest a test harness.

Currently the tests are all TEST rather than TEST_F.

Add a test harness (TEST_F) to reduce some boilerplate and encapsulate
some subtle setup code required for overlay scrollers on Mac.

Part of this is to do with ScopedPreferredScrollerStyle: Interleaving
swizzlers like `std::unique_ptr x(new X1); x.reset(new X2);` causes
incorrect behaviour and leakage of the swizzled methods across tests run
in the same process. Specifically, X1's destructor restores the original
methods, but then X2's destructor will restore X1's swizzled methods,
since they were in effect when X2 was constructed. Make it harder for
devs to shoot themselves in the foot with this.

BUG=615948

Review-Url: https://codereview.chromium.org/2442223002
Cr-Commit-Position: refs/heads/master@{#427253}
parent 59676aad
......@@ -6,12 +6,17 @@
#import <AppKit/AppKit.h>
#include "base/logging.h"
#import "base/mac/scoped_objc_class_swizzler.h"
using base::mac::ScopedObjCClassSwizzler;
namespace {
// Swizzling can be stacked, but not interleaved without creating unexpected
// states. Require that there is only one swizzler rather than tracking a stack.
bool g_swizzling = false;
void NotifyStyleChanged() {
[[NSNotificationCenter defaultCenter]
postNotificationName:NSPreferredScrollerStyleDidChangeNotification
......@@ -60,6 +65,8 @@ ScopedPreferredScrollerStyle::ScopedPreferredScrollerStyle(bool overlay)
? [FakeNSScrollerPreferredStyleOverlayDonor class]
: [FakeNSScrollerPreferredStyleLegacyDonor class];
DCHECK(!g_swizzling);
g_swizzling = true;
swizzler_.reset(new ScopedObjCClassSwizzler(
[NSScroller class], style_class, @selector(preferredScrollerStyle)));
......@@ -69,6 +76,8 @@ ScopedPreferredScrollerStyle::ScopedPreferredScrollerStyle(bool overlay)
ScopedPreferredScrollerStyle::~ScopedPreferredScrollerStyle() {
swizzler_.reset();
DCHECK(g_swizzling);
g_swizzling = false;
if ([NSScroller preferredScrollerStyle] != GetScrollerStyle(overlay_))
NotifyStyleChanged();
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment