Gauthier Ambard | 9f3a49f | 2022-01-03 13:45:27 | [diff] [blame^] | 1 | // Copyright 2022 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 "ios/chrome/browser/ui/settings/content_settings/default_page_mode_table_view_controller.h" |
| 6 | |
| 7 | #import "base/mac/foundation_util.h" |
| 8 | #import "ios/chrome/browser/ui/table_view/chrome_table_view_controller_test.h" |
| 9 | #include "testing/gtest_mac.h" |
| 10 | |
| 11 | #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 12 | #error "This file requires ARC support." |
| 13 | #endif |
| 14 | |
| 15 | class DefaultPageModeTableViewControllerTest |
| 16 | : public ChromeTableViewControllerTest { |
| 17 | protected: |
| 18 | ChromeTableViewController* InstantiateController() override { |
| 19 | return [[DefaultPageModeTableViewController alloc] |
| 20 | initWithStyle:UITableViewStyleGrouped]; |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | // Tests that there are 2 items in the Table View. |
| 25 | TEST_F(DefaultPageModeTableViewControllerTest, TestItems) { |
| 26 | CreateController(); |
| 27 | CheckController(); |
| 28 | CheckTitle(nil); |
| 29 | |
| 30 | ASSERT_EQ(1, NumberOfSections()); |
| 31 | ASSERT_EQ(2, NumberOfItemsInSection(0)); |
| 32 | CheckTextCellText(@"TEST - Mobile", 0, 0); |
| 33 | CheckTextCellText(@"TEST - Desktop", 0, 1); |
| 34 | |
| 35 | CheckAccessoryType(UITableViewCellAccessoryNone, 0, 0); |
| 36 | CheckAccessoryType(UITableViewCellAccessoryNone, 0, 1); |
| 37 | } |
| 38 | |
| 39 | // Tests that the checkmark gets correctly updated when set before the model is |
| 40 | // loaded. |
| 41 | TEST_F(DefaultPageModeTableViewControllerTest, TestCheckmarkAtLoad) { |
| 42 | // Load the controller manually as this is testing setting the DefaultPageMode |
| 43 | // before the model is loaded. |
| 44 | DefaultPageModeTableViewController* controller = |
| 45 | [[DefaultPageModeTableViewController alloc] |
| 46 | initWithStyle:UITableViewStyleGrouped]; |
| 47 | |
| 48 | [controller setDefaultPageMode:DefaultPageModeDesktop]; |
| 49 | |
| 50 | [controller loadModel]; |
| 51 | // Force the tableView to be built. |
| 52 | ASSERT_TRUE([controller view]); |
| 53 | |
| 54 | UITableViewCellAccessoryType first_accesory = |
| 55 | [controller.tableViewModel |
| 56 | itemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] |
| 57 | .accessoryType; |
| 58 | EXPECT_EQ(UITableViewCellAccessoryNone, first_accesory); |
| 59 | UITableViewCellAccessoryType second_accesory = |
| 60 | [controller.tableViewModel |
| 61 | itemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] |
| 62 | .accessoryType; |
| 63 | EXPECT_EQ(UITableViewCellAccessoryCheckmark, second_accesory); |
| 64 | } |
| 65 | |
| 66 | // Tests that the checkmark gets correctly updated. |
| 67 | TEST_F(DefaultPageModeTableViewControllerTest, TestCheckmark) { |
| 68 | ChromeTableViewController* chrome_controller = controller(); |
| 69 | DefaultPageModeTableViewController* controller = |
| 70 | base::mac::ObjCCastStrict<DefaultPageModeTableViewController>( |
| 71 | chrome_controller); |
| 72 | |
| 73 | CheckAccessoryType(UITableViewCellAccessoryNone, 0, 0); |
| 74 | CheckAccessoryType(UITableViewCellAccessoryNone, 0, 1); |
| 75 | |
| 76 | [controller setDefaultPageMode:DefaultPageModeMobile]; |
| 77 | |
| 78 | CheckAccessoryType(UITableViewCellAccessoryCheckmark, 0, 0); |
| 79 | CheckAccessoryType(UITableViewCellAccessoryNone, 0, 1); |
| 80 | |
| 81 | [controller setDefaultPageMode:DefaultPageModeDesktop]; |
| 82 | |
| 83 | CheckAccessoryType(UITableViewCellAccessoryNone, 0, 0); |
| 84 | CheckAccessoryType(UITableViewCellAccessoryCheckmark, 0, 1); |
| 85 | } |