OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/dom_distiller/content/dom_distiller_viewer_source.h" | 5 #include "components/dom_distiller/content/dom_distiller_viewer_source.h" |
6 | 6 |
7 #include <vector> | |
8 | |
9 #include "base/callback.h" | |
10 #include "components/dom_distiller/core/article_distillation_update.h" | |
11 #include "components/dom_distiller/core/article_entry.h" | |
12 #include "components/dom_distiller/core/dom_distiller_service.h" | |
13 #include "components/dom_distiller/core/dom_distiller_store.h" | |
14 #include "components/dom_distiller/core/dom_distiller_test_util.h" | |
15 #include "components/dom_distiller/core/fake_db.h" | |
16 #include "components/dom_distiller/core/fake_distiller.h" | |
17 #include "components/dom_distiller/core/task_tracker.h" | |
18 #include "components/dom_distiller/core/url_constants.h" | 7 #include "components/dom_distiller/core/url_constants.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
20 | 9 |
21 namespace dom_distiller { | 10 namespace dom_distiller { |
22 | 11 |
23 const char kTestScheme[] = "myscheme"; | 12 const char kTestScheme[] = "myscheme"; |
24 | 13 |
25 class FakeViewRequestDelegate : public ViewRequestDelegate { | |
26 public: | |
27 virtual ~FakeViewRequestDelegate() {} | |
28 MOCK_METHOD1(OnArticleReady, void(const DistilledArticleProto* proto)); | |
29 MOCK_METHOD1(OnArticleUpdated, | |
30 void(ArticleDistillationUpdate article_update)); | |
31 }; | |
32 | |
33 class TestDomDistillerService : public DomDistillerServiceInterface { | |
34 public: | |
35 TestDomDistillerService() {} | |
36 virtual ~TestDomDistillerService() {} | |
37 | |
38 MOCK_CONST_METHOD0(GetSyncableService, syncer::SyncableService*()); | |
39 MOCK_METHOD2(AddToList, | |
40 const std::string(const GURL&, const ArticleAvailableCallback&)); | |
41 MOCK_CONST_METHOD0(GetEntries, std::vector<ArticleEntry>()); | |
42 MOCK_METHOD1(AddObserver, void(DomDistillerObserver*)); | |
43 MOCK_METHOD1(RemoveObserver, void(DomDistillerObserver*)); | |
44 MOCK_METHOD0(ViewUrlImpl, ViewerHandle*()); | |
45 virtual scoped_ptr<ViewerHandle> ViewUrl(ViewRequestDelegate*, const GURL&) { | |
46 return scoped_ptr<ViewerHandle>(ViewUrlImpl()); | |
47 } | |
48 MOCK_METHOD0(ViewEntryImpl, ViewerHandle*()); | |
49 virtual scoped_ptr<ViewerHandle> ViewEntry(ViewRequestDelegate*, | |
50 const std::string&) { | |
51 return scoped_ptr<ViewerHandle>(ViewEntryImpl()); | |
52 } | |
53 MOCK_METHOD0(RemoveEntryImpl, ArticleEntry*()); | |
54 virtual scoped_ptr<ArticleEntry> RemoveEntry(const std::string&) { | |
55 return scoped_ptr<ArticleEntry>(RemoveEntryImpl()); | |
56 } | |
57 }; | |
58 | |
59 class DomDistillerViewerSourceTest : public testing::Test { | 14 class DomDistillerViewerSourceTest : public testing::Test { |
60 public: | 15 public: |
61 virtual void SetUp() OVERRIDE { | 16 virtual void SetUp() OVERRIDE { |
62 service_.reset(new TestDomDistillerService()); | 17 source_.reset(new DomDistillerViewerSource(NULL, kTestScheme)); |
63 source_.reset(new DomDistillerViewerSource(service_.get(), kTestScheme)); | |
64 } | 18 } |
65 | 19 |
66 protected: | 20 protected: |
67 scoped_ptr<ViewerHandle> CreateViewRequest( | |
68 const std::string& path, | |
69 ViewRequestDelegate* view_request_delegate) { | |
70 return source_.get()->CreateViewRequest(path, view_request_delegate); | |
71 } | |
72 | |
73 scoped_ptr<TestDomDistillerService> service_; | |
74 scoped_ptr<DomDistillerViewerSource> source_; | 21 scoped_ptr<DomDistillerViewerSource> source_; |
75 }; | 22 }; |
76 | 23 |
77 TEST_F(DomDistillerViewerSourceTest, TestMimeType) { | 24 TEST_F(DomDistillerViewerSourceTest, TestMimeType) { |
78 EXPECT_EQ("text/css", source_.get()->GetMimeType(kCssPath)); | 25 EXPECT_EQ("text/css", source_.get()->GetMimeType(kCssPath)); |
79 EXPECT_EQ("text/html", source_.get()->GetMimeType("anythingelse")); | 26 EXPECT_EQ("text/html", source_.get()->GetMimeType("anythingelse")); |
80 } | 27 } |
81 | 28 |
82 TEST_F(DomDistillerViewerSourceTest, TestCreatingViewUrlRequest) { | |
83 scoped_ptr<FakeViewRequestDelegate> view_request_delegate( | |
84 new FakeViewRequestDelegate()); | |
85 ViewerHandle* viewer_handle(new ViewerHandle(ViewerHandle::CancelCallback())); | |
86 EXPECT_CALL(*service_.get(), ViewUrlImpl()) | |
87 .WillOnce(testing::Return(viewer_handle)); | |
88 EXPECT_CALL(*service_.get(), ViewEntryImpl()).Times(0); | |
89 CreateViewRequest( | |
90 std::string("?") + kUrlKey + "=http%3A%2F%2Fwww.example.com%2F", | |
91 view_request_delegate.get()); | |
92 } | |
93 | |
94 TEST_F(DomDistillerViewerSourceTest, TestCreatingViewEntryRequest) { | |
95 scoped_ptr<FakeViewRequestDelegate> view_request_delegate( | |
96 new FakeViewRequestDelegate()); | |
97 ViewerHandle* viewer_handle(new ViewerHandle(ViewerHandle::CancelCallback())); | |
98 EXPECT_CALL(*service_.get(), ViewEntryImpl()) | |
99 .WillOnce(testing::Return(viewer_handle)); | |
100 EXPECT_CALL(*service_.get(), ViewUrlImpl()).Times(0); | |
101 CreateViewRequest(std::string("?") + kEntryIdKey + "=abc-def", | |
102 view_request_delegate.get()); | |
103 } | |
104 | |
105 TEST_F(DomDistillerViewerSourceTest, TestCreatingInvalidViewRequest) { | |
106 scoped_ptr<FakeViewRequestDelegate> view_request_delegate( | |
107 new FakeViewRequestDelegate()); | |
108 EXPECT_CALL(*service_.get(), ViewEntryImpl()).Times(0); | |
109 EXPECT_CALL(*service_.get(), ViewUrlImpl()).Times(0); | |
110 // Specify none of the required query parameters. | |
111 CreateViewRequest("?foo=bar", view_request_delegate.get()); | |
112 // Specify both of the required query parameters. | |
113 CreateViewRequest("?" + std::string(kUrlKey) + | |
114 "=http%3A%2F%2Fwww.example.com%2F&" + | |
115 std::string(kEntryIdKey) + "=abc-def", | |
116 view_request_delegate.get()); | |
117 // Specify an internal Chrome page. | |
118 CreateViewRequest("?" + std::string(kUrlKey) + "=chrome%3A%2F%2Fsettings%2F", | |
119 view_request_delegate.get()); | |
120 // Specify a recursive URL. | |
121 CreateViewRequest("?" + std::string(kUrlKey) + "=" + | |
122 std::string(kTestScheme) + "%3A%2F%2Fabc-def%2F", | |
123 view_request_delegate.get()); | |
124 } | |
125 | |
126 } // namespace dom_distiller | 29 } // namespace dom_distiller |
OLD | NEW |