1 // Copyright 2014 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 "base/metrics/field_trial.h"
6 #include "base/prefs/pref_service.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/search/hotword_service.h"
9 #include "chrome/browser/search/hotword_service_factory.h"
10 #include "chrome/common/pref_names.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 class HotwordServiceTest : public testing::Test {
16 HotwordServiceTest() : field_trial_list_(NULL) {}
17 virtual ~HotwordServiceTest() {}
19 void SetApplicationLocale(Profile* profile, const std::string& new_locale) {
20 #if defined(OS_CHROMEOS)
21 // On ChromeOS locale is per-profile.
22 profile->GetPrefs()->SetString(prefs::kApplicationLocale, new_locale);
24 g_browser_process->SetApplicationLocale(new_locale);
29 base::FieldTrialList field_trial_list_;
32 TEST_F(HotwordServiceTest, ShouldShowOptInPopup) {
33 TestingProfile::Builder profile_builder;
34 TestingProfile::Builder otr_profile_builder;
35 otr_profile_builder.SetIncognito();
36 scoped_ptr<TestingProfile> profile = profile_builder.Build();
37 scoped_ptr<TestingProfile> otr_profile = otr_profile_builder.Build();
39 // Popup should not be shown for incognito profiles.
40 EXPECT_TRUE(otr_profile.get() != NULL);
41 EXPECT_FALSE(HotwordServiceFactory::ShouldShowOptInPopup(otr_profile.get()));
43 HotwordServiceFactory* hotword_service_factory =
44 HotwordServiceFactory::GetInstance();
45 HotwordService* hotword_service =
46 hotword_service_factory->GetForProfile(profile.get());
47 EXPECT_TRUE(hotword_service != NULL);
49 // Popup should not be shown if hotword search has been enabled or turned off.
50 // Test both paths for accessing whether to do the popup. Just in case.
51 profile->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, true);
52 EXPECT_FALSE(HotwordServiceFactory::ShouldShowOptInPopup(profile.get()));
53 EXPECT_FALSE(hotword_service->ShouldShowOptInPopup());
55 profile->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, false);
56 EXPECT_FALSE(HotwordServiceFactory::ShouldShowOptInPopup(profile.get()));
57 EXPECT_FALSE(hotword_service->ShouldShowOptInPopup());
59 // Rest the enabled pref for the next part of the test.
60 profile->GetPrefs()->ClearPref(prefs::kHotwordSearchEnabled);
62 // Popup should be shown until max number of times are reached.
63 for (int i = 0; i < hotword_service->MaxNumberTimesToShowOptInPopup(); i++) {
64 EXPECT_TRUE(HotwordServiceFactory::ShouldShowOptInPopup(profile.get()));
65 EXPECT_TRUE(hotword_service->ShouldShowOptInPopup());
66 hotword_service->ShowOptInPopup();
69 // The opt in popup should not be shown if it has already been shown the
70 // maximum number of times.
71 EXPECT_FALSE(HotwordServiceFactory::ShouldShowOptInPopup(profile.get()));
72 EXPECT_FALSE(hotword_service->ShouldShowOptInPopup());
76 TEST_F(HotwordServiceTest, IsHotwordAllowedBadFieldTrial) {
77 TestingProfile::Builder profile_builder;
78 TestingProfile::Builder otr_profile_builder;
79 otr_profile_builder.SetIncognito();
80 scoped_ptr<TestingProfile> profile = profile_builder.Build();
81 scoped_ptr<TestingProfile> otr_profile = otr_profile_builder.Build();
83 HotwordServiceFactory* hotword_service_factory =
84 HotwordServiceFactory::GetInstance();
86 // Check that the service exists so that a NULL service be ruled out in
88 HotwordService* hotword_service =
89 hotword_service_factory->GetForProfile(profile.get());
90 EXPECT_TRUE(hotword_service != NULL);
92 // When the field trial is empty or Disabled, it should not be allowed.
93 std::string group = base::FieldTrialList::FindFullName(
94 hotword_internal::kHotwordFieldTrialName);
95 EXPECT_TRUE(group.empty());
96 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
98 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
99 hotword_internal::kHotwordFieldTrialName,
100 hotword_internal::kHotwordFieldTrialDisabledGroupName));
101 group = base::FieldTrialList::FindFullName(
102 hotword_internal::kHotwordFieldTrialName);
103 EXPECT_TRUE(group ==hotword_internal::kHotwordFieldTrialDisabledGroupName);
104 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
106 // Set a valid locale with invalid field trial to be sure it is
108 SetApplicationLocale(static_cast<Profile*>(profile.get()), "en");
109 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
111 // Test that incognito returns false as well.
112 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(otr_profile.get()));
115 TEST_F(HotwordServiceTest, IsHotwordAllowedLocale) {
116 TestingProfile::Builder profile_builder;
117 TestingProfile::Builder otr_profile_builder;
118 otr_profile_builder.SetIncognito();
119 scoped_ptr<TestingProfile> profile = profile_builder.Build();
120 scoped_ptr<TestingProfile> otr_profile = otr_profile_builder.Build();
122 HotwordServiceFactory* hotword_service_factory =
123 HotwordServiceFactory::GetInstance();
125 // Check that the service exists so that a NULL service be ruled out in
127 HotwordService* hotword_service =
128 hotword_service_factory->GetForProfile(profile.get());
129 EXPECT_TRUE(hotword_service != NULL);
131 // Set the field trial to a valid one.
132 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
133 hotword_internal::kHotwordFieldTrialName, "Good"));
135 // Set the language to an invalid one.
136 SetApplicationLocale(static_cast<Profile*>(profile.get()), "non-english");
137 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
139 // Now with valid locales it should be fine.
140 SetApplicationLocale(static_cast<Profile*>(profile.get()), "en");
141 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
142 SetApplicationLocale(static_cast<Profile*>(profile.get()), "en-US");
143 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
144 SetApplicationLocale(static_cast<Profile*>(profile.get()), "en_us");
145 EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
147 // Test that incognito even with a valid locale and valid field trial
148 // still returns false.
149 SetApplicationLocale(static_cast<Profile*>(otr_profile.get()), "en");
150 EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(otr_profile.get()));