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 "base/bind.h" |
| 6 #include "base/run_loop.h" |
| 7 #include "content/public/test/test_browser_thread_bundle.h" |
| 8 #include "extensions/browser/extension_api_frame_id_map.h" |
| 9 #include "ipc/ipc_message.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 using FrameIdCallback = extensions::ExtensionApiFrameIdMap::FrameIdCallback; |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 namespace { |
| 17 |
| 18 int ToTestFrameId(int render_process_id, int frame_routing_id) { |
| 19 if (render_process_id < 0 && frame_routing_id < 0) |
| 20 return ExtensionApiFrameIdMap::kInvalidFrameId; |
| 21 // Return a deterministic value (yet different from the input) for testing. |
| 22 // To make debugging easier: Ending with 0 = frame ID. |
| 23 return render_process_id * 1000 + frame_routing_id * 10; |
| 24 } |
| 25 |
| 26 int ToTestParentFrameId(int render_process_id, int frame_routing_id) { |
| 27 if (render_process_id < 0 && frame_routing_id < 0) |
| 28 return ExtensionApiFrameIdMap::kInvalidFrameId; |
| 29 // Return a deterministic value (yet different from the input) for testing. |
| 30 // To make debugging easier: Ending with 7 = parent frame ID. |
| 31 return render_process_id * 1000 + frame_routing_id * 10 + 7; |
| 32 } |
| 33 |
| 34 class TestExtensionApiFrameIdMap : public ExtensionApiFrameIdMap { |
| 35 public: |
| 36 int GetInternalSize() { return frame_id_map_.size(); } |
| 37 int GetInternalCallbackCount() { |
| 38 int count = 0; |
| 39 for (auto& it : callbacks_map_) |
| 40 count += it.second.callbacks.size(); |
| 41 return count; |
| 42 } |
| 43 |
| 44 // These indirections are used because we cannot mock RenderFrameHost with |
| 45 // fixed IDs in unit tests. |
| 46 // TODO(robwu): Use content/public/test/test_renderer_host.h to mock |
| 47 // RenderFrameHosts and update the tests to test against these mocks. |
| 48 // After doing that, there is no need for CacheFrameId/RemoveFrameId methods |
| 49 // that take a RenderFrameIdKey, so the methods can be merged. |
| 50 void SetInternalFrameId(int render_process_id, int frame_routing_id) { |
| 51 CacheFrameId(RenderFrameIdKey(render_process_id, frame_routing_id)); |
| 52 } |
| 53 void RemoveInternalFrameId(int render_process_id, int frame_routing_id) { |
| 54 RemoveFrameId(RenderFrameIdKey(render_process_id, frame_routing_id)); |
| 55 } |
| 56 |
| 57 private: |
| 58 // ExtensionApiFrameIdMap: |
| 59 CachedFrameIdPair KeyToValue(const RenderFrameIdKey& key) const override { |
| 60 return CachedFrameIdPair( |
| 61 ToTestFrameId(key.render_process_id, key.frame_routing_id), |
| 62 ToTestParentFrameId(key.render_process_id, key.frame_routing_id)); |
| 63 } |
| 64 }; |
| 65 |
| 66 class ExtensionApiFrameIdMapTest : public testing::Test { |
| 67 public: |
| 68 ExtensionApiFrameIdMapTest() |
| 69 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 70 |
| 71 FrameIdCallback CreateCallback(int render_process_id, |
| 72 int frame_routing_id, |
| 73 const std::string& callback_name_for_testing) { |
| 74 return base::Bind(&ExtensionApiFrameIdMapTest::OnCalledCallback, |
| 75 base::Unretained(this), render_process_id, |
| 76 frame_routing_id, callback_name_for_testing); |
| 77 } |
| 78 |
| 79 void OnCalledCallback(int render_process_id, |
| 80 int frame_routing_id, |
| 81 const std::string& callback_name_for_testing, |
| 82 int extension_api_frame_id, |
| 83 int extension_api_parent_frame_id) { |
| 84 results_.push_back(callback_name_for_testing); |
| 85 |
| 86 // If this fails, then the mapping is completely wrong. |
| 87 EXPECT_EQ(ToTestFrameId(render_process_id, frame_routing_id), |
| 88 extension_api_frame_id); |
| 89 EXPECT_EQ(ToTestParentFrameId(render_process_id, frame_routing_id), |
| 90 extension_api_parent_frame_id); |
| 91 } |
| 92 |
| 93 const std::vector<std::string>& results() { return results_; } |
| 94 void ClearResults() { results_.clear(); } |
| 95 |
| 96 private: |
| 97 content::TestBrowserThreadBundle thread_bundle_; |
| 98 // Used to verify the order of callbacks. |
| 99 std::vector<std::string> results_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(ExtensionApiFrameIdMapTest); |
| 102 }; |
| 103 |
| 104 } // namespace |
| 105 |
| 106 TEST_F(ExtensionApiFrameIdMapTest, GetFrameIdOnIO) { |
| 107 TestExtensionApiFrameIdMap map; |
| 108 EXPECT_EQ(0, map.GetInternalSize()); |
| 109 |
| 110 // Two identical calls, should be processed at the next message loop. |
| 111 map.GetFrameIdOnIO(1, 2, CreateCallback(1, 2, "first")); |
| 112 EXPECT_EQ(1, map.GetInternalCallbackCount()); |
| 113 EXPECT_EQ(0, map.GetInternalSize()); |
| 114 |
| 115 map.GetFrameIdOnIO(1, 2, CreateCallback(1, 2, "first again")); |
| 116 EXPECT_EQ(2, map.GetInternalCallbackCount()); |
| 117 EXPECT_EQ(0, map.GetInternalSize()); |
| 118 |
| 119 // First get the frame ID on IO (queued on message loop), then set it on UI. |
| 120 // No callbacks should be invoked because the IO thread cannot know that the |
| 121 // frame ID was set on the UI thread. |
| 122 map.GetFrameIdOnIO(2, 1, CreateCallback(2, 1, "something else")); |
| 123 EXPECT_EQ(3, map.GetInternalCallbackCount()); |
| 124 EXPECT_EQ(0, map.GetInternalSize()); |
| 125 |
| 126 map.SetInternalFrameId(2, 1); |
| 127 EXPECT_EQ(1, map.GetInternalSize()); |
| 128 EXPECT_EQ(0U, results().size()); |
| 129 |
| 130 // Run some self-contained test. They should not affect the above callbacks. |
| 131 { |
| 132 // Callbacks for invalid IDs should immediately be run because it doesn't |
| 133 // require a thread hop to determine their invalidity. |
| 134 map.GetFrameIdOnIO(-1, MSG_ROUTING_NONE, |
| 135 CreateCallback(-1, MSG_ROUTING_NONE, "invalid IDs")); |
| 136 EXPECT_EQ(3, map.GetInternalCallbackCount()); // No change. |
| 137 EXPECT_EQ(1, map.GetInternalSize()); // No change. |
| 138 ASSERT_EQ(1U, results().size()); // +1 |
| 139 EXPECT_EQ("invalid IDs", results()[0]); |
| 140 ClearResults(); |
| 141 } |
| 142 |
| 143 { |
| 144 // First set the frame ID on UI, then get it on IO. Callback should |
| 145 // immediately be invoked. |
| 146 map.SetInternalFrameId(3, 1); |
| 147 EXPECT_EQ(2, map.GetInternalSize()); |
| 148 |
| 149 map.GetFrameIdOnIO(3, 1, CreateCallback(3, 1, "the only result")); |
| 150 EXPECT_EQ(3, map.GetInternalCallbackCount()); // No change. |
| 151 EXPECT_EQ(2, map.GetInternalSize()); // +1 |
| 152 ASSERT_EQ(1U, results().size()); // +1 |
| 153 EXPECT_EQ("the only result", results()[0]); |
| 154 ClearResults(); |
| 155 } |
| 156 |
| 157 { |
| 158 // Request the frame ID on IO, set the frame ID (in reality, set on the UI), |
| 159 // and request another frame ID. The last query should cause both callbacks |
| 160 // to run because the frame ID is known at the time of the call. |
| 161 map.GetFrameIdOnIO(7, 2, CreateCallback(7, 2, "queued")); |
| 162 EXPECT_EQ(4, map.GetInternalCallbackCount()); // +1 |
| 163 |
| 164 map.SetInternalFrameId(7, 2); |
| 165 EXPECT_EQ(3, map.GetInternalSize()); // +1 |
| 166 |
| 167 map.GetFrameIdOnIO(7, 2, CreateCallback(7, 2, "not queued")); |
| 168 EXPECT_EQ(3, map.GetInternalCallbackCount()); // -1 (first callback ran). |
| 169 EXPECT_EQ(3, map.GetInternalSize()); // No change. |
| 170 ASSERT_EQ(2U, results().size()); // +2 (both callbacks ran). |
| 171 EXPECT_EQ("queued", results()[0]); |
| 172 EXPECT_EQ("not queued", results()[1]); |
| 173 ClearResults(); |
| 174 } |
| 175 |
| 176 // A call identical to the very first call. |
| 177 map.GetFrameIdOnIO(1, 2, CreateCallback(1, 2, "same as first")); |
| 178 EXPECT_EQ(4, map.GetInternalCallbackCount()); |
| 179 EXPECT_EQ(3, map.GetInternalSize()); |
| 180 |
| 181 // Trigger the queued callbacks. |
| 182 base::RunLoop().RunUntilIdle(); |
| 183 EXPECT_EQ(0, map.GetInternalCallbackCount()); // -4 (no queued callbacks). |
| 184 |
| 185 EXPECT_EQ(4, map.GetInternalSize()); // +1 (1 new cached frame ID). |
| 186 ASSERT_EQ(4U, results().size()); // +4 (callbacks ran). |
| 187 |
| 188 // PostTasks are processed in order, so the very first callbacks should be |
| 189 // processed. As soon as the first callback is available, all of its callbacks |
| 190 // should be run (no deferrals!). |
| 191 EXPECT_EQ("first", results()[0]); |
| 192 EXPECT_EQ("first again", results()[1]); |
| 193 EXPECT_EQ("same as first", results()[2]); |
| 194 // This was queued after "first again", but has a different frame ID, so it |
| 195 // is received after "same as first". |
| 196 EXPECT_EQ("something else", results()[3]); |
| 197 ClearResults(); |
| 198 |
| 199 // Request the frame ID for input that was already looked up. Should complete |
| 200 // synchronously. |
| 201 map.GetFrameIdOnIO(1, 2, CreateCallback(1, 2, "first and cached")); |
| 202 EXPECT_EQ(0, map.GetInternalCallbackCount()); // No change. |
| 203 EXPECT_EQ(4, map.GetInternalSize()); // No change. |
| 204 ASSERT_EQ(1U, results().size()); // +1 (synchronous callback). |
| 205 EXPECT_EQ("first and cached", results()[0]); |
| 206 ClearResults(); |
| 207 |
| 208 // Trigger frame removal and look up frame ID. The frame ID should no longer |
| 209 // be available. and GetFrameIdOnIO() should require a thread hop. |
| 210 map.RemoveInternalFrameId(1, 2); |
| 211 EXPECT_EQ(3, map.GetInternalSize()); // -1 |
| 212 map.GetFrameIdOnIO(1, 2, CreateCallback(1, 2, "first was removed")); |
| 213 EXPECT_EQ(1, map.GetInternalCallbackCount()); // +1 |
| 214 ASSERT_EQ(0U, results().size()); // No change (queued callback). |
| 215 base::RunLoop().RunUntilIdle(); |
| 216 EXPECT_EQ(0, map.GetInternalCallbackCount()); // -1 (callback not in queue). |
| 217 EXPECT_EQ(4, map.GetInternalSize()); // +1 (cached frame ID). |
| 218 ASSERT_EQ(1U, results().size()); // +1 (callback ran). |
| 219 EXPECT_EQ("first was removed", results()[0]); |
| 220 } |
| 221 |
| 222 } // namespace extensions |
OLD | NEW |