Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: extensions/browser/extension_api_frame_id_map_unittest.cc

Issue 1413543005: Use FrameTreeNode ID as frameId in extension APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add unit test for ExtensionApiFrameIdMap Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "testing/gtest/include/gtest/gtest.h"
10
11 using FrameIdCallback = extensions::ExtensionApiFrameIdMap::FrameIdCallback;
12
13 namespace extensions {
14
15 namespace {
16
17 ExtensionApiFrameId ToTestFrameId(int render_process_id, int frame_routing_id) {
18 // Return a deterministic value (yet different from the input) for testing.
19 return ExtensionApiFrameId(render_process_id + 1, frame_routing_id + 1);
20 }
21
22 class TestExtensionApiFrameIdMap : public ExtensionApiFrameIdMap {
23 public:
24 int GetInternalSize() { return frame_id_map_.size(); }
25
26 private:
27 // ExtensionApiFrameIdMap:
28 ExtensionApiFrameId KeyToValue(const RenderFrameIdKey& key) const override {
29 return ToTestFrameId(key.render_process_id, key.frame_routing_id);
30 }
31 };
32
33 class ExtensionApiFrameIdMapTest : public testing::Test {
34 public:
35 ExtensionApiFrameIdMapTest()
36 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
37
38 FrameIdCallback CreateCallback(int render_process_id, int frame_routing_id) {
39 return base::Bind(&ExtensionApiFrameIdMapTest::OnCalledCallback,
40 base::Unretained(this), render_process_id,
41 frame_routing_id);
42 }
43
44 void OnCalledCallback(int render_process_id,
45 int frame_routing_id,
46 const ExtensionApiFrameId& extension_api_frame_id) {
47 // Save results for later, to see whether the ordering is OK.
48 results_.push_back(extension_api_frame_id);
49
50 // If this fails, then the mapping is completely wrong.
51 EXPECT_EQ(ToTestFrameId(render_process_id, frame_routing_id),
52 extension_api_frame_id);
53 }
54
55 const std::vector<ExtensionApiFrameId>& results() { return results_; }
56
57 private:
58 content::TestBrowserThreadBundle thread_bundle_;
59 std::vector<ExtensionApiFrameId> results_;
60
61 DISALLOW_COPY_AND_ASSIGN(ExtensionApiFrameIdMapTest);
62 };
63
64 } // namespace
65
66 // Tests whether getting data from the map gives consistent results.
67 TEST_F(ExtensionApiFrameIdMapTest, Basics) {
68 scoped_ptr<TestExtensionApiFrameIdMap> map(new TestExtensionApiFrameIdMap());
69 EXPECT_EQ(0, map->GetInternalSize());
70
71 EXPECT_EQ(ToTestFrameId(1, 2), map->GetFrameId(1, 2));
72 EXPECT_EQ(1, map->GetInternalSize());
73
74 EXPECT_EQ(ToTestFrameId(1, 2), map->GetFrameId(1, 2));
75 EXPECT_EQ(1, map->GetInternalSize());
76
77 EXPECT_EQ(ToTestFrameId(2, 1), map->GetFrameId(2, 1));
78 EXPECT_EQ(2, map->GetInternalSize());
79 }
80
81 TEST_F(ExtensionApiFrameIdMapTest, GetFrameIdOnIO) {
82 scoped_ptr<TestExtensionApiFrameIdMap> map(new TestExtensionApiFrameIdMap());
83 EXPECT_EQ(0, map->GetInternalSize());
84
85 // Two identical calls, should be processed at the next message loop.
86 map->GetFrameIdOnIO(1, 2, CreateCallback(1, 2));
87 EXPECT_EQ(0, map->GetInternalSize());
88
89 map->GetFrameIdOnIO(1, 2, CreateCallback(1, 2));
90 EXPECT_EQ(0, map->GetInternalSize());
91
92 // First get the frame ID on IO, then on UI.
93 map->GetFrameIdOnIO(2, 1, CreateCallback(2, 1));
94 EXPECT_EQ(0, map->GetInternalSize());
95
96 EXPECT_EQ(ToTestFrameId(2, 1), map->GetFrameId(2, 1));
97 EXPECT_EQ(1, map->GetInternalSize());
98
99 // First get the frame ID on UI, then on UI.
100 EXPECT_EQ(ToTestFrameId(3, 1), map->GetFrameId(3, 1));
101 EXPECT_EQ(2, map->GetInternalSize());
102
103 map->GetFrameIdOnIO(3, 1, CreateCallback(3, 1));
104 EXPECT_EQ(2, map->GetInternalSize());
105
106 base::RunLoop().RunUntilIdle();
107 EXPECT_EQ(3, map->GetInternalSize());
108 EXPECT_EQ(4U, results().size());
109
110 // Disabled these checks because the order is not guaranteed.
111 // EXPECT_EQ(ToTestFrameId(1, 2), results()[0]);
112 // EXPECT_EQ(ToTestFrameId(1, 2), results()[1]);
113 // EXPECT_EQ(ToTestFrameId(2, 1), results()[2]);
114 // EXPECT_EQ(ToTestFrameId(3, 1), results()[3]);
115
116 // Request the frame ID for input that was already looked up. Should complete
117 // synchronously.
118 map->GetFrameIdOnIO(1, 2, CreateCallback(1, 2));
119 EXPECT_EQ(3, map->GetInternalSize());
120 EXPECT_EQ(5U, results().size());
121 EXPECT_EQ(ToTestFrameId(1, 2), results()[4]);
122 }
123
124 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698