autofill_sign_in_container_unittest.mm revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
1// Copyright (c) 2013 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#import "chrome/browser/ui/cocoa/autofill/autofill_sign_in_container.h" 6 7#include "base/memory/scoped_nsobject.h" 8#include "chrome/browser/ui/autofill/mock_autofill_dialog_controller.h" 9#import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 10#include "chrome/test/base/chrome_render_view_host_test_harness.h" 11#include "chrome/test/base/testing_profile.h" 12#include "content/public/browser/web_contents.h" 13#include "content/public/browser/web_contents_view.h" 14#include "testing/gtest/include/gtest/gtest.h" 15#include "testing/platform_test.h" 16#import "ui/base/test/ui_cocoa_test_helper.h" 17 18@interface AutofillSignInContainer (ExposedForTesting) 19- (content::WebContents*)webContents; 20@end 21 22@implementation AutofillSignInContainer (ExposedForTesting) 23- (content::WebContents*)webContents { return webContents_.get(); } 24@end 25 26namespace { 27 28class AutofillSignInContainerTest : public ChromeRenderViewHostTestHarness { 29 public: 30 AutofillSignInContainerTest() : test_window_(nil) {} 31 virtual void SetUp() { 32 ChromeRenderViewHostTestHarness::SetUp(); 33 // Inherting from ChromeRenderViewHostTestHarness means we can't inherit 34 // from from CocoaTest, so do a bootstrap and create test window. 35 CocoaTest::BootstrapCocoa(); 36 container_.reset( 37 [[AutofillSignInContainer alloc] initWithController:&controller_]); 38 EXPECT_CALL(controller_, profile()) 39 .WillOnce(testing::Return(this->profile())); 40 [[test_window() contentView] addSubview:[container_ view]]; 41 } 42 43 virtual void TearDown() { 44 container_.reset(); // must reset at teardown - depends on test harness. 45 [test_window_ close]; 46 test_window_ = nil; 47 ChromeRenderViewHostTestHarness::TearDown(); 48 } 49 50 CocoaTestHelperWindow* test_window() { 51 if (!test_window_) { 52 test_window_ = [[CocoaTestHelperWindow alloc] init]; 53 if (base::debug::BeingDebugged()) { 54 [test_window_ orderFront:nil]; 55 } else { 56 [test_window_ orderBack:nil]; 57 } 58 } 59 return test_window_; 60 } 61 62 protected: 63 scoped_nsobject<AutofillSignInContainer> container_; 64 testing::NiceMock<autofill::MockAutofillDialogController> controller_; 65 CocoaTestHelperWindow* test_window_; 66}; 67 68} // namespace 69 70TEST_VIEW(AutofillSignInContainerTest, [container_ view]) 71 72TEST_F(AutofillSignInContainerTest, Subviews) { 73 bool hasButton = false; 74 bool hasWebView = false; 75 76 for (NSView* view in [[container_ view] subviews]) { 77 if ([view isKindOfClass:[NSButton class]]) { 78 hasButton = true; 79 } else { 80 // isKindOfClass would be the better choice, but 81 // WebContentsViewCocoaClass is defined in content, and not public. 82 hasWebView = [view isEqual: 83 [container_ webContents]->GetView()->GetNativeView()]; 84 } 85 } 86 87 EXPECT_TRUE(hasButton); 88 EXPECT_TRUE(hasWebView); 89}