OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "components/ntp_snippets/content_suggestions_provider.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "components/ntp_snippets/category_factory.h" | |
9 #include "testing/gmock/include/gmock/gmock.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 using testing::Eq; | |
13 using testing::StrictMock; | |
14 | |
15 namespace ntp_snippets { | |
16 | |
17 // We do not care about any of these methods. We use mock methods so that we can | |
18 // declare a StrictMock below and ensure none of them are called. | |
19 class MockContentSuggestionsProvider : public ContentSuggestionsProvider { | |
20 public: | |
21 MockContentSuggestionsProvider(Observer* observer, | |
22 CategoryFactory* category_factory) | |
23 : ContentSuggestionsProvider(observer, category_factory) {} | |
24 virtual ~MockContentSuggestionsProvider() = default; | |
25 | |
26 MOCK_METHOD1(GetCategoryStatus, CategoryStatus(Category)); | |
27 MOCK_METHOD1(GetCategoryInfo, CategoryInfo(Category)); | |
28 MOCK_METHOD1(DismissSuggestion, void(const std::string&)); | |
29 MOCK_METHOD2(FetchSuggestionImage, | |
30 void(const std::string&, const ImageFetchedCallback&)); | |
31 MOCK_METHOD3(ClearHistory, | |
32 void(base::Time, | |
33 base::Time, | |
34 const base::Callback<bool(const GURL&)>&)); | |
35 MOCK_METHOD1(ClearCachedSuggestions, void(Category)); | |
36 MOCK_METHOD2(GetDismissedSuggestionsForDebugging, | |
37 void(Category, const DismissedSuggestionsCallback&)); | |
38 MOCK_METHOD1(ClearDismissedSuggestionsForDebugging, void(Category)); | |
39 }; | |
40 | |
41 class ContentSuggestionsProviderTest : public ::testing::Test { | |
42 protected: | |
43 ContentSuggestionsProviderTest() : provider_(nullptr, &factory_) {} | |
44 | |
45 std::string MakeUniqueID(Category category, | |
46 const std::string& within_category_id) const { | |
47 return provider_.MakeUniqueID(category, within_category_id); | |
48 } | |
49 | |
50 Category GetCategoryFromUniqueID(const std::string& unique_id) const { | |
51 return provider_.GetCategoryFromUniqueID(unique_id); | |
52 } | |
53 | |
54 std::string GetWithinCategoryIDFromUniqueID( | |
55 const std::string& unique_id) const { | |
56 return provider_.GetWithinCategoryIDFromUniqueID(unique_id); | |
57 } | |
58 | |
59 StrictMock<MockContentSuggestionsProvider> provider_; | |
60 CategoryFactory factory_; | |
Marc Treib
2016/09/02 12:43:25
nit: swap these two, so the provider gets deleted
sfiera
2016/09/02 12:49:23
Done.
| |
61 }; | |
62 | |
63 TEST_F(ContentSuggestionsProviderTest, Articles) { | |
64 Category kArticles = factory_.FromKnownCategory(KnownCategories::ARTICLES); | |
65 const std::string kUrl = "https://chromium.org/"; | |
66 | |
67 std::string uid = MakeUniqueID(kArticles, kUrl); | |
68 EXPECT_THAT(uid, Eq("10001|https://chromium.org/")); | |
69 EXPECT_THAT(GetCategoryFromUniqueID(uid), Eq(kArticles)); | |
70 EXPECT_THAT(GetWithinCategoryIDFromUniqueID(uid), Eq(kUrl)); | |
71 } | |
72 | |
73 TEST_F(ContentSuggestionsProviderTest, Remote) { | |
74 Category kRemoteCategory = factory_.FromRemoteCategory(2); | |
75 const std::string kUrl = "https://chromium.org/"; | |
76 | |
77 std::string uid = MakeUniqueID(kRemoteCategory, kUrl); | |
78 EXPECT_THAT(uid, Eq("10002|https://chromium.org/")); | |
79 EXPECT_THAT(GetCategoryFromUniqueID(uid), Eq(kRemoteCategory)); | |
80 EXPECT_THAT(GetWithinCategoryIDFromUniqueID(uid), Eq(kUrl)); | |
81 } | |
82 | |
83 TEST_F(ContentSuggestionsProviderTest, FromID) { | |
84 Category kCategory = factory_.FromIDValue(10003); | |
85 const std::string kUrl = "https://chromium.org/"; | |
86 | |
87 std::string uid = MakeUniqueID(kCategory, kUrl); | |
88 EXPECT_THAT(uid, Eq("10003|https://chromium.org/")); | |
89 EXPECT_THAT(GetCategoryFromUniqueID(uid), Eq(kCategory)); | |
90 EXPECT_THAT(GetWithinCategoryIDFromUniqueID(uid), Eq(kUrl)); | |
91 } | |
92 | |
93 TEST_F(ContentSuggestionsProviderTest, Death) { | |
94 const std::string kUrl = "https://chromium.org/"; | |
95 #if DCHECK_IS_ON() | |
96 EXPECT_DEATH(GetCategoryFromUniqueID(kUrl), "Not a valid unique_id"); | |
97 EXPECT_DEATH(GetWithinCategoryIDFromUniqueID(kUrl), "Not a valid unique_id"); | |
98 | |
99 EXPECT_DEATH(GetCategoryFromUniqueID("one|" + kUrl), "Non-numeric category"); | |
100 EXPECT_THAT(GetWithinCategoryIDFromUniqueID("one|" + kUrl), Eq(kUrl)); | |
101 #else | |
102 const Category kNoCategory = factory_.FromIDValue(-1); | |
103 | |
104 EXPECT_THAT(GetCategoryFromUniqueID(kUrl), Eq(kNoCategory)); | |
Marc Treib
2016/09/02 12:43:25
Is the DCHECK-off part here for any particular rea
sfiera
2016/09/02 12:49:23
Nothing in particular.
But why wouldn't we want t
Marc Treib
2016/09/02 13:02:02
No, DCHECKS are treated like asserts, i.e. somethi
grt (UTC plus 2)
2016/09/02 19:21:47
I think you're looking for this: https://chromium.
| |
105 EXPECT_THAT(GetWithinCategoryIDFromUniqueID(kUrl), Eq(kUrl)); | |
106 | |
107 EXPECT_THAT(GetCategoryFromUniqueID("one|" + kUrl), Eq(kNoCategory)); | |
108 EXPECT_THAT(GetWithinCategoryIDFromUniqueID("one|" + kUrl), Eq(kUrl)); | |
109 #endif | |
110 } | |
111 | |
112 } // namespace ntp_snippets | |
OLD | NEW |