Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: chrome/browser/cocoa/translate/translate_infobar_unittest.mm

Issue 2815013: Refactor the translate infobars on mac to match the new windows code. (Closed)
Patch Set: Move unittest stuff back to class files Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 #import "chrome/browser/cocoa/translate_infobar.h"
7 6
8 #include "base/scoped_nsobject.h" 7 #import "base/scoped_nsobject.h"
9 #include "base/string_util.h" 8 #import "base/string_util.h"
10 #include "chrome/app/chrome_dll_resource.h" // For translate menu command ids. 9 #import "chrome/app/chrome_dll_resource.h" // For translate menu command ids.
10 #import "chrome/browser/cocoa/browser_test_helper.h"
11 #import "chrome/browser/cocoa/cocoa_test_helper.h" 11 #import "chrome/browser/cocoa/cocoa_test_helper.h"
12 #include "chrome/browser/translate/translate_infobars_delegates.h" 12 #import "chrome/browser/cocoa/infobar.h"
13 #include "testing/gmock/include/gmock/gmock.h" 13 #import "chrome/browser/cocoa/translate/translate_infobar_base.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #import "chrome/browser/cocoa/translate/before_translate_infobar_controller.h"
15 #include "testing/platform_test.h" 15 #import "chrome/browser/cocoa/translate/after_translate_infobar_controller.h"
16 #import "chrome/browser/cocoa/translate/translate_message_infobar_controller.h"
17 #import "chrome/browser/renderer_host/site_instance.h"
18 #import "chrome/browser/tab_contents/tab_contents.h"
19 #import "chrome/browser/translate/translate_infobar_delegate2.h"
20 #import "ipc/ipc_channel.h"
21 #import "testing/gmock/include/gmock/gmock.h"
22 #import "testing/gtest/include/gtest/gtest.h"
23 #import "testing/platform_test.h"
16 24
17 namespace { 25 namespace {
18 26
19 // All states the translate toolbar can assume. 27 // All states the translate toolbar can assume.
20 TranslateInfoBarDelegate::TranslateState kTranslateToolbarStates[] = { 28 TranslateInfoBarDelegate2::Type kTranslateToolbarStates[] = {
21 TranslateInfoBarDelegate::kBeforeTranslate, 29 TranslateInfoBarDelegate2::BEFORE_TRANSLATE,
22 TranslateInfoBarDelegate::kAfterTranslate, 30 TranslateInfoBarDelegate2::AFTER_TRANSLATE,
23 TranslateInfoBarDelegate::kTranslateError}; 31 TranslateInfoBarDelegate2::TRANSLATING,
32 TranslateInfoBarDelegate2::TRANSLATION_ERROR
33 };
24 34
25 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate { 35 class MockTranslateInfoBarDelegate : public TranslateInfoBarDelegate2 {
26 public: 36 public:
27 MockTranslateInfoBarDelegate() { 37 MockTranslateInfoBarDelegate(TranslateInfoBarDelegate2::Type type,
38 TranslateErrors::Type error,
39 TabContents* contents)
40 : TranslateInfoBarDelegate2(type, error, contents, "en", "es"){
28 // Start out in the "Before Translate" state. 41 // Start out in the "Before Translate" state.
29 UpdateState(kBeforeTranslate, TranslateErrors::NONE); 42 type_ = type;
43
30 } 44 }
31 45
32 virtual string16 GetDisplayNameForLocale(const std::string& language_code) { 46 virtual string16 GetDisplayNameForLocale(const std::string& language_code) {
33 return ASCIIToUTF16("Foo"); 47 return ASCIIToUTF16("Foo");
34 } 48 }
35 49
36 virtual bool IsLanguageBlacklisted() { 50 virtual bool IsLanguageBlacklisted() {
37 return false; 51 return false;
38 } 52 }
39 53
40 virtual bool IsSiteBlacklisted() { 54 virtual bool IsSiteBlacklisted() {
41 return false; 55 return false;
42 } 56 }
43 57
44 virtual bool ShouldAlwaysTranslate() { 58 virtual bool ShouldAlwaysTranslate() {
45 return false; 59 return false;
46 } 60 }
47 61
48 MOCK_METHOD0(Translate, void()); 62 MOCK_METHOD0(Translate, void());
49 MOCK_METHOD0(RevertTranslation, void()); 63 MOCK_METHOD0(RevertTranslation, void());
50 MOCK_METHOD0(TranslationDeclined, void()); 64 MOCK_METHOD0(TranslationDeclined, void());
51 MOCK_METHOD0(ToggleLanguageBlacklist, void()); 65 MOCK_METHOD0(ToggleLanguageBlacklist, void());
52 MOCK_METHOD0(ToggleSiteBlacklist, void()); 66 MOCK_METHOD0(ToggleSiteBlacklist, void());
53 MOCK_METHOD0(ToggleAlwaysTranslate, void()); 67 MOCK_METHOD0(ToggleAlwaysTranslate, void());
54
55 }; 68 };
56 69
57 class TranslationBarInfoTest : public CocoaTest { 70 class TranslationInfoBarTest : public CocoaTest {
58 public: 71 public:
59 scoped_ptr<MockTranslateInfoBarDelegate> infobar_delegate; 72 scoped_ptr<MockTranslateInfoBarDelegate> infobar_delegate;
60 scoped_nsobject<TranslateInfoBarController> infobar_controller; 73 scoped_nsobject<TranslateInfoBarControllerBase> infobar_controller;
74 BrowserTestHelper browser_helper_;
61 75
62 public: 76 public:
63 // Each test gets a single Mock translate delegate for the lifetime of 77 // Each test gets a single Mock translate delegate for the lifetime of
64 // the test. 78 // the test.
65 virtual void SetUp() { 79 virtual void SetUp() {
66 CocoaTest::SetUp(); 80 CocoaTest::SetUp();
67 infobar_delegate.reset(new MockTranslateInfoBarDelegate); 81 CreateInfoBar();
68 } 82 }
69 83
70 void CreateInfoBar() { 84 void CreateInfoBar() {
71 CreateInfoBar(TranslateInfoBarDelegate::kBeforeTranslate); 85 CreateInfoBar(TranslateInfoBarDelegate2::BEFORE_TRANSLATE);
72 } 86 }
73 87
74 void CreateInfoBar(TranslateInfoBarDelegate::TranslateState initial_state) { 88 void CreateInfoBar(TranslateInfoBarDelegate2::Type type) {
75 infobar_delegate->UpdateState(initial_state, TranslateErrors::NONE); 89 SiteInstance* instance =
90 SiteInstance::CreateSiteInstance(browser_helper_.profile());
91 scoped_ptr<TabContents> tab_contents(
92 new TabContents(browser_helper_.profile(),
93 instance,
94 MSG_ROUTING_NONE,
95 NULL));
96 TranslateErrors::Type error = TranslateErrors::NONE;
97 if (type == TranslateInfoBarDelegate2::TRANSLATION_ERROR)
98 error = TranslateErrors::NETWORK;
99 infobar_delegate.reset(
100 new MockTranslateInfoBarDelegate(type, error, tab_contents.get()));
76 [[infobar_controller view] removeFromSuperview]; 101 [[infobar_controller view] removeFromSuperview];
102 scoped_ptr<InfoBar> infobar(infobar_delegate->CreateInfoBar());
77 infobar_controller.reset( 103 infobar_controller.reset(
78 [[TranslateInfoBarController alloc] 104 reinterpret_cast<TranslateInfoBarControllerBase*>(
79 initWithDelegate:infobar_delegate.get()]); 105 infobar->controller()));
80 // Need to call this to get the view to load from nib. 106 // Need to call this to get the view to load from nib.
81 [[test_window() contentView] addSubview:[infobar_controller view]]; 107 [[test_window() contentView] addSubview:[infobar_controller view]];
82 } 108 }
83 }; 109 };
84 110
85 // Check that we can instantiate a Translate Infobar correctly. 111 // Check that we can instantiate a Translate Infobar correctly.
86 TEST_F(TranslationBarInfoTest, Instantiate) { 112 TEST_F(TranslationInfoBarTest, Instantiate) {
87 CreateInfoBar(); 113 CreateInfoBar();
88 ASSERT_TRUE(infobar_controller.get()); 114 ASSERT_TRUE(infobar_controller.get());
89 } 115 }
90 116
91 // Check that clicking the Translate button calls Translate(). 117 // Check that clicking the Translate button calls Translate().
92 TEST_F(TranslationBarInfoTest, TranslateCalledOnButtonPress) { 118 TEST_F(TranslationInfoBarTest, TranslateCalledOnButtonPress) {
93 CreateInfoBar(); 119 CreateInfoBar();
94 120
95 EXPECT_CALL(*infobar_delegate, Translate()).Times(1); 121 EXPECT_CALL(*infobar_delegate, Translate()).Times(1);
96 [infobar_controller ok:nil]; 122 [infobar_controller ok:nil];
97 } 123 }
98 124
99 // Check that clicking the "Retry" button calls Translate() when we're 125 // Check that clicking the "Retry" button calls Translate() when we're
100 // in the error mode - http://crbug.com/41315 . 126 // in the error mode - http://crbug.com/41315 .
101 TEST_F(TranslationBarInfoTest, TranslateCalledInErrorMode) { 127 TEST_F(TranslationInfoBarTest, TranslateCalledInErrorMode) {
102 CreateInfoBar(); 128 CreateInfoBar(TranslateInfoBarDelegate2::TRANSLATION_ERROR);
103 129
104 EXPECT_CALL(*infobar_delegate, Translate()).Times(1); 130 EXPECT_CALL(*infobar_delegate, Translate()).Times(1);
105 131
106 infobar_delegate->UpdateState(TranslateInfoBarDelegate::kTranslateError,
107 TranslateErrors::NONE);
108 [infobar_controller ok:nil]; 132 [infobar_controller ok:nil];
109 } 133 }
110 134
111 // Check that clicking the "Show Original button calls RevertTranslation(). 135 // Check that clicking the "Show Original button calls RevertTranslation().
112 TEST_F(TranslationBarInfoTest, RevertCalledOnButtonPress) { 136 TEST_F(TranslationInfoBarTest, RevertCalledOnButtonPress) {
113 CreateInfoBar(); 137 CreateInfoBar();
114 138
115 EXPECT_CALL(*infobar_delegate, RevertTranslation()).Times(1); 139 EXPECT_CALL(*infobar_delegate, RevertTranslation()).Times(1);
116 [infobar_controller showOriginal:nil]; 140 [infobar_controller showOriginal:nil];
117 } 141 }
118 142
119 // Check that UI is layed out correctly as we transition synchronously through 143 // Check that items in the options menu are hooked up correctly.
120 // toolbar states. 144 TEST_F(TranslationInfoBarTest, OptionsMenuItemsHookedUp) {
121 TEST_F(TranslationBarInfoTest, StateTransitions) {
122 EXPECT_CALL(*infobar_delegate, Translate()) 145 EXPECT_CALL(*infobar_delegate, Translate())
123 .Times(0); 146 .Times(0);
124 CreateInfoBar();
125
126 for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) {
127 TranslateInfoBarDelegate::TranslateState state = kTranslateToolbarStates[i];
128 TranslateErrors::Type error = TranslateErrors::NONE;
129
130 infobar_delegate->UpdateState(state, error);
131
132 // Pending Translation == false.
133 if (i != 0) {
134 // First time around, the toolbar should already be layed out.
135 [infobar_controller updateState:state
136 translationPending:false
137 error:error];
138 }
139
140 bool result =
141 [infobar_controller verifyLayout:state
142 translationPending:false];
143 EXPECT_TRUE(result) << "Layout wrong, for state " << state <<
144 "translatePending=false";
145
146 // Pending Translation == true.
147 [infobar_controller updateState:state
148 translationPending:true
149 error:error];
150
151 result = [infobar_controller verifyLayout:state translationPending:true];
152 EXPECT_TRUE(result) << "Layout wrong, for state " << state <<
153 "translatePending=true";
154
155 }
156 }
157
158 // Check that items in the options menu are hooked up correctly.
159 TEST_F(TranslationBarInfoTest, OptionsMenuItemsHookedUp) {
160 EXPECT_CALL(*infobar_delegate, Translate())
161 .Times(0);
162 CreateInfoBar();
163 147
164 [infobar_controller rebuildOptionsMenu]; 148 [infobar_controller rebuildOptionsMenu];
165 NSMenu* optionsMenu = [infobar_controller optionsMenu]; 149 NSMenu* optionsMenu = [infobar_controller optionsMenu];
166 NSArray* optionsMenuItems = [optionsMenu itemArray]; 150 NSArray* optionsMenuItems = [optionsMenu itemArray];
167 151
168 EXPECT_EQ([optionsMenuItems count], 5U); 152 EXPECT_EQ([optionsMenuItems count], 5U);
169 153
170 // First item is the options menu button's title, so there's no need to test 154 // First item is the options menu button's title, so there's no need to test
171 // that the target on that is setup correctly. 155 // that the target on that is setup correctly.
172 for (NSUInteger i = 1; i < [optionsMenuItems count]; ++i) { 156 for (NSUInteger i = 1; i < [optionsMenuItems count]; ++i) {
173 NSMenuItem* item = [optionsMenuItems objectAtIndex:i]; 157 NSMenuItem* item = [optionsMenuItems objectAtIndex:i];
174 EXPECT_EQ([item target], infobar_controller.get()); 158 EXPECT_EQ([item target], infobar_controller.get());
175 } 159 }
176 NSMenuItem* alwaysTranslateLanguateItem = [optionsMenuItems objectAtIndex:1]; 160 NSMenuItem* alwaysTranslateLanguateItem = [optionsMenuItems objectAtIndex:1];
177 NSMenuItem* neverTranslateLanguateItem = [optionsMenuItems objectAtIndex:2]; 161 NSMenuItem* neverTranslateLanguateItem = [optionsMenuItems objectAtIndex:2];
178 NSMenuItem* neverTranslateSiteItem = [optionsMenuItems objectAtIndex:3]; 162 NSMenuItem* neverTranslateSiteItem = [optionsMenuItems objectAtIndex:3];
179 NSMenuItem* aboutTranslateItem = [optionsMenuItems objectAtIndex:4]; 163 NSMenuItem* aboutTranslateItem = [optionsMenuItems objectAtIndex:4];
180 164
181 { 165 {
182 EXPECT_CALL(*infobar_delegate, ToggleAlwaysTranslate()) 166 EXPECT_CALL(*infobar_delegate, ToggleAlwaysTranslate())
183 .Times(1); 167 .Times(1);
184 [infobar_controller menuItemSelected:alwaysTranslateLanguateItem]; 168 [infobar_controller optionsMenuChanged:alwaysTranslateLanguateItem];
185 } 169 }
186 170
187 { 171 {
188 EXPECT_CALL(*infobar_delegate, ToggleLanguageBlacklist()) 172 EXPECT_CALL(*infobar_delegate, ToggleLanguageBlacklist())
189 .Times(1); 173 .Times(1);
190 [infobar_controller menuItemSelected:neverTranslateLanguateItem]; 174 [infobar_controller optionsMenuChanged:neverTranslateLanguateItem];
191 } 175 }
192 176
193 { 177 {
194 EXPECT_CALL(*infobar_delegate, ToggleSiteBlacklist()) 178 EXPECT_CALL(*infobar_delegate, ToggleSiteBlacklist())
195 .Times(1); 179 .Times(1);
196 [infobar_controller menuItemSelected:neverTranslateSiteItem]; 180 [infobar_controller optionsMenuChanged:neverTranslateSiteItem];
197 } 181 }
198 182
199 { 183 {
200 // Can't mock this effectively, so just check that the tag is set correctly. 184 // Can't mock this effectively, so just check that the tag is set correctly.
201 EXPECT_EQ([aboutTranslateItem tag], IDC_TRANSLATE_OPTIONS_ABOUT); 185 EXPECT_EQ([aboutTranslateItem tag], IDC_TRANSLATE_OPTIONS_ABOUT);
202 } 186 }
203 } 187 }
204 188
205 // Check that selecting a new item from the "Source Language" popup in "before 189 // Check that selecting a new item from the "Source Language" popup in "before
206 // translate" mode doesn't trigger a translation or change state. 190 // translate" mode doesn't trigger a translation or change state.
207 // http://crbug.com/36666 191 // http://crbug.com/36666
208 TEST_F(TranslationBarInfoTest, Bug36666) { 192 TEST_F(TranslationInfoBarTest, Bug36666) {
209 EXPECT_CALL(*infobar_delegate, Translate()) 193 EXPECT_CALL(*infobar_delegate, Translate())
210 .Times(0); 194 .Times(0);
211 195
212 CreateInfoBar(); 196 CreateInfoBar();
213 int arbitrary_index = 2; 197 int arbitrary_index = 2;
214 [infobar_controller sourceLanguageModified:arbitrary_index]; 198 [infobar_controller sourceLanguageModified:arbitrary_index];
215 EXPECT_EQ(infobar_delegate->state(), 199 EXPECT_CALL(*infobar_delegate, Translate())
216 TranslateInfoBarDelegate::kBeforeTranslate); 200 .Times(0);
217 EXPECT_EQ([infobar_controller state],
218 TranslateInfoBarDelegate::kBeforeTranslate);
219 } 201 }
220 202
221 // Check that the infobar lays itself out correctly when instantiated in 203 // Check that the infobar lays itself out correctly when instantiated in
222 // each of the states. 204 // each of the states.
223 // http://crbug.com/36895 205 // http://crbug.com/36895
224 TEST_F(TranslationBarInfoTest, Bug36895) { 206 TEST_F(TranslationInfoBarTest, Bug36895) {
225 EXPECT_CALL(*infobar_delegate, Translate()) 207 EXPECT_CALL(*infobar_delegate, Translate())
226 .Times(0); 208 .Times(0);
227 209
228 for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) { 210 for (size_t i = 0; i < arraysize(kTranslateToolbarStates); ++i) {
229 CreateInfoBar(kTranslateToolbarStates[i]); 211 CreateInfoBar(kTranslateToolbarStates[i]);
230 EXPECT_TRUE( 212 EXPECT_TRUE(
231 [infobar_controller verifyLayout:kTranslateToolbarStates[i] 213 [infobar_controller verifyLayout]) << "Layout wrong, for state #" << i;
232 translationPending:false]) <<
233 "Layout wrong, for state #" << i;
234 } 214 }
235 } 215 }
236 216
237 } // namespace 217 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698