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