chromium/ios/chrome/browser/ui/settings/search_engine_table_view_controller_unittest.h

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_TABLE_VIEW_CONTROLLER_UNITTEST_H_
#define IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_TABLE_VIEW_CONTROLLER_UNITTEST_H_
#import <UIKit/UIKit.h>

#import <string>

#import "base/test/metrics/histogram_tester.h"
#import "base/test/scoped_feature_list.h"
#import "base/time/time.h"
#import "components/search_engines/search_engines_switches.h"
#import "components/search_engines/template_url_service.h"
#import "components/sync_preferences/testing_pref_service_syncable.h"
#import "ios/chrome/browser/shared/model/profile/test/test_profile_ios.h"
#import "ios/chrome/browser/shared/ui/table_view/legacy_chrome_table_view_controller_test.h"
#import "ios/web/public/test/web_task_environment.h"
#import "url/gurl.h"

class TemplateURL;

typedef struct {
  const std::string short_name;
  const GURL searchable_url;
} SearchEngineTest;

// Unit tests for SearchEngineTableViewController when the choice screen feature
// is disabled (using `kDisableSearchEngineChoiceScreen`).
class SearchEngineTableViewControllerTest
    : public LegacyChromeTableViewControllerTest {
 public:
  SearchEngineTableViewControllerTest();
  ~SearchEngineTableViewControllerTest() override;

 protected:
  void SetUp() override;
  void TearDown() override;
  LegacyChromeTableViewController* InstantiateController() override;

  // Adds a prepopulated search engine to TemplateURLService.
  // `prepopulate_id` should be big enough (>1000) to avoid collision with real
  // prepopulated search engines. The collision happens when
  // TemplateURLService::SetUserSelectedDefaultSearchProvider is called, in the
  // callback of PrefService the DefaultSearchManager will update the searchable
  // URL of default search engine from prepopulated search engines list.
  TemplateURL* AddPriorSearchEngine(const SearchEngineTest& search_engine,
                                    int prepopulate_id,
                                    bool set_default);
  // Adds a custom search engine to TemplateURLService.
  TemplateURL* AddCustomSearchEngine(const SearchEngineTest& search_engine,
                                     base::Time last_visited_time,
                                     bool set_default);
  // Checks SettingsSearchEngineItem at `row` in `section`.
  void CheckItem(NSString* expected_text,
                 NSString* expected_detail_text,
                 bool expected_checked,
                 int section,
                 int row,
                 bool enabled);
  // Checks a SettingsSearchEngineItem with data from a fabricated
  // TemplateURL. The SettingsSearchEngineItem in the `row` of `section`
  // should contain a title and a subtitle that are equal to `expected_text` and
  // an URL which can be generated by filling empty query word into
  // `expected_searchable_url`. If `expected_checked` is true, the
  // SettingsSearchEngineItem should have a
  // UITableViewCellAccessoryCheckmark.
  void CheckPrepopulatedItem(const SearchEngineTest& expected_search_engine,
                             bool expected_checked,
                             int section,
                             int row,
                             bool enabled = true);
  // Checks a SettingsSearchEngineItem with data from a fabricated
  // TemplateURL. The SettingsSearchEngineItem in the `row` of `section`
  // should contain a title and a subtitle that are equal to `expected_text` and
  // an URL which can be generated from `expected_searchable_url` by
  // TemplateURL::GenerateFaviconURL. If `expected_checked` is true, the
  // SettingsSearchEngineItem should have a
  // UITableViewCellAccessoryCheckmark.
  void CheckCustomItem(const SearchEngineTest& expected_search_engine,
                       bool expected_checked,
                       int section,
                       int row,
                       bool enabled = true);
  // Checks a SettingsSearchEngineItem with data from a real prepopulated
  // TemplateURL. The SettingsSearchEngineItem in the `row` of `section`
  // should contain a title equal to `expected_text`, a subtitle equal to
  // `expected_detail_text`, and an URL equal to `expected_favicon_url`. If
  // `expected_checked` is true, the SettingsSearchEngineItem should have
  // a UITableViewCellAccessoryCheckmark.
  void CheckRealItem(const TemplateURL* turl,
                     bool expected_checked,
                     int section,
                     int row,
                     bool enabled = true);
  // Deletes items at `indexes` and wait util condition returns true or timeout.
  [[nodiscard]] bool DeleteItemsAndWait(NSArray<NSIndexPath*>* indexes,
                                        ConditionBlock condition);

  base::test::ScopedFeatureList feature_list_{
      switches::kSearchEngineChoiceTrigger};
  web::WebTaskEnvironment task_environment_;
  std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
  base::HistogramTester histogram_tester_;
  raw_ptr<TemplateURLService> template_url_service_;  // weak
  raw_ptr<sync_preferences::TestingPrefServiceSyncable> pref_service_;
  const std::vector<SearchEngineTest> prepopulated_search_engine_;
  const std::vector<SearchEngineTest> custom_search_engine_;
};

#endif  // IOS_CHROME_BROWSER_UI_SETTINGS_SEARCH_ENGINE_TABLE_VIEW_CONTROLLER_UNITTEST_H_