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