| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/json/json_reader.h" |
| 5 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/values.h" |
| 6 #include "chrome/browser/ui/autofill/save_card_bubble_controller.h" | 8 #include "chrome/browser/ui/autofill/save_card_bubble_controller.h" |
| 7 #import "chrome/browser/ui/cocoa/autofill/save_card_bubble_view_bridge.h" | 9 #import "chrome/browser/ui/cocoa/autofill/save_card_bubble_view_bridge.h" |
| 8 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 10 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 9 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" | 11 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
| 10 #include "components/autofill/core/browser/credit_card.h" | 12 #include "components/autofill/core/browser/credit_card.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #import "ui/events/test/cocoa_test_event_utils.h" | 14 #import "ui/events/test/cocoa_test_event_utils.h" |
| 13 | 15 |
| 14 #include <Carbon/Carbon.h> // For the kVK_* constants. | 16 #include <Carbon/Carbon.h> // For the kVK_* constants. |
| 15 | 17 |
| 16 namespace autofill { | 18 namespace autofill { |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 class TestSaveCardBubbleController : public SaveCardBubbleController { | 22 class TestSaveCardBubbleController : public SaveCardBubbleController { |
| 21 public: | 23 public: |
| 22 TestSaveCardBubbleController() { | 24 TestSaveCardBubbleController() { |
| 23 lines_.reset(new LegalMessageLines()); | 25 ParseLegalMessageJson(); |
| 24 | 26 |
| 25 on_save_button_was_called_ = false; | 27 on_save_button_was_called_ = false; |
| 26 on_cancel_button_was_called_ = false; | 28 on_cancel_button_was_called_ = false; |
| 27 on_learn_more_was_called_ = false; | 29 on_learn_more_was_called_ = false; |
| 30 on_legal_message_was_called_ = false; |
| 28 on_bubble_closed_was_called_ = false; | 31 on_bubble_closed_was_called_ = false; |
| 29 } | 32 } |
| 30 | 33 |
| 31 // SaveCardBubbleController: | 34 // SaveCardBubbleController: |
| 32 base::string16 GetWindowTitle() const override { return base::string16(); } | 35 base::string16 GetWindowTitle() const override { return base::string16(); } |
| 33 | 36 |
| 34 base::string16 GetExplanatoryMessage() const override { | 37 base::string16 GetExplanatoryMessage() const override { |
| 35 return base::string16(); | 38 return base::string16(); |
| 36 } | 39 } |
| 37 | 40 |
| 38 const CreditCard GetCard() const override { | 41 const CreditCard GetCard() const override { |
| 39 return CreditCard(); | 42 return CreditCard(); |
| 40 } | 43 } |
| 41 | 44 |
| 42 void OnSaveButton() override { on_save_button_was_called_ = true; } | 45 void OnSaveButton() override { on_save_button_was_called_ = true; } |
| 43 void OnCancelButton() override { on_cancel_button_was_called_ = true; } | 46 void OnCancelButton() override { on_cancel_button_was_called_ = true; } |
| 44 void OnLearnMoreClicked() override { on_learn_more_was_called_ = true; } | 47 void OnLearnMoreClicked() override { on_learn_more_was_called_ = true; } |
| 45 void OnLegalMessageLinkClicked(const GURL& url) override {} | 48 void OnLegalMessageLinkClicked(const GURL& url) override { |
| 49 on_legal_message_was_called_ = true; |
| 50 legal_message_url_ = url.spec(); |
| 51 } |
| 46 void OnBubbleClosed() override { on_bubble_closed_was_called_ = true; } | 52 void OnBubbleClosed() override { on_bubble_closed_was_called_ = true; } |
| 47 | 53 |
| 48 const LegalMessageLines& GetLegalMessageLines() const override { | 54 const LegalMessageLines& GetLegalMessageLines() const override { |
| 49 return *lines_; | 55 return lines_; |
| 50 } | 56 } |
| 51 | 57 |
| 52 // Testing state. | 58 // Testing state. |
| 53 bool on_save_button_was_called() { return on_save_button_was_called_; } | 59 bool on_save_button_was_called() { return on_save_button_was_called_; } |
| 54 bool on_cancel_button_was_called() { return on_cancel_button_was_called_; } | 60 bool on_cancel_button_was_called() { return on_cancel_button_was_called_; } |
| 55 bool on_learn_more_was_called() { return on_learn_more_was_called_; } | 61 bool on_learn_more_was_called() { return on_learn_more_was_called_; } |
| 62 bool on_legal_message_was_called() { return on_legal_message_was_called_; } |
| 63 std::string legal_message_url() { return legal_message_url_; } |
| 56 bool on_bubble_closed_was_called() { return on_bubble_closed_was_called_; } | 64 bool on_bubble_closed_was_called() { return on_bubble_closed_was_called_; } |
| 57 | 65 |
| 58 private: | 66 private: |
| 59 scoped_ptr<LegalMessageLines> lines_; | 67 void ParseLegalMessageJson() { |
| 68 std::string message_json = |
| 69 "{" |
| 70 " \"line\" : [" |
| 71 " {" |
| 72 " \"template\": \"Please check out our {0}.\"," |
| 73 " \"template_parameter\": [" |
| 74 " {" |
| 75 " \"display_text\": \"terms of service\"," |
| 76 " \"url\": \"http://help.example.com/legal_message\"" |
| 77 " }" |
| 78 " ]" |
| 79 " }," |
| 80 " {" |
| 81 " \"template\": \"We also have a {0} and {1}.\"," |
| 82 " \"template_parameter\": [" |
| 83 " {" |
| 84 " \"display_text\": \"mission statement\"," |
| 85 " \"url\": \"http://www.example.com/our_mission\"" |
| 86 " }," |
| 87 " {" |
| 88 " \"display_text\": \"privacy policy\"," |
| 89 " \"url\": \"http://help.example.com/privacy_policy\"" |
| 90 " }" |
| 91 " ]" |
| 92 " }" |
| 93 " ]" |
| 94 "}"; |
| 95 scoped_ptr<base::Value> value(base::JSONReader::Read(message_json)); |
| 96 ASSERT_TRUE(value); |
| 97 base::DictionaryValue* dictionary = nullptr; |
| 98 ASSERT_TRUE(value->GetAsDictionary(&dictionary)); |
| 99 LegalMessageLine::Parse(*dictionary, &lines_); |
| 100 } |
| 101 |
| 102 LegalMessageLines lines_; |
| 60 | 103 |
| 61 bool on_save_button_was_called_; | 104 bool on_save_button_was_called_; |
| 62 bool on_cancel_button_was_called_; | 105 bool on_cancel_button_was_called_; |
| 63 bool on_learn_more_was_called_; | 106 bool on_learn_more_was_called_; |
| 107 bool on_legal_message_was_called_; |
| 108 std::string legal_message_url_; |
| 64 bool on_bubble_closed_was_called_; | 109 bool on_bubble_closed_was_called_; |
| 65 }; | 110 }; |
| 66 | 111 |
| 67 class SaveCardBubbleViewTest : public CocoaProfileTest { | 112 class SaveCardBubbleViewTest : public CocoaProfileTest { |
| 68 public: | 113 public: |
| 69 void SetUp() override { | 114 void SetUp() override { |
| 70 CocoaProfileTest::SetUp(); | 115 CocoaProfileTest::SetUp(); |
| 71 ASSERT_TRUE(browser()); | 116 ASSERT_TRUE(browser()); |
| 72 | 117 |
| 73 browser_window_controller_ = | 118 browser_window_controller_ = |
| (...skipping 19 matching lines...) Expand all Loading... |
| 93 }; | 138 }; |
| 94 | 139 |
| 95 } // namespace | 140 } // namespace |
| 96 | 141 |
| 97 TEST_F(SaveCardBubbleViewTest, SaveShouldClose) { | 142 TEST_F(SaveCardBubbleViewTest, SaveShouldClose) { |
| 98 [bridge_->view_controller_ onSaveButton:nil]; | 143 [bridge_->view_controller_ onSaveButton:nil]; |
| 99 | 144 |
| 100 EXPECT_TRUE(bubble_controller_->on_save_button_was_called()); | 145 EXPECT_TRUE(bubble_controller_->on_save_button_was_called()); |
| 101 EXPECT_FALSE(bubble_controller_->on_cancel_button_was_called()); | 146 EXPECT_FALSE(bubble_controller_->on_cancel_button_was_called()); |
| 102 EXPECT_FALSE(bubble_controller_->on_learn_more_was_called()); | 147 EXPECT_FALSE(bubble_controller_->on_learn_more_was_called()); |
| 148 EXPECT_FALSE(bubble_controller_->on_legal_message_was_called()); |
| 103 | 149 |
| 104 EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called()); | 150 EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called()); |
| 105 } | 151 } |
| 106 | 152 |
| 107 TEST_F(SaveCardBubbleViewTest, CancelShouldClose) { | 153 TEST_F(SaveCardBubbleViewTest, CancelShouldClose) { |
| 108 [bridge_->view_controller_ onCancelButton:nil]; | 154 [bridge_->view_controller_ onCancelButton:nil]; |
| 109 | 155 |
| 110 EXPECT_FALSE(bubble_controller_->on_save_button_was_called()); | 156 EXPECT_FALSE(bubble_controller_->on_save_button_was_called()); |
| 111 EXPECT_TRUE(bubble_controller_->on_cancel_button_was_called()); | 157 EXPECT_TRUE(bubble_controller_->on_cancel_button_was_called()); |
| 112 EXPECT_FALSE(bubble_controller_->on_learn_more_was_called()); | 158 EXPECT_FALSE(bubble_controller_->on_learn_more_was_called()); |
| 159 EXPECT_FALSE(bubble_controller_->on_legal_message_was_called()); |
| 113 | 160 |
| 114 EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called()); | 161 EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called()); |
| 115 } | 162 } |
| 116 | 163 |
| 117 TEST_F(SaveCardBubbleViewTest, LearnMoreShouldNotClose) { | 164 TEST_F(SaveCardBubbleViewTest, LearnMoreShouldNotClose) { |
| 118 NSTextView* textView = nil; | 165 NSTextView* textView = nil; |
| 119 NSObject* link = nil; | 166 NSObject* link = nil; |
| 120 [bridge_->view_controller_ textView:textView clickedOnLink:link atIndex:0]; | 167 [bridge_->view_controller_ textView:textView clickedOnLink:link atIndex:0]; |
| 121 | 168 |
| 122 EXPECT_FALSE(bubble_controller_->on_save_button_was_called()); | 169 EXPECT_FALSE(bubble_controller_->on_save_button_was_called()); |
| 123 EXPECT_FALSE(bubble_controller_->on_cancel_button_was_called()); | 170 EXPECT_FALSE(bubble_controller_->on_cancel_button_was_called()); |
| 124 EXPECT_TRUE(bubble_controller_->on_learn_more_was_called()); | 171 EXPECT_TRUE(bubble_controller_->on_learn_more_was_called()); |
| 172 EXPECT_FALSE(bubble_controller_->on_legal_message_was_called()); |
| 125 | 173 |
| 126 EXPECT_FALSE(bubble_controller_->on_bubble_closed_was_called()); | 174 EXPECT_FALSE(bubble_controller_->on_bubble_closed_was_called()); |
| 127 } | 175 } |
| 176 |
| 177 TEST_F(SaveCardBubbleViewTest, LegalMessageShouldNotClose) { |
| 178 NSString* legalText = @"We also have a mission statement and privacy policy."; |
| 179 base::scoped_nsobject<NSTextView> textView( |
| 180 [[NSTextView alloc] initWithFrame:NSZeroRect]); |
| 181 base::scoped_nsobject<NSAttributedString> attributedMessage( |
| 182 [[NSAttributedString alloc] initWithString:legalText attributes:@{}]); |
| 183 [[textView textStorage] setAttributedString:attributedMessage]; |
| 184 |
| 185 NSObject* link = nil; |
| 186 [bridge_->view_controller_ textView:textView clickedOnLink:link atIndex:40]; |
| 187 |
| 188 EXPECT_FALSE(bubble_controller_->on_save_button_was_called()); |
| 189 EXPECT_FALSE(bubble_controller_->on_cancel_button_was_called()); |
| 190 EXPECT_FALSE(bubble_controller_->on_learn_more_was_called()); |
| 191 EXPECT_TRUE(bubble_controller_->on_legal_message_was_called()); |
| 192 |
| 193 std::string url("http://help.example.com/privacy_policy"); |
| 194 EXPECT_EQ(url, bubble_controller_->legal_message_url()); |
| 195 |
| 196 EXPECT_FALSE(bubble_controller_->on_bubble_closed_was_called()); |
| 197 } |
| 128 | 198 |
| 129 TEST_F(SaveCardBubbleViewTest, ReturnInvokesDefaultAction) { | 199 TEST_F(SaveCardBubbleViewTest, ReturnInvokesDefaultAction) { |
| 130 [[bridge_->view_controller_ window] | 200 [[bridge_->view_controller_ window] |
| 131 performKeyEquivalent:cocoa_test_event_utils::KeyEventWithKeyCode( | 201 performKeyEquivalent:cocoa_test_event_utils::KeyEventWithKeyCode( |
| 132 kVK_Return, '\r', NSKeyDown, 0)]; | 202 kVK_Return, '\r', NSKeyDown, 0)]; |
| 133 | 203 |
| 134 EXPECT_TRUE(bubble_controller_->on_save_button_was_called()); | 204 EXPECT_TRUE(bubble_controller_->on_save_button_was_called()); |
| 135 EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called()); | 205 EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called()); |
| 136 } | 206 } |
| 137 | 207 |
| 138 TEST_F(SaveCardBubbleViewTest, EscapeCloses) { | 208 TEST_F(SaveCardBubbleViewTest, EscapeCloses) { |
| 139 [[bridge_->view_controller_ window] | 209 [[bridge_->view_controller_ window] |
| 140 performKeyEquivalent:cocoa_test_event_utils::KeyEventWithKeyCode( | 210 performKeyEquivalent:cocoa_test_event_utils::KeyEventWithKeyCode( |
| 141 kVK_Escape, '\e', NSKeyDown, 0)]; | 211 kVK_Escape, '\e', NSKeyDown, 0)]; |
| 142 | 212 |
| 143 EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called()); | 213 EXPECT_TRUE(bubble_controller_->on_bubble_closed_was_called()); |
| 144 } | 214 } |
| 145 | 215 |
| 146 } // namespace autofill | 216 } // namespace autofill |
| OLD | NEW |