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 "blimp/client/session/navigation_feature.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "blimp/common/create_blimp_message.h" | |
10 #include "blimp/common/proto/blimp_message.pb.h" | |
11 #include "blimp/net/test_common.h" | |
12 #include "net/base/net_errors.h" | |
13 #include "net/base/test_completion_callback.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 #include "third_party/skia/include/core/SkBitmap.h" | |
17 #include "url/gurl.h" | |
18 | |
19 using testing::_; | |
20 | |
21 namespace blimp { | |
22 namespace client { | |
23 | |
24 class MockNavigationFeatureDelegate | |
25 : public NavigationFeature::NavigationFeatureDelegate { | |
26 public: | |
27 // NavigationFeatureDelegate implementation. | |
28 MOCK_METHOD2(OnUrlChanged, void(int tab_id, const GURL& url)); | |
29 MOCK_METHOD2(OnFaviconChanged, void(int tab_id, const SkBitmap& favicon)); | |
30 MOCK_METHOD2(OnTitleChanged, void(int tab_id, const std::string& title)); | |
31 MOCK_METHOD2(OnLoadingChanged, void(int tab_id, bool loading)); | |
32 }; | |
33 | |
34 void SendMockNavigationStateChangedMessage(BlimpMessageProcessor* processor, | |
35 int tab_id, | |
36 const GURL* url, | |
37 const std::string* title, | |
38 const bool* loading) { | |
39 NavigationMessage* navigation_message; | |
40 scoped_ptr<BlimpMessage> message = | |
41 CreateBlimpMessage(&navigation_message, tab_id); | |
42 navigation_message->set_type(NavigationMessage::NAVIGATION_STATE_CHANGED); | |
43 NavigationStateChangeMessage* state = | |
44 navigation_message->mutable_navigation_state_change(); | |
45 if (url) | |
46 state->set_url(url->spec()); | |
47 | |
48 if (title) | |
49 state->set_title(*title); | |
50 | |
51 if (loading) | |
52 state->set_loading(*loading); | |
53 | |
54 net::TestCompletionCallback cb; | |
55 processor->ProcessMessage(std::move(message), cb.callback()); | |
56 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
57 } | |
58 | |
59 MATCHER_P2(EqualsNavigateToUrlText, tab_id, text, "") { | |
60 return arg.target_tab_id() == tab_id && | |
61 arg.navigation().type() == NavigationMessage::LOAD_URL && | |
62 arg.navigation().load_url().url() == text; | |
63 } | |
64 | |
65 MATCHER_P(EqualsNavigateForward, tab_id, "") { | |
66 return arg.target_tab_id() == tab_id && | |
67 arg.navigation().type() == NavigationMessage::GO_FORWARD; | |
68 } | |
69 | |
70 MATCHER_P(EqualsNavigateBack, tab_id, "") { | |
71 return arg.target_tab_id() == tab_id && | |
72 arg.navigation().type() == NavigationMessage::GO_BACK; | |
73 } | |
74 | |
75 MATCHER_P(EqualsNavigateReload, tab_id, "") { | |
76 return arg.target_tab_id() == tab_id && | |
77 arg.navigation().type() == NavigationMessage::RELOAD; | |
78 } | |
79 | |
80 class NavigationFeatureTest : public testing::Test { | |
81 public: | |
82 NavigationFeatureTest() : out_processor_(nullptr) {} | |
83 | |
84 void SetUp() override { | |
85 out_processor_ = new MockBlimpMessageProcessor(); | |
86 feature_.set_outgoing_message_processor(make_scoped_ptr(out_processor_)); | |
87 | |
88 feature_.SetDelegate(1, &delegate1_); | |
89 feature_.SetDelegate(2, &delegate2_); | |
90 } | |
91 | |
92 protected: | |
93 // This is a raw pointer to a class that is owned by the NavigationFeature. | |
94 MockBlimpMessageProcessor* out_processor_; | |
95 | |
96 MockNavigationFeatureDelegate delegate1_; | |
97 MockNavigationFeatureDelegate delegate2_; | |
98 | |
99 NavigationFeature feature_; | |
100 }; | |
101 | |
102 TEST_F(NavigationFeatureTest, DispatchesToCorrectDelegate) { | |
103 GURL url("https://www.google.com"); | |
104 EXPECT_CALL(delegate1_, OnUrlChanged(1, url)).Times(1); | |
105 SendMockNavigationStateChangedMessage(&feature_, 1, &url, nullptr, nullptr); | |
106 | |
107 EXPECT_CALL(delegate2_, OnUrlChanged(2, url)).Times(1); | |
108 SendMockNavigationStateChangedMessage(&feature_, 2, &url, nullptr, nullptr); | |
109 } | |
110 | |
111 TEST_F(NavigationFeatureTest, AllDelegateFieldsCalled) { | |
112 GURL url("https://www.google.com"); | |
113 std::string title = "Google"; | |
114 bool loading = true; | |
115 | |
116 EXPECT_CALL(delegate1_, OnUrlChanged(1, url)).Times(1); | |
117 EXPECT_CALL(delegate1_, OnTitleChanged(1, title)).Times(1); | |
118 EXPECT_CALL(delegate1_, OnLoadingChanged(1, loading)).Times(1); | |
119 SendMockNavigationStateChangedMessage(&feature_, 1, &url, &title, &loading); | |
120 } | |
121 | |
122 TEST_F(NavigationFeatureTest, PartialDelegateFieldsCalled) { | |
123 std::string title = "Google"; | |
124 bool loading = true; | |
125 | |
126 EXPECT_CALL(delegate1_, OnUrlChanged(_, _)).Times(0); | |
127 EXPECT_CALL(delegate1_, OnTitleChanged(1, title)).Times(1); | |
128 EXPECT_CALL(delegate1_, OnLoadingChanged(1, loading)).Times(1); | |
129 SendMockNavigationStateChangedMessage(&feature_, 1, nullptr, &title, | |
130 &loading); | |
131 } | |
132 | |
133 TEST_F(NavigationFeatureTest, TestNavigateToUrlMessage) { | |
134 std::string text = "text"; | |
135 | |
136 EXPECT_CALL(*out_processor_, | |
137 MockableProcessMessage(EqualsNavigateToUrlText(1, text), _)) | |
138 .Times(1); | |
139 feature_.NavigateToUrlText(1, text); | |
140 } | |
141 | |
142 TEST_F(NavigationFeatureTest, TestNavigateForwardMessage) { | |
143 EXPECT_CALL(*out_processor_, | |
144 MockableProcessMessage(EqualsNavigateForward(1), _)) | |
145 .Times(1); | |
146 feature_.GoForward(1); | |
147 } | |
148 | |
149 TEST_F(NavigationFeatureTest, TestNavigateBackMessage) { | |
150 EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsNavigateBack(1), _)) | |
151 .Times(1); | |
152 feature_.GoBack(1); | |
153 } | |
154 | |
155 TEST_F(NavigationFeatureTest, TestNavigateReloadMessage) { | |
156 EXPECT_CALL(*out_processor_, | |
157 MockableProcessMessage(EqualsNavigateReload(1), _)) | |
158 .Times(1); | |
159 feature_.Reload(1); | |
160 } | |
161 | |
162 } // namespace client | |
163 } // namespace blimp | |
OLD | NEW |