1 // Copyright 2014 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 #import "ios/web/web_state/js/crw_js_window_id_manager.h"
7 #include "base/mac/scoped_nsobject.h"
8 #import "ios/web/public/test/crw_test_js_injection_receiver.h"
9 #import "ios/web/public/test/js_test_util.h"
10 #include "ios/web/public/web_client.h"
11 #import "testing/gtest_mac.h"
12 #include "testing/platform_test.h"
16 class JSWindowIDManagerTest : public PlatformTest {
18 void SetUp() override {
19 PlatformTest::SetUp();
20 receiver_.reset([[CRWTestJSInjectionReceiver alloc] init]);
21 manager_.reset([[CRWJSWindowIdManager alloc] initWithReceiver:receiver_]);
22 web::SetWebClient(&web_client_);
24 void TearDown() override {
25 web::SetWebClient(nullptr);
26 PlatformTest::TearDown();
28 // Required for CRWJSWindowIdManager creation.
29 base::scoped_nsobject<CRWTestJSInjectionReceiver> receiver_;
30 // Testable CRWJSWindowIdManager.
31 base::scoped_nsobject<CRWJSWindowIdManager> manager_;
32 // WebClient required for getting early page script, which must be injected
33 // before CRWJSWindowIdManager.
34 web::WebClient web_client_;
37 // Tests that reinjection of window ID JS results in a different window ID.
38 // TODO(ios): This test only works for the current implementation using
39 // UIWebView. CRWTestJSInjectionReceiver should be re-written to eliminate
40 // web view specificity (crbug.com/486840).
41 TEST_F(JSWindowIDManagerTest, WindowIDReinjection) {
42 EXPECT_TRUE(manager_.get());
44 NSString* windowID = [manager_ windowId];
45 EXPECT_EQ(32U, [windowID length]);
46 // Reset the __gCrWeb object to enable reinjection.
47 web::EvaluateJavaScriptAsString(manager_, @"__gCrWeb = undefined;");
48 // Inject a second time to check that the ID is different.
50 NSString* windowID2 = [manager_ windowId];
51 EXPECT_FALSE([windowID isEqualToString:windowID2]);
54 // Tests that window ID injection by a second manager results in a different
56 TEST_F(JSWindowIDManagerTest, WindowIDDifferentManager) {
58 NSString* windowID = [manager_ windowId];
59 base::scoped_nsobject<CRWTestJSInjectionReceiver> receiver2(
60 [[CRWTestJSInjectionReceiver alloc] init]);
61 base::scoped_nsobject<CRWJSWindowIdManager> manager2(
62 [[CRWJSWindowIdManager alloc] initWithReceiver:receiver2]);
64 NSString* windowID2 = [manager2 windowId];
65 EXPECT_NSNE(windowID, windowID2);