1 // Copyright 2016 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/webui/settings/profile_info_handler.h"
6 
7 #include <memory>
8 
9 #include "base/memory/ptr_util.h"
10 #include "base/values.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "chrome/test/base/testing_profile_manager.h"
15 #include "components/prefs/scoped_user_pref_update.h"
16 #include "content/public/browser/web_ui_data_source.h"
17 #include "content/public/test/browser_task_environment.h"
18 #include "content/public/test/test_web_ui.h"
19 #include "net/base/data_url.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "ui/gfx/codec/png_codec.h"
23 #include "url/gurl.h"
24 
25 #if defined(OS_CHROMEOS)
26 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
27 #include "components/user_manager/scoped_user_manager.h"
28 #endif
29 
30 namespace settings {
31 
32 namespace {
33 
34 #if defined(OS_CHROMEOS)
35 constexpr char fake_id[] = "fake_id";
36 constexpr char fake_email[] = "fake_id@gmail.com";
37 #endif
38 
39 class TestProfileInfoHandler : public ProfileInfoHandler {
40  public:
TestProfileInfoHandler(Profile * profile)41   explicit TestProfileInfoHandler(Profile* profile)
42       : ProfileInfoHandler(profile) {}
43 
44   using ProfileInfoHandler::set_web_ui;
45 };
46 
47 }  // namespace
48 
49 class ProfileInfoHandlerTest : public testing::Test {
50  public:
ProfileInfoHandlerTest()51   ProfileInfoHandlerTest()
52       : profile_manager_(TestingBrowserProcess::GetGlobal()),
53         profile_(nullptr) {}
54 
SetUp()55   void SetUp() override {
56     ASSERT_TRUE(profile_manager_.SetUp());
57 
58 #if defined(OS_CHROMEOS)
59     chromeos::FakeChromeUserManager* fake_user_manager =
60         new chromeos::FakeChromeUserManager;
61     user_manager_enabler_ = std::make_unique<user_manager::ScopedUserManager>(
62         base::WrapUnique(fake_user_manager));
63     profile_ = profile_manager_.CreateTestingProfile(fake_email);
64     fake_user_manager->AddUser(AccountId::FromUserEmail(fake_email));
65 #else
66     profile_ = profile_manager_.CreateTestingProfile("Profile 1");
67 #endif
68 
69     handler_ = std::make_unique<TestProfileInfoHandler>(profile_);
70     handler_->set_web_ui(&web_ui_);
71   }
72 
VerifyProfileInfo(const base::Value * call_argument)73   void VerifyProfileInfo(const base::Value* call_argument) {
74     const base::DictionaryValue* response = nullptr;
75     ASSERT_TRUE(call_argument->GetAsDictionary(&response));
76 
77     std::string name;
78     std::string icon_url;
79     ASSERT_TRUE(response->GetString("name", &name));
80     ASSERT_TRUE(response->GetString("iconUrl", &icon_url));
81 
82 #if defined(OS_CHROMEOS)
83     EXPECT_EQ(fake_id, name);
84     EXPECT_FALSE(icon_url.empty());
85 #else
86     EXPECT_EQ("Profile 1", name);
87 
88     std::string mime, charset, data;
89     EXPECT_TRUE(net::DataURL::Parse(GURL(icon_url), &mime, &charset, &data));
90 
91     EXPECT_EQ("image/png", mime);
92     SkBitmap bitmap;
93     EXPECT_TRUE(gfx::PNGCodec::Decode(
94         reinterpret_cast<const unsigned char*>(data.data()), data.size(),
95         &bitmap));
96 #endif
97   }
98 
web_ui()99   content::TestWebUI* web_ui() { return &web_ui_; }
profile() const100   Profile* profile() const { return profile_; }
handler() const101   TestProfileInfoHandler* handler() const { return handler_.get(); }
102 
103  private:
104   content::BrowserTaskEnvironment task_environment_;
105   TestingProfileManager profile_manager_;
106   content::TestWebUI web_ui_;
107 
108 #if defined(OS_CHROMEOS)
109   std::unique_ptr<user_manager::ScopedUserManager> user_manager_enabler_;
110 #endif
111 
112   Profile* profile_;
113   std::unique_ptr<TestProfileInfoHandler> handler_;
114 };
115 
TEST_F(ProfileInfoHandlerTest,GetProfileInfo)116 TEST_F(ProfileInfoHandlerTest, GetProfileInfo) {
117   base::ListValue list_args;
118   list_args.AppendString("get-profile-info-callback-id");
119   handler()->HandleGetProfileInfo(&list_args);
120 
121   EXPECT_EQ(1U, web_ui()->call_data().size());
122 
123   const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
124   EXPECT_EQ("cr.webUIResponse", data.function_name());
125 
126   std::string callback_id;
127   ASSERT_TRUE(data.arg1()->GetAsString(&callback_id));
128   EXPECT_EQ("get-profile-info-callback-id", callback_id);
129 
130   bool success = false;
131   ASSERT_TRUE(data.arg2()->GetAsBoolean(&success));
132   EXPECT_TRUE(success);
133 
134   VerifyProfileInfo(data.arg3());
135 }
136 
TEST_F(ProfileInfoHandlerTest,PushProfileInfo)137 TEST_F(ProfileInfoHandlerTest, PushProfileInfo) {
138   handler()->AllowJavascript();
139 
140   handler()->OnProfileAvatarChanged(base::FilePath());
141 
142   EXPECT_EQ(1U, web_ui()->call_data().size());
143 
144   const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
145   EXPECT_EQ("cr.webUIListenerCallback", data.function_name());
146 
147   std::string event_id;
148   ASSERT_TRUE(data.arg1()->GetAsString(&event_id));
149   EXPECT_EQ(ProfileInfoHandler::kProfileInfoChangedEventName, event_id);
150 
151   VerifyProfileInfo(data.arg2());
152 }
153 
154 }  // namespace settings
155