Chromium Code Reviews| Index: chrome/browser/webshare/share_target_pref_helper_unittest.cc |
| diff --git a/chrome/browser/webshare/share_target_pref_helper_unittest.cc b/chrome/browser/webshare/share_target_pref_helper_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..eb6da8276486575d8752229c9a43bd6745e0f1db |
| --- /dev/null |
| +++ b/chrome/browser/webshare/share_target_pref_helper_unittest.cc |
| @@ -0,0 +1,215 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/strings/string16.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/webshare/share_target_pref_helper.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "components/prefs/pref_registry_simple.h" |
| +#include "components/prefs/testing_pref_service.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +class PrefRegistrySimple; |
| + |
|
Matt Giuca
2017/01/24 03:26:09
I think this entire file (except for the forward d
constantina
2017/01/24 23:39:05
Done.
|
| +class ShareTargetPrefHelperUnittest : public testing::Test { |
| + protected: |
| + ShareTargetPrefHelperUnittest() {} |
| + ~ShareTargetPrefHelperUnittest() override {} |
| + |
| + void SetUp() override { |
| + pref_service.registry()->RegisterDictionaryPref( |
|
Matt Giuca
2017/01/24 03:26:10
This will need a corresponding TearDown which rese
constantina
2017/01/24 23:39:05
As in, an UnregisterPref? I can't find anything th
Matt Giuca
2017/01/25 00:25:53
Acknowledged.
|
| + prefs::kWebShareVisitedTargets); |
| + |
| + share_target_dict = nullptr; |
| + share_target_info_dict = nullptr; |
| + } |
| + |
| + TestingPrefServiceSimple pref_service; |
|
Matt Giuca
2017/01/24 03:26:10
These should follow standard style rules: fields s
constantina
2017/01/24 23:39:05
Done.
|
| + std::string share_target_origin; |
|
Matt Giuca
2017/01/24 03:26:10
These four (share_target_origin .. share_target_in
constantina
2017/01/24 23:39:05
Done.
|
| + base::Optional<std::string> expected_url_template; |
| + const base::DictionaryValue* share_target_dict; |
| + const base::DictionaryValue* share_target_info_dict; |
| +}; |
| + |
| +constexpr char kUrlTemplateKey[] = "url_template"; |
| + |
| +TEST_F(ShareTargetPrefHelperUnittest, AddMultipleShareTargets) { |
|
Matt Giuca
2017/01/24 03:26:10
Good, the tests feel much better split up.
constantina
2017/01/24 23:39:05
:D
|
| + // Add a share target to prefs that wasn't previously stored. |
| + { |
|
Matt Giuca
2017/01/24 03:26:10
I don't think these braces are really warranted. T
constantina
2017/01/24 23:39:05
Done.
|
| + share_target_origin = "https://www.sharetarget.com/"; |
| + expected_url_template = base::Optional<std::string>("share/?title={title}"); |
|
Matt Giuca
2017/01/24 03:26:10
This shouldn't be called "expected_url_template" b
constantina
2017/01/24 23:39:05
There's already a url_template, which you said to
Matt Giuca
2017/01/25 00:25:53
Oh I see what I did there.
Yeah I think it makes
constantina
2017/01/25 03:18:27
Done.
|
| + |
| + webshare::AddShareTargetToPrefs( |
| + share_target_origin, expected_url_template, &pref_service); |
| + |
| + share_target_dict = |
| + pref_service.GetDictionary(prefs::kWebShareVisitedTargets); |
| + EXPECT_EQ(1UL, share_target_dict->size()); |
| + ASSERT_TRUE(share_target_dict->GetDictionaryWithoutPathExpansion( |
| + share_target_origin, &share_target_info_dict)); |
| + EXPECT_EQ(1UL, share_target_info_dict->size()); |
| + std::string url_template; |
| + EXPECT_TRUE(share_target_info_dict->GetString(kUrlTemplateKey, |
| + &url_template)); |
| + EXPECT_EQ(url_template, expected_url_template); |
| + } |
| + |
| + // Add second share target to prefs that wasn't previously stored. |
| + { |
| + share_target_origin = "https://www.sharetarget2.com/"; |
| + expected_url_template = base::Optional<std::string>("share/?title={title}"); |
| + |
| + webshare::AddShareTargetToPrefs(share_target_origin, expected_url_template, |
| + &pref_service); |
| + |
| + share_target_dict = |
| + pref_service.GetDictionary(prefs::kWebShareVisitedTargets); |
| + EXPECT_EQ(2UL, share_target_dict->size()); |
| + ASSERT_TRUE(share_target_dict->GetDictionaryWithoutPathExpansion( |
| + share_target_origin, &share_target_info_dict)); |
| + EXPECT_EQ(1UL, share_target_info_dict->size()); |
| + std::string url_template; |
| + EXPECT_TRUE(share_target_info_dict->GetString(kUrlTemplateKey, |
| + &url_template)); |
| + EXPECT_EQ(url_template, expected_url_template); |
| + } |
| +} |
| + |
| +TEST_F(ShareTargetPrefHelperUnittest, AddShareTargetTwice) { |
|
Matt Giuca
2017/01/24 03:26:10
I think this test can be removed ... it is essenti
constantina
2017/01/24 23:39:05
One adds the same share target with the same url_t
|
| + share_target_origin = "https://www.sharetarget.com/"; |
| + expected_url_template = base::Optional<std::string>("share/?title={title}"); |
| + |
| + // Add a share target to prefs that wasn't previously stored. |
| + { |
| + webshare::AddShareTargetToPrefs( |
| + share_target_origin, expected_url_template, &pref_service); |
| + |
| + share_target_dict = |
| + pref_service.GetDictionary(prefs::kWebShareVisitedTargets); |
| + EXPECT_EQ(1UL, share_target_dict->size()); |
| + ASSERT_TRUE(share_target_dict->GetDictionaryWithoutPathExpansion( |
| + share_target_origin, &share_target_info_dict)); |
| + EXPECT_EQ(1UL, share_target_info_dict->size()); |
| + std::string url_template; |
| + EXPECT_TRUE(share_target_info_dict->GetString(kUrlTemplateKey, |
| + &url_template)); |
| + EXPECT_EQ(url_template, expected_url_template); |
| + } |
| + |
| + // Add same share target to prefs that was previously stored; shouldn't |
| + // duplicate it. |
| + { |
| + webshare::AddShareTargetToPrefs( |
| + share_target_origin, expected_url_template, &pref_service); |
| + |
| + share_target_dict = |
| + pref_service.GetDictionary(prefs::kWebShareVisitedTargets); |
| + EXPECT_EQ(1UL, share_target_dict->size()); |
| + ASSERT_TRUE(share_target_dict->GetDictionaryWithoutPathExpansion( |
| + share_target_origin, &share_target_info_dict)); |
| + EXPECT_EQ(1UL, share_target_info_dict->size()); |
| + std::string url_template; |
| + EXPECT_TRUE(share_target_info_dict->GetString(kUrlTemplateKey, |
| + &url_template)); |
| + EXPECT_EQ(url_template, expected_url_template); |
| + } |
| +} |
| + |
| +TEST_F(ShareTargetPrefHelperUnittest, DontAddNonShareTarget) { |
|
Matt Giuca
2017/01/24 03:26:10
Move this down right before RemoveShareTarget....
constantina
2017/01/24 23:39:05
Done.
|
| + // Don't add a site that has a null template. |
| + { |
|
Matt Giuca
2017/01/24 03:26:10
Don't need the extra braces for a test that only h
constantina
2017/01/24 23:39:05
Done.
|
| + share_target_origin = "https://www.dudsharetarget.com/"; |
| + expected_url_template = base::nullopt; |
| + |
| + webshare::AddShareTargetToPrefs( |
| + share_target_origin, expected_url_template, &pref_service); |
| + |
| + share_target_dict = |
| + pref_service.GetDictionary(prefs::kWebShareVisitedTargets); |
| + EXPECT_EQ(0UL, share_target_dict->size()); |
| + ASSERT_FALSE(share_target_dict->GetDictionaryWithoutPathExpansion( |
| + share_target_origin, &share_target_info_dict)); |
| + } |
| +} |
| + |
| +TEST_F(ShareTargetPrefHelperUnittest, UpdateShareTarget) { |
| + // Add a share target to prefs that wasn't previously stored. |
| + { |
| + share_target_origin = "https://www.sharetarget.com/"; |
| + expected_url_template = base::Optional<std::string>("share/?title={title}"); |
| + |
| + webshare::AddShareTargetToPrefs( |
| + share_target_origin, expected_url_template, &pref_service); |
| + |
| + share_target_dict = |
| + pref_service.GetDictionary(prefs::kWebShareVisitedTargets); |
| + EXPECT_EQ(1UL, share_target_dict->size()); |
| + ASSERT_TRUE(share_target_dict->GetDictionaryWithoutPathExpansion( |
| + share_target_origin, &share_target_info_dict)); |
| + EXPECT_EQ(1UL, share_target_info_dict->size()); |
| + std::string url_template; |
| + EXPECT_TRUE(share_target_info_dict->GetString(kUrlTemplateKey, |
| + &url_template)); |
| + EXPECT_EQ(url_template, expected_url_template); |
| + } |
| + |
| + // Add same share target to prefs that was previously stored, with new |
| + // url_template; should update the value. |
| + { |
| + share_target_origin = "https://www.sharetarget.com/"; |
| + expected_url_template = |
| + base::Optional<std::string>("share/?title={title}&text={text}"); |
| + |
| + webshare::AddShareTargetToPrefs( |
| + share_target_origin, expected_url_template, &pref_service); |
| + |
| + share_target_dict = |
| + pref_service.GetDictionary(prefs::kWebShareVisitedTargets); |
| + EXPECT_EQ(1UL, share_target_dict->size()); |
| + ASSERT_TRUE(share_target_dict->GetDictionaryWithoutPathExpansion( |
| + share_target_origin, &share_target_info_dict)); |
| + EXPECT_EQ(1UL, share_target_info_dict->size()); |
| + std::string url_template; |
| + EXPECT_TRUE(share_target_info_dict->GetString(kUrlTemplateKey, |
| + &url_template)); |
| + EXPECT_EQ(url_template, expected_url_template); |
| + } |
| +} |
| + |
| +TEST_F(ShareTargetPrefHelperUnittest, RemoveShareTargetThatIsNoLongerTarget) { |
|
Matt Giuca
2017/01/24 03:26:10
Maybe just rename this to "RemoveShareTarget".
constantina
2017/01/24 23:39:05
Done.
|
| + // Add a share target to prefs that wasn't previously stored. |
| + { |
| + share_target_origin = "https://www.sharetarget.com/"; |
| + expected_url_template = base::Optional<std::string>("share/?title={title}"); |
| + |
| + webshare::AddShareTargetToPrefs( |
| + share_target_origin, expected_url_template, &pref_service); |
| + |
| + share_target_dict = |
| + pref_service.GetDictionary(prefs::kWebShareVisitedTargets); |
| + EXPECT_EQ(1UL, share_target_dict->size()); |
| + ASSERT_TRUE(share_target_dict->GetDictionaryWithoutPathExpansion( |
| + share_target_origin, &share_target_info_dict)); |
| + EXPECT_EQ(1UL, share_target_info_dict->size()); |
| + std::string url_template; |
| + EXPECT_TRUE(share_target_info_dict->GetString(kUrlTemplateKey, |
| + &url_template)); |
| + EXPECT_EQ(url_template, expected_url_template); |
| + } |
| + |
| + // Share target already added now has null template. Remove from prefs. |
| + { |
| + share_target_origin = "https://www.sharetarget.com/"; |
| + expected_url_template = base::nullopt; |
| + |
| + webshare::AddShareTargetToPrefs( |
| + share_target_origin, expected_url_template, &pref_service); |
| + |
| + share_target_dict = |
| + pref_service.GetDictionary(prefs::kWebShareVisitedTargets); |
| + EXPECT_EQ(0UL, share_target_dict->size()); |
| + } |
| +} |