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