OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include "app/l10n_util.h" |
| 6 #include "base/scoped_nsobject.h" |
| 7 #include "base/string_util.h" |
| 8 #include "base/string_number_conversions.h" |
| 9 #include "base/sys_string_conversions.h" |
| 10 #include "base/utf_string_conversions.h" |
| 11 #import "chrome/browser/cocoa/page_info_bubble_controller.h" |
| 12 #include "chrome/browser/cocoa/browser_test_helper.h" |
| 13 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 14 #include "chrome/browser/page_info_model.h" |
| 15 #include "grit/generated_resources.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 class FakeModel : public PageInfoModel { |
| 20 public: |
| 21 void AddSection(SectionInfoState state, |
| 22 const string16& title, |
| 23 const string16& description, |
| 24 SectionInfoType type) { |
| 25 sections_.push_back(SectionInfo( |
| 26 state, title, string16(), description, type)); |
| 27 } |
| 28 }; |
| 29 |
| 30 class FakeBridge : public PageInfoModel::PageInfoModelObserver { |
| 31 public: |
| 32 void ModelChanged() {} |
| 33 }; |
| 34 |
| 35 class PageInfoBubbleControllerTest : public CocoaTest { |
| 36 public: |
| 37 PageInfoBubbleControllerTest() { |
| 38 controller_ = nil; |
| 39 model_ = new FakeModel(); |
| 40 } |
| 41 |
| 42 virtual void TearDown() { |
| 43 [controller_ close]; |
| 44 CocoaTest::TearDown(); |
| 45 } |
| 46 |
| 47 void CreateBubble() { |
| 48 // The controller cleans up after itself when the window closes. |
| 49 controller_ = |
| 50 [[PageInfoBubbleController alloc] initWithPageInfoModel:model_ |
| 51 modelObserver:NULL |
| 52 parentWindow:test_window()]; |
| 53 window_ = [controller_ window]; |
| 54 [controller_ showWindow:nil]; |
| 55 } |
| 56 |
| 57 // Checks the controller's window for the requisite subviews in the given |
| 58 // numbers. |
| 59 void CheckWindow(int text_count, |
| 60 int image_count, |
| 61 int spacer_count, |
| 62 int button_count) { |
| 63 for (NSView* view in [[window_ contentView] subviews]) { |
| 64 if ([view isKindOfClass:[NSTextField class]]) { |
| 65 --text_count; |
| 66 } else if ([view isKindOfClass:[NSImageView class]]) { |
| 67 --image_count; |
| 68 } else if ([view isKindOfClass:[NSBox class]]) { |
| 69 --spacer_count; |
| 70 } else if ([view isKindOfClass:[NSButton class]]) { |
| 71 --button_count; |
| 72 CheckButton(static_cast<NSButton*>(view)); |
| 73 } else { |
| 74 ADD_FAILURE() << "Unknown subview: " << [[view description] UTF8String]; |
| 75 } |
| 76 } |
| 77 EXPECT_EQ(0, text_count); |
| 78 EXPECT_EQ(0, image_count); |
| 79 EXPECT_EQ(0, spacer_count); |
| 80 EXPECT_EQ(0, button_count); |
| 81 EXPECT_EQ([window_ delegate], controller_); |
| 82 } |
| 83 |
| 84 // Checks that a button is hooked up correctly. |
| 85 void CheckButton(NSButton* button) { |
| 86 EXPECT_EQ(@selector(showCertWindow:), [button action]); |
| 87 EXPECT_EQ(controller_, [button target]); |
| 88 EXPECT_TRUE([button stringValue]); |
| 89 } |
| 90 |
| 91 BrowserTestHelper helper_; |
| 92 |
| 93 PageInfoBubbleController* controller_; // Weak, owns self. |
| 94 FakeModel* model_; // Weak, owned by controller. |
| 95 NSWindow* window_; // Weak, owned by controller. |
| 96 }; |
| 97 |
| 98 |
| 99 TEST_F(PageInfoBubbleControllerTest, NoHistoryNoSecurity) { |
| 100 model_->AddSection(PageInfoModel::SECTION_STATE_ERROR, |
| 101 l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_IDENTITY_TITLE), |
| 102 l10n_util::GetStringFUTF16( |
| 103 IDS_PAGE_INFO_SECURITY_TAB_UNKNOWN_PARTY, |
| 104 ASCIIToUTF16("google.com")), |
| 105 PageInfoModel::SECTION_INFO_IDENTITY); |
| 106 model_->AddSection(PageInfoModel::SECTION_STATE_ERROR, |
| 107 l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_CONNECTION_TITLE), |
| 108 l10n_util::GetStringFUTF16( |
| 109 IDS_PAGE_INFO_SECURITY_TAB_NOT_ENCRYPTED_CONNECTION_TEXT, |
| 110 ASCIIToUTF16("google.com")), |
| 111 PageInfoModel::SECTION_INFO_CONNECTION); |
| 112 |
| 113 CreateBubble(); |
| 114 CheckWindow(/*text=*/4, /*image=*/2, /*spacer=*/1, /*button=*/1); |
| 115 } |
| 116 |
| 117 |
| 118 TEST_F(PageInfoBubbleControllerTest, HistoryNoSecurity) { |
| 119 model_->AddSection(PageInfoModel::SECTION_STATE_ERROR, |
| 120 l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_IDENTITY_TITLE), |
| 121 l10n_util::GetStringFUTF16( |
| 122 IDS_PAGE_INFO_SECURITY_TAB_UNKNOWN_PARTY, |
| 123 ASCIIToUTF16("google.com")), |
| 124 PageInfoModel::SECTION_INFO_IDENTITY); |
| 125 model_->AddSection(PageInfoModel::SECTION_STATE_ERROR, |
| 126 l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_CONNECTION_TITLE), |
| 127 l10n_util::GetStringFUTF16( |
| 128 IDS_PAGE_INFO_SECURITY_TAB_NOT_ENCRYPTED_CONNECTION_TEXT, |
| 129 ASCIIToUTF16("google.com")), |
| 130 PageInfoModel::SECTION_INFO_CONNECTION); |
| 131 |
| 132 // In practice, the history information comes later because it's queried |
| 133 // asynchronously, so replicate the double-build here. |
| 134 CreateBubble(); |
| 135 |
| 136 model_->AddSection(PageInfoModel::SECTION_STATE_ERROR, |
| 137 l10n_util::GetStringUTF16( |
| 138 IDS_PAGE_INFO_SECURITY_TAB_PERSONAL_HISTORY_TITLE), |
| 139 l10n_util::GetStringUTF16( |
| 140 IDS_PAGE_INFO_SECURITY_TAB_FIRST_VISITED_TODAY), |
| 141 PageInfoModel::SECTION_INFO_FIRST_VISIT); |
| 142 |
| 143 [controller_ performLayout]; |
| 144 |
| 145 CheckWindow(/*text=*/6, /*image=*/2, /*spacer=*/2, /*button=*/1); |
| 146 } |
| 147 |
| 148 |
| 149 TEST_F(PageInfoBubbleControllerTest, NoHistoryMixedSecurity) { |
| 150 model_->AddSection(PageInfoModel::SECTION_STATE_OK, |
| 151 l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_IDENTITY_TITLE), |
| 152 l10n_util::GetStringFUTF16( |
| 153 IDS_PAGE_INFO_SECURITY_TAB_SECURE_IDENTITY, |
| 154 ASCIIToUTF16("Goat Security Systems")), |
| 155 PageInfoModel::SECTION_INFO_IDENTITY); |
| 156 |
| 157 // This string is super long and the text should overflow the default clip |
| 158 // region (kImageSize). |
| 159 string16 description = l10n_util::GetStringFUTF16( |
| 160 IDS_PAGE_INFO_SECURITY_TAB_ENCRYPTED_SENTENCE_LINK, |
| 161 l10n_util::GetStringFUTF16( |
| 162 IDS_PAGE_INFO_SECURITY_TAB_ENCRYPTED_CONNECTION_TEXT, |
| 163 ASCIIToUTF16("chrome.google.com"), |
| 164 base::IntToString16(1024)), |
| 165 l10n_util::GetStringUTF16( |
| 166 IDS_PAGE_INFO_SECURITY_TAB_ENCRYPTED_INSECURE_CONTENT_WARNING)); |
| 167 |
| 168 model_->AddSection(PageInfoModel::SECTION_STATE_OK, |
| 169 l10n_util::GetStringUTF16(IDS_PAGE_INFO_SECURITY_TAB_CONNECTION_TITLE), |
| 170 description, |
| 171 PageInfoModel::SECTION_INFO_CONNECTION); |
| 172 |
| 173 CreateBubble(); |
| 174 |
| 175 NSArray* subviews = [[window_ contentView] subviews]; |
| 176 CheckWindow(/*text=*/4, /*image=*/2, /*spacer=*/1, /*button=*/1); |
| 177 |
| 178 // Look for the over-sized box. |
| 179 NSString* targetDesc = base::SysUTF16ToNSString(description); |
| 180 for (NSView* subview in subviews) { |
| 181 if ([subview isKindOfClass:[NSTextField class]]) { |
| 182 NSTextField* desc = static_cast<NSTextField*>(subview); |
| 183 if ([[desc stringValue] isEqualToString:targetDesc]) { |
| 184 // Typical box frame is ~55px, make sure this is extra large. |
| 185 EXPECT_LT(75, NSHeight([desc frame])); |
| 186 } |
| 187 } |
| 188 } |
| 189 } |
| 190 |
| 191 } // namespace |
OLD | NEW |