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 "chrome/browser/ui/toolbar/encoding_menu_controller.h" 6 7 #include <string> 8 9 #include "base/basictypes.h" 10 #include "base/message_loop/message_loop.h" 11 #include "base/prefs/pref_service.h" 12 #include "chrome/app/chrome_command_ids.h" 13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/common/pref_names.h" 15 #include "chrome/test/base/testing_profile.h" 16 #include "content/public/test/test_browser_thread.h" 17 #include "testing/gtest/include/gtest/gtest.h" 18 19 using content::BrowserThread; 20 21 class EncodingMenuControllerTest : public testing::Test { 22 public: 23 EncodingMenuControllerTest() 24 : ui_thread_(BrowserThread::UI, &message_loop_) {} 25 private: 26 base::MessageLoop message_loop_; 27 content::TestBrowserThread ui_thread_; 28 }; 29 30 TEST_F(EncodingMenuControllerTest, EncodingIDsBelongTest) { 31 EncodingMenuController controller; 32 33 // Check some bogus ids to make sure they're never valid. 34 ASSERT_FALSE(controller.DoesCommandBelongToEncodingMenu(0)); 35 ASSERT_FALSE(controller.DoesCommandBelongToEncodingMenu(-1)); 36 37 int num_valid_encoding_ids = controller.NumValidGUIEncodingIDs(); 38 const int* valid_encodings = controller.ValidGUIEncodingIDs(); 39 ASSERT_TRUE(controller.DoesCommandBelongToEncodingMenu( 40 IDC_ENCODING_AUTO_DETECT)); 41 // Check that all valid encodings are accepted. 42 for (int i = 0; i < num_valid_encoding_ids; ++i) { 43 ASSERT_TRUE(controller.DoesCommandBelongToEncodingMenu(valid_encodings[i])); 44 } 45 46 // This test asserts that we haven't added a new valid ID without including it 47 // in the kValidEncodingIds test list above. 48 // The premise is that new encodings will be added directly after the current 49 // ones so we'll catch such cases. 50 int one_past_largest_id = valid_encodings[num_valid_encoding_ids - 1] + 1; 51 ASSERT_FALSE(controller.DoesCommandBelongToEncodingMenu(one_past_largest_id)); 52 } 53 54 TEST_F(EncodingMenuControllerTest, ListEncodingMenuItems) { 55 typedef EncodingMenuController::EncodingMenuItemList EncodingMenuItemList; 56 EncodingMenuController controller; 57 58 EncodingMenuItemList english_items; 59 TestingProfile profile_en; 60 61 controller.GetEncodingMenuItems(&profile_en, &english_items); 62 63 // Make sure there are items in the lists. 64 ASSERT_FALSE(english_items.empty()); 65 // Make sure that autodetect is the first item on both menus 66 ASSERT_EQ(english_items[0].first, IDC_ENCODING_AUTO_DETECT); 67 } 68 69 TEST_F(EncodingMenuControllerTest, IsItemChecked) { 70 TestingProfile profile_en; 71 std::string encoding("UTF-8"); 72 73 // Check that enabling and disabling autodetect works. 74 bool autodetect_enabed[] = {true, false}; 75 PrefService* prefs = profile_en.GetPrefs(); 76 EncodingMenuController controller; 77 for (size_t i = 0; i < arraysize(autodetect_enabed); ++i) { 78 bool enabled = autodetect_enabed[i]; 79 prefs->SetBoolean(prefs::kWebKitUsesUniversalDetector, enabled); 80 ASSERT_TRUE(controller.IsItemChecked(&profile_en, 81 encoding, 82 IDC_ENCODING_AUTO_DETECT) == enabled); 83 } 84 85 // Check all valid encodings, make sure only one is enabled when autodetection 86 // is turned off. 87 prefs->SetBoolean(prefs::kWebKitUsesUniversalDetector, false); 88 bool encoding_is_enabled = false; 89 size_t num_valid_encoding_ids = controller.NumValidGUIEncodingIDs(); 90 const int* valid_encodings = controller.ValidGUIEncodingIDs(); 91 for (size_t i = 0; i < num_valid_encoding_ids; ++i) { 92 bool on = controller.IsItemChecked(&profile_en, 93 encoding, 94 valid_encodings[i]); 95 // Only one item in the encoding menu can be selected at a time. 96 ASSERT_FALSE(on && encoding_is_enabled); 97 encoding_is_enabled |= on; 98 } 99 100 // Make sure at least one encoding is enabled. 101 ASSERT_TRUE(encoding_is_enabled); 102 } 103