1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ios/chrome/browser/ui/keyboard/hardware_keyboard_watcher.h"
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/test/histogram_tester.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
11 #import "third_party/ocmock/OCMock/OCMock.h"
15 void PostKeyboardWillChangeNotification(CGRect beginFrame, CGRect endFrame) {
16 [[NSNotificationCenter defaultCenter]
17 postNotificationName:UIKeyboardWillChangeFrameNotification
20 UIKeyboardFrameBeginUserInfoKey :
21 [NSValue valueWithCGRect:beginFrame],
22 UIKeyboardFrameEndUserInfoKey :
23 [NSValue valueWithCGRect:endFrame],
27 typedef PlatformTest HardwareKeyboardWatcherTest;
29 TEST_F(HardwareKeyboardWatcherTest, AccessoryViewNotInHierarchy_NoHistogram) {
30 base::HistogramTester histogram_tester;
31 id mockView = [OCMockObject niceMockForClass:[UIView class]];
32 [[mockView stub] andReturn:nil];
33 base::scoped_nsobject<HardwareKeyboardWatcher> watcher(
34 [[HardwareKeyboardWatcher alloc] initWithAccessoryView:mockView]);
36 PostKeyboardWillChangeNotification(CGRectZero, CGRectZero);
37 PostKeyboardWillChangeNotification(CGRectInfinite, CGRectInfinite);
39 histogram_tester.ExpectTotalCount("Omnibox.HardwareKeyboardModeEnabled", 0);
42 TEST_F(HardwareKeyboardWatcherTest, EmptyBeginFrame_SoftwareKeyboardHistogram) {
43 base::HistogramTester histogram_tester;
44 id mockView = [OCMockObject niceMockForClass:[UIView class]];
45 [[[mockView stub] andReturn:[[UIApplication sharedApplication] keyWindow]]
47 base::scoped_nsobject<HardwareKeyboardWatcher> watcher(
48 [[HardwareKeyboardWatcher alloc] initWithAccessoryView:mockView]);
50 PostKeyboardWillChangeNotification(CGRectZero, CGRectInfinite);
52 histogram_tester.ExpectUniqueSample("Omnibox.HardwareKeyboardModeEnabled",
56 TEST_F(HardwareKeyboardWatcherTest, EmptyEndFrame_SoftwareKeyboardHistogram) {
57 base::HistogramTester histogram_tester;
58 id mockView = [OCMockObject niceMockForClass:[UIView class]];
59 [[[mockView stub] andReturn:[[UIApplication sharedApplication] keyWindow]]
61 base::scoped_nsobject<HardwareKeyboardWatcher> watcher(
62 [[HardwareKeyboardWatcher alloc] initWithAccessoryView:mockView]);
64 PostKeyboardWillChangeNotification(CGRectInfinite, CGRectZero);
66 histogram_tester.ExpectUniqueSample("Omnibox.HardwareKeyboardModeEnabled",
70 TEST_F(HardwareKeyboardWatcherTest,
71 KeyboardFullyOnScreen_SoftwareKeyboardHistogram) {
72 base::HistogramTester histogram_tester;
73 id mockView = [OCMockObject niceMockForClass:[UIView class]];
74 [[[mockView stub] andReturn:[[UIApplication sharedApplication] keyWindow]]
76 base::scoped_nsobject<HardwareKeyboardWatcher> watcher(
77 [[HardwareKeyboardWatcher alloc] initWithAccessoryView:mockView]);
79 PostKeyboardWillChangeNotification(CGRectMake(0, 0, 100, 100),
80 CGRectMake(0, 100, 100, 100));
82 histogram_tester.ExpectUniqueSample("Omnibox.HardwareKeyboardModeEnabled",
86 TEST_F(HardwareKeyboardWatcherTest,
87 KeyboardFullyOffScreen_SoftwareKeyboardHistogram) {
88 base::HistogramTester histogram_tester;
89 id mockView = [OCMockObject niceMockForClass:[UIView class]];
90 [[[mockView stub] andReturn:[[UIApplication sharedApplication] keyWindow]]
92 base::scoped_nsobject<HardwareKeyboardWatcher> watcher(
93 [[HardwareKeyboardWatcher alloc] initWithAccessoryView:mockView]);
95 PostKeyboardWillChangeNotification(CGRectMake(0, -100, 100, 100),
96 CGRectMake(0, 0, 100, 100));
98 histogram_tester.ExpectUniqueSample("Omnibox.HardwareKeyboardModeEnabled",
102 TEST_F(HardwareKeyboardWatcherTest,
103 KeyboardPartiallyOnScreen_SoftwareKeyboardHistogram) {
104 base::HistogramTester histogram_tester;
105 id mockView = [OCMockObject niceMockForClass:[UIView class]];
106 [[[mockView stub] andReturn:[[UIApplication sharedApplication] keyWindow]]
108 base::scoped_nsobject<HardwareKeyboardWatcher> watcher(
109 [[HardwareKeyboardWatcher alloc] initWithAccessoryView:mockView]);
111 PostKeyboardWillChangeNotification(CGRectMake(0, -50, 100, 100),
112 CGRectMake(0, 0, 100, 100));
114 histogram_tester.ExpectUniqueSample("Omnibox.HardwareKeyboardModeEnabled",