1 // Copyright (c) 2010 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 #import "chrome/browser/ui/cocoa/table_model_array_controller.h" 6 7 #include "base/auto_reset.h" 8 #include "base/command_line.h" 9 #include "base/utf_string_conversions.h" 10 #include "chrome/browser/mock_plugin_exceptions_table_model.h" 11 #include "chrome/browser/ui/cocoa/browser_test_helper.h" 12 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 13 #include "chrome/common/chrome_switches.h" 14 #include "chrome/test/testing_profile.h" 15 #include "grit/generated_resources.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest_mac.h" 18 #include "webkit/plugins/npapi/plugin_list.h" 19 #include "webkit/plugins/npapi/webplugininfo.h" 20 21 class TableModelArrayControllerTest : public CocoaTest { 22 public: 23 TableModelArrayControllerTest() 24 : command_line_(CommandLine::ForCurrentProcess(), 25 *CommandLine::ForCurrentProcess()) {} 26 27 virtual void SetUp() { 28 CocoaTest::SetUp(); 29 30 CommandLine::ForCurrentProcess()->AppendSwitch( 31 switches::kEnableResourceContentSettings); 32 33 TestingProfile* profile = browser_helper_.profile(); 34 HostContentSettingsMap* map = profile->GetHostContentSettingsMap(); 35 36 ContentSettingsPattern example_com("[*.]example.com"); 37 ContentSettingsPattern moose_org("[*.]moose.org"); 38 map->SetContentSetting(example_com, 39 CONTENT_SETTINGS_TYPE_PLUGINS, 40 "a-foo", 41 CONTENT_SETTING_ALLOW); 42 map->SetContentSetting(moose_org, 43 CONTENT_SETTINGS_TYPE_PLUGINS, 44 "b-bar", 45 CONTENT_SETTING_BLOCK); 46 map->SetContentSetting(example_com, 47 CONTENT_SETTINGS_TYPE_PLUGINS, 48 "b-bar", 49 CONTENT_SETTING_ALLOW); 50 51 model_.reset(new MockPluginExceptionsTableModel(map, NULL)); 52 53 std::vector<webkit::npapi::PluginGroup> plugins; 54 webkit::npapi::WebPluginInfo foo_plugin; 55 foo_plugin.path = FilePath(FILE_PATH_LITERAL("a-foo")); 56 foo_plugin.name = ASCIIToUTF16("FooPlugin"); 57 foo_plugin.enabled = 58 webkit::npapi::WebPluginInfo::USER_ENABLED_POLICY_UNMANAGED; 59 scoped_ptr<webkit::npapi::PluginGroup> foo_group( 60 webkit::npapi::PluginGroup::FromWebPluginInfo(foo_plugin)); 61 plugins.push_back(*foo_group); 62 webkit::npapi::WebPluginInfo bar_plugin; 63 bar_plugin.path = FilePath(FILE_PATH_LITERAL("b-bar")); 64 bar_plugin.name = ASCIIToUTF16("BarPlugin"); 65 bar_plugin.enabled = 66 webkit::npapi::WebPluginInfo::USER_ENABLED_POLICY_UNMANAGED; 67 scoped_ptr<webkit::npapi::PluginGroup> bar_group( 68 webkit::npapi::PluginGroup::FromWebPluginInfo(bar_plugin)); 69 plugins.push_back(*bar_group); 70 webkit::npapi::WebPluginInfo blurp_plugin; 71 blurp_plugin.path = FilePath(FILE_PATH_LITERAL("c-blurp")); 72 blurp_plugin.name = ASCIIToUTF16("BlurpPlugin"); 73 blurp_plugin.enabled = 74 webkit::npapi::WebPluginInfo::USER_ENABLED_POLICY_UNMANAGED; 75 scoped_ptr<webkit::npapi::PluginGroup> blurp_group( 76 webkit::npapi::PluginGroup::FromWebPluginInfo(blurp_plugin)); 77 plugins.push_back(*blurp_group); 78 79 model_->set_plugins(plugins); 80 model_->LoadSettings(); 81 82 id content = [NSMutableArray array]; 83 controller_.reset( 84 [[TableModelArrayController alloc] initWithContent:content]); 85 NSDictionary* columns = [NSDictionary dictionaryWithObjectsAndKeys: 86 [NSNumber numberWithInt:IDS_EXCEPTIONS_HOSTNAME_HEADER], @"title", 87 [NSNumber numberWithInt:IDS_EXCEPTIONS_ACTION_HEADER], @"action", 88 nil]; 89 [controller_.get() bindToTableModel:model_.get() 90 withColumns:columns 91 groupTitleColumn:@"title"]; 92 } 93 94 protected: 95 BrowserTestHelper browser_helper_; 96 scoped_ptr<MockPluginExceptionsTableModel> model_; 97 scoped_nsobject<TableModelArrayController> controller_; 98 99 private: 100 AutoReset<CommandLine> command_line_; 101 }; 102 103 TEST_F(TableModelArrayControllerTest, CheckTitles) { 104 NSArray* titles = [[controller_.get() arrangedObjects] valueForKey:@"title"]; 105 EXPECT_NSEQ(@"(\n" 106 @" FooPlugin,\n" 107 @" \"[*.]example.com\",\n" 108 @" BarPlugin,\n" 109 @" \"[*.]example.com\",\n" 110 @" \"[*.]moose.org\"\n" 111 @")", 112 [titles description]); 113 } 114 115 TEST_F(TableModelArrayControllerTest, RemoveRows) { 116 NSArrayController* controller = controller_.get(); 117 [controller setSelectionIndex:1]; 118 [controller remove:nil]; 119 NSArray* titles = [[controller arrangedObjects] valueForKey:@"title"]; 120 EXPECT_NSEQ(@"(\n" 121 @" BarPlugin,\n" 122 @" \"[*.]example.com\",\n" 123 @" \"[*.]moose.org\"\n" 124 @")", 125 [titles description]); 126 127 [controller setSelectionIndex:2]; 128 [controller remove:nil]; 129 titles = [[controller arrangedObjects] valueForKey:@"title"]; 130 EXPECT_NSEQ(@"(\n" 131 @" BarPlugin,\n" 132 @" \"[*.]example.com\"\n" 133 @")", 134 [titles description]); 135 } 136 137 TEST_F(TableModelArrayControllerTest, RemoveAll) { 138 [controller_.get() removeAll:nil]; 139 EXPECT_EQ(0u, [[controller_.get() arrangedObjects] count]); 140 } 141 142 TEST_F(TableModelArrayControllerTest, AddException) { 143 TestingProfile* profile = browser_helper_.profile(); 144 HostContentSettingsMap* map = profile->GetHostContentSettingsMap(); 145 ContentSettingsPattern example_com("[*.]example.com"); 146 map->SetContentSetting(example_com, 147 CONTENT_SETTINGS_TYPE_PLUGINS, 148 "c-blurp", 149 CONTENT_SETTING_BLOCK); 150 151 NSArrayController* controller = controller_.get(); 152 NSArray* titles = [[controller arrangedObjects] valueForKey:@"title"]; 153 EXPECT_NSEQ(@"(\n" 154 @" FooPlugin,\n" 155 @" \"[*.]example.com\",\n" 156 @" BarPlugin,\n" 157 @" \"[*.]example.com\",\n" 158 @" \"[*.]moose.org\",\n" 159 @" BlurpPlugin,\n" 160 @" \"[*.]example.com\"\n" 161 @")", 162 [titles description]); 163 NSMutableIndexSet* indexes = [NSMutableIndexSet indexSetWithIndex:1]; 164 [indexes addIndex:6]; 165 [controller setSelectionIndexes:indexes]; 166 [controller remove:nil]; 167 titles = [[controller arrangedObjects] valueForKey:@"title"]; 168 EXPECT_NSEQ(@"(\n" 169 @" BarPlugin,\n" 170 @" \"[*.]example.com\",\n" 171 @" \"[*.]moose.org\"\n" 172 @")", 173 [titles description]); 174 } 175 176