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