PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/chrome/browser/ui/app_list/search/people/people_provider_browsertest.cc

https://gitlab.com/0072016/Facebook-SDK-
C++ | 292 lines | 248 code | 33 blank | 11 comment | 8 complexity | 547c6c3bb84b67de535b0608f458b548 MD5 | raw file
  1. // Copyright 2013 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. #include "chrome/browser/ui/app_list/search/people/people_provider.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "base/bind.h"
  10. #include "base/command_line.h"
  11. #include "base/macros.h"
  12. #include "base/run_loop.h"
  13. #include "base/strings/utf_string_conversions.h"
  14. #include "chrome/browser/profiles/profile_manager.h"
  15. #include "chrome/browser/ui/app_list/test/test_app_list_controller_delegate.h"
  16. #include "chrome/common/chrome_switches.h"
  17. #include "chrome/test/base/in_process_browser_test.h"
  18. #include "content/public/browser/browser_thread.h"
  19. #include "net/test/embedded_test_server/embedded_test_server.h"
  20. #include "net/test/embedded_test_server/http_request.h"
  21. #include "net/test/embedded_test_server/http_response.h"
  22. #include "ui/app_list/search_result.h"
  23. using content::BrowserThread;
  24. using net::test_server::BasicHttpResponse;
  25. using net::test_server::HttpRequest;
  26. using net::test_server::HttpResponse;
  27. namespace app_list {
  28. namespace test {
  29. namespace {
  30. // Mock results.
  31. const char kOneResult[] = "{"
  32. "\"items\":["
  33. "{"
  34. "\"person\" : {"
  35. "\"id\": \"1\","
  36. "\"metadata\" : {"
  37. "\"ownerId\": \"1\""
  38. "},"
  39. "\"names\" : [{"
  40. "\"displayName\": \"first person\""
  41. "}],"
  42. "\"emails\" : [{"
  43. "\"value\": \"first@person.com\""
  44. "}],"
  45. "\"images\" : [{"
  46. "\"url\": \"http://host/icon\""
  47. "}],"
  48. "\"sortKeys\" : {"
  49. "\"interactionRank\": \"0.98\""
  50. "}"
  51. "}"
  52. "}"
  53. "]}";
  54. const char kThreeValidResults[] = "{"
  55. "\"items\":["
  56. "{"
  57. "\"person\" : {"
  58. "\"id\": \"1\","
  59. "\"metadata\" : {"
  60. "\"ownerId\": \"1\""
  61. "},"
  62. "\"names\" : [{"
  63. "\"displayName\": \"first person\""
  64. "}],"
  65. "\"emails\" : [{"
  66. "\"value\": \"first@person.com\""
  67. "}],"
  68. "\"images\" : [{"
  69. "\"url\": \"http://host/icon\""
  70. "}],"
  71. "\"sortKeys\" : {"
  72. "\"interactionRank\": \"0.98\""
  73. "}"
  74. "}"
  75. "},"
  76. "{"
  77. "\"person\" : {"
  78. "\"id\": \"2\","
  79. "\"metadata\" : {"
  80. "\"ownerId\": \"37\""
  81. "},"
  82. "\"names\" : [{"
  83. "\"displayName\": \"second person\""
  84. "}],"
  85. "\"emails\" : [{"
  86. "\"value\": \"second@person.com\""
  87. "}],"
  88. "\"images\" : [{"
  89. "\"url\": \"http://host/icon\""
  90. "}],"
  91. "\"sortKeys\" : {"
  92. "\"interactionRank\": \"0.84\""
  93. "}"
  94. "}"
  95. "},"
  96. "{"
  97. "\"person\" : {"
  98. "\"id\": \"3\","
  99. "\"metadata\" : {"
  100. "\"ownerId\": \"3\""
  101. "},"
  102. "\"names\" : [{"
  103. "\"displayName\": \"third person\""
  104. "}],"
  105. "\"emails\" : [{"
  106. "\"value\": \"third@person.com\""
  107. "}],"
  108. "\"images\" : [{"
  109. "\"url\": \"http://host/icon\""
  110. "}],"
  111. "\"sortKeys\" : {"
  112. "\"interactionRank\": \"0.67\""
  113. "}"
  114. "}"
  115. "},"
  116. "{"
  117. "\"person\" : {"
  118. "\"id\": \"4\","
  119. "\"metadata\" : {"
  120. "\"ownerId\": \"4\""
  121. "},"
  122. "\"names\" : [{"
  123. "\"displayName\": \"fourth person\""
  124. "}],"
  125. "\"emails\" : [{"
  126. "\"value\": \"fourth@person.com\""
  127. "}],"
  128. "\"images\" : [{"
  129. "\"url\": \"http://host/icon\""
  130. "}],"
  131. "\"sortKeys\" : {"
  132. "\"interactionRank\": \"0.0\""
  133. "}"
  134. "}"
  135. "},"
  136. "{"
  137. "\"person\" : {"
  138. "\"id\": \"5\","
  139. "\"metadata\" : {"
  140. "\"ownerId\": \"5\""
  141. "},"
  142. "\"names\" : [{"
  143. "\"displayName\": \"fifth person\""
  144. "}],"
  145. "\"emails\" : [{"
  146. "\"value\": \"fifth@person.com\""
  147. "}],"
  148. // Images field is missing on purpose.
  149. "\"sortKeys\" : {"
  150. "\"interactionRank\": \"0.98\""
  151. "}"
  152. "}"
  153. "}"
  154. "]}";
  155. } // namespace
  156. class PeopleProviderTest : public InProcessBrowserTest {
  157. public:
  158. PeopleProviderTest() {}
  159. ~PeopleProviderTest() override {}
  160. // InProcessBrowserTest overrides:
  161. void SetUpOnMainThread() override {
  162. embedded_test_server()->RegisterRequestHandler(
  163. base::Bind(&PeopleProviderTest::HandleRequest,
  164. base::Unretained(this)));
  165. ASSERT_TRUE(embedded_test_server()->Start());
  166. people_provider_.reset(new PeopleProvider(
  167. ProfileManager::GetActiveUserProfile(), &test_controller_));
  168. people_provider_->SetupForTest(
  169. base::Bind(&PeopleProviderTest::OnSearchResultsFetched,
  170. base::Unretained(this)),
  171. embedded_test_server()->base_url());
  172. people_provider_->set_use_throttling(false);
  173. }
  174. std::string RunQuery(const std::string& query,
  175. const std::string& mock_server_response) {
  176. people_provider_->Start(false, base::UTF8ToUTF16(query));
  177. if (people_provider_->people_search_ && !mock_server_response.empty()) {
  178. mock_server_response_ = mock_server_response;
  179. DCHECK(!run_loop_);
  180. run_loop_.reset(new base::RunLoop);
  181. run_loop_->Run();
  182. run_loop_.reset();
  183. mock_server_response_.clear();
  184. }
  185. people_provider_->Stop();
  186. return GetResults();
  187. }
  188. std::string GetResults() const {
  189. std::string results;
  190. for (SearchProvider::Results::const_iterator it =
  191. people_provider_->results().begin();
  192. it != people_provider_->results().end();
  193. ++it) {
  194. if (!results.empty())
  195. results += ',';
  196. results += base::UTF16ToUTF8((*it)->title());
  197. }
  198. return results;
  199. }
  200. PeopleProvider* people_provider() { return people_provider_.get(); }
  201. private:
  202. std::unique_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
  203. std::unique_ptr<BasicHttpResponse> response(new BasicHttpResponse);
  204. response->set_code(net::HTTP_OK);
  205. response->set_content(mock_server_response_);
  206. return std::move(response);
  207. }
  208. void OnSearchResultsFetched() {
  209. if (run_loop_)
  210. run_loop_->Quit();
  211. }
  212. std::unique_ptr<base::RunLoop> run_loop_;
  213. std::string mock_server_response_;
  214. std::unique_ptr<PeopleProvider> people_provider_;
  215. ::test::TestAppListControllerDelegate test_controller_;
  216. DISALLOW_COPY_AND_ASSIGN(PeopleProviderTest);
  217. };
  218. IN_PROC_BROWSER_TEST_F(PeopleProviderTest, Basic) {
  219. struct {
  220. const char* query;
  221. const char* mock_server_response;
  222. const char* expected_results_content;
  223. } kTestCases[] = {
  224. {"first", kOneResult, "first person" },
  225. {"person", kThreeValidResults, "first person,second person,third person" },
  226. };
  227. for (size_t i = 0; i < arraysize(kTestCases); ++i) {
  228. EXPECT_EQ(kTestCases[i].expected_results_content,
  229. RunQuery(kTestCases[i].query,
  230. kTestCases[i].mock_server_response))
  231. << "Case " << i << ": q=" << kTestCases[i].query;
  232. }
  233. }
  234. IN_PROC_BROWSER_TEST_F(PeopleProviderTest, NoSearchForSensitiveData) {
  235. // None of the following input strings should be accepted because they may
  236. // contain private data.
  237. const char* inputs[] = {
  238. // file: scheme is bad.
  239. "file://filename",
  240. "FILE://filename",
  241. // URLs with usernames, ports, queries or refs are bad.
  242. "http://username:password@hostname/",
  243. "http://www.example.com:1000",
  244. "http://foo:1000",
  245. "http://hostname/?query=q",
  246. "http://hostname/path#ref",
  247. // A https URL with path is bad.
  248. "https://hostname/path",
  249. };
  250. for (size_t i = 0; i < arraysize(inputs); ++i)
  251. EXPECT_EQ("", RunQuery(inputs[i], kOneResult));
  252. }
  253. IN_PROC_BROWSER_TEST_F(PeopleProviderTest, NoSearchForShortQueries) {
  254. EXPECT_EQ("", RunQuery("f", kOneResult));
  255. EXPECT_EQ("", RunQuery("fi", kOneResult));
  256. EXPECT_EQ("first person", RunQuery("fir", kOneResult));
  257. }
  258. } // namespace test
  259. } // namespace app_list