Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / browser / web_contents / web_contents_user_data_unittest.cc
blobeeb2acb3de23920df007cbc4328e44e8c07bb6fb
1 // Copyright (c) 2012 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.
5 #include "content/public/browser/web_contents_user_data.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "content/public/browser/web_contents.h"
9 #include "content/public/test/test_renderer_host.h"
10 #include "content/public/test/web_contents_tester.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace content {
15 class WebContentsAttachedClass1
16 : public WebContentsUserData<WebContentsAttachedClass1> {
17 public:
18 ~WebContentsAttachedClass1() override {}
20 private:
21 explicit WebContentsAttachedClass1(WebContents* contents) {}
22 friend class WebContentsUserData<WebContentsAttachedClass1>;
25 class WebContentsAttachedClass2
26 : public WebContentsUserData<WebContentsAttachedClass2> {
27 public:
28 ~WebContentsAttachedClass2() override {}
30 private:
31 explicit WebContentsAttachedClass2(WebContents* contents) {}
32 friend class WebContentsUserData<WebContentsAttachedClass2>;
35 DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsAttachedClass1);
36 DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsAttachedClass2);
38 typedef RenderViewHostTestHarness WebContentsUserDataTest;
40 TEST_F(WebContentsUserDataTest, OneInstanceTwoAttachments) {
41 WebContents* contents = web_contents();
42 WebContentsAttachedClass1* class1 =
43 WebContentsAttachedClass1::FromWebContents(contents);
44 ASSERT_EQ(NULL, class1);
45 WebContentsAttachedClass2* class2 =
46 WebContentsAttachedClass2::FromWebContents(contents);
47 ASSERT_EQ(NULL, class2);
49 WebContentsAttachedClass1::CreateForWebContents(contents);
50 class1 = WebContentsAttachedClass1::FromWebContents(contents);
51 ASSERT_TRUE(class1 != NULL);
52 class2 = WebContentsAttachedClass2::FromWebContents(contents);
53 ASSERT_EQ(NULL, class2);
55 WebContentsAttachedClass2::CreateForWebContents(contents);
56 WebContentsAttachedClass1* class1again =
57 WebContentsAttachedClass1::FromWebContents(contents);
58 ASSERT_TRUE(class1again != NULL);
59 class2 = WebContentsAttachedClass2::FromWebContents(contents);
60 ASSERT_TRUE(class2 != NULL);
61 ASSERT_EQ(class1, class1again);
62 ASSERT_NE(static_cast<void*>(class1), static_cast<void*>(class2));
65 TEST_F(WebContentsUserDataTest, TwoInstancesOneAttachment) {
66 WebContents* contents1 = web_contents();
67 scoped_ptr<WebContents> contents2(
68 WebContentsTester::CreateTestWebContents(browser_context(), NULL));
70 WebContentsAttachedClass1* one_class =
71 WebContentsAttachedClass1::FromWebContents(contents1);
72 ASSERT_EQ(NULL, one_class);
73 WebContentsAttachedClass1* two_class =
74 WebContentsAttachedClass1::FromWebContents(contents2.get());
75 ASSERT_EQ(NULL, two_class);
77 WebContentsAttachedClass1::CreateForWebContents(contents1);
78 one_class = WebContentsAttachedClass1::FromWebContents(contents1);
79 ASSERT_TRUE(one_class != NULL);
80 two_class = WebContentsAttachedClass1::FromWebContents(contents2.get());
81 ASSERT_EQ(NULL, two_class);
83 WebContentsAttachedClass1::CreateForWebContents(contents2.get());
84 WebContentsAttachedClass1* one_class_again =
85 WebContentsAttachedClass1::FromWebContents(contents1);
86 ASSERT_TRUE(one_class_again != NULL);
87 two_class = WebContentsAttachedClass1::FromWebContents(contents2.get());
88 ASSERT_TRUE(two_class != NULL);
89 ASSERT_EQ(one_class, one_class_again);
90 ASSERT_NE(one_class, two_class);
93 TEST_F(WebContentsUserDataTest, Idempotence) {
94 WebContents* contents = web_contents();
95 WebContentsAttachedClass1* clazz =
96 WebContentsAttachedClass1::FromWebContents(contents);
97 ASSERT_EQ(NULL, clazz);
99 WebContentsAttachedClass1::CreateForWebContents(contents);
100 clazz = WebContentsAttachedClass1::FromWebContents(contents);
101 ASSERT_TRUE(clazz != NULL);
103 WebContentsAttachedClass1::CreateForWebContents(contents);
104 WebContentsAttachedClass1* class_again =
105 WebContentsAttachedClass1::FromWebContents(contents);
106 ASSERT_TRUE(class_again != NULL);
107 ASSERT_EQ(clazz, class_again);
110 } // namespace content