Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 "base/compiler_specific.h" | |
| 6 #include "base/memory/scoped_nsobject.h" | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/string16.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
| 11 #import "chrome/browser/ui/cocoa/confirm_bubble_controller.h" | |
| 12 #import "chrome/browser/ui/cocoa/confirm_bubble_view.h" | |
| 13 #include "chrome/browser/ui/confirm_bubble_model.h" | |
| 14 #include "grit/theme_resources.h" | |
| 15 #import "testing/gtest_mac.h" | |
| 16 #include "ui/base/resource/resource_bundle.h" | |
| 17 #import "ui/gfx/point.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // The model object used in this test. This model implements all methods and | |
| 22 // updates its status when ConfirmBubbleController calls its methods. | |
| 23 class TestConfirmBubbleModel : public ConfirmBubbleModel { | |
| 24 public: | |
| 25 TestConfirmBubbleModel(); | |
| 26 virtual ~TestConfirmBubbleModel() OVERRIDE; | |
| 27 virtual string16 GetTitle() const OVERRIDE; | |
| 28 virtual string16 GetMessageText() const OVERRIDE; | |
| 29 virtual gfx::Image* GetIcon() const OVERRIDE; | |
| 30 virtual int GetButtons() const OVERRIDE; | |
| 31 virtual string16 GetButtonLabel(BubbleButton button) const OVERRIDE; | |
| 32 virtual void Accept() OVERRIDE; | |
| 33 virtual void Cancel() OVERRIDE; | |
| 34 virtual string16 GetLinkText() const OVERRIDE; | |
| 35 virtual void LinkClicked() OVERRIDE; | |
| 36 | |
| 37 bool accept_clicked() const { return accept_clicked_; } | |
| 38 bool cancel_clicked() const { return cancel_clicked_; } | |
| 39 bool link_clicked() const { return link_clicked_; } | |
| 40 | |
| 41 private: | |
| 42 bool accept_clicked_; | |
| 43 bool cancel_clicked_; | |
| 44 bool link_clicked_; | |
| 45 }; | |
| 46 | |
| 47 TestConfirmBubbleModel::TestConfirmBubbleModel() : | |
| 48 accept_clicked_(false), | |
| 49 cancel_clicked_(false), | |
| 50 link_clicked_(false) { | |
| 51 } | |
| 52 | |
| 53 TestConfirmBubbleModel::~TestConfirmBubbleModel() { | |
| 54 } | |
| 55 | |
| 56 string16 TestConfirmBubbleModel::GetTitle() const { | |
| 57 return ASCIIToUTF16("Test"); | |
| 58 } | |
| 59 | |
| 60 string16 TestConfirmBubbleModel::GetMessageText() const { | |
| 61 return ASCIIToUTF16("Test Message"); | |
| 62 } | |
| 63 | |
| 64 gfx::Image* TestConfirmBubbleModel::GetIcon() const { | |
| 65 return &ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 66 IDR_PRODUCT_LOGO_16); | |
| 67 } | |
| 68 | |
| 69 int TestConfirmBubbleModel::GetButtons() const { | |
| 70 return BUTTON_OK | BUTTON_CANCEL; | |
| 71 } | |
| 72 | |
| 73 string16 TestConfirmBubbleModel::GetButtonLabel(BubbleButton button) const { | |
| 74 return button == BUTTON_OK ? ASCIIToUTF16("OK") : ASCIIToUTF16("Cancel"); | |
| 75 } | |
| 76 | |
| 77 void TestConfirmBubbleModel::Accept() { | |
| 78 accept_clicked_ = true; | |
| 79 } | |
| 80 | |
| 81 void TestConfirmBubbleModel::Cancel() { | |
| 82 cancel_clicked_ = true; | |
| 83 } | |
| 84 | |
| 85 string16 TestConfirmBubbleModel::GetLinkText() const { | |
| 86 return ASCIIToUTF16("Link"); | |
| 87 } | |
| 88 | |
| 89 void TestConfirmBubbleModel::LinkClicked() { | |
| 90 link_clicked_ = true; | |
| 91 } | |
| 92 | |
| 93 } // namespace | |
| 94 | |
| 95 class ConfirmBubbleControllerTest : public CocoaTest { | |
| 96 public: | |
| 97 ConfirmBubbleControllerTest() { | |
| 98 NSView* view = [test_window() contentView]; | |
| 99 model_.reset(new TestConfirmBubbleModel); | |
| 100 gfx::Point origin(0, 0); | |
| 101 controller_ = | |
| 102 [[ConfirmBubbleController alloc] initWithParent:view | |
| 103 origin:origin.ToCGPoint() | |
| 104 model:model_.get()]; | |
| 105 [view addSubview:[controller_ view] | |
| 106 positioned:NSWindowAbove | |
| 107 relativeTo:nil]; | |
| 108 } | |
| 109 | |
| 110 virtual ~ConfirmBubbleControllerTest() { | |
|
Robert Sesek
2011/12/12 15:42:43
No need to provide an empty dtor
Hironori Bono
2011/12/15 13:06:14
Done. Thank you for your comment.
| |
| 111 } | |
| 112 | |
| 113 ConfirmBubbleView* GetBubbleView() const { | |
| 114 return (ConfirmBubbleView*)[controller_ view]; | |
| 115 } | |
| 116 | |
| 117 TestConfirmBubbleModel* model() const { | |
| 118 return model_.get(); | |
| 119 } | |
| 120 | |
| 121 private: | |
| 122 ConfirmBubbleController* controller_; | |
|
Robert Sesek
2011/12/12 15:42:43
// weak; owns self
Hironori Bono
2011/12/15 13:06:14
Done. Thank you for your comment.
| |
| 123 scoped_ptr<TestConfirmBubbleModel> model_; | |
| 124 }; | |
| 125 | |
| 126 // Verify clicking a button or a link removes the ConfirmBubbleView object and | |
| 127 // calls an appropriate model method. | |
| 128 TEST_F(ConfirmBubbleControllerTest, ClickOk) { | |
| 129 NSView* view = [test_window() contentView]; | |
| 130 ConfirmBubbleView* bubble_view = GetBubbleView(); | |
| 131 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; | |
| 132 EXPECT_TRUE(contains_bubble_view); | |
| 133 | |
| 134 // Click its OK button and verify this view has been removed from the test | |
| 135 // window. Also verify TestConfirmBubbleModel::Accept() has been called. | |
| 136 [bubble_view clickOk]; | |
| 137 | |
| 138 contains_bubble_view = [[view subviews] containsObject:bubble_view]; | |
| 139 EXPECT_FALSE(contains_bubble_view); | |
| 140 EXPECT_TRUE(model()->accept_clicked()); | |
| 141 EXPECT_FALSE(model()->cancel_clicked()); | |
| 142 EXPECT_FALSE(model()->link_clicked()); | |
| 143 } | |
| 144 | |
| 145 TEST_F(ConfirmBubbleControllerTest, ClickCancel) { | |
| 146 NSView* view = [test_window() contentView]; | |
| 147 ConfirmBubbleView* bubble_view = GetBubbleView(); | |
| 148 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; | |
| 149 EXPECT_TRUE(contains_bubble_view); | |
| 150 | |
| 151 // Click its cancel button and verify this view has been removed from the test | |
| 152 // window. Also verify TestConfirmBubbleModel::Cancel() has been called. | |
| 153 [bubble_view clickCancel]; | |
| 154 | |
| 155 contains_bubble_view = [[view subviews] containsObject:bubble_view]; | |
| 156 EXPECT_FALSE(contains_bubble_view); | |
| 157 EXPECT_FALSE(model()->accept_clicked()); | |
| 158 EXPECT_TRUE(model()->cancel_clicked()); | |
| 159 EXPECT_FALSE(model()->link_clicked()); | |
| 160 } | |
| 161 | |
| 162 TEST_F(ConfirmBubbleControllerTest, ClickLink) { | |
| 163 NSView* view = [test_window() contentView]; | |
| 164 ConfirmBubbleView* bubble_view = GetBubbleView(); | |
| 165 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; | |
| 166 EXPECT_TRUE(contains_bubble_view); | |
| 167 | |
| 168 // Click its link and verify this view has been removed from the test window. | |
| 169 // Also verify TestConfirmBubbleModel::LinkClicked() has been called. | |
| 170 [bubble_view clickLink]; | |
| 171 | |
| 172 contains_bubble_view = [[view subviews] containsObject:bubble_view]; | |
| 173 EXPECT_FALSE(contains_bubble_view); | |
| 174 EXPECT_FALSE(model()->accept_clicked()); | |
| 175 EXPECT_FALSE(model()->cancel_clicked()); | |
| 176 EXPECT_TRUE(model()->link_clicked()); | |
| 177 } | |
| OLD | NEW |