Skip to content

Commit

Permalink
Upstream two UI-related test utilities for iOS
Browse files Browse the repository at this point in the history
This upstreams two test utility files:
- KeyboardAppearanceListener, which watches for the software keyboard
  showing and hiding
- ui_view_test_utils, which can Dismiss any visible alerts, and can
  force the display of a view (to ensure state changes that the OS
  might defer until drawing take effect).

These are located in ui/ as they will be used in both ios/web/ and
ios/chrome/.

BUG=464810

Review URL: https://codereview.chromium.org/1122543002

Cr-Commit-Position: refs/heads/master@{#328775}
  • Loading branch information
stuartmorgan authored and Commit bot committed May 7, 2015
1 parent d0abaa3 commit 0f91f7a
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 17 deletions.
18 changes: 18 additions & 0 deletions ui/base/test/ios/keyboard_appearance_listener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2012 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.

#ifndef UI_BASE_TEST_IOS_KEYBOARD_APPEARANCE_LISTENER_H_
#define UI_BASE_TEST_IOS_KEYBOARD_APPEARANCE_LISTENER_H_

#import <UIKit/UIKit.h>

// Listener to observe the keyboard coming up or down.
@interface KeyboardAppearanceListener : NSObject

// Returns YES if the keyboard is currently visible.
@property(nonatomic, readonly, getter=isKeyboardVisible) bool keyboardVisible;

@end

#endif // UI_BASE_TEST_IOS_KEYBOARD_APPEARANCE_LISTENER_H_
46 changes: 46 additions & 0 deletions ui/base/test/ios/keyboard_appearance_listener.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2012 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.

#import "ui/base/test/ios/keyboard_appearance_listener.h"

#include <vector>

@implementation KeyboardAppearanceListener {
@private
std::vector<id> _notificationObservers;
}

@synthesize keyboardVisible = _keyboardVisible;

- (id)init {
self = [super init];
if (self) {
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
_notificationObservers.push_back(
[center addObserverForName:UIKeyboardDidShowNotification
object:nil
queue:nil
usingBlock:^(NSNotification* arg) {
_keyboardVisible = true;
}]);
_notificationObservers.push_back(
[center addObserverForName:UIKeyboardWillHideNotification
object:nil
queue:nil
usingBlock:^(NSNotification* arg) {
_keyboardVisible = false;
}]);
}
return self;
}

- (void)dealloc {
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
for (const auto& observer : _notificationObservers) {
[nc removeObserver:observer];
}
_notificationObservers.clear();
[super dealloc];
}
@end
26 changes: 26 additions & 0 deletions ui/base/test/ios/ui_view_test_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2014 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.

#ifndef UI_BASE_TEST_IOS_UI_VIEW_TEST_UTILS_H_
#define UI_BASE_TEST_IOS_UI_VIEW_TEST_UTILS_H_

#import <UIKit/UIKit.h>

namespace ui {
namespace test {
namespace uiview_utils {

// Cancels any visible UIAlertViews.
void CancelAlerts();

// Forces rendering of a UIView. This is used in tests to make sure that UIKit
// optimizations don't have the views return the previous values (such as
// zoomScale).
void ForceViewRendering(UIView* view);

} // namespace uiview_utils
} // test
} // namespace ui

#endif // UI_BASE_TEST_IOS_UI_VIEW_TEST_UTILS_H_
38 changes: 38 additions & 0 deletions ui/base/test/ios/ui_view_test_utils.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2014 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 "ui/base/test/ios/ui_view_test_utils.h"

#include "base/logging.h"

namespace ui {
namespace test {
namespace uiview_utils {

void CancelAlerts() {
for (UIWindow* window in [UIApplication sharedApplication].windows) {
for (UIView* view in [window subviews]) {
if ([view isKindOfClass:[UIAlertView class]]) {
UIAlertView* alertView = (UIAlertView*)view;
[alertView dismissWithClickedButtonIndex:0 animated:NO];
}
}
}
}

void ForceViewRendering(UIView* view) {
DCHECK(view);
CALayer* layer = view.layer;
DCHECK(layer);
// 19 is an arbitrary non-zero value.
UIGraphicsBeginImageContext(CGSizeMake(19, 19));
CGContext* context = UIGraphicsGetCurrentContext();
DCHECK(context);
[layer renderInContext:context];
UIGraphicsEndImageContext();
}

} // namespace uiview_utils
} // namespace test
} // namespace ui
31 changes: 14 additions & 17 deletions ui/base/ui_base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@
{
# GN version: //ui/base:test_support
'target_name': 'ui_base_test_support',
'type': 'static_library',
'dependencies': [
'../../base/base.gyp:base',
'../../skia/skia.gyp:skia',
Expand All @@ -642,6 +643,10 @@
],
'sources': [
# Note: file list duplicated in GN build.
'test/ios/keyboard_appearance_listener.h',
'test/ios/keyboard_appearance_listener.mm',
'test/ios/ui_view_test_utils.h',
'test/ios/ui_view_test_utils.mm',
'test/test_clipboard.cc',
'test/test_clipboard.h',
'test/ui_controls.h',
Expand All @@ -656,23 +661,15 @@
],
'conditions': [
['OS!="ios"', {
'type': 'static_library',
'dependecies': [
'ime/ui_base_ime.gyp:ui_base_ime',
],
'sources': [
'ime/dummy_input_method.cc',
'ime/dummy_input_method.h',
'ime/dummy_text_input_client.cc',
'ime/dummy_text_input_client.h',
],
}, { # OS=="ios"
# None of the sources in this target are built on iOS, resulting in
# link errors when building targets that depend on this target
# because the static library isn't found. If this target is changed
# to have sources that are built on iOS, the target should be changed
# to be of type static_library on all platforms.
'type': 'none',
'dependecies': [
'ime/ui_base_ime.gyp:ui_base_ime',
],
'sources': [
'ime/dummy_input_method.cc',
'ime/dummy_input_method.h',
'ime/dummy_text_input_client.cc',
'ime/dummy_text_input_client.h',
],
}],
['use_aura==1', {
'sources!': [
Expand Down

0 comments on commit 0f91f7a

Please sign in to comment.