ambient_photo_cache_unittest.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2020 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 "ash/ambient/ambient_photo_cache.h"
  5. #include <fstream>
  6. #include <iostream>
  7. #include "ash/ambient/ambient_access_token_controller.h"
  8. #include "ash/ambient/ambient_constants.h"
  9. #include "ash/ambient/test/ambient_ash_test_helper.h"
  10. #include "ash/ambient/test/test_ambient_client.h"
  11. #include "ash/constants/ash_features.h"
  12. #include "ash/public/cpp/ambient/proto/photo_cache_entry.pb.h"
  13. #include "base/bind.h"
  14. #include "base/files/file_path.h"
  15. #include "base/files/file_util.h"
  16. #include "base/path_service.h"
  17. #include "base/run_loop.h"
  18. #include "base/test/bind.h"
  19. #include "base/test/scoped_feature_list.h"
  20. #include "base/test/task_environment.h"
  21. #include "services/network/public/cpp/shared_url_loader_factory.h"
  22. #include "services/network/test/test_url_loader_factory.h"
  23. #include "testing/gtest/include/gtest/gtest.h"
  24. namespace ash {
  25. namespace {
  26. base::FilePath GetTestPath() {
  27. base::FilePath path;
  28. EXPECT_TRUE(base::PathService::Get(base::DIR_TEMP, &path));
  29. path = path.Append(FILE_PATH_LITERAL(kAmbientModeDirectoryName));
  30. return path;
  31. }
  32. } // namespace
  33. class AmbientPhotoCacheTest : public testing::Test {
  34. public:
  35. AmbientPhotoCacheTest() = default;
  36. void SetUp() override {
  37. ambient_ash_test_helper_ = std::make_unique<AmbientAshTestHelper>();
  38. access_token_controller_ = std::make_unique<AmbientAccessTokenController>();
  39. auto test_path = GetTestPath();
  40. base::DeletePathRecursively(test_path);
  41. photo_cache_ = AmbientPhotoCache::Create(
  42. test_path, ambient_ash_test_helper_->ambient_client(),
  43. *access_token_controller_);
  44. }
  45. void TearDown() override { base::DeletePathRecursively(GetTestPath()); }
  46. AmbientPhotoCache* photo_cache() { return photo_cache_.get(); }
  47. network::TestURLLoaderFactory& test_url_loader_factory() {
  48. return ambient_ash_test_helper_->ambient_client().test_url_loader_factory();
  49. }
  50. bool IsAccessTokenRequestPending() {
  51. return ambient_ash_test_helper_->ambient_client()
  52. .IsAccessTokenRequestPending();
  53. }
  54. void IssueAccessToken() {
  55. ambient_ash_test_helper_->ambient_client().IssueAccessToken(
  56. /*is_empty=*/false);
  57. }
  58. void RunUntilIdle() { task_environment_.RunUntilIdle(); }
  59. private:
  60. base::test::TaskEnvironment task_environment_;
  61. std::unique_ptr<AmbientAshTestHelper> ambient_ash_test_helper_;
  62. std::unique_ptr<AmbientAccessTokenController> access_token_controller_;
  63. std::unique_ptr<AmbientPhotoCache> photo_cache_;
  64. };
  65. TEST_F(AmbientPhotoCacheTest, ReadsBackWrittenFiles) {
  66. int cache_index = 0;
  67. std::string image("image");
  68. std::string details("details");
  69. std::string related_image("related image");
  70. std::string related_details("related details");
  71. bool is_portrait = true;
  72. {
  73. ambient::PhotoCacheEntry cache;
  74. cache.mutable_primary_photo()->set_image(image);
  75. cache.mutable_primary_photo()->set_details(details);
  76. cache.mutable_primary_photo()->set_is_portrait(is_portrait);
  77. cache.mutable_related_photo()->set_image(related_image);
  78. cache.mutable_related_photo()->set_details(related_details);
  79. cache.mutable_related_photo()->set_is_portrait(is_portrait);
  80. base::RunLoop loop;
  81. photo_cache()->WritePhotoCache(cache_index, cache, loop.QuitClosure());
  82. loop.Run();
  83. }
  84. {
  85. base::RunLoop loop;
  86. // Read the files back using photo cache.
  87. ambient::PhotoCacheEntry cache_read;
  88. photo_cache()->ReadPhotoCache(
  89. cache_index,
  90. base::BindLambdaForTesting(
  91. [&cache_read, &loop](ambient::PhotoCacheEntry cache_entry_in) {
  92. cache_read = std::move(cache_entry_in);
  93. loop.Quit();
  94. }));
  95. loop.Run();
  96. EXPECT_EQ(cache_read.primary_photo().image(), "image");
  97. EXPECT_EQ(cache_read.primary_photo().details(), "details");
  98. EXPECT_TRUE(cache_read.primary_photo().is_portrait());
  99. EXPECT_EQ(cache_read.related_photo().image(), "related image");
  100. EXPECT_EQ(cache_read.related_photo().details(), "related details");
  101. EXPECT_TRUE(cache_read.related_photo().is_portrait());
  102. }
  103. }
  104. TEST_F(AmbientPhotoCacheTest, SetsDataToEmptyStringWhenFilesMissing) {
  105. base::FilePath test_path = GetTestPath();
  106. EXPECT_FALSE(base::DirectoryExists(test_path));
  107. {
  108. base::RunLoop loop;
  109. ambient::PhotoCacheEntry cache_read;
  110. photo_cache()->ReadPhotoCache(
  111. /*cache_index=*/1,
  112. base::BindLambdaForTesting(
  113. [&cache_read, &loop](ambient::PhotoCacheEntry cache_entry_in) {
  114. cache_read = std::move(cache_entry_in);
  115. loop.Quit();
  116. }));
  117. loop.Run();
  118. EXPECT_TRUE(cache_read.primary_photo().image().empty());
  119. EXPECT_TRUE(cache_read.primary_photo().details().empty());
  120. EXPECT_FALSE(cache_read.primary_photo().is_portrait());
  121. EXPECT_TRUE(cache_read.related_photo().image().empty());
  122. EXPECT_TRUE(cache_read.related_photo().details().empty());
  123. EXPECT_FALSE(cache_read.related_photo().is_portrait());
  124. }
  125. }
  126. TEST_F(AmbientPhotoCacheTest, AttachTokenToDownloadRequest) {
  127. std::string fake_url = "https://faketesturl/";
  128. photo_cache()->DownloadPhoto(fake_url, base::BindOnce([](std::string&&) {}));
  129. RunUntilIdle();
  130. EXPECT_TRUE(IsAccessTokenRequestPending());
  131. IssueAccessToken();
  132. EXPECT_FALSE(IsAccessTokenRequestPending());
  133. auto* pending_requests = test_url_loader_factory().pending_requests();
  134. EXPECT_EQ(pending_requests->size(), std::size_t{1});
  135. EXPECT_EQ(pending_requests->at(0).request.url, fake_url);
  136. std::string header;
  137. pending_requests->at(0).request.headers.GetHeader("Authorization", &header);
  138. EXPECT_EQ(header,
  139. std::string("Bearer ") + TestAmbientClient::kTestAccessToken);
  140. }
  141. TEST_F(AmbientPhotoCacheTest, AttachTokenToDownloadToFileRequest) {
  142. std::string fake_url = "https://faketesturl/";
  143. photo_cache()->DownloadPhotoToFile(fake_url, /*cache_index=*/1,
  144. base::BindOnce([](bool) {}));
  145. RunUntilIdle();
  146. EXPECT_TRUE(IsAccessTokenRequestPending());
  147. IssueAccessToken();
  148. EXPECT_FALSE(IsAccessTokenRequestPending());
  149. auto* pending_requests = test_url_loader_factory().pending_requests();
  150. EXPECT_EQ(pending_requests->size(), std::size_t{1});
  151. EXPECT_EQ(pending_requests->at(0).request.url, fake_url);
  152. std::string header;
  153. pending_requests->at(0).request.headers.GetHeader("Authorization", &header);
  154. EXPECT_EQ(header,
  155. std::string("Bearer ") + TestAmbientClient::kTestAccessToken);
  156. }
  157. } // namespace ash