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"
15 class WebContentsAttachedClass1
16 : public WebContentsUserData
<WebContentsAttachedClass1
> {
18 virtual ~WebContentsAttachedClass1() {}
20 explicit WebContentsAttachedClass1(WebContents
* contents
) {}
21 friend class WebContentsUserData
<WebContentsAttachedClass1
>;
24 class WebContentsAttachedClass2
25 : public WebContentsUserData
<WebContentsAttachedClass2
> {
27 virtual ~WebContentsAttachedClass2() {}
29 explicit WebContentsAttachedClass2(WebContents
* contents
) {}
30 friend class WebContentsUserData
<WebContentsAttachedClass2
>;
33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsAttachedClass1
);
34 DEFINE_WEB_CONTENTS_USER_DATA_KEY(WebContentsAttachedClass2
);
36 typedef RenderViewHostTestHarness WebContentsUserDataTest
;
38 TEST_F(WebContentsUserDataTest
, OneInstanceTwoAttachments
) {
39 WebContents
* contents
= web_contents();
40 WebContentsAttachedClass1
* class1
=
41 WebContentsAttachedClass1::FromWebContents(contents
);
42 ASSERT_EQ(NULL
, class1
);
43 WebContentsAttachedClass2
* class2
=
44 WebContentsAttachedClass2::FromWebContents(contents
);
45 ASSERT_EQ(NULL
, class2
);
47 WebContentsAttachedClass1::CreateForWebContents(contents
);
48 class1
= WebContentsAttachedClass1::FromWebContents(contents
);
49 ASSERT_TRUE(class1
!= NULL
);
50 class2
= WebContentsAttachedClass2::FromWebContents(contents
);
51 ASSERT_EQ(NULL
, class2
);
53 WebContentsAttachedClass2::CreateForWebContents(contents
);
54 WebContentsAttachedClass1
* class1again
=
55 WebContentsAttachedClass1::FromWebContents(contents
);
56 ASSERT_TRUE(class1again
!= NULL
);
57 class2
= WebContentsAttachedClass2::FromWebContents(contents
);
58 ASSERT_TRUE(class2
!= NULL
);
59 ASSERT_EQ(class1
, class1again
);
60 ASSERT_NE(static_cast<void*>(class1
), static_cast<void*>(class2
));
63 TEST_F(WebContentsUserDataTest
, TwoInstancesOneAttachment
) {
64 WebContents
* contents1
= web_contents();
65 scoped_ptr
<WebContents
> contents2(
66 WebContentsTester::CreateTestWebContents(browser_context(), NULL
));
68 WebContentsAttachedClass1
* one_class
=
69 WebContentsAttachedClass1::FromWebContents(contents1
);
70 ASSERT_EQ(NULL
, one_class
);
71 WebContentsAttachedClass1
* two_class
=
72 WebContentsAttachedClass1::FromWebContents(contents2
.get());
73 ASSERT_EQ(NULL
, two_class
);
75 WebContentsAttachedClass1::CreateForWebContents(contents1
);
76 one_class
= WebContentsAttachedClass1::FromWebContents(contents1
);
77 ASSERT_TRUE(one_class
!= NULL
);
78 two_class
= WebContentsAttachedClass1::FromWebContents(contents2
.get());
79 ASSERT_EQ(NULL
, two_class
);
81 WebContentsAttachedClass1::CreateForWebContents(contents2
.get());
82 WebContentsAttachedClass1
* one_class_again
=
83 WebContentsAttachedClass1::FromWebContents(contents1
);
84 ASSERT_TRUE(one_class_again
!= NULL
);
85 two_class
= WebContentsAttachedClass1::FromWebContents(contents2
.get());
86 ASSERT_TRUE(two_class
!= NULL
);
87 ASSERT_EQ(one_class
, one_class_again
);
88 ASSERT_NE(one_class
, two_class
);
91 TEST_F(WebContentsUserDataTest
, Idempotence
) {
92 WebContents
* contents
= web_contents();
93 WebContentsAttachedClass1
* clazz
=
94 WebContentsAttachedClass1::FromWebContents(contents
);
95 ASSERT_EQ(NULL
, clazz
);
97 WebContentsAttachedClass1::CreateForWebContents(contents
);
98 clazz
= WebContentsAttachedClass1::FromWebContents(contents
);
99 ASSERT_TRUE(clazz
!= NULL
);
101 WebContentsAttachedClass1::CreateForWebContents(contents
);
102 WebContentsAttachedClass1
* class_again
=
103 WebContentsAttachedClass1::FromWebContents(contents
);
104 ASSERT_TRUE(class_again
!= NULL
);
105 ASSERT_EQ(clazz
, class_again
);
108 } // namespace content