OLD | NEW |
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 | 6 |
7 #import "base/scoped_nsobject.h" | 7 #import "base/scoped_nsobject.h" |
8 #include "chrome/app/chrome_dll_resource.h" | 8 #include "chrome/app/chrome_dll_resource.h" |
9 #include "chrome/browser/cocoa/browser_test_helper.h" | 9 #include "chrome/browser/cocoa/browser_test_helper.h" |
10 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 10 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
11 #import "chrome/browser/cocoa/toolbar_controller.h" | 11 #import "chrome/browser/cocoa/toolbar_controller.h" |
| 12 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/common/pref_service.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 class ToolbarControllerTest : public testing::Test { | 18 class ToolbarControllerTest : public testing::Test { |
17 public: | 19 public: |
18 | 20 |
19 // Indexes that match the ordering returned by the private ToolbarController | 21 // Indexes that match the ordering returned by the private ToolbarController |
20 // |-toolbarViews| method. | 22 // |-toolbarViews| method. |
21 enum { | 23 enum { |
22 kBackIndex, kForwardIndex, kReloadIndex, kStarIndex, kGoIndex, | 24 kBackIndex, kForwardIndex, kReloadIndex, kHomeIndex, kStarIndex, kGoIndex, |
23 kLocationIndex, | 25 kPageIndex, kWrenchIndex, kLocationIndex, |
24 }; | 26 }; |
25 | 27 |
26 ToolbarControllerTest() { | 28 ToolbarControllerTest() { |
27 Browser* browser = helper_.browser(); | 29 Browser* browser = helper_.browser(); |
28 CommandUpdater* updater = browser->command_updater(); | 30 CommandUpdater* updater = browser->command_updater(); |
29 // The default state for the commands is true, set a couple to false to | 31 // The default state for the commands is true, set a couple to false to |
30 // ensure they get picked up correct on initialization | 32 // ensure they get picked up correct on initialization |
31 updater->UpdateCommandEnabled(IDC_BACK, false); | 33 updater->UpdateCommandEnabled(IDC_BACK, false); |
32 updater->UpdateCommandEnabled(IDC_FORWARD, false); | 34 updater->UpdateCommandEnabled(IDC_FORWARD, false); |
33 bar_.reset( | 35 bar_.reset( |
34 [[ToolbarController alloc] initWithModel:browser->toolbar_model() | 36 [[ToolbarController alloc] initWithModel:browser->toolbar_model() |
35 commands:browser->command_updater() | 37 commands:browser->command_updater() |
36 profile:helper_.profile()]); | 38 profile:helper_.profile()]); |
37 EXPECT_TRUE([bar_ view]); | 39 EXPECT_TRUE([bar_ view]); |
38 NSView* parent = [cocoa_helper_.window() contentView]; | 40 NSView* parent = [cocoa_helper_.window() contentView]; |
39 [parent addSubview:[bar_ view]]; | 41 [parent addSubview:[bar_ view]]; |
40 } | 42 } |
41 | 43 |
42 // Make sure the enabled state of the view is the same as the corresponding | 44 // Make sure the enabled state of the view is the same as the corresponding |
43 // command in the updater. The views are in the declaration order of outlets. | 45 // command in the updater. The views are in the declaration order of outlets. |
44 void CompareState(CommandUpdater* updater, NSArray* views) { | 46 void CompareState(CommandUpdater* updater, NSArray* views) { |
45 EXPECT_EQ(updater->IsCommandEnabled(IDC_BACK), | 47 EXPECT_EQ(updater->IsCommandEnabled(IDC_BACK), |
46 [[views objectAtIndex:kBackIndex] isEnabled] ? true : false); | 48 [[views objectAtIndex:kBackIndex] isEnabled] ? true : false); |
47 EXPECT_EQ(updater->IsCommandEnabled(IDC_FORWARD), | 49 EXPECT_EQ(updater->IsCommandEnabled(IDC_FORWARD), |
48 [[views objectAtIndex:kForwardIndex] isEnabled] ? true : false); | 50 [[views objectAtIndex:kForwardIndex] isEnabled] ? true : false); |
49 EXPECT_EQ(updater->IsCommandEnabled(IDC_RELOAD), | 51 EXPECT_EQ(updater->IsCommandEnabled(IDC_RELOAD), |
50 [[views objectAtIndex:kReloadIndex] isEnabled] ? true : false); | 52 [[views objectAtIndex:kReloadIndex] isEnabled] ? true : false); |
51 // TODO(pinkerton): Add IDC_HOME when we get a view for it | 53 EXPECT_EQ(updater->IsCommandEnabled(IDC_HOME), |
| 54 [[views objectAtIndex:kHomeIndex] isEnabled] ? true : false); |
52 EXPECT_EQ(updater->IsCommandEnabled(IDC_STAR), | 55 EXPECT_EQ(updater->IsCommandEnabled(IDC_STAR), |
53 [[views objectAtIndex:kStarIndex] isEnabled] ? true : false); | 56 [[views objectAtIndex:kStarIndex] isEnabled] ? true : false); |
54 } | 57 } |
55 | 58 |
56 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... | 59 CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc... |
57 BrowserTestHelper helper_; | 60 BrowserTestHelper helper_; |
58 scoped_nsobject<ToolbarController> bar_; | 61 scoped_nsobject<ToolbarController> bar_; |
59 }; | 62 }; |
60 | 63 |
61 // Test the initial state that everything is sync'd up | 64 // Test the initial state that everything is sync'd up |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 // In its initial state, the go button has a tag of IDC_GO. When loading, | 107 // In its initial state, the go button has a tag of IDC_GO. When loading, |
105 // it should be IDC_STOP. | 108 // it should be IDC_STOP. |
106 NSButton* go = [[bar_ toolbarViews] objectAtIndex:kGoIndex]; | 109 NSButton* go = [[bar_ toolbarViews] objectAtIndex:kGoIndex]; |
107 EXPECT_EQ([go tag], IDC_GO); | 110 EXPECT_EQ([go tag], IDC_GO); |
108 [bar_ setIsLoading:YES]; | 111 [bar_ setIsLoading:YES]; |
109 EXPECT_EQ([go tag], IDC_STOP); | 112 EXPECT_EQ([go tag], IDC_STOP); |
110 [bar_ setIsLoading:NO]; | 113 [bar_ setIsLoading:NO]; |
111 EXPECT_EQ([go tag], IDC_GO); | 114 EXPECT_EQ([go tag], IDC_GO); |
112 } | 115 } |
113 | 116 |
| 117 // Check that toggling the state of the home button changes the visible |
| 118 // state of the home button and moves the other buttons accordingly. |
| 119 TEST_F(ToolbarControllerTest, ToggleHome) { |
| 120 PrefService* prefs = helper_.profile()->GetPrefs(); |
| 121 bool showHome = prefs->GetBoolean(prefs::kShowHomeButton); |
| 122 NSView* homeButton = [[bar_ toolbarViews] objectAtIndex:kHomeIndex]; |
| 123 EXPECT_EQ(showHome, ![homeButton isHidden]); |
| 124 |
| 125 NSView* starButton = [[bar_ toolbarViews] objectAtIndex:kStarIndex]; |
| 126 NSView* locationBar = [[bar_ toolbarViews] objectAtIndex:kLocationIndex]; |
| 127 NSRect originalStarFrame = [starButton frame]; |
| 128 NSRect originalLocationBarFrame = [locationBar frame]; |
| 129 |
| 130 // Toggle the pref and make sure the button changed state and the other |
| 131 // views moved. |
| 132 prefs->SetBoolean(prefs::kShowHomeButton, !showHome); |
| 133 EXPECT_EQ(showHome, [homeButton isHidden]); |
| 134 EXPECT_NE(NSMinX(originalStarFrame), NSMinX([starButton frame])); |
| 135 EXPECT_NE(NSMinX(originalLocationBarFrame), NSMinX([locationBar frame])); |
| 136 EXPECT_NE(NSWidth(originalLocationBarFrame), NSWidth([locationBar frame])); |
| 137 } |
| 138 |
| 139 TEST_F(ToolbarControllerTest, TogglePageWrench) { |
| 140 PrefService* prefs = helper_.profile()->GetPrefs(); |
| 141 bool showButtons = prefs->GetBoolean(prefs::kShowPageOptionsButtons); |
| 142 NSView* pageButton = [[bar_ toolbarViews] objectAtIndex:kPageIndex]; |
| 143 NSView* wrenchButton = [[bar_ toolbarViews] objectAtIndex:kWrenchIndex]; |
| 144 EXPECT_EQ(showButtons, ![pageButton isHidden]); |
| 145 EXPECT_EQ(showButtons, ![wrenchButton isHidden]); |
| 146 |
| 147 NSView* goButton = [[bar_ toolbarViews] objectAtIndex:kGoIndex]; |
| 148 NSView* locationBar = [[bar_ toolbarViews] objectAtIndex:kLocationIndex]; |
| 149 NSRect originalGoFrame = [goButton frame]; |
| 150 NSRect originalLocationBarFrame = [locationBar frame]; |
| 151 |
| 152 // Toggle the pref and make sure the buttons changed state and the other |
| 153 // views moved (or in the case of the location bar, it changed width). |
| 154 prefs->SetBoolean(prefs::kShowPageOptionsButtons, !showButtons); |
| 155 EXPECT_EQ(showButtons, [pageButton isHidden]); |
| 156 EXPECT_EQ(showButtons, [wrenchButton isHidden]); |
| 157 EXPECT_NE(NSMinX(originalGoFrame), NSMinX([goButton frame])); |
| 158 EXPECT_NE(NSWidth(originalLocationBarFrame), NSWidth([locationBar frame])); |
| 159 } |
| 160 |
114 } // namespace | 161 } // namespace |
OLD | NEW |