Index: chrome/browser/ui/cocoa/permission_bubble_controller_unittest.mm |
diff --git a/chrome/browser/ui/cocoa/permission_bubble_controller_unittest.mm b/chrome/browser/ui/cocoa/permission_bubble_controller_unittest.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..21965ea23b3251a1a4163ab10eeaa34cc5b2901f |
--- /dev/null |
+++ b/chrome/browser/ui/cocoa/permission_bubble_controller_unittest.mm |
@@ -0,0 +1,258 @@ |
+// 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. |
+ |
+#import "chrome/browser/ui/cocoa/permission_bubble_controller.h" |
+ |
+#include "base/mac/foundation_util.h" |
+#include "base/strings/sys_string_conversions.h" |
+#include "base/strings/utf_string_conversions.h" |
+#import "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
+#import "chrome/browser/ui/cocoa/permission_bubble_cocoa.h" |
+#include "chrome/browser/ui/cocoa/run_loop_testing.h" |
+#include "chrome/browser/ui/website_settings/mock_permission_bubble_delegate.h" |
+#import "ui/base/test/cocoa_test_event_utils.h" |
+ |
+@interface PermissionBubbleController (ExposedForTesting) |
+- (void)ok:(id)sender; |
+- (void)onAllow:(id)sender; |
+- (void)onBlock:(id)sender; |
+- (void)onCustomize:(id)sender; |
+- (void)onCheckboxChanged:(id)sender; |
+@end |
+ |
+namespace { |
+const char* const kOKButtonLabel = "OK"; |
+const char* const kAllowButtonLabel = "Allow"; |
+const char* const kBlockButtonLabel = "Block"; |
+const char* const kCustomizeLabel = "Customize"; |
+const char* const kPermissionA = "Permission A"; |
+const char* const kPermissionB = "Permission B"; |
+const char* const kPermissionC = "Permission C"; |
+} |
+ |
+class PermissionBubbleControllerTest : public CocoaTest, |
+ public PermissionBubbleView::Delegate { |
+ public: |
+ |
+ MOCK_METHOD2(ToggleAccept, void(int, bool)); |
+ MOCK_METHOD0(SetCustomizationMode, void()); |
+ MOCK_METHOD0(Accept, void()); |
+ MOCK_METHOD0(Deny, void()); |
+ MOCK_METHOD0(Closing, void()); |
+ |
+ virtual void SetUp() OVERRIDE { |
+ CocoaTest::SetUp(); |
+ bridge_.reset(new PermissionBubbleCocoa(nil)); |
+ controller_ = [[PermissionBubbleController alloc] |
+ initWithParentWindow:test_window() |
+ bridge:bridge_.get()]; |
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ [controller_ close]; |
+ chrome::testing::NSRunLoopRunAllPending(); |
+ CocoaTest::TearDown(); |
+ } |
+ |
+ NSButton* FindButtonWithTitle(const std::string& text) { |
+ NSView* parent = base::mac::ObjCCast<NSView>([controller_ bubble]); |
groby-ooo-7-16
2014/02/04 02:39:30
Since parent _must_ be an NSView, ObjCCastStrict i
leng
2014/02/04 21:48:22
Done.
|
+ NSString* title = base::SysUTF8ToNSString(text); |
+ for (NSView* child in [parent subviews]) { |
+ if ([child isKindOfClass:[NSButton class]]) { |
+ NSButton* button = base::mac::ObjCCast<NSButton>(child); |
groby-ooo-7-16
2014/02/04 02:39:30
You can skip the isKindOfClass - ObjCCast will ret
leng
2014/02/04 21:48:22
Cool! Done.
|
+ if ([title isEqualToString:[button title]]) { |
+ return button; |
+ } |
+ } |
+ } |
+ return nil; |
+ } |
+ |
+ NSTextField* FindTextFieldWithString(const std::string& text) { |
+ NSView* parent = base::mac::ObjCCast<NSView>([controller_ bubble]); |
groby-ooo-7-16
2014/02/04 02:39:30
See above on strict
leng
2014/02/04 21:48:22
Done.
|
+ NSString* title = base::SysUTF8ToNSString(text); |
+ for (NSView* child in [parent subviews]) { |
+ if ([child isKindOfClass:[NSTextField class]]) { |
+ NSTextField* textField = base::mac::ObjCCast<NSTextField>(child); |
groby-ooo-7-16
2014/02/04 02:39:30
See above on isKindOfClass
leng
2014/02/04 21:48:22
Done.
|
+ if ([[textField stringValue] hasSuffix:title]) { |
+ return textField; |
+ } |
+ } |
+ } |
+ return nil; |
+ } |
+ |
+ protected: |
+ PermissionBubbleController* controller_; // Weak; it deletes itself. |
+ scoped_ptr<PermissionBubbleCocoa> bridge_; |
+}; |
+ |
+TEST_F(PermissionBubbleControllerTest, ShowAndClose) { |
+ EXPECT_FALSE([[controller_ window] isVisible]); |
+ [controller_ showWindow:nil]; |
+ EXPECT_TRUE([[controller_ window] isVisible]); |
+} |
+ |
+TEST_F(PermissionBubbleControllerTest, ShowSinglePermission) { |
+ MockPermissionBubbleDelegate delegate_a(kPermissionA); |
+ EXPECT_CALL(delegate_a, GetMessageTextFragment()).Times(1); |
+ |
+ std::vector<PermissionBubbleDelegate*> requests; |
+ requests.push_back(&delegate_a); |
+ std::vector<bool> acceptStates; |
+ |
+ [controller_ showAtAnchor:NSZeroPoint |
+ withDelegate:this |
+ forRequests:requests |
+ acceptStates:acceptStates |
+ customizationMode:NO]; |
+ |
+ EXPECT_TRUE(FindTextFieldWithString(kPermissionA)); |
+ EXPECT_TRUE(FindButtonWithTitle(kAllowButtonLabel)); |
+ EXPECT_TRUE(FindButtonWithTitle(kBlockButtonLabel)); |
+ EXPECT_FALSE(FindButtonWithTitle(kOKButtonLabel)); |
+ EXPECT_FALSE(FindButtonWithTitle(kCustomizeLabel)); |
+} |
+ |
+TEST_F(PermissionBubbleControllerTest, ShowMultiplePermissions) { |
+ MockPermissionBubbleDelegate delegate_a(kPermissionA); |
groby-ooo-7-16
2014/02/04 02:39:30
You _could_ factor out the MockDelegate stuff into
leng
2014/02/04 21:48:22
Another good suggestion. I did the same with acce
|
+ MockPermissionBubbleDelegate delegate_b(kPermissionB); |
+ MockPermissionBubbleDelegate delegate_c(kPermissionC); |
+ EXPECT_CALL(delegate_a, GetMessageTextFragment()).Times(1); |
+ EXPECT_CALL(delegate_b, GetMessageTextFragment()).Times(1); |
+ EXPECT_CALL(delegate_c, GetMessageTextFragment()).Times(1); |
+ |
+ std::vector<PermissionBubbleDelegate*> requests; |
+ requests.push_back(&delegate_a); |
+ requests.push_back(&delegate_b); |
+ requests.push_back(&delegate_c); |
+ std::vector<bool> acceptStates; |
+ |
+ [controller_ showAtAnchor:NSZeroPoint |
+ withDelegate:this |
+ forRequests:requests |
+ acceptStates:acceptStates |
+ customizationMode:NO]; |
+ |
+ EXPECT_TRUE(FindTextFieldWithString(kPermissionA)); |
+ EXPECT_TRUE(FindTextFieldWithString(kPermissionB)); |
+ EXPECT_TRUE(FindTextFieldWithString(kPermissionC)); |
+ |
+ EXPECT_TRUE(FindButtonWithTitle(kAllowButtonLabel)); |
+ EXPECT_TRUE(FindButtonWithTitle(kBlockButtonLabel)); |
+ EXPECT_TRUE(FindButtonWithTitle(kCustomizeLabel)); |
+ EXPECT_FALSE(FindButtonWithTitle(kOKButtonLabel)); |
+} |
+ |
+TEST_F(PermissionBubbleControllerTest, ShowCustomizationMode) { |
+ MockPermissionBubbleDelegate delegate_a(kPermissionA); |
+ MockPermissionBubbleDelegate delegate_b(kPermissionB); |
+ EXPECT_CALL(delegate_a, GetMessageTextFragment()).Times(1); |
+ EXPECT_CALL(delegate_b, GetMessageTextFragment()).Times(1); |
+ |
+ std::vector<PermissionBubbleDelegate*> requests; |
+ requests.push_back(&delegate_a); |
+ requests.push_back(&delegate_b); |
+ |
+ std::vector<bool> acceptStates; |
+ acceptStates.push_back(true); |
+ acceptStates.push_back(false); |
+ |
+ [controller_ showAtAnchor:NSZeroPoint |
+ withDelegate:this |
+ forRequests:requests |
+ acceptStates:acceptStates |
+ customizationMode:YES]; |
+ |
+ // Test that each checkbox is visible and only the first is checked. |
+ NSButton* checkbox_a = FindButtonWithTitle(kPermissionA); |
+ NSButton* checkbox_b = FindButtonWithTitle(kPermissionB); |
+ EXPECT_TRUE(checkbox_a); |
+ EXPECT_TRUE(checkbox_b); |
+ EXPECT_EQ(NSOnState, [checkbox_a state]); |
+ EXPECT_EQ(NSOffState, [checkbox_b state]); |
+ |
+ EXPECT_TRUE(FindButtonWithTitle(kOKButtonLabel)); |
+ EXPECT_FALSE(FindButtonWithTitle(kAllowButtonLabel)); |
+ EXPECT_FALSE(FindButtonWithTitle(kBlockButtonLabel)); |
+ EXPECT_FALSE(FindButtonWithTitle(kCustomizeLabel)); |
+} |
+ |
+TEST_F(PermissionBubbleControllerTest, OK) { |
+ std::vector<PermissionBubbleDelegate*> requests; |
+ std::vector<bool> acceptStates; |
+ [controller_ showAtAnchor:NSZeroPoint |
+ withDelegate:this |
+ forRequests:requests |
+ acceptStates:acceptStates |
+ customizationMode:NO]; |
+ |
+ EXPECT_CALL(*this, Closing()).Times(1); |
+ [FindButtonWithTitle(kOKButtonLabel) performClick:nil]; |
+} |
+ |
+TEST_F(PermissionBubbleControllerTest, Allow) { |
+ std::vector<PermissionBubbleDelegate*> requests; |
+ std::vector<bool> acceptStates; |
+ [controller_ showAtAnchor:NSZeroPoint |
+ withDelegate:this |
+ forRequests:requests |
+ acceptStates:acceptStates |
+ customizationMode:NO]; |
+ |
+ EXPECT_CALL(*this, Accept()).Times(1); |
+ [FindButtonWithTitle(kAllowButtonLabel) performClick:nil]; |
+} |
+ |
+TEST_F(PermissionBubbleControllerTest, Deny) { |
+ std::vector<PermissionBubbleDelegate*> requests; |
+ std::vector<bool> acceptStates; |
+ [controller_ showAtAnchor:NSZeroPoint |
+ withDelegate:this |
+ forRequests:requests |
+ acceptStates:acceptStates |
+ customizationMode:NO]; |
+ |
+ EXPECT_CALL(*this, Deny()).Times(1); |
+ [FindButtonWithTitle(kBlockButtonLabel) performClick:nil]; |
+} |
+ |
+TEST_F(PermissionBubbleControllerTest, ToggleCheckbox) { |
+ MockPermissionBubbleDelegate delegate_a(kPermissionA); |
+ MockPermissionBubbleDelegate delegate_b(kPermissionB); |
+ EXPECT_CALL(delegate_a, GetMessageTextFragment()).Times(1); |
+ EXPECT_CALL(delegate_b, GetMessageTextFragment()).Times(1); |
+ |
+ std::vector<PermissionBubbleDelegate*> requests; |
+ requests.push_back(&delegate_a); |
+ requests.push_back(&delegate_b); |
+ |
+ std::vector<bool> acceptStates; |
+ acceptStates.push_back(true); |
+ acceptStates.push_back(false); |
+ |
+ [controller_ showAtAnchor:NSZeroPoint |
+ withDelegate:this |
+ forRequests:requests |
+ acceptStates:acceptStates |
+ customizationMode:YES]; |
+ |
+ EXPECT_CALL(*this, ToggleAccept(0, false)).Times(1); |
+ EXPECT_CALL(*this, ToggleAccept(1, true)).Times(1); |
+ [FindButtonWithTitle(kPermissionA) performClick:nil]; |
+ [FindButtonWithTitle(kPermissionB) performClick:nil]; |
+} |
+ |
+TEST_F(PermissionBubbleControllerTest, ClickCustomize) { |
+ std::vector<PermissionBubbleDelegate*> requests; |
+ std::vector<bool> acceptStates; |
+ [controller_ showAtAnchor:NSZeroPoint |
+ withDelegate:this |
+ forRequests:requests |
+ acceptStates:acceptStates |
+ customizationMode:NO]; |
+ |
+ EXPECT_CALL(*this, SetCustomizationMode()).Times(1); |
+ [FindButtonWithTitle(kCustomizeLabel) performClick:nil]; |
+} |