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
3 // found in the LICENSE file.
5 #include "chrome/browser/sync/test/integration/extensions_helper.h"
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
14 #include "chrome/browser/sync/test/integration/sync_extension_helper.h"
15 #include "extensions/common/manifest.h"
17 using sync_datatype_helper::test
;
19 namespace extensions_helper
{
21 const char extension_name_prefix
[] = "fakeextension";
23 bool HasSameExtensionsAsVerifier(int index
) {
24 return SyncExtensionHelper::GetInstance()->ExtensionStatesMatch(
25 test()->GetProfile(index
), test()->verifier());
28 bool AllProfilesHaveSameExtensionsAsVerifier() {
29 for (int i
= 0; i
< test()->num_clients(); ++i
) {
30 if (!HasSameExtensionsAsVerifier(i
)) {
31 LOG(ERROR
) << "Profile " << i
<< " doesn't have the same extensions as"
32 " the verifier profile.";
39 bool AllProfilesHaveSameExtensions() {
40 for (int i
= 1; i
< test()->num_clients(); ++i
) {
41 if (!SyncExtensionHelper::GetInstance()->ExtensionStatesMatch(
42 test()->GetProfile(0), test()->GetProfile(i
))) {
43 LOG(ERROR
) << "Profile " << i
<< " doesnt have the same extensions as"
52 std::string
InstallExtension(Profile
* profile
, int index
) {
53 return SyncExtensionHelper::GetInstance()->InstallExtension(
55 CreateFakeExtensionName(index
),
56 extensions::Manifest::TYPE_EXTENSION
);
59 std::string
InstallExtensionForAllProfiles(int index
) {
60 for (int i
= 0; i
< test()->num_clients(); ++i
)
61 InstallExtension(test()->GetProfile(i
), index
);
62 return InstallExtension(test()->verifier(), index
);
65 void UninstallExtension(Profile
* profile
, int index
) {
66 return SyncExtensionHelper::GetInstance()->UninstallExtension(
67 profile
, CreateFakeExtensionName(index
));
70 std::vector
<int> GetInstalledExtensions(Profile
* profile
) {
71 std::vector
<int> indices
;
72 std::vector
<std::string
> names
=
73 SyncExtensionHelper::GetInstance()->GetInstalledExtensionNames(profile
);
74 for (std::vector
<std::string
>::const_iterator it
= names
.begin();
75 it
!= names
.end(); ++it
) {
77 if (ExtensionNameToIndex(*it
, &index
)) {
78 indices
.push_back(index
);
84 void EnableExtension(Profile
* profile
, int index
) {
85 return SyncExtensionHelper::GetInstance()->EnableExtension(
86 profile
, CreateFakeExtensionName(index
));
89 void DisableExtension(Profile
* profile
, int index
) {
90 return SyncExtensionHelper::GetInstance()->DisableExtension(
91 profile
, CreateFakeExtensionName(index
));
94 bool IsExtensionEnabled(Profile
* profile
, int index
) {
95 return SyncExtensionHelper::GetInstance()->IsExtensionEnabled(
96 profile
, CreateFakeExtensionName(index
));
99 void IncognitoEnableExtension(Profile
* profile
, int index
) {
100 return SyncExtensionHelper::GetInstance()->IncognitoEnableExtension(
101 profile
, CreateFakeExtensionName(index
));
104 void IncognitoDisableExtension(Profile
* profile
, int index
) {
105 return SyncExtensionHelper::GetInstance()->IncognitoDisableExtension(
106 profile
, CreateFakeExtensionName(index
));
109 bool IsIncognitoEnabled(Profile
* profile
, int index
) {
110 return SyncExtensionHelper::GetInstance()->IsIncognitoEnabled(
111 profile
, CreateFakeExtensionName(index
));
114 void InstallExtensionsPendingForSync(Profile
* profile
) {
115 SyncExtensionHelper::GetInstance()->InstallExtensionsPendingForSync(profile
);
118 std::string
CreateFakeExtensionName(int index
) {
119 return extension_name_prefix
+ base::IntToString(index
);
122 bool ExtensionNameToIndex(const std::string
& name
, int* index
) {
123 if (!StartsWithASCII(name
, extension_name_prefix
, true) ||
124 !base::StringToInt(name
.substr(strlen(extension_name_prefix
)), index
)) {
125 LOG(WARNING
) << "Unable to convert extension name \"" << name
132 } // namespace extensions_helper