| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/display/display_preferences.h" | 5 #include "chrome/browser/chromeos/display/display_preferences.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "ash/content/display/screen_orientation_controller_chromeos.h" | 10 #include "ash/content/display/screen_orientation_controller_chromeos.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 | 42 |
| 43 // The mean acceleration due to gravity on Earth in m/s^2. | 43 // The mean acceleration due to gravity on Earth in m/s^2. |
| 44 const float kMeanGravity = 9.80665f; | 44 const float kMeanGravity = 9.80665f; |
| 45 | 45 |
| 46 bool IsRotationLocked() { | 46 bool IsRotationLocked() { |
| 47 return ash::Shell::GetInstance() | 47 return ash::Shell::GetInstance() |
| 48 ->screen_orientation_controller() | 48 ->screen_orientation_controller() |
| 49 ->rotation_locked(); | 49 ->rotation_locked(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 std::string ToPairString(const ash::DisplayIdPair& pair) { |
| 53 return base::Int64ToString(pair.first) + "," + |
| 54 base::Int64ToString(pair.second); |
| 55 } |
| 56 |
| 52 class DisplayPreferencesTest : public ash::test::AshTestBase { | 57 class DisplayPreferencesTest : public ash::test::AshTestBase { |
| 53 protected: | 58 protected: |
| 54 DisplayPreferencesTest() | 59 DisplayPreferencesTest() |
| 55 : mock_user_manager_(new MockUserManager), | 60 : mock_user_manager_(new MockUserManager), |
| 56 user_manager_enabler_(mock_user_manager_) { | 61 user_manager_enabler_(mock_user_manager_) { |
| 57 } | 62 } |
| 58 | 63 |
| 59 ~DisplayPreferencesTest() override {} | 64 ~DisplayPreferencesTest() override {} |
| 60 | 65 |
| 61 void SetUp() override { | 66 void SetUp() override { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 85 EXPECT_CALL(*mock_user_manager_, IsUserLoggedIn()) | 90 EXPECT_CALL(*mock_user_manager_, IsUserLoggedIn()) |
| 86 .WillRepeatedly(testing::Return(true)); | 91 .WillRepeatedly(testing::Return(true)); |
| 87 EXPECT_CALL(*mock_user_manager_, IsLoggedInAsUserWithGaiaAccount()) | 92 EXPECT_CALL(*mock_user_manager_, IsLoggedInAsUserWithGaiaAccount()) |
| 88 .WillRepeatedly(testing::Return(false)); | 93 .WillRepeatedly(testing::Return(false)); |
| 89 EXPECT_CALL(*mock_user_manager_, IsLoggedInAsSupervisedUser()) | 94 EXPECT_CALL(*mock_user_manager_, IsLoggedInAsSupervisedUser()) |
| 90 .WillRepeatedly(testing::Return(false)); | 95 .WillRepeatedly(testing::Return(false)); |
| 91 } | 96 } |
| 92 | 97 |
| 93 // Do not use the implementation of display_preferences.cc directly to avoid | 98 // Do not use the implementation of display_preferences.cc directly to avoid |
| 94 // notifying the update to the system. | 99 // notifying the update to the system. |
| 95 void StoreDisplayLayoutPrefForName(const std::string& name, | 100 void StoreDisplayLayoutPrefForPair(const ash::DisplayIdPair& pair, |
| 96 ash::DisplayLayout::Position layout, | 101 ash::DisplayLayout::Position layout, |
| 97 int offset, | 102 int offset, |
| 98 int64 primary_id) { | 103 int64 primary_id) { |
| 104 std::string name = ToPairString(pair); |
| 99 DictionaryPrefUpdate update(&local_state_, prefs::kSecondaryDisplays); | 105 DictionaryPrefUpdate update(&local_state_, prefs::kSecondaryDisplays); |
| 100 ash::DisplayLayout display_layout(layout, offset); | 106 ash::DisplayLayout display_layout(layout, offset); |
| 101 display_layout.primary_id = primary_id; | 107 display_layout.primary_id = primary_id; |
| 102 | 108 |
| 103 DCHECK(!name.empty()); | 109 DCHECK(!name.empty()); |
| 104 | 110 |
| 105 base::DictionaryValue* pref_data = update.Get(); | 111 base::DictionaryValue* pref_data = update.Get(); |
| 106 scoped_ptr<base::Value>layout_value(new base::DictionaryValue()); | 112 scoped_ptr<base::Value>layout_value(new base::DictionaryValue()); |
| 107 if (pref_data->HasKey(name)) { | 113 if (pref_data->HasKey(name)) { |
| 108 base::Value* value = NULL; | 114 base::Value* value = NULL; |
| 109 if (pref_data->Get(name, &value) && value != NULL) | 115 if (pref_data->Get(name, &value) && value != NULL) |
| 110 layout_value.reset(value->DeepCopy()); | 116 layout_value.reset(value->DeepCopy()); |
| 111 } | 117 } |
| 112 if (ash::DisplayLayout::ConvertToValue(display_layout, layout_value.get())) | 118 if (ash::DisplayLayout::ConvertToValue(display_layout, layout_value.get())) |
| 113 pref_data->Set(name, layout_value.release()); | 119 pref_data->Set(name, layout_value.release()); |
| 114 } | 120 } |
| 115 | 121 |
| 116 void StoreDisplayLayoutPrefForPair(int64 id1, | 122 void StoreDisplayPropertyForPair(const ash::DisplayIdPair& pair, |
| 117 int64 id2, | 123 std::string key, |
| 124 scoped_ptr<base::Value> value) { |
| 125 std::string name = ToPairString(pair); |
| 126 |
| 127 DictionaryPrefUpdate update(&local_state_, prefs::kSecondaryDisplays); |
| 128 base::DictionaryValue* pref_data = update.Get(); |
| 129 |
| 130 if (pref_data->HasKey(name)) { |
| 131 base::Value* layout_value = NULL; |
| 132 pref_data->Get(name, &layout_value); |
| 133 if (layout_value) |
| 134 static_cast<base::DictionaryValue*>(layout_value) |
| 135 ->Set(key, value.Pass()); |
| 136 } else { |
| 137 scoped_ptr<base::DictionaryValue> layout_value( |
| 138 new base::DictionaryValue()); |
| 139 layout_value->SetBoolean(key, value); |
| 140 pref_data->Set(name, layout_value.release()); |
| 141 } |
| 142 } |
| 143 |
| 144 void StoreDisplayBoolPropertyForPair(const ash::DisplayIdPair& pair, |
| 145 const std::string& key, |
| 146 bool value) { |
| 147 StoreDisplayPropertyForPair( |
| 148 pair, key, make_scoped_ptr(new base::FundamentalValue(value))); |
| 149 } |
| 150 |
| 151 void StoreDisplayLayoutPrefForPair(const ash::DisplayIdPair& pair, |
| 118 ash::DisplayLayout::Position layout, | 152 ash::DisplayLayout::Position layout, |
| 119 int offset) { | 153 int offset) { |
| 120 StoreDisplayLayoutPrefForName( | 154 StoreDisplayLayoutPrefForPair(pair, layout, offset, pair.first); |
| 121 base::Int64ToString(id1) + "," + base::Int64ToString(id2), | |
| 122 layout, offset, id1); | |
| 123 } | |
| 124 | |
| 125 void StoreDisplayLayoutPrefForSecondary(int64 id, | |
| 126 ash::DisplayLayout::Position layout, | |
| 127 int offset, | |
| 128 int64 primary_id) { | |
| 129 StoreDisplayLayoutPrefForName( | |
| 130 base::Int64ToString(id), layout, offset, primary_id); | |
| 131 } | 155 } |
| 132 | 156 |
| 133 void StoreDisplayOverscan(int64 id, const gfx::Insets& insets) { | 157 void StoreDisplayOverscan(int64 id, const gfx::Insets& insets) { |
| 134 DictionaryPrefUpdate update(&local_state_, prefs::kDisplayProperties); | 158 DictionaryPrefUpdate update(&local_state_, prefs::kDisplayProperties); |
| 135 const std::string name = base::Int64ToString(id); | 159 const std::string name = base::Int64ToString(id); |
| 136 | 160 |
| 137 base::DictionaryValue* pref_data = update.Get(); | 161 base::DictionaryValue* pref_data = update.Get(); |
| 138 base::DictionaryValue* insets_value = new base::DictionaryValue(); | 162 base::DictionaryValue* insets_value = new base::DictionaryValue(); |
| 139 insets_value->SetInteger("insets_top", insets.top()); | 163 insets_value->SetInteger("insets_top", insets.top()); |
| 140 insets_value->SetInteger("insets_left", insets.left()); | 164 insets_value->SetInteger("insets_left", insets.left()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 154 } | 178 } |
| 155 | 179 |
| 156 void StoreDisplayRotationPrefsForTest(bool rotation_lock, | 180 void StoreDisplayRotationPrefsForTest(bool rotation_lock, |
| 157 gfx::Display::Rotation rotation) { | 181 gfx::Display::Rotation rotation) { |
| 158 DictionaryPrefUpdate update(local_state(), prefs::kDisplayRotationLock); | 182 DictionaryPrefUpdate update(local_state(), prefs::kDisplayRotationLock); |
| 159 base::DictionaryValue* pref_data = update.Get(); | 183 base::DictionaryValue* pref_data = update.Get(); |
| 160 pref_data->SetBoolean("lock", rotation_lock); | 184 pref_data->SetBoolean("lock", rotation_lock); |
| 161 pref_data->SetInteger("orientation", static_cast<int>(rotation)); | 185 pref_data->SetInteger("orientation", static_cast<int>(rotation)); |
| 162 } | 186 } |
| 163 | 187 |
| 164 std::string GetRegisteredDisplayLayoutStr(int64 id1, int64 id2) { | 188 std::string GetRegisteredDisplayLayoutStr(const ash::DisplayIdPair& pair) { |
| 165 ash::DisplayIdPair pair; | |
| 166 pair.first = id1; | |
| 167 pair.second = id2; | |
| 168 return ash::Shell::GetInstance()->display_manager()->layout_store()-> | 189 return ash::Shell::GetInstance()->display_manager()->layout_store()-> |
| 169 GetRegisteredDisplayLayout(pair).ToString(); | 190 GetRegisteredDisplayLayout(pair).ToString(); |
| 170 } | 191 } |
| 171 | 192 |
| 172 PrefService* local_state() { return &local_state_; } | 193 PrefService* local_state() { return &local_state_; } |
| 173 | 194 |
| 174 private: | 195 private: |
| 175 MockUserManager* mock_user_manager_; // Not owned. | 196 MockUserManager* mock_user_manager_; // Not owned. |
| 176 ScopedUserManagerEnabler user_manager_enabler_; | 197 ScopedUserManagerEnabler user_manager_enabler_; |
| 177 TestingPrefServiceSimple local_state_; | 198 TestingPrefServiceSimple local_state_; |
| 178 scoped_ptr<DisplayConfigurationObserver> observer_; | 199 scoped_ptr<DisplayConfigurationObserver> observer_; |
| 179 | 200 |
| 180 DISALLOW_COPY_AND_ASSIGN(DisplayPreferencesTest); | 201 DISALLOW_COPY_AND_ASSIGN(DisplayPreferencesTest); |
| 181 }; | 202 }; |
| 182 | 203 |
| 183 } // namespace | 204 } // namespace |
| 184 | 205 |
| 185 TEST_F(DisplayPreferencesTest, PairedLayoutOverrides) { | 206 TEST_F(DisplayPreferencesTest, PairedLayoutOverrides) { |
| 186 UpdateDisplay("100x100,200x200"); | 207 UpdateDisplay("100x100,200x200"); |
| 187 int64 id1 = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().id(); | 208 ash::DisplayManager* display_manager = |
| 188 int64 id2 = ash::ScreenUtil::GetSecondaryDisplay().id(); | 209 ash::Shell::GetInstance()->display_manager(); |
| 189 int64 dummy_id = id2 + 1; | |
| 190 ASSERT_NE(id1, dummy_id); | |
| 191 | 210 |
| 192 StoreDisplayLayoutPrefForPair(id1, id2, ash::DisplayLayout::TOP, 20); | 211 ash::DisplayIdPair pair = display_manager->GetCurrentDisplayIdPair(); |
| 193 StoreDisplayLayoutPrefForPair(id1, dummy_id, ash::DisplayLayout::LEFT, 30); | 212 ash::DisplayIdPair dummy_pair = std::make_pair(pair.first, pair.second + 1); |
| 213 ASSERT_NE(pair.first, dummy_pair.second); |
| 214 |
| 215 StoreDisplayLayoutPrefForPair(pair, ash::DisplayLayout::TOP, 20); |
| 216 StoreDisplayLayoutPrefForPair(dummy_pair, ash::DisplayLayout::LEFT, 30); |
| 194 StoreDisplayPowerStateForTest( | 217 StoreDisplayPowerStateForTest( |
| 195 chromeos::DISPLAY_POWER_INTERNAL_OFF_EXTERNAL_ON); | 218 chromeos::DISPLAY_POWER_INTERNAL_OFF_EXTERNAL_ON); |
| 196 | 219 |
| 197 ash::Shell* shell = ash::Shell::GetInstance(); | 220 ash::Shell* shell = ash::Shell::GetInstance(); |
| 198 | 221 |
| 199 LoadDisplayPreferences(true); | 222 LoadDisplayPreferences(true); |
| 200 // DisplayPowerState should be ignored at boot. | 223 // DisplayPowerState should be ignored at boot. |
| 201 EXPECT_EQ(chromeos::DISPLAY_POWER_ALL_ON, | 224 EXPECT_EQ(chromeos::DISPLAY_POWER_ALL_ON, |
| 202 shell->display_configurator()->requested_power_state()); | 225 shell->display_configurator()->requested_power_state()); |
| 203 | 226 |
| 204 shell->display_manager()->UpdateDisplays(); | 227 shell->display_manager()->UpdateDisplays(); |
| 205 // Check if the layout settings are notified to the system properly. | 228 // Check if the layout settings are notified to the system properly. |
| 206 // The paired layout overrides old layout. | 229 // The paired layout overrides old layout. |
| 207 // Inverted one of for specified pair (id1, id2). Not used for the pair | 230 // Inverted one of for specified pair (id1, id2). Not used for the pair |
| 208 // (id1, dummy_id) since dummy_id is not connected right now. | 231 // (id1, dummy_id) since dummy_id is not connected right now. |
| 209 EXPECT_EQ("top, 20", | 232 EXPECT_EQ("top, 20", |
| 210 shell->display_manager()->GetCurrentDisplayLayout().ToString()); | 233 shell->display_manager()->GetCurrentDisplayLayout().ToString()); |
| 211 EXPECT_EQ("top, 20", GetRegisteredDisplayLayoutStr(id1, id2)); | 234 EXPECT_EQ("top, 20", GetRegisteredDisplayLayoutStr(pair)); |
| 212 EXPECT_EQ("left, 30", GetRegisteredDisplayLayoutStr(id1, dummy_id)); | 235 EXPECT_EQ("left, 30", GetRegisteredDisplayLayoutStr(dummy_pair)); |
| 213 } | 236 } |
| 214 | 237 |
| 215 TEST_F(DisplayPreferencesTest, BasicStores) { | 238 TEST_F(DisplayPreferencesTest, BasicStores) { |
| 216 ash::DisplayController* display_controller = | 239 ash::DisplayController* display_controller = |
| 217 ash::Shell::GetInstance()->display_controller(); | 240 ash::Shell::GetInstance()->display_controller(); |
| 218 ash::DisplayManager* display_manager = | 241 ash::DisplayManager* display_manager = |
| 219 ash::Shell::GetInstance()->display_manager(); | 242 ash::Shell::GetInstance()->display_manager(); |
| 220 | 243 |
| 221 UpdateDisplay("200x200*2, 400x300#400x400|300x200*1.25"); | 244 UpdateDisplay("200x200*2, 400x300#400x400|300x200*1.25"); |
| 222 int64 id1 = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().id(); | 245 int64 id1 = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().id(); |
| (...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 true); | 870 true); |
| 848 | 871 |
| 849 EXPECT_TRUE(local_state()->HasPrefPath(prefs::kDisplayRotationLock)); | 872 EXPECT_TRUE(local_state()->HasPrefPath(prefs::kDisplayRotationLock)); |
| 850 | 873 |
| 851 const base::DictionaryValue* properties = | 874 const base::DictionaryValue* properties = |
| 852 local_state()->GetDictionary(prefs::kDisplayRotationLock); | 875 local_state()->GetDictionary(prefs::kDisplayRotationLock); |
| 853 bool rotation_lock; | 876 bool rotation_lock; |
| 854 EXPECT_TRUE(properties->GetBoolean("lock", &rotation_lock)); | 877 EXPECT_TRUE(properties->GetBoolean("lock", &rotation_lock)); |
| 855 } | 878 } |
| 856 | 879 |
| 880 TEST_F(DisplayPreferencesTest, SaveUnifiedMode) { |
| 881 UpdateDisplay("100x100,200x200"); |
| 882 LoggedInAsUser(); |
| 883 ash::DisplayManager* display_manager = |
| 884 ash::Shell::GetInstance()->display_manager(); |
| 885 ash::DisplayIdPair pair = display_manager->GetCurrentDisplayIdPair(); |
| 886 std::string pair_key = ToPairString(pair); |
| 887 |
| 888 // Unified mode should be recorded. |
| 889 display_manager->SetDefaultMultiDisplayMode(ash::DisplayManager::UNIFIED); |
| 890 display_manager->ReconfigureDisplays(); |
| 891 |
| 892 const base::DictionaryValue* displays = |
| 893 local_state()->GetDictionary(prefs::kSecondaryDisplays); |
| 894 const base::DictionaryValue* new_value = NULL; |
| 895 EXPECT_TRUE(displays->GetDictionary(ToPairString(pair), &new_value)); |
| 896 |
| 897 ash::DisplayLayout stored_layout; |
| 898 EXPECT_TRUE(ash::DisplayLayout::ConvertFromValue(*new_value, &stored_layout)); |
| 899 EXPECT_TRUE(stored_layout.default_unified); |
| 900 EXPECT_FALSE(stored_layout.mirrored); |
| 901 |
| 902 // Mirror mode should remember if the default mode was unified. |
| 903 display_manager->SetMirrorMode(true); |
| 904 EXPECT_TRUE(displays->GetDictionary(ToPairString(pair), &new_value)); |
| 905 EXPECT_TRUE(ash::DisplayLayout::ConvertFromValue(*new_value, &stored_layout)); |
| 906 EXPECT_TRUE(stored_layout.default_unified); |
| 907 EXPECT_TRUE(stored_layout.mirrored); |
| 908 |
| 909 display_manager->SetMirrorMode(false); |
| 910 EXPECT_TRUE(displays->GetDictionary(ToPairString(pair), &new_value)); |
| 911 EXPECT_TRUE(ash::DisplayLayout::ConvertFromValue(*new_value, &stored_layout)); |
| 912 EXPECT_TRUE(stored_layout.default_unified); |
| 913 EXPECT_FALSE(stored_layout.mirrored); |
| 914 |
| 915 // Exit unified mode. |
| 916 display_manager->SetDefaultMultiDisplayMode(ash::DisplayManager::EXTENDED); |
| 917 display_manager->ReconfigureDisplays(); |
| 918 EXPECT_TRUE(displays->GetDictionary(ToPairString(pair), &new_value)); |
| 919 EXPECT_TRUE(ash::DisplayLayout::ConvertFromValue(*new_value, &stored_layout)); |
| 920 EXPECT_FALSE(stored_layout.default_unified); |
| 921 EXPECT_FALSE(stored_layout.mirrored); |
| 922 } |
| 923 |
| 924 TEST_F(DisplayPreferencesTest, RestoreUnifiedMode) { |
| 925 int64 id1 = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().id(); |
| 926 ash::DisplayIdPair pair = std::make_pair(id1, id1 + 1); |
| 927 StoreDisplayBoolPropertyForPair(pair, "default_unified", true); |
| 928 StoreDisplayPropertyForPair( |
| 929 pair, "primary-id", |
| 930 make_scoped_ptr(new base::StringValue(base::Int64ToString(id1)))); |
| 931 LoadDisplayPreferences(false); |
| 932 |
| 933 // Should not restore to unified unless unified desktop is enabled. |
| 934 UpdateDisplay("100x100,200x200"); |
| 935 ash::DisplayManager* display_manager = |
| 936 ash::Shell::GetInstance()->display_manager(); |
| 937 EXPECT_FALSE(display_manager->IsInUnifiedMode()); |
| 938 |
| 939 // Restored to unified. |
| 940 ash::test::DisplayManagerTestApi::EnableUnifiedDesktopForTest(); |
| 941 StoreDisplayBoolPropertyForPair(pair, "default_unified", true); |
| 942 LoadDisplayPreferences(false); |
| 943 UpdateDisplay("100x100,200x200"); |
| 944 EXPECT_TRUE(display_manager->IsInUnifiedMode()); |
| 945 |
| 946 // Restored to mirror, then unified. |
| 947 StoreDisplayBoolPropertyForPair(pair, "mirrored", true); |
| 948 StoreDisplayBoolPropertyForPair(pair, "default_unified", true); |
| 949 LoadDisplayPreferences(false); |
| 950 UpdateDisplay("100x100,200x200"); |
| 951 EXPECT_TRUE(display_manager->IsInMirrorMode()); |
| 952 |
| 953 display_manager->SetMirrorMode(false); |
| 954 EXPECT_TRUE(display_manager->IsInUnifiedMode()); |
| 955 |
| 956 // Sanity check. Restore to extended. |
| 957 StoreDisplayBoolPropertyForPair(pair, "default_unified", false); |
| 958 StoreDisplayBoolPropertyForPair(pair, "mirrored", false); |
| 959 LoadDisplayPreferences(false); |
| 960 UpdateDisplay("100x100,200x200"); |
| 961 EXPECT_FALSE(display_manager->IsInMirrorMode()); |
| 962 EXPECT_FALSE(display_manager->IsInUnifiedMode()); |
| 963 } |
| 964 |
| 857 } // namespace chromeos | 965 } // namespace chromeos |
| OLD | NEW |