app_source_url_recorder_test.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2018 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 "components/ukm/app_source_url_recorder.h"
  5. #include "base/test/scoped_feature_list.h"
  6. #include "base/test/task_environment.h"
  7. #include "components/ukm/test_ukm_recorder.h"
  8. #include "services/metrics/public/cpp/ukm_source.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "url/gurl.h"
  11. namespace ukm {
  12. class AppSourceUrlRecorderTest : public testing::Test {
  13. public:
  14. void SetUp() override {
  15. scoped_feature_list_.InitAndEnableFeature(kUkmAppLogging);
  16. }
  17. protected:
  18. SourceId GetSourceIdForChromeApp(const std::string& app_id) {
  19. return AppSourceUrlRecorder::GetSourceIdForChromeApp(app_id);
  20. }
  21. SourceId GetSourceIdForArcPackageName(const std::string& package_name) {
  22. return AppSourceUrlRecorder::GetSourceIdForArcPackageName(package_name);
  23. }
  24. SourceId GetSourceIdForArc(const std::string& package_name) {
  25. return AppSourceUrlRecorder::GetSourceIdForArc(package_name);
  26. }
  27. SourceId GetSourceIdForPWA(const GURL& url) {
  28. return AppSourceUrlRecorder::GetSourceIdForPWA(url);
  29. }
  30. SourceId GetSourceIdForBorealis(const std::string& app) {
  31. return AppSourceUrlRecorder::GetSourceIdForBorealis(app);
  32. }
  33. SourceId GetSourceIdForCrostini(const std::string& desktop_id,
  34. const std::string& app_name) {
  35. return AppSourceUrlRecorder::GetSourceIdForCrostini(desktop_id, app_name);
  36. }
  37. base::test::ScopedFeatureList scoped_feature_list_;
  38. base::test::TaskEnvironment task_environment_;
  39. TestAutoSetUkmRecorder test_ukm_recorder_;
  40. };
  41. TEST_F(AppSourceUrlRecorderTest, CheckChromeApp) {
  42. const std::string app_id = "unique_app_id";
  43. SourceId id = GetSourceIdForChromeApp(app_id);
  44. GURL expected_url("app://" + app_id);
  45. const auto& sources = test_ukm_recorder_.GetSources();
  46. ASSERT_EQ(1ul, sources.size());
  47. ASSERT_NE(kInvalidSourceId, id);
  48. auto it = sources.find(id);
  49. ASSERT_NE(sources.end(), it);
  50. EXPECT_EQ(expected_url, it->second->url());
  51. EXPECT_EQ(1u, it->second->urls().size());
  52. }
  53. TEST_F(AppSourceUrlRecorderTest, CheckArcPackageName) {
  54. const std::string package_name = "com.google.play";
  55. SourceId id = GetSourceIdForArcPackageName(package_name);
  56. GURL expected_url("app://" + package_name);
  57. const auto& sources = test_ukm_recorder_.GetSources();
  58. ASSERT_EQ(1ul, sources.size());
  59. ASSERT_NE(kInvalidSourceId, id);
  60. auto it = sources.find(id);
  61. ASSERT_NE(sources.end(), it);
  62. EXPECT_EQ(expected_url, it->second->url());
  63. EXPECT_EQ(1u, it->second->urls().size());
  64. }
  65. TEST_F(AppSourceUrlRecorderTest, CheckArc) {
  66. SourceId id = GetSourceIdForArc("com.google.play");
  67. std::string com_google_play_hash("pjhgmeephkiehhlkfcoginnkbphkdang");
  68. GURL expected_url("app://play/" + com_google_play_hash);
  69. const auto& sources = test_ukm_recorder_.GetSources();
  70. ASSERT_EQ(1ul, sources.size());
  71. ASSERT_NE(kInvalidSourceId, id);
  72. auto it = sources.find(id);
  73. ASSERT_NE(sources.end(), it);
  74. EXPECT_EQ(expected_url, it->second->url());
  75. EXPECT_EQ(1u, it->second->urls().size());
  76. }
  77. TEST_F(AppSourceUrlRecorderTest, CheckPWA) {
  78. GURL url("https://pwa_example_url.com");
  79. SourceId id = GetSourceIdForPWA(url);
  80. const auto& sources = test_ukm_recorder_.GetSources();
  81. ASSERT_EQ(1ul, sources.size());
  82. ASSERT_NE(kInvalidSourceId, id);
  83. auto it = sources.find(id);
  84. ASSERT_NE(sources.end(), it);
  85. EXPECT_EQ(url, it->second->url());
  86. EXPECT_EQ(1u, it->second->urls().size());
  87. }
  88. TEST_F(AppSourceUrlRecorderTest, CheckBorealis) {
  89. GURL expected_url("app://borealis/123");
  90. SourceId id = GetSourceIdForBorealis("123");
  91. const auto& sources = test_ukm_recorder_.GetSources();
  92. ASSERT_EQ(1ul, sources.size());
  93. ASSERT_NE(kInvalidSourceId, id);
  94. auto it = sources.find(id);
  95. ASSERT_NE(sources.end(), it);
  96. EXPECT_EQ(expected_url, it->second->url());
  97. EXPECT_EQ(1u, it->second->urls().size());
  98. }
  99. TEST_F(AppSourceUrlRecorderTest, CheckCrostini) {
  100. // Typically a desktop ID won't use much besides [a-zA-Z0-9.-] but it's
  101. // untrusted user-supplied data so make sure it's all escaped anyway.
  102. std::string desktop_id("I-💖.unicode!\nUnd der Eisbär?");
  103. GURL expected_url("app://I-💖.unicode!\nUnd der Eisbär?/Name");
  104. SourceId id = GetSourceIdForCrostini(desktop_id, "Name");
  105. const auto& sources = test_ukm_recorder_.GetSources();
  106. ASSERT_EQ(1ul, sources.size());
  107. ASSERT_NE(kInvalidSourceId, id);
  108. auto it = sources.find(id);
  109. ASSERT_NE(sources.end(), it);
  110. EXPECT_EQ(expected_url, it->second->url());
  111. EXPECT_EQ(1u, it->second->urls().size());
  112. }
  113. } // namespace ukm