Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / ios / chrome / browser / ui / keyboard / hardware_keyboard_watcher_unittest.mm
blob6200e151ad0339a7d6df51274d7a2b995e91cad0
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"
13 namespace {
15 void PostKeyboardWillChangeNotification(CGRect beginFrame, CGRect endFrame) {
16   [[NSNotificationCenter defaultCenter]
17       postNotificationName:UIKeyboardWillChangeFrameNotification
18                     object:nil
19                   userInfo:@{
20                     UIKeyboardFrameBeginUserInfoKey :
21                         [NSValue valueWithCGRect:beginFrame],
22                     UIKeyboardFrameEndUserInfoKey :
23                         [NSValue valueWithCGRect:endFrame],
24                   }];
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]]
46       window];
47   base::scoped_nsobject<HardwareKeyboardWatcher> watcher(
48       [[HardwareKeyboardWatcher alloc] initWithAccessoryView:mockView]);
50   PostKeyboardWillChangeNotification(CGRectZero, CGRectInfinite);
52   histogram_tester.ExpectUniqueSample("Omnibox.HardwareKeyboardModeEnabled",
53                                       false, 1);
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]]
60       window];
61   base::scoped_nsobject<HardwareKeyboardWatcher> watcher(
62       [[HardwareKeyboardWatcher alloc] initWithAccessoryView:mockView]);
64   PostKeyboardWillChangeNotification(CGRectInfinite, CGRectZero);
66   histogram_tester.ExpectUniqueSample("Omnibox.HardwareKeyboardModeEnabled",
67                                       false, 1);
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]]
75       window];
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",
83                                       false, 1);
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]]
91       window];
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",
99                                       false, 1);
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]]
107       window];
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",
115                                       true, 1);
118 }  // namespace