summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/app_controller_mac.mm8
-rw-r--r--chrome/browser/browser.vcproj8
-rw-r--r--chrome/browser/cocoa/browser_window_controller.mm22
-rw-r--r--chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h22
-rw-r--r--chrome/browser/cocoa/encoding_menu_controller_delegate_mac.mm63
-rw-r--r--chrome/browser/encoding_menu_controller.cc144
-rw-r--r--chrome/browser/encoding_menu_controller.h53
-rw-r--r--chrome/browser/encoding_menu_controller_delegate.cc109
-rw-r--r--chrome/browser/encoding_menu_controller_delegate.h2
-rw-r--r--chrome/browser/encoding_menu_controller_unittest.cc92
10 files changed, 441 insertions, 82 deletions
diff --git a/chrome/browser/app_controller_mac.mm b/chrome/browser/app_controller_mac.mm
index 2261342..7632024 100644
--- a/chrome/browser/app_controller_mac.mm
+++ b/chrome/browser/app_controller_mac.mm
@@ -13,6 +13,7 @@
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_shutdown.h"
#import "chrome/browser/cocoa/bookmark_menu_bridge.h"
+#import "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h"
#import "chrome/browser/cocoa/preferences_window_controller.h"
#include "chrome/browser/command_updater.h"
#include "chrome/common/pref_names.h"
@@ -66,6 +67,13 @@
// Register any Mac-specific preferences.
PrefService* prefs = [self defaultProfile]->GetPrefs();
prefs->RegisterBooleanPref(prefs::kShowPageOptionsButtons, false);
+
+ // Build up the encoding menu, the order of the items differs based on the
+ // current locale (see http://crbug.com/7647 for details).
+ // We need a valid g_browser_process to get the profile which is why we can't
+ // call this from awakeFromNib.
+ EncodingMenuControllerDelegate::BuildEncodingMenu([self defaultProfile]);
+
}
- (void)dealloc {
diff --git a/chrome/browser/browser.vcproj b/chrome/browser/browser.vcproj
index 6653ca2..87cf969 100644
--- a/chrome/browser/browser.vcproj
+++ b/chrome/browser/browser.vcproj
@@ -2631,6 +2631,14 @@
>
</File>
<File
+ RelativePath=".\encoding_menu_controller.cc"
+ >
+ </File>
+ <File
+ RelativePath=".\encoding_menu_controller.h"
+ >
+ </File>
+ <File
RelativePath=".\encoding_menu_controller_delegate.cc"
>
</File>
diff --git a/chrome/browser/cocoa/browser_window_controller.mm b/chrome/browser/cocoa/browser_window_controller.mm
index 283eb1e..92f3527 100644
--- a/chrome/browser/cocoa/browser_window_controller.mm
+++ b/chrome/browser/cocoa/browser_window_controller.mm
@@ -8,6 +8,7 @@
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_process.h"
+#include "chrome/browser/encoding_menu_controller.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_contents_view.h"
#include "chrome/browser/tabs/tab_strip_model.h"
@@ -263,6 +264,27 @@ willPositionSheet:(NSWindow *)sheet
if (oldState != newState)
[item setState:newState];
}
+
+ // Update the checked/Unchecked state of items in the encoding menu.
+ // On Windows this logic is part of encoding_menu_controller_delegate.cc
+ EncodingMenuController encoding_controller;
+ if (encoding_controller.DoesCommandBelongToEncodingMenu(tag)) {
+ DCHECK(browser_.get());
+ Profile *profile = browser_->profile();
+ DCHECK(profile);
+ TabContents* current_tab = browser_->GetSelectedTabContents();
+ if (!current_tab) {
+ return;
+ }
+ const std::wstring encoding = current_tab->encoding();
+
+ bool toggled = encoding_controller.IsItemChecked(profile, encoding, tag);
+ NSInteger oldState = [item state];
+ NSInteger newState = toggled ? NSOnState : NSOffState;
+ if (oldState != newState)
+ [item setState:newState];
+ }
+
}
// Called to validate menu and toolbar items when this window is key. All the
diff --git a/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h b/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h
new file mode 100644
index 0000000..a23b1aa
--- /dev/null
+++ b/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h
@@ -0,0 +1,22 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_COCOA_ENCODING_MENU_CONTROLLER_DELEGATE_MAC_H_
+#define CHROME_BROWSER_COCOA_ENCODING_MENU_CONTROLLER_DELEGATE_MAC_H_
+
+#include "base/basictypes.h" // For DISALLOW_IMPLICIT_CONSTRUCTORS
+
+class Profile;
+
+// The Windows version of this class manages the Encoding Menu, but since Cocoa
+// does that for us automagically, the only thing left to do is construct
+// the encoding menu.
+class EncodingMenuControllerDelegate {
+ public:
+ static void BuildEncodingMenu(Profile *profile);
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(EncodingMenuControllerDelegate);
+};
+
+#endif // CHROME_BROWSER_COCOA_ENCODING_MENU_CONTROLLER_DELEGATE_MAC_H_
diff --git a/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.mm b/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.mm
new file mode 100644
index 0000000..4f9468f
--- /dev/null
+++ b/chrome/browser/cocoa/encoding_menu_controller_delegate_mac.mm
@@ -0,0 +1,63 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h"
+
+#import <Cocoa/Cocoa.h>
+
+#include "base/sys_string_conversions.h"
+#include "chrome/app/chrome_dll_resource.h"
+#include "chrome/browser/browser.h"
+#include "chrome/browser/encoding_menu_controller.h"
+#include "chrome/browser/profile.h"
+
+namespace {
+
+void AddSeparatorToMenu(NSMenu *parent_menu) {
+ NSMenuItem* separator = [NSMenuItem separatorItem];
+ [parent_menu addItem:separator];
+}
+
+void AppendMenuItem(NSMenu *parent_menu, int tag, NSString *title) {
+
+ NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:title
+ action:nil
+ keyEquivalent:@""] autorelease];
+ [parent_menu addItem:item];
+ [item setAction:@selector(commandDispatch:)];
+ [item setTag:tag];
+}
+
+} // namespace
+
+// static
+void EncodingMenuControllerDelegate::BuildEncodingMenu(Profile *profile) {
+ DCHECK(profile);
+
+ // Get hold of the Cocoa encoding menu.
+ NSMenu* view_menu = [[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] submenu];
+ NSMenuItem* encoding_menu_item = [view_menu itemWithTag:IDC_ENCODING_MENU];
+ NSMenu *encoding_menu = [encoding_menu_item submenu];
+
+ typedef EncodingMenuController::EncodingMenuItemList EncodingMenuItemList;
+ EncodingMenuItemList menuItems;
+ EncodingMenuController controller;
+ controller.GetEncodingMenuItems(profile, &menuItems);
+
+ for (EncodingMenuItemList::iterator it = menuItems.begin();
+ it != menuItems.end();
+ ++it) {
+ int item_id = it->first;
+ std::wstring &localized_title_wstring = it->second;
+
+ if (item_id == 0) {
+ AddSeparatorToMenu(encoding_menu);
+ } else {
+ using base::SysWideToNSString;
+ NSString *localized_title = SysWideToNSString(localized_title_wstring);
+ AppendMenuItem(encoding_menu, item_id, localized_title);
+ }
+ }
+
+}
diff --git a/chrome/browser/encoding_menu_controller.cc b/chrome/browser/encoding_menu_controller.cc
new file mode 100644
index 0000000..ca8c964
--- /dev/null
+++ b/chrome/browser/encoding_menu_controller.cc
@@ -0,0 +1,144 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/encoding_menu_controller.h"
+
+#include "app/l10n_util.h"
+#include "chrome/app/chrome_dll_resource.h"
+#include "chrome/browser/browser.h"
+#include "chrome/browser/character_encoding.h"
+#include "chrome/browser/profile.h"
+#include "chrome/common/pref_names.h"
+#include "chrome/common/pref_service.h"
+#include "grit/generated_resources.h"
+
+const int EncodingMenuController::kValidEncodingIds[] = {
+ IDC_ENCODING_UTF8,
+ IDC_ENCODING_UTF16LE,
+ IDC_ENCODING_ISO88591,
+ IDC_ENCODING_WINDOWS1252,
+ IDC_ENCODING_GBK,
+ IDC_ENCODING_GB18030,
+ IDC_ENCODING_BIG5,
+ IDC_ENCODING_BIG5HKSCS,
+ IDC_ENCODING_KOREAN,
+ IDC_ENCODING_SHIFTJIS,
+ IDC_ENCODING_ISO2022JP,
+ IDC_ENCODING_EUCJP,
+ IDC_ENCODING_THAI,
+ IDC_ENCODING_ISO885915,
+ IDC_ENCODING_MACINTOSH,
+ IDC_ENCODING_ISO88592,
+ IDC_ENCODING_WINDOWS1250,
+ IDC_ENCODING_ISO88595,
+ IDC_ENCODING_WINDOWS1251,
+ IDC_ENCODING_KOI8R,
+ IDC_ENCODING_KOI8U,
+ IDC_ENCODING_ISO88597,
+ IDC_ENCODING_WINDOWS1253,
+ IDC_ENCODING_ISO88594,
+ IDC_ENCODING_ISO885913,
+ IDC_ENCODING_WINDOWS1257,
+ IDC_ENCODING_ISO88593,
+ IDC_ENCODING_ISO885910,
+ IDC_ENCODING_ISO885914,
+ IDC_ENCODING_ISO885916,
+ IDC_ENCODING_WINDOWS1254,
+ IDC_ENCODING_ISO88596,
+ IDC_ENCODING_WINDOWS1256,
+ IDC_ENCODING_ISO88598,
+ IDC_ENCODING_WINDOWS1255,
+ IDC_ENCODING_WINDOWS1258,
+ IDC_ENCODING_ISO88598I,
+};
+
+bool EncodingMenuController::DoesCommandBelongToEncodingMenu(int id) {
+ if (id == IDC_ENCODING_AUTO_DETECT) {
+ return true;
+ }
+
+ for (size_t i = 0; i < arraysize(kValidEncodingIds); ++i) {
+ if (id == kValidEncodingIds[i]) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+const int* EncodingMenuController::ValidGUIEncodingIDs() {
+ return kValidEncodingIds;
+}
+
+int EncodingMenuController::NumValidGUIEncodingIDs() {
+ return arraysize(kValidEncodingIds);
+}
+
+bool EncodingMenuController::IsItemChecked(
+ Profile* browser_profile,
+ const std::wstring& current_tab_encoding,
+ int item_id) {
+ if (!DoesCommandBelongToEncodingMenu(item_id)) {
+ return false;
+ }
+
+ std::wstring encoding = current_tab_encoding;
+ if (encoding.empty()) {
+ encoding = browser_profile->GetPrefs()->GetString(prefs::kDefaultCharset);
+ }
+
+ if (item_id == IDC_ENCODING_AUTO_DETECT) {
+ return browser_profile->GetPrefs()->GetBoolean(
+ prefs::kWebKitUsesUniversalDetector);
+ }
+
+ if (!encoding.empty()) {
+ return encoding ==
+ CharacterEncoding::GetCanonicalEncodingNameByCommandId(item_id);
+ }
+
+ return false;
+}
+
+void EncodingMenuController::GetEncodingMenuItems(Profile* profile,
+ EncodingMenuItemList* menuItems) {
+
+ DCHECK(menuItems);
+ EncodingMenuItem separator(0, L"");
+
+ menuItems->clear();
+ menuItems->push_back(
+ EncodingMenuItem(IDC_ENCODING_AUTO_DETECT,
+ l10n_util::GetString(IDS_ENCODING_AUTO_DETECT)));
+ menuItems->push_back(separator);
+
+ // Create current display encoding list.
+ const std::vector<CharacterEncoding::EncodingInfo>* encodings;
+
+ // Build the list of encoding ids : It is made of the
+ // locale-dependent short list, the cache of recently selected
+ // encodings and other encodings.
+ encodings = CharacterEncoding::GetCurrentDisplayEncodings(
+ g_browser_process->GetApplicationLocale(),
+ profile->GetPrefs()->GetString(prefs::kStaticEncodings),
+ profile->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding));
+ DCHECK(encodings);
+ DCHECK(!encodings->empty());
+
+ // Build up output list for menu.
+ std::vector<CharacterEncoding::EncodingInfo>::const_iterator it;
+ for (it = encodings->begin(); it != encodings->end(); ++it) {
+ if (it->encoding_id) {
+ std::wstring encoding = it->encoding_display_name;
+ std::wstring bidi_safe_encoding;
+ if (l10n_util::AdjustStringForLocaleDirection(encoding,
+ &bidi_safe_encoding))
+ encoding.swap(bidi_safe_encoding);
+ menuItems->push_back(EncodingMenuItem(it->encoding_id, encoding));
+ } else {
+ menuItems->push_back(separator);
+ }
+ }
+
+}
diff --git a/chrome/browser/encoding_menu_controller.h b/chrome/browser/encoding_menu_controller.h
new file mode 100644
index 0000000..9134b54
--- /dev/null
+++ b/chrome/browser/encoding_menu_controller.h
@@ -0,0 +1,53 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHORME_BROWSER_ENCODING_MENU_CONTROLLER_H_
+#define CHORME_BROWSER_ENCODING_MENU_CONTROLLER_H_
+
+#include <vector>
+
+#include "base/basictypes.h" // For DISALLOW_IMPLICIT_CONSTRUCTORS
+#include "testing/gtest/include/gtest/gtest_prod.h" // For FRIEND_TEST
+
+class Browser;
+class Profile;
+
+// Cross-platform logic needed for the encoding menu.
+// For now, we don't need to track state so all methods are static.
+class EncodingMenuController {
+ FRIEND_TEST(EncodingMenuControllerTest, EncodingIDsBelongTest);
+ FRIEND_TEST(EncodingMenuControllerTest, IsItemChecked);
+
+ public:
+ typedef std::pair<int, std::wstring> EncodingMenuItem;
+ typedef std::vector<EncodingMenuItem> EncodingMenuItemList;
+
+ public:
+ EncodingMenuController() {}
+
+ // Given a command ID, does this command belong to the encoding menu?
+ bool DoesCommandBelongToEncodingMenu(int id);
+
+ // Returns true if the given encoding menu item (specified by item_id)
+ // is checked. Note that this header is included from objc, where the name
+ // "id" is reserved.
+ bool IsItemChecked(Profile* browser_profile,
+ const std::wstring& current_tab_encoding,
+ int item_id);
+
+ // Fills in a list of menu items in the order they should appear in the menu.
+ // Items whose ids are 0 are separators.
+ void GetEncodingMenuItems(Profile* profile,
+ EncodingMenuItemList* menuItems);
+
+ private:
+ // List of all valid encoding GUI IDs.
+ static const int kValidEncodingIds[];
+ const int* ValidGUIEncodingIDs();
+ int NumValidGUIEncodingIDs();
+ private:
+ DISALLOW_COPY_AND_ASSIGN(EncodingMenuController);
+};
+
+#endif // CHORME_BROWSER_ENCODING_MENU_CONTROLLER_H_
diff --git a/chrome/browser/encoding_menu_controller_delegate.cc b/chrome/browser/encoding_menu_controller_delegate.cc
index a7a925a..6faac2d 100644
--- a/chrome/browser/encoding_menu_controller_delegate.cc
+++ b/chrome/browser/encoding_menu_controller_delegate.cc
@@ -4,77 +4,34 @@
#include "chrome/browser/encoding_menu_controller_delegate.h"
-#include "app/l10n_util.h"
#include "chrome/app/chrome_dll_resource.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/character_encoding.h"
+#include "chrome/browser/encoding_menu_controller.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
-#include "grit/generated_resources.h"
EncodingMenuControllerDelegate::EncodingMenuControllerDelegate(Browser* browser)
: browser_(browser) {
}
bool EncodingMenuControllerDelegate::IsItemChecked(int id) const {
- Profile* profile = browser_->profile();
+ if (!browser_)
+ return false;
+ Profile *profile = browser_->profile();
if (!profile)
return false;
TabContents* current_tab = browser_->GetSelectedTabContents();
- if (!current_tab)
+ if (!current_tab) {
return false;
- std::wstring encoding = current_tab->encoding();
- if (encoding.empty())
- encoding = profile->GetPrefs()->GetString(prefs::kDefaultCharset);
- switch (id) {
- case IDC_ENCODING_AUTO_DETECT:
- return profile->GetPrefs()->GetBoolean(
- prefs::kWebKitUsesUniversalDetector);
- case IDC_ENCODING_UTF8:
- case IDC_ENCODING_UTF16LE:
- case IDC_ENCODING_ISO88591:
- case IDC_ENCODING_WINDOWS1252:
- case IDC_ENCODING_GBK:
- case IDC_ENCODING_GB18030:
- case IDC_ENCODING_BIG5:
- case IDC_ENCODING_BIG5HKSCS:
- case IDC_ENCODING_KOREAN:
- case IDC_ENCODING_SHIFTJIS:
- case IDC_ENCODING_ISO2022JP:
- case IDC_ENCODING_EUCJP:
- case IDC_ENCODING_THAI:
- case IDC_ENCODING_ISO885915:
- case IDC_ENCODING_MACINTOSH:
- case IDC_ENCODING_ISO88592:
- case IDC_ENCODING_WINDOWS1250:
- case IDC_ENCODING_ISO88595:
- case IDC_ENCODING_WINDOWS1251:
- case IDC_ENCODING_KOI8R:
- case IDC_ENCODING_KOI8U:
- case IDC_ENCODING_ISO88597:
- case IDC_ENCODING_WINDOWS1253:
- case IDC_ENCODING_ISO88594:
- case IDC_ENCODING_ISO885913:
- case IDC_ENCODING_WINDOWS1257:
- case IDC_ENCODING_ISO88593:
- case IDC_ENCODING_ISO885910:
- case IDC_ENCODING_ISO885914:
- case IDC_ENCODING_ISO885916:
- case IDC_ENCODING_WINDOWS1254:
- case IDC_ENCODING_ISO88596:
- case IDC_ENCODING_WINDOWS1256:
- case IDC_ENCODING_ISO88598:
- case IDC_ENCODING_ISO88598I:
- case IDC_ENCODING_WINDOWS1255:
- case IDC_ENCODING_WINDOWS1258:
- return (!encoding.empty() && encoding ==
- CharacterEncoding::GetCanonicalEncodingNameByCommandId(id));
- default:
- return false;
}
+ const std::wstring encoding = current_tab->encoding();
+
+ EncodingMenuController controller;
+ return controller.IsItemChecked(profile, encoding, id);
}
bool EncodingMenuControllerDelegate::SupportsCommand(int id) const {
@@ -97,38 +54,26 @@ void EncodingMenuControllerDelegate::ExecuteCommand(int id) {
void EncodingMenuControllerDelegate::BuildEncodingMenu(
Profile* profile, Menu* encoding_menu) {
- // Append auto-detection item.
- encoding_menu->AppendMenuItem(IDC_ENCODING_AUTO_DETECT,
- l10n_util::GetString(IDS_ENCODING_AUTO_DETECT),
- Menu::CHECKBOX);
+ typedef EncodingMenuController::EncodingMenuItemList EncodingMenuItemList;
+ EncodingMenuItemList menuItems;
+ EncodingMenuController controller;
+ controller.GetEncodingMenuItems(profile, &menuItems);
- // Append encoding item.
- encoding_menu->AppendSeparator();
- // Create current display encoding list.
- std::wstring cur_locale = g_browser_process->GetApplicationLocale();
- const std::vector<CharacterEncoding::EncodingInfo>* encodings;
- // Build the list of encoding ids : It is made of the
- // locale-dependent short list, the cache of recently selected
- // encodings and other encodings.
- encodings = CharacterEncoding::GetCurrentDisplayEncodings(
- cur_locale,
- profile->GetPrefs()->GetString(prefs::kStaticEncodings),
- profile->GetPrefs()->GetString(prefs::kRecentlySelectedEncoding));
- DCHECK(encodings);
- DCHECK(!encodings->empty());
- unsigned len = static_cast<unsigned>(encodings->size());
- // Add encoding menus.
- std::vector<CharacterEncoding::EncodingInfo>::const_iterator it;
- for (it = encodings->begin(); it != encodings->end(); ++it) {
- if (it->encoding_id) {
- std::wstring encoding = it->encoding_display_name;
- std::wstring bidi_safe_encoding;
- if (l10n_util::AdjustStringForLocaleDirection(encoding,
- &bidi_safe_encoding))
- encoding.swap(bidi_safe_encoding);
- encoding_menu->AppendMenuItem(it->encoding_id, encoding, Menu::RADIO);
- } else {
+ for (EncodingMenuItemList::iterator it = menuItems.begin();
+ it != menuItems.end();
+ ++it) {
+ Menu::MenuItemType type = Menu::RADIO;
+ int id = it->first;
+ std::wstring &localized_title = it->second;
+
+ if (id == 0) {
encoding_menu->AppendSeparator();
+ } else {
+ if (id == IDC_ENCODING_AUTO_DETECT) {
+ type = Menu::CHECKBOX;
+ }
+
+ encoding_menu->AppendMenuItem(id, localized_title, type);
}
}
}
diff --git a/chrome/browser/encoding_menu_controller_delegate.h b/chrome/browser/encoding_menu_controller_delegate.h
index 83d42ec..f35a5e4 100644
--- a/chrome/browser/encoding_menu_controller_delegate.h
+++ b/chrome/browser/encoding_menu_controller_delegate.h
@@ -5,6 +5,7 @@
#ifndef CHORME_BROWSER_ENCODING_MENU_CONTROLLER_DELEGATE_H__
#define CHORME_BROWSER_ENCODING_MENU_CONTROLLER_DELEGATE_H__
+#include "base/basictypes.h" // For DISALLOW_IMPLICIT_CONSTRUCTORS
#include "views/controls/menu/menu.h"
class Browser;
@@ -32,6 +33,7 @@ class EncodingMenuControllerDelegate : public Menu::Delegate {
private:
Browser* browser_;
+ DISALLOW_IMPLICIT_CONSTRUCTORS(EncodingMenuControllerDelegate);
};
#endif // CHORME_BROWSER_ENCODING_MENU_CONTROLLER_DELEGATE_H__
diff --git a/chrome/browser/encoding_menu_controller_unittest.cc b/chrome/browser/encoding_menu_controller_unittest.cc
new file mode 100644
index 0000000..a8258bd6
--- /dev/null
+++ b/chrome/browser/encoding_menu_controller_unittest.cc
@@ -0,0 +1,92 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/encoding_menu_controller.h"
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "chrome/app/chrome_dll_resource.h"
+#include "chrome/browser/profile.h"
+#include "chrome/common/pref_names.h"
+#include "chrome/test/testing_profile.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+
+class EncodingMenuControllerTest : public testing::Test {
+};
+
+TEST_F(EncodingMenuControllerTest, EncodingIDsBelongTest) {
+ EncodingMenuController controller;
+
+ // Check some bogus ids to make sure they're never valid.
+ ASSERT_FALSE(controller.DoesCommandBelongToEncodingMenu(0));
+ ASSERT_FALSE(controller.DoesCommandBelongToEncodingMenu(-1));
+
+ int num_valid_encoding_ids = controller.NumValidGUIEncodingIDs();
+ const int* valid_encodings = controller.ValidGUIEncodingIDs();
+ ASSERT_TRUE(controller.DoesCommandBelongToEncodingMenu(
+ IDC_ENCODING_AUTO_DETECT));
+ // Check that all valid encodings are accepted.
+ for (int i = 0; i < num_valid_encoding_ids; ++i) {
+ ASSERT_TRUE(controller.DoesCommandBelongToEncodingMenu(valid_encodings[i]));
+ }
+
+ // This test asserts that we haven't added a new valid ID without including it
+ // in the kValidEncodingIds test list above.
+ // The premise is that new encodings will be added directly after the current
+ // ones so we'll catch such cases.
+ int one_past_largest_id = valid_encodings[num_valid_encoding_ids - 1] + 1;
+ ASSERT_FALSE(controller.DoesCommandBelongToEncodingMenu(one_past_largest_id));
+}
+
+TEST_F(EncodingMenuControllerTest, ListEncodingMenuItems) {
+ typedef EncodingMenuController::EncodingMenuItemList EncodingMenuItemList;
+ EncodingMenuController controller;
+
+ EncodingMenuItemList english_items;
+ TestingProfile profile_en;
+
+ controller.GetEncodingMenuItems(&profile_en, &english_items);
+
+ // Make sure there are items in the lists.
+ ASSERT_TRUE(english_items.size() > 0);
+ // Make sure that autodetect is the first item on both menus
+ ASSERT_EQ(english_items[0].first, IDC_ENCODING_AUTO_DETECT);
+}
+
+TEST_F(EncodingMenuControllerTest, IsItemChecked) {
+ TestingProfile profile_en;
+ std::wstring encoding(L"UTF-8");
+
+ // Check that enabling and disabling autodetect works.
+ bool autodetect_enabed[] = {true, false};
+ PrefService* prefs = profile_en.GetPrefs();
+ EncodingMenuController controller;
+ for (size_t i = 0; i < arraysize(autodetect_enabed); ++i) {
+ bool enabled = autodetect_enabed[i];
+ prefs->SetBoolean(prefs::kWebKitUsesUniversalDetector, enabled);
+ ASSERT_TRUE(controller.IsItemChecked(&profile_en,
+ encoding,
+ IDC_ENCODING_AUTO_DETECT) == enabled);
+ }
+
+ // Check all valid encodings, make sure only one is enabled when autodetection
+ // is turned off.
+ prefs->SetBoolean(prefs::kWebKitUsesUniversalDetector, false);
+ bool encoding_is_enabled = false;
+ size_t num_valid_encoding_ids = controller.NumValidGUIEncodingIDs();
+ const int* valid_encodings = controller.ValidGUIEncodingIDs();
+ for (size_t i = 0; i < num_valid_encoding_ids; ++i) {
+ bool on = controller.IsItemChecked(&profile_en,
+ encoding,
+ valid_encodings[i]);
+ // Only one item in the encoding menu can be selected at a time.
+ ASSERT_FALSE(on && encoding_is_enabled);
+ encoding_is_enabled |= on;
+ }
+
+ // Make sure at least one encoding is enabled.
+ ASSERT_TRUE(encoding_is_enabled);
+}