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