Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/sync/test/integration/themes_helper.h" | 5 #include "chrome/browser/sync/test/integration/themes_helper.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/stringprintf.h" | |
| 11 #include "chrome/browser/chrome_notification_types.h" | |
| 12 #include "chrome/browser/sync/test/integration/status_change_checker.h" | |
| 9 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | 13 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
| 10 #include "chrome/browser/sync/test/integration/sync_extension_helper.h" | 14 #include "chrome/browser/sync/test/integration/sync_extension_helper.h" |
| 11 #include "chrome/browser/themes/theme_service.h" | 15 #include "chrome/browser/themes/theme_service.h" |
| 12 #include "chrome/browser/themes/theme_service_factory.h" | 16 #include "chrome/browser/themes/theme_service_factory.h" |
| 17 #include "content/public/browser/notification_observer.h" | |
| 18 #include "content/public/browser/notification_registrar.h" | |
| 19 #include "content/public/browser/notification_source.h" | |
| 13 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
| 14 #include "extensions/common/id_util.h" | 21 #include "extensions/common/id_util.h" |
| 15 #include "extensions/common/manifest.h" | 22 #include "extensions/common/manifest.h" |
| 16 | 23 |
| 17 using sync_datatype_helper::test; | 24 using sync_datatype_helper::test; |
| 18 | 25 |
| 19 namespace { | 26 namespace { |
| 20 | 27 |
| 21 // Make a name to pass to an extension helper. | 28 // Make a name to pass to an extension helper. |
| 22 std::string MakeName(int index) { | 29 std::string MakeName(int index) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 49 | 56 |
| 50 bool UsingSystemTheme(Profile* profile) { | 57 bool UsingSystemTheme(Profile* profile) { |
| 51 return GetThemeService(profile)->UsingSystemTheme(); | 58 return GetThemeService(profile)->UsingSystemTheme(); |
| 52 } | 59 } |
| 53 | 60 |
| 54 bool ThemeIsPendingInstall(Profile* profile, const std::string& id) { | 61 bool ThemeIsPendingInstall(Profile* profile, const std::string& id) { |
| 55 return SyncExtensionHelper::GetInstance()-> | 62 return SyncExtensionHelper::GetInstance()-> |
| 56 IsExtensionPendingInstallForSync(profile, id); | 63 IsExtensionPendingInstallForSync(profile, id); |
| 57 } | 64 } |
| 58 | 65 |
| 59 bool HasOrWillHaveCustomTheme(Profile* profile, const std::string& id) { | |
| 60 return (GetThemeID(profile) == id) || ThemeIsPendingInstall(profile, id); | |
| 61 } | |
| 62 | |
| 63 void UseCustomTheme(Profile* profile, int index) { | 66 void UseCustomTheme(Profile* profile, int index) { |
| 64 SyncExtensionHelper::GetInstance()->InstallExtension( | 67 SyncExtensionHelper::GetInstance()->InstallExtension( |
| 65 profile, MakeName(index), extensions::Manifest::TYPE_THEME); | 68 profile, MakeName(index), extensions::Manifest::TYPE_THEME); |
| 66 } | 69 } |
| 67 | 70 |
| 68 void UseDefaultTheme(Profile* profile) { | 71 void UseDefaultTheme(Profile* profile) { |
| 69 GetThemeService(profile)->UseDefaultTheme(); | 72 GetThemeService(profile)->UseDefaultTheme(); |
| 70 } | 73 } |
| 71 | 74 |
| 72 void UseSystemTheme(Profile* profile) { | 75 void UseSystemTheme(Profile* profile) { |
| 73 GetThemeService(profile)->UseSystemTheme(); | 76 GetThemeService(profile)->UseSystemTheme(); |
| 74 } | 77 } |
| 75 | 78 |
| 79 namespace { | |
| 80 | |
| 81 // Helper to wait until the specified theme is pending for install on the | |
| 82 // specified profile. | |
| 83 // | |
| 84 // The themes sync integration tests don't actually install any custom themes, | |
| 85 // but they do occasionally check that the ThemeService is attempts to install | |
|
pval...(no longer on Chromium)
2014/07/15 20:34:47
s/attempts/attempting/ ?
rlarocque
2014/07/15 20:47:01
I removed the preceding "is".
| |
| 86 // synced themes. | |
| 87 class ThemePendingInstallChecker : public StatusChangeChecker, | |
| 88 public content::NotificationObserver { | |
| 89 public: | |
| 90 ThemePendingInstallChecker(Profile* profile, const std::string& theme); | |
| 91 virtual ~ThemePendingInstallChecker(); | |
| 92 | |
| 93 // Implementation of StatusChangeChecker. | |
| 94 virtual std::string GetDebugMessage() const OVERRIDE; | |
| 95 virtual bool IsExitConditionSatisfied() OVERRIDE; | |
| 96 | |
| 97 // Implementation of content::NotificationObserver. | |
| 98 virtual void Observe(int type, | |
| 99 const content::NotificationSource& source, | |
| 100 const content::NotificationDetails& details) OVERRIDE; | |
| 101 | |
| 102 // Waits until the condition to be met or a timeout occurs. | |
| 103 void Wait(); | |
| 104 | |
| 105 private: | |
| 106 Profile* profile_; | |
| 107 const std::string& theme_; | |
| 108 | |
| 109 content::NotificationRegistrar registrar_; | |
| 110 }; | |
| 111 | |
| 112 ThemePendingInstallChecker::ThemePendingInstallChecker(Profile* profile, | |
| 113 const std::string& theme) | |
| 114 : profile_(profile), theme_(theme) { | |
| 115 } | |
| 116 | |
| 117 ThemePendingInstallChecker::~ThemePendingInstallChecker() { | |
| 118 } | |
| 119 | |
| 120 std::string ThemePendingInstallChecker::GetDebugMessage() const { | |
| 121 return base::StringPrintf("Waiting for pending theme to be '%s'", | |
| 122 theme_.c_str()); | |
| 123 } | |
| 124 | |
| 125 bool ThemePendingInstallChecker::IsExitConditionSatisfied() { | |
| 126 return ThemeIsPendingInstall(profile_, theme_); | |
| 127 } | |
| 128 | |
| 129 void ThemePendingInstallChecker::Observe( | |
| 130 int type, | |
| 131 const content::NotificationSource& source, | |
| 132 const content::NotificationDetails& details) { | |
| 133 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UPDATING_STARTED, type); | |
| 134 CheckExitCondition(); | |
| 135 } | |
| 136 | |
| 137 void ThemePendingInstallChecker::Wait() { | |
| 138 // We'll check to see if the condition is met whenever the extension system | |
| 139 // tries to contact the web store. | |
| 140 registrar_.Add(this, | |
| 141 chrome::NOTIFICATION_EXTENSION_UPDATING_STARTED, | |
| 142 content::Source<Profile>(profile_)); | |
| 143 | |
| 144 if (IsExitConditionSatisfied()) { | |
| 145 return; | |
| 146 } | |
| 147 | |
| 148 StartBlockingWait(); | |
| 149 } | |
| 150 | |
| 151 } // namespace | |
| 152 | |
| 153 bool AwaitThemeIsPendingInstall(Profile* profile, const std::string& theme) { | |
| 154 ThemePendingInstallChecker checker(profile, theme); | |
| 155 checker.Wait(); | |
| 156 return !checker.TimedOut(); | |
| 157 } | |
| 158 | |
| 159 namespace { | |
| 160 | |
| 161 // Helper to wait until a given condition is met, checking every time the | |
| 162 // current theme changes. | |
| 163 // | |
| 164 // The |exit_condition_| closure may be invoked zero or more times. | |
| 165 class ThemeConditionChecker : public StatusChangeChecker, | |
| 166 public content::NotificationObserver { | |
| 167 public: | |
| 168 ThemeConditionChecker(Profile* profile, | |
| 169 const std::string& debug_message_, | |
| 170 base::Callback<bool(ThemeService*)> exit_condition); | |
| 171 virtual ~ThemeConditionChecker(); | |
| 172 | |
| 173 // Implementation of StatusChangeChecker. | |
| 174 virtual std::string GetDebugMessage() const OVERRIDE; | |
| 175 virtual bool IsExitConditionSatisfied() OVERRIDE; | |
| 176 | |
| 177 // Implementation of content::NotificationObserver. | |
| 178 virtual void Observe(int type, | |
| 179 const content::NotificationSource& source, | |
| 180 const content::NotificationDetails& details) OVERRIDE; | |
| 181 | |
| 182 // Waits until the condition to be met or a timeout occurs. | |
| 183 void Wait(); | |
| 184 | |
| 185 private: | |
| 186 Profile* profile_; | |
| 187 const std::string debug_message_; | |
| 188 base::Callback<bool(ThemeService*)> exit_condition_; | |
| 189 | |
| 190 content::NotificationRegistrar registrar_; | |
| 191 }; | |
| 192 | |
| 193 ThemeConditionChecker::ThemeConditionChecker( | |
| 194 Profile* profile, | |
| 195 const std::string& debug_message, | |
| 196 base::Callback<bool(ThemeService*)> exit_condition) | |
| 197 : profile_(profile), | |
| 198 debug_message_(debug_message), | |
| 199 exit_condition_(exit_condition) { | |
| 200 } | |
| 201 | |
| 202 ThemeConditionChecker::~ThemeConditionChecker() { | |
| 203 } | |
| 204 | |
| 205 std::string ThemeConditionChecker::GetDebugMessage() const { | |
| 206 return debug_message_; | |
| 207 } | |
| 208 | |
| 209 bool ThemeConditionChecker::IsExitConditionSatisfied() { | |
| 210 return exit_condition_.Run(GetThemeService(profile_)); | |
| 211 } | |
| 212 | |
| 213 void ThemeConditionChecker::Observe( | |
| 214 int type, | |
| 215 const content::NotificationSource& source, | |
| 216 const content::NotificationDetails& details) { | |
| 217 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); | |
| 218 CheckExitCondition(); | |
| 219 } | |
| 220 | |
| 221 void ThemeConditionChecker::Wait() { | |
| 222 registrar_.Add(this, | |
| 223 chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | |
| 224 content::Source<ThemeService>(GetThemeService(profile_))); | |
| 225 | |
| 226 if (IsExitConditionSatisfied()) { | |
| 227 return; | |
| 228 } | |
| 229 | |
| 230 StartBlockingWait(); | |
| 231 } | |
| 232 | |
| 233 // Helper function to let us bind this functionality into a base::Callback. | |
| 234 bool UsingSystemThemeFunc(ThemeService* theme_service) { | |
| 235 return theme_service->UsingSystemTheme(); | |
| 236 } | |
| 237 | |
| 238 // Helper function to let us bind this functionality into a base::Callback. | |
| 239 bool UsingDefaultThemeFunc(ThemeService* theme_service) { | |
| 240 return theme_service->UsingDefaultTheme(); | |
| 241 } | |
| 242 | |
| 243 } // namespace | |
| 244 | |
| 245 bool AwaitUsingSystemTheme(Profile* profile) { | |
| 246 ThemeConditionChecker checker( | |
| 247 profile, | |
| 248 std::string("Waiting until profile is using system theme"), | |
| 249 base::Bind(&UsingSystemThemeFunc)); | |
| 250 VLOG(1) << "Current theme ID is: " << GetThemeID(profile); | |
|
pval...(no longer on Chromium)
2014/07/15 20:34:47
should this be *D*VLOG?
rlarocque
2014/07/15 20:47:01
Actually, I should just remove them. I was using
| |
| 251 checker.Wait(); | |
| 252 return !checker.TimedOut(); | |
| 253 } | |
| 254 | |
| 255 bool AwaitUsingDefaultTheme(Profile* profile) { | |
| 256 ThemeConditionChecker checker( | |
| 257 profile, | |
| 258 std::string("Waiting until profile is using default theme"), | |
| 259 base::Bind(&UsingDefaultThemeFunc)); | |
| 260 VLOG(1) << "Current theme ID is: " << GetThemeID(profile); | |
| 261 checker.Wait(); | |
| 262 return !checker.TimedOut(); | |
| 263 } | |
| 264 | |
| 76 } // namespace themes_helper | 265 } // namespace themes_helper |
| OLD | NEW |